Skip to content

Commit

Permalink
Merge pull request #220 from stoqey/issue219
Browse files Browse the repository at this point in the history
Issue219 fixed
  • Loading branch information
rylorin authored Jul 21, 2024
2 parents 7b0d422 + f186803 commit 494d1b3
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 157 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^18.19.34",
"@types/node": "^18.19.41",
"@types/source-map-support": "^0.5.10",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"ajv": "^8.16.0",
"@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "^7.16.1",
"ajv": "^8.17.1",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-rxjs": "^5.0.3",
"jest": "^29.7.0",
"jest-environment-node": "^29.7.0",
"jest-junit": "^16.0.0",
"ts-jest": "^29.1.5",
"typedoc": "^0.25.13",
"typescript": "^5.4.5"
"ts-jest": "^29.2.3",
"typedoc": "^0.26.4",
"typescript": "^5.5.3"
},
"engines": {
"node": ">=18.0.0"
Expand Down
4 changes: 4 additions & 0 deletions src/common/errorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum ErrorCode {

/** No trading permissions. */
NO_TRADING_PERMISSIONS = 460,

/**
* Couldn't connect to TWS.
*
Expand Down Expand Up @@ -282,6 +283,9 @@ export enum ErrorCode {
/** FA Profile is not supported anymore, use FA Group instead */
FA_PROFILE_NOT_SUPPORTED = 585,

/** Invalid position trade derived value */
INVALID_POSITION_TRADE_DERIVATED_VALUE = 2150,

/** Part of requested market data is not subscribed. */
PART_OF_REQUESTED_DATA_NOT_SUBSCRIBED = 10090,

Expand Down
2 changes: 1 addition & 1 deletion src/core/io/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2735,7 +2735,7 @@ function tagValuesToTokens(tagValues: TagValue[]): unknown[] {

const version = 1;

this.sendMsg(OUT_MSG_ID.REQ_POSITIONS_MULTI, version, reqId);
this.sendMsg(OUT_MSG_ID.CANCEL_POSITIONS_MULTI, version, reqId);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/tests/unit/api-next-live/subscription-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("Subscription registry Tests", () => {
// Two active subscriptions for the same Event issue #193
subscription$ = api.getOpenOrders().subscribe({
next: (data) => {

Check warning on line 47 in src/tests/unit/api-next-live/subscription-registry.test.ts

View workflow job for this annotation

GitHub Actions / job

'data' is defined but never used. Allowed unused args must match /^_/u
console.log(data);
// console.log(data);
},
complete: () => {
console.log("getOpenOrders completed.");
Expand Down
84 changes: 38 additions & 46 deletions src/tests/unit/api/historical-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,60 +113,52 @@ describe("IBApi Historical data Tests", () => {
ib.reqHistoricalData(
refId,
contract,
"20240508-17:00:00",
"20240719-17:00:00",
"30 S",
BarSizeSetting.SECONDS_FIFTEEN,
WhatToShow.BID_ASK,
0,
2,
false,
);
})
.on(
EventName.historicalData,
(
reqId: number,
time: string,
open: number,
high: number,
low: number,
close: number,
volume: number,
count: number | undefined,
WAP: number,
) => {
// console.log(
// counter,
// time,
// open,
// high,
// low,
// close,
// volume,
// count,
// WAP,
// );
expect(reqId).toEqual(refId);
if (time.startsWith("finished")) {
expect(counter).toEqual(2);
done();
} else if (counter++ == 1) {
expect(time).toEqual("1715187585");
expect(open).toEqual(25.35);
expect(high).toEqual(25.35);
expect(low).toEqual(25.35);
expect(close).toEqual(25.35);
expect(volume).toEqual(-1);
expect(count).toEqual(-1);
expect(WAP).toEqual(-1);
}
},
)
.on(EventName.error, (err, code, reqId) => {
if (reqId == refId) done(`[${reqId}] ${err.message} (#${code})`);
});
}).on(
EventName.historicalData,
(
reqId: number,
time: string,
open: number,
high: number,
low: number,
close: number,
volume: number,
count: number | undefined,
WAP: number,
) => {
// console.log(counter, time, open, high, low, close, volume, count, WAP);
expect(reqId).toEqual(refId);
if (time.startsWith("finished")) {
expect(counter).toEqual(2);
done();
} else if (counter++ == 1) {
expect(time).toEqual("1721408385");
expect(open).toEqual(11.95);
expect(high).toEqual(11.95);
expect(low).toEqual(11.9);
expect(close).toEqual(11.95);
expect(volume).toEqual(-1);
expect(count).toEqual(-1);
expect(WAP).toEqual(-1);
}
},
);

ib.connect();
ib.on(EventName.disconnected, () => done())
.on(EventName.info, (msg, code) => console.info("INFO", code, msg))
.on(EventName.error, (err, code, reqId) => {
if (reqId > 0) done(`[${reqId}] ${err.message} (#${code})`);
else console.error("ERROR", err.message, code, reqId);
})
.connect();
});

it("Weekly market data", (done) => {
Expand Down
22 changes: 20 additions & 2 deletions src/tests/unit/api/order/placeConditionalOrder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const sample_percent_condition: OrderCondition = new PercentChangeCondition(
ConjunctionConnection.OR,
);
const sample_time_condition: OrderCondition = new TimeCondition(
"20250101-12:00:00",
"20250102-17:00:00",
true,
ConjunctionConnection.OR,
);
Expand Down Expand Up @@ -369,7 +369,25 @@ describe("Place Conditional Orders", () => {
},
);

ib.connect();
ib.connect()
.on(EventName.error, (error, code, reqId) => {
if (reqId > 0) {
const msg = `[${reqId}] ${error.message} (Error #${code})`;
if (
error.message.includes("Warning:") ||
error.message.includes("Order Message:")
) {
logger.warn(msg);
} else {
ib.disconnect();
done(msg);
}
} else {
console.error("ERROR", error.message, code, reqId);
}
})
.on(EventName.info, (msg, code) => console.info("INFO", code, msg))
.on(EventName.disconnected, () => done());
});

test("placeOrder with VolumeCondition", (done) => {
Expand Down
7 changes: 2 additions & 5 deletions src/tests/unit/api/order/placeOrder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import logger from "../../../../common/logger";
import {
sample_crypto,
sample_etf,
sample_future,
sample_option,
sample_stock,
} from "../../sample-data/contracts";
Expand Down Expand Up @@ -284,11 +285,7 @@ describe("Place Orders", () => {
test("Issue #203", (done) => {
let refId: number;

const refContract: Contract = {
conId: 708846212,
exchange: "CME",
symbol: "MES",
};
const refContract: Contract = sample_future;
const refOrder: Order = {
action: OrderAction.BUY,
lmtPrice: 1,
Expand Down
40 changes: 32 additions & 8 deletions src/tests/unit/api/api.test.ts → src/tests/unit/api/pnl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe("IBApi Tests", () => {
}
if (_conId === undefined && pos) {
_conId = contract.conId;
// console.info(JSON.stringify(contract));
}
expect(account).toBeTruthy();
expect(contract).toBeTruthy();
Expand Down Expand Up @@ -90,19 +91,31 @@ describe("IBApi Tests", () => {
const refId = 44;
let received = false;

ib.on(
ib.once(EventName.connected, () => {
console.log("reqPnLSingle", refId);
ib.reqPnLSingle(refId, _account, "", _conId);
}).on(
EventName.pnlSingle,
(
reqId: number,
pos: number,
dailyPnL: number,
_dailyPnL: number,
unrealizedPnL: number,
realizedPnL: number,
_realizedPnL: number,
value: number,
) => {
console.log(
"pnlSingle",
reqId,
pos,
_dailyPnL,
unrealizedPnL,
_realizedPnL,
value,
);
expect(reqId).toEqual(refId);
expect(pos).toBeTruthy();
expect(dailyPnL).toBeTruthy();
// expect(dailyPnL).toBeTruthy(); We may have no daily PnL (on week-ends)
expect(unrealizedPnL).toBeTruthy();
// expect(realizedPnL).toBeTruthy(); We may have no realized PnL today
expect(value).toBeTruthy();
Expand All @@ -113,10 +126,21 @@ describe("IBApi Tests", () => {
}
received = true;
},
).on(EventName.error, (err, code, reqId) => {
if (reqId == refId) done(`[${reqId}] ${err.message} (#${code})`);
});
);

ib.connect().reqPnLSingle(refId, _account, null, _conId);
ib.on(EventName.disconnected, () => done())
.on(EventName.info, (msg, code) => console.info("INFO", code, msg))
.on(EventName.error, (err, code, reqId) => {
const msg = `[${reqId}] ${err.message} (#${code})`;
if (
reqId > 0 &&
code != ErrorCode.INVALID_POSITION_TRADE_DERIVATED_VALUE
) {
done(msg);
} else {
console.error("ERROR", msg);
}
})
.connect();
});
});
Loading

0 comments on commit 494d1b3

Please sign in to comment.