Skip to content

Commit

Permalink
Merge pull request #200 from innovatingdev/fix_encodings
Browse files Browse the repository at this point in the history
Fixed reqHeadTimestamp and reqHistogramData
  • Loading branch information
rylorin authored Jan 11, 2024
2 parents aae3f5d + 942daf9 commit 8b8943e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/core/io/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}

Expand Down Expand Up @@ -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,
);
}
Expand Down Expand Up @@ -3127,8 +3126,8 @@ function tagValuesToTokens(tagValues: TagValue[]): unknown[] {
OUT_MSG_ID.REQ_HISTOGRAM_DATA,
tickerId,
this.encodeContract(contract),
timePeriod,
useRTH ? 1 : 0,
timePeriod,
);
}

Expand Down
53 changes: 53 additions & 0 deletions src/tests/unit/api/head-timestamp.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
54 changes: 54 additions & 0 deletions src/tests/unit/api/histogram-data.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 8b8943e

Please sign in to comment.