From 942daf9dc8d1285c15b5719f4244d570dfa3ad17 Mon Sep 17 00:00:00 2001 From: Innovating Dev Date: Fri, 5 Jan 2024 11:44:55 +0100 Subject: [PATCH] Fixed reqHeadTimestamp and reqHistogramData --- src/core/io/encoder.ts | 9 ++-- src/tests/unit/api/head-timestamp.test.ts | 53 ++++++++++++++++++++++ src/tests/unit/api/histogram-data.test.ts | 54 +++++++++++++++++++++++ 3 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 src/tests/unit/api/head-timestamp.test.ts create mode 100644 src/tests/unit/api/histogram-data.test.ts diff --git a/src/core/io/encoder.ts b/src/core/io/encoder.ts index dbcbb41d..d1facdc1 100644 --- a/src/core/io/encoder.ts +++ b/src/core/io/encoder.ts @@ -216,12 +216,11 @@ export class Encoder { contract.right, contract.multiplier, contract.exchange, + contract.primaryExch, contract.currency, contract.localSymbol, contract.tradingClass, - contract.primaryExch, - contract.secIdType, - contract.secId, + contract.includeExpired ? 1 : 0, ]; } @@ -1856,8 +1855,8 @@ function tagValuesToTokens(tagValues: TagValue[]): unknown[] { OUT_MSG_ID.REQ_HEAD_TIMESTAMP, reqId, this.encodeContract(contract), - whatToShow, useRTH ? 1 : 0, + whatToShow, formatDate, ); } @@ -3127,8 +3126,8 @@ function tagValuesToTokens(tagValues: TagValue[]): unknown[] { OUT_MSG_ID.REQ_HISTOGRAM_DATA, tickerId, this.encodeContract(contract), - timePeriod, useRTH ? 1 : 0, + timePeriod, ); } diff --git a/src/tests/unit/api/head-timestamp.test.ts b/src/tests/unit/api/head-timestamp.test.ts new file mode 100644 index 00000000..247a75ba --- /dev/null +++ b/src/tests/unit/api/head-timestamp.test.ts @@ -0,0 +1,53 @@ +/** + * This file implement test code for the public API interfaces. + */ +import { Contract, EventName, IBApi, Stock, WhatToShow } from "../../.."; +import configuration from "../../../common/configuration"; + +describe("IBApi Historical data Tests", () => { + jest.setTimeout(10 * 1000); + + let ib: IBApi; + const clientId = Math.floor(Math.random() * 32766) + 1; // ensure unique client + + beforeEach(() => { + ib = new IBApi({ + host: configuration.ib_host, + port: configuration.ib_port, + clientId, + }); + // logger.info("IBApi created"); + }); + + afterEach(() => { + if (ib) { + ib.disconnect(); + ib = undefined; + } + // logger.info("IBApi disconnected"); + }); + + it("Returns the correct head timestamp", (done) => { + const referenceID = 42; + + ib.once(EventName.connected, () => { + const contract: Contract = new Stock("AAPL"); + + ib.reqHeadTimestamp(referenceID, contract, WhatToShow.TRADES, true, 2) + .on(EventName.headTimestamp, (requestId, headTimestamp) => { + if (requestId === referenceID) { + expect(headTimestamp).toEqual("345479400"); + ib.disconnect(); + } + }) + .on(EventName.disconnected, done) + .on(EventName.error, (err, code, requestId) => { + if (requestId === referenceID) { + done(`[${requestId}] ${err.message} (#${code})`); + } + }); + }); + + ib.connect(); + }); +}); diff --git a/src/tests/unit/api/histogram-data.test.ts b/src/tests/unit/api/histogram-data.test.ts new file mode 100644 index 00000000..f7fd77dc --- /dev/null +++ b/src/tests/unit/api/histogram-data.test.ts @@ -0,0 +1,54 @@ +/** + * This file implement test code for the public API interfaces. + */ +import { Contract, DurationUnit, EventName, IBApi, Stock } from "../../.."; +import configuration from "../../../common/configuration"; + +describe("IBApi Histogram data Tests", () => { + jest.setTimeout(10 * 1000); + + let ib: IBApi; + const clientId = Math.floor(Math.random() * 32766) + 1; // ensure unique client + + beforeEach(() => { + ib = new IBApi({ + host: configuration.ib_host, + port: configuration.ib_port, + clientId, + }); + // logger.info("IBApi created"); + }); + + afterEach(() => { + if (ib) { + ib.disconnect(); + ib = undefined; + } + // logger.info("IBApi disconnected"); + }); + + it("Histogram market data", (done) => { + const referenceID = 46; + + ib.once(EventName.connected, () => { + const contract: Contract = new Stock("AAPL"); + + ib.reqHistogramData(referenceID, contract, true, 1, DurationUnit.WEEK) + .on(EventName.histogramData, (requestID, data) => { + if (requestID === referenceID) { + expect(requestID).toEqual(referenceID); + expect(data.length).toBeGreaterThan(0); + ib.disconnect(); + } + }) + .on(EventName.disconnected, done) + .on(EventName.error, (err, code, requestID) => { + if (requestID == referenceID) { + done(`[${requestID}] ${err.message} (#${code})`); + } + }); + }); + + ib.connect(); + }); +});