diff --git a/src/tools/open-orders-all.ts b/src/tools/open-orders-all.ts new file mode 100644 index 00000000..873cd2d4 --- /dev/null +++ b/src/tools/open-orders-all.ts @@ -0,0 +1,53 @@ +/** + * This App will print IBKR account open orders to console. + */ + +import { IBApiNextError } from ".."; +import { IBApiNextApp } from "./common/ib-api-next-app"; + +///////////////////////////////////////////////////////////////////////////////// +// The help text. // +///////////////////////////////////////////////////////////////////////////////// + +const DESCRIPTION_TEXT = "Prints the account open orders."; +const USAGE_TEXT = "Usage: open-orders.js "; +const OPTION_ARGUMENTS: [string, string][] = []; +const EXAMPLE_TEXT = "open-orders.js"; + +////////////////////////////////////////////////////////////////////////////// +// The App code // +////////////////////////////////////////////////////////////////////////////// + +class OpenOrdersApp extends IBApiNextApp { + constructor() { + super(DESCRIPTION_TEXT, USAGE_TEXT, OPTION_ARGUMENTS, EXAMPLE_TEXT); + } + + /** + * Start the app. + */ + start(): void { + super.start(); + + this.api + .getAllOpenOrders() + .then((orders) => { + this.printObject(orders); + this.stop(); + }) + .catch((err: IBApiNextError) => { + this.error(`getAllOpenOrders failed with '${err}'`); + }); + } + + /** + * Stop the app with success code. + */ + stop() { + this.exit(); + } +} + +// run the app + +new OpenOrdersApp().start(); diff --git a/src/tools/open-orders-auto.ts b/src/tools/open-orders-auto.ts index 252d997f..3f12c856 100644 --- a/src/tools/open-orders-auto.ts +++ b/src/tools/open-orders-auto.ts @@ -41,10 +41,10 @@ class OpenOrdersApp extends IBApiNextApp { this.printObject(data); }, error: (err: IBApiNextError) => { - this.error(`getOpenOrders failed with '${err.error.message}'`); + this.error(`getAutoOpenOrders failed with '${err.error.message}'`); }, complete: () => { - console.log("getOpenOrders completed."); + console.log("getAutoOpenOrders completed."); }, }); } diff --git a/src/tools/open-orders.ts b/src/tools/open-orders.ts index 80c2aa2a..0e8172b0 100644 --- a/src/tools/open-orders.ts +++ b/src/tools/open-orders.ts @@ -1,7 +1,9 @@ /** - * This App will print IBKR account open orders to console. + * This App will print real-time updates of the IBKR account open orders. */ +import { Subscription } from "rxjs"; + import { IBApiNextError } from "../"; import { IBApiNextApp } from "./common/ib-api-next-app"; @@ -10,9 +12,9 @@ import { IBApiNextApp } from "./common/ib-api-next-app"; ///////////////////////////////////////////////////////////////////////////////// const DESCRIPTION_TEXT = "Prints the account open orders."; -const USAGE_TEXT = "Usage: open-orders.js "; -const OPTION_ARGUMENTS: [string, string][] = []; -const EXAMPLE_TEXT = "open-orders.js"; +const USAGE_TEXT = "Usage: open-orders-updates.js "; +const OPTION_ARGUMENTS: [string, string][] = [["bind", "auto bind orders"]]; +const EXAMPLE_TEXT = "open-orders-updates.js"; ////////////////////////////////////////////////////////////////////////////// // The App code // @@ -23,27 +25,34 @@ class OpenOrdersApp extends IBApiNextApp { super(DESCRIPTION_TEXT, USAGE_TEXT, OPTION_ARGUMENTS, EXAMPLE_TEXT); } + /** The [[Subscription]] on the open orders. */ + private subscription$: Subscription; + /** * Start the app. */ start(): void { super.start(); - this.api - .getAllOpenOrders() - .then((orders) => { - this.printObject(orders); - this.stop(); - }) - .catch((err: IBApiNextError) => { - this.error(`getAllOpenOrders failed with '${err}'`); - }); + this.subscription$ = this.api.getOpenOrders().subscribe({ + next: (data) => { + this.printObject(data); + }, + error: (err: IBApiNextError) => { + this.error(`getOpenOrders failed with '${err.error.message}'`); + }, + complete: () => { + console.log("getOpenOrders completed."); + }, + }); } /** * Stop the app with success code. */ stop() { + console.log("app stopping."); + this.subscription$?.unsubscribe(); this.exit(); } }