import ibkr , { EventName } from '@stoqey/ib' ; import includes from 'lodash/includes' ; import isEmpty from 'lodash/isEmpty' ; 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 } from '../_utils/text.utils' ; import { IBKRAccountSummary } from './account-summary.interfaces' ; const appEvents = IbkrEvents . Instance ; export class AccountSummary { ib : ibkr ; accountReady = false ; tickerId = getRadomReqId ( ) ; AccountId ; accountIds = [ ] ; accounts : Record < string , IBKRAccountSummary > = { } ; accountSummary : IBKRAccountSummary = { } as any ; 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 ( ) { const self = this ; const ib = IBKRConnection . Instance . getIBKR ( ) ; self . ib = ib ; ib . on ( EventName . accountSummary , ( reqId , account , tag , value , currency ) => { self . tickerId = reqId ; self . AccountId = account ; self . accountSummary . AccountId = account ; self . accountSummary [ tag ] = value ; self . accountSummary . Currency = currency ; self . accounts [ account ] = { ... self . accounts [ account ] , [ tag ] : value , AccountId : account , Currency : currency , } ; } ) ; ib . once ( EventName . accountSummaryEnd , ( ) => { const { AccountId = 'unknown' , tickerId , accountReady , accountSummary } = self ; log ( 'accountSummaryEnd' , { AccountId , tickerId , accountReady } ) ; ib . cancelAccountSummary ( tickerId ) ; publishDataToTopic ( { topic : IBKREVENTS . ON_ACCOUNT_SUMMARY , data : accountSummary , } ) ; self . accountReady = true ; } ) ; self . reqAccountSummary ( ) ; } /**
     * reqAccountSummary
     */ public reqAccountSummary = ( ) : void => { 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' , ] . join ( ',' ) ) ; } ; /**
     * initialiseDep
     */ public initialiseDep ( ) { Portfolios . Instance ; } /**
     * isLiveAccount
     * Check whether this is the live account
     */ public isLiveAccount ( ) { return includes ( LIVE_ACCOUNT_IDS , this . AccountId ) ; } /**
     * getAccountSummary
     */ public getAccountSummary ( ) { const { accountSummary , reqAccountSummary } = this ; return new Promise ( ( resolve ) => { if ( ! isEmpty ( accountSummary ) ) { return resolve ( accountSummary ) ; } const handleAccountSummary = ( accountSummaryData ) => { appEvents . off ( IBKREVENTS . ON_ACCOUNT_SUMMARY , handleAccountSummary ) ; resolve ( accountSummaryData ) ; } ; appEvents . on ( IBKREVENTS . ON_ACCOUNT_SUMMARY , handleAccountSummary ) ; reqAccountSummary ( ) ; } ) ; } } export default AccountSummary ;