From 1bee81fac86a4bec5c784875c2016bf03415b1af Mon Sep 17 00:00:00 2001 From: Ronan-Yann Lorin Date: Wed, 30 Aug 2023 15:49:49 +0200 Subject: [PATCH] Option data type bug fix --- src/api/contract/option.ts | 6 +++++- src/tools/market-data.ts | 29 ++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/api/contract/option.ts b/src/api/contract/option.ts index b9d6a4aa..dc001ba3 100644 --- a/src/api/contract/option.ts +++ b/src/api/contract/option.ts @@ -8,7 +8,7 @@ import { Contract } from "./contract"; export class Option implements Contract { constructor( public symbol: string, - public lastTradeDateOrContractMonth: string, + public expiry: string, public strike: number, public right: OptionType, public exchange?: string, @@ -20,6 +20,10 @@ export class Option implements Contract { public secType = SecType.OPT; public multiplier = 100; + + public get lastTradeDateOrContractMonth(): string { + return this.expiry; + } } export default Option; diff --git a/src/tools/market-data.ts b/src/tools/market-data.ts index 7e45aa9e..f7ba4362 100644 --- a/src/tools/market-data.ts +++ b/src/tools/market-data.ts @@ -5,7 +5,7 @@ import path from "path"; import { Subscription } from "rxjs"; -import { SecType, OptionType } from "../"; +import { SecType, OptionType ,Option, Contract} from "../"; import { IBApiNextError, IBApiNextTickType, IBApiTickType, MarketDataType } from "../api-next"; import logger from "../common/logger"; import { IBApiNextApp } from "./common/ib-api-next-app"; @@ -17,7 +17,7 @@ import { IBApiNextApp } from "./common/ib-api-next-app"; const DESCRIPTION_TEXT = "Print real time market data of a given contract id."; const USAGE_TEXT = "Usage: market-data.js "; const OPTION_ARGUMENTS: [string, string][] = [ - ["conid=", "Contract ID (conId) of the contract."], + // ["conid=", "Contract ID (conId) of the contract."], ["symbol=", "The symbol name."], [ "sectype=", @@ -53,22 +53,25 @@ class PrintMarketDataApp extends IBApiNextApp { * Start the app. */ start(): void { + let contract: Contract | Option; const scriptName = path.basename(__filename); logger.debug(`Starting ${scriptName} script`); this.connect(this.cmdLineArgs.watch ? 10000 : 0); this.api.setMarketDataType(MarketDataType.DELAYED); + if (this.cmdLineArgs.sectype as SecType == SecType.OPT) { + contract = new Option( this.cmdLineArgs.symbol as string, this.cmdLineArgs.expiry as string,+this.cmdLineArgs.strike,this.cmdLineArgs.right as OptionType,this.cmdLineArgs.exchange as string,this.cmdLineArgs.currency as string); + } else contract= { + conId: this.cmdLineArgs.conid as number ?? undefined, + symbol: this.cmdLineArgs.symbol as string, + secType: this.cmdLineArgs.sectype as SecType, + exchange: this.cmdLineArgs.exchange as string, + currency: this.cmdLineArgs.currency as string, + lastTradeDateOrContractMonth: this.cmdLineArgs.expiry as string, + strike: (this.cmdLineArgs.strike as number) ?? undefined, + right: this.cmdLineArgs.right as OptionType, + } this.subscription$ = this.api - .getMarketData( - { - conId: this.cmdLineArgs.conid as number ?? undefined, - symbol: this.cmdLineArgs.symbol as string, - secType: this.cmdLineArgs.sectype as SecType, - exchange: this.cmdLineArgs.exchange as string, - currency: this.cmdLineArgs.currency as string, - lastTradeDateOrContractMonth: this.cmdLineArgs.expiry as string, - strike: (this.cmdLineArgs.strike as number) ?? undefined, - right: this.cmdLineArgs.right as OptionType, - }, + .getMarketData(contract, this.cmdLineArgs.ticks as string, false, false