Created
February 16, 2021 00:28
-
-
Save bayareacoder/0f173afe96f2a601e21d6f581396f739 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {LIVE_ACCOUNT_IDS} from '../config'; | |
import IBKRConnection from '../connection/IBKRConnection'; | |
import {IBKREVENTS, IbkrEvents, publishDataToTopic} from '../events'; | |
import {log} from '../log'; | |
import {Portfolios} from '../portfolios'; | |
import {getRadomReqId as getRandomReqId} from '../_utils/text.utils'; | |
import {IBKRAccountSummary} from './account-summary.interfaces'; | |
const appEvents = IbkrEvents.Instance; | |
export class AccountSummary { | |
ib: any; | |
accountIds: string[] = []; | |
accountReady = false; | |
tickerId = getRandomReqId(); | |
accountSummary: {[account: string]: IBKRAccountSummary} = {}; | |
private static _instance: AccountSummary; | |
public static get Instance(): AccountSummary { | |
return this._instance || (this._instance = new this()); | |
} | |
private constructor() { | |
this.ib = IBKRConnection.Instance.getIBKR(); | |
} | |
public init(): void { | |
const self = this; | |
const ib = IBKRConnection.Instance.getIBKR(); | |
self.ib = ib; | |
// Record values from here | |
ib.on('accountSummary', (reqId, account, tag, value, currency) => { | |
self.tickerId = reqId; | |
if (!self.accountIds.includes(account)) { | |
self.accountIds.push(account); | |
} | |
self.accountSummary[account] = { | |
...self.accountSummary[account], | |
account, | |
currency, | |
[lowercaseWithExceptions(tag)]: value, | |
}; | |
}); | |
// Return values from here | |
ib.once('accountSummaryEnd', () => { | |
const {tickerId, accountReady, accountSummary} = self; | |
log('accountSummaryEnd', {tickerId, accountReady, accountSummary}); | |
ib.cancelAccountSummary(tickerId); | |
publishDataToTopic({ | |
topic: IBKREVENTS.ON_ACCOUNT_SUMMARY, | |
data: accountSummary, | |
}); | |
self.accountReady = true; | |
}); | |
self.reqAccountSummary(); | |
} | |
/** | |
* reqAccountSummary | |
*/ | |
public reqAccountSummary = (): void => { | |
// Request Account summary from here | |
this.ib.reqAccountSummary(this.tickerId, 'All', [ | |
'AccountType', | |
'NetLiquidation', | |
'TotalCashValue', | |
'SettledCash', | |
'AccruedCash', | |
'BuyingPower', | |
'EquityWithLoanValue', | |
'PreviousEquityWithLoanValue', | |
'GrossPositionValue', | |
'RegTEquity', | |
'RegTMargin', | |
'SMA', | |
'InitMarginReq', | |
'MaintMarginReq', | |
'AvailableFunds', | |
'ExcessLiquidity', | |
'Cushion', | |
'FullInitMarginReq', | |
'FullMaintMarginReq', | |
'FullAvailableFunds', | |
'FullExcessLiquidity', | |
'LookAheadNextChange', | |
'LookAheadInitMarginReq', | |
'LookAheadMaintMarginReq', | |
'LookAheadAvailableFunds', | |
'LookAheadExcessLiquidity', | |
'HighestSeverity', | |
'DayTradesRemaining', | |
'Leverage', | |
]); | |
}; | |
/** | |
* initialiseDep | |
*/ | |
public initialiseDep(): void { | |
Portfolios.Instance /* */; | |
} /* */ | |
/** | |
* isLiveAccount | |
* Check whether a given account ID is a live account | |
*/ | |
public isLiveAccount(accountId: string): boolean { | |
return LIVE_ACCOUNT_IDS.includes(accountId); | |
} | |
/** | |
* getAccountSummary | |
*/ | |
public getAccountSummary(): Promise<IBKRAccountSummary> { | |
const {reqAccountSummary} = this; | |
return new Promise((resolve) => { | |
// listen for account summary | |
const handleAccountSummary = (accountSummary) => { | |
appEvents.off(IBKREVENTS.ON_ACCOUNT_SUMMARY, handleAccountSummary); | |
resolve(accountSummary); | |
}; | |
appEvents.on(IBKREVENTS.ON_ACCOUNT_SUMMARY, handleAccountSummary); | |
reqAccountSummary(); | |
}); | |
} | |
} | |
export default AccountSummary; | |
const exceptions = ['SMA']; | |
function lowercaseWithExceptions(str: string) { | |
if (exceptions.includes(str)) return str; | |
return str[0].toLowerCase() + str.slice(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment