-
-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getContractDetails implemented for bonds #198
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import SecType from "../data/enum/sec-type"; | ||
import { Contract } from "./contract"; | ||
|
||
/** | ||
* A Bond Contract | ||
*/ | ||
export class Bond implements Contract { | ||
constructor( | ||
public symbol: string, | ||
public maturity?: string, | ||
public exchange?: string, | ||
public currency?: string, | ||
) { | ||
this.currency = this.currency ?? "USD"; | ||
} | ||
|
||
public secType = SecType.BOND; | ||
|
||
public get lastTradeDateOrContractMonth(): string { | ||
return this.maturity; | ||
} | ||
} | ||
|
||
export default Bond; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,20 @@ | |
* This file implements tests for the [[IBApiNext.getContractDetails]] function. | ||
*/ | ||
|
||
import { ContractDetails, EventName, IBApi, IBApiNext, IBApiNextError } from "../../.."; | ||
import { Subscription } from "rxjs"; | ||
import { | ||
ContractDetails, | ||
EventName, | ||
IBApi, | ||
IBApiNext, | ||
IBApiNextError, | ||
} from "../../.."; | ||
import { | ||
sample_bond, | ||
sample_future, | ||
sample_option, | ||
sample_stock, | ||
} from "../contracts"; | ||
|
||
describe("RxJS Wrapper: getContractDetails()", () => { | ||
test("Error Event", (done) => { | ||
|
@@ -62,3 +75,119 @@ | |
api.emit(EventName.contractDetailsEnd, 1); | ||
}); | ||
}); | ||
|
||
describe("ApiNext: getContractDetails()", () => { | ||
jest.setTimeout(10 * 1000); | ||
|
||
const clientId = Math.floor(Math.random() * 32766) + 1; // ensure unique client | ||
|
||
let subscription$: Subscription; | ||
let api: IBApiNext; | ||
let error$: Subscription; | ||
|
||
beforeEach(() => { | ||
api = new IBApiNext(); | ||
|
||
if (!error$) { | ||
error$ = api.errorSubject.subscribe((error) => { | ||
if (error.reqId === -1) { | ||
console.warn(`${error.error.message} (Error #${error.code})`); | ||
} else { | ||
console.error( | ||
`${error.error.message} (Error #${error.code}) ${ | ||
error.advancedOrderReject ? error.advancedOrderReject : "" | ||
}`, | ||
); | ||
} | ||
}); | ||
} | ||
|
||
try { | ||
api.connect(clientId); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
}); | ||
|
||
afterEach(() => { | ||
if (api) { | ||
api.disconnect(); | ||
api = undefined; | ||
} | ||
}); | ||
|
||
test("Stock contract details", (done) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 3 locations. Consider refactoring. |
||
api | ||
.getContractDetails(sample_stock) | ||
.then((result) => { | ||
// console.log(result); | ||
expect(result.length).toBeGreaterThan(0); | ||
if (result.length) { | ||
expect(result[0].contract.symbol).toEqual(sample_stock.symbol); | ||
expect(result[0].contract.secType).toEqual(sample_stock.secType); | ||
} | ||
done(); | ||
}) | ||
.catch((err: IBApiNextError) => { | ||
done( | ||
`getContractDetails failed with '${err.error.message}' (Error #${err.code})`, | ||
); | ||
}); | ||
}); | ||
|
||
test("Future contract details", (done) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 3 locations. Consider refactoring. |
||
api | ||
.getContractDetails(sample_future) | ||
.then((result) => { | ||
// console.log(result); | ||
expect(result.length).toBeGreaterThan(0); | ||
if (result.length) { | ||
expect(result[0].contract.symbol).toEqual(sample_future.symbol); | ||
expect(result[0].contract.secType).toEqual(sample_future.secType); | ||
} | ||
done(); | ||
}) | ||
.catch((err: IBApiNextError) => { | ||
done( | ||
`getContractDetails failed with '${err.error.message}' (Error #${err.code})`, | ||
); | ||
}); | ||
}); | ||
|
||
test("Option contract details", (done) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 3 locations. Consider refactoring. |
||
api | ||
.getContractDetails(sample_option) | ||
.then((result) => { | ||
// console.log(result); | ||
expect(result.length).toBeGreaterThan(0); | ||
if (result.length) { | ||
expect(result[0].contract.symbol).toEqual(sample_option.symbol); | ||
expect(result[0].contract.secType).toEqual(sample_option.secType); | ||
} | ||
done(); | ||
}) | ||
.catch((err: IBApiNextError) => { | ||
done( | ||
`getContractDetails failed with '${err.error.message}' (Error #${err.code})`, | ||
); | ||
}); | ||
}); | ||
|
||
test("Bond contract details", (done) => { | ||
api | ||
.getContractDetails(sample_bond) | ||
.then((result) => { | ||
// console.log(result); | ||
expect(result.length).toBeGreaterThan(0); | ||
if (result.length) { | ||
expect(result[0].contract.secType).toEqual(sample_bond.secType); | ||
} | ||
done(); | ||
}) | ||
.catch((err: IBApiNextError) => { | ||
done( | ||
`getContractDetails failed with '${err.error.message}' (Error #${err.code})`, | ||
); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* This file describe sample contracts to be used in various tests code. | ||
*/ | ||
import { Bond, Contract, Future, Option, OptionType, Stock } from "../.."; | ||
|
||
export const sample_stock: Contract = new Stock("AAPL"); | ||
export const sample_etf: Contract = new Stock("SPY"); | ||
export const sample_future: Contract = new Future( | ||
"ES", | ||
"ESZ3", | ||
"202312", | ||
"CME", | ||
50, | ||
); | ||
export const sample_option: Contract = new Option( | ||
"AAPL", | ||
"20251219", | ||
200, | ||
OptionType.Put, | ||
); | ||
export const sample_bond: Contract = new Bond("912828C57"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.