Skip to content

Commit

Permalink
reqPositionsMulti / cancelPositionsMulti test added
Browse files Browse the repository at this point in the history
  • Loading branch information
rylorin committed Jul 21, 2024
1 parent 7b0d422 commit 68e81d7
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/tests/unit/api/positions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* This file implement test code for the public API interfaces.
*/
import { Contract, ErrorCode, EventName, IBApi } from "../../..";
import configuration from "../../../common/configuration";
import logger from "../../../common/logger";

describe("IBApi Tests", () => {
jest.setTimeout(15_000);

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,
});
});

afterEach(() => {
if (ib) {
ib.disconnect();
ib = undefined;
}
});

async function delay(secs) {

Check warning on line 29 in src/tests/unit/api/positions.test.ts

View workflow job for this annotation

GitHub Actions / job

'delay' is defined but never used. Allowed unused vars must match /^_/u
const res = await new Promise((resolve, reject) => {

Check warning on line 30 in src/tests/unit/api/positions.test.ts

View workflow job for this annotation

GitHub Actions / job

'reject' is defined but never used. Allowed unused args must match /^_/u
setTimeout(() => {
return resolve(true);
}, secs * 1_000);
});
return res;
}

let _account: string; // maintain account name for further tests
let _conId: number; // maintain for conId for further tests

it("Test reqPositions / cancelPositions", (done) => {
let positionsCount = 0;

ib.on(EventName.error, (err: Error, code: ErrorCode, id: number) => {
expect(`${err.message} - code: ${code} - id: ${id}`).toBeFalsy();
})
.on(
EventName.position,
(account: string, contract: Contract, pos: number, avgCost: number) => {
if (_account === undefined) {
_account = account;
}
if (_conId === undefined && pos) {
_conId = contract.conId;
}
expect(account).toBeTruthy();
expect(contract).toBeTruthy();
// expect(pos).toBeTruthy(); pos can be 0 when it has been closed today
if (pos) expect(avgCost).toBeTruthy();
positionsCount++;
},
)
.on(EventName.positionEnd, () => {
if (positionsCount) {
ib.disconnect();
done();
} else {
logger.error("No Positions received");
}
});

ib.connect().reqPositions();
});

it("Test reqPositionsMulti / cancelPositionsMulti", (done) => {
let refId = 45;

ib.once(EventName.connected, () => {
ib.reqPositionsMulti(refId, "", "");
})
.on(
EventName.positionMulti,
(reqId, account, modelCode, contract, pos, avgCost) => {
console.log(
"positionMulti",
reqId,
account,
modelCode,
JSON.stringify(contract),
pos,
avgCost,
);
},
)
.on(EventName.positionMultiEnd, (reqId) => {
console.log("positionMultiEnd", reqId);
refId = reqId + 1;
ib.cancelPositionsMulti(refId);
console.log("cancelPositionsMulti sent", refId);
refId = refId + 1;
ib.reqPositionsMulti(refId, "", "");
console.log("reqPositionsMulti sent", refId);
});

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

0 comments on commit 68e81d7

Please sign in to comment.