def __init__(self, exch, spotDict, n=None): self.isDone = False self.exch = exch self.name = exch.upper() self.spotDict = spotDict self.n = n if self.n is not None: self.name += str(n) ##### self.validCcys = cl.getValidCcys(exch) ##### ccyList = list(SHARED_CCY_DICT.keys()) cl.appendUnique(ccyList, 'USD') cl.appendUnique(ccyList, 'USDT') zeroes = [0] * len(ccyList) self.spots = pd.DataFrame({'Ccy': ccyList, 'SpotDelta': zeroes, 'SpotDeltaUSD':zeroes}).set_index('Ccy') self.futures = pd.DataFrame({'Ccy':ccyList, 'FutDelta': zeroes, 'FutDeltaUSD':zeroes}).set_index('Ccy') self.oneDayFlows = 0 self.flowsDict = dict() self.oneDayIncome = 0 self.nav = 0 self.incomesStr = '' self.fundingStrDict=dict() self.liqDict=dict() self.liqStr = ''
def ftxInit(self): def cleanBorrows(ccy, df): dummy = pd.DataFrame([[0, 0, 0, 0, 0, 0]], columns=['time', 'coin', 'size', 'rate', 'cost', 'proceeds']) if len(df) == 0: return dummy df2 = df.copy() df2 = df2[df2['coin'] == ccy] if len(df2) == 0: return dummy df2 = df2.set_index('time').sort_index() return df2 ###### def makeFlows(ccy): start_time = cl.getYest() borrows = cleanBorrows(ccy, pd.DataFrame(self.api.private_get_spot_margin_borrow_history({'limit': 1000, 'start_time': start_time})['result'])) loans = cleanBorrows(ccy, pd.DataFrame(self.api.private_get_spot_margin_lending_history({'limit': 1000, 'start_time': start_time})['result'])) cl.dfSetFloat(borrows, ['cost','rate']) cl.dfSetFloat(loans, 'proceeds') prevBorrow = borrows.iloc[-1]['cost'] if borrows.index[-1] == self.payments.index[-1] else 0 prevLoan = loans.iloc[-1]['proceeds'] if loans.index[-1] == self.payments.index[-1] else 0 absBalUSD = abs(self.wallet.loc[ccy, 'total']*self.spotDict[ccy]) d=dict() d['oneDayFlows'] = (loans['proceeds'].sum() - borrows['cost'].sum()) * self.spotDict[ccy] d['oneDayFlowsAnnRet'] = d['oneDayFlows'] * 365 / absBalUSD d['oneDayBorrowRate'] = borrows['rate'].mean() * 24 * 365 d['prevFlows']=(prevLoan - prevBorrow) * self.spotDict[ccy] d['prevFlowsAnnRet']=d['prevFlows'] * 24 * 365 / absBalUSD d['prevBorrowRate'] = borrows.iloc[-1]['rate'] * 24 * 365 if borrows.index[-1] == self.payments.index[-1] else 0 return d ###### self.api = cl.ftxCCXTInit() self.wallet = cl.ftxGetWallet(self.api,self.validCcys) ccys=self.validCcys.copy() cl.appendUnique(ccys,'USDT') for ccy in ccys: self.spots.loc[ccy,'SpotDelta'] = self.wallet.loc[ccy,'total'] self.calcSpotDeltaUSD() ###### info = self.api.private_get_account()['result'] futs = pd.DataFrame(info['positions']).set_index('future') cl.dfSetFloat(futs, 'size') for ccy in self.validCcys: ccy2 = ccy + '-PERP' if ccy=='SHIB': ccy2='K'+ccy2 # Special fix for SHIB if ccy2 in futs.index: mult = -1 if futs.loc[ccy2, 'side']=='sell' else 1 if ccy == 'SHIB': mult*=1000 # Special fix for SHIB self.futures.loc[ccy,'FutDelta']=futs.loc[ccy2,'size']*mult self.calcFuturesDeltaUSD() ###### start_time=cl.getYest() pmts = pd.DataFrame() for ccy in self.validCcys: pmts = pmts.append(pd.DataFrame(self.api.private_get_funding_payments({'future':ccy+'-PERP', 'start_time': start_time})['result'])) pmts = pmts.set_index('time').sort_index() cl.dfSetFloat(pmts, 'payment') self.payments = pmts ##### self.oneDayIncome = -self.payments['payment'].sum() self.prevIncome = -self.payments.loc[self.payments.index[-1]]['payment'].sum() self.setAnnRets() ##### flowsCcy=CR_FTX_FLOWS_CCYS.copy() cl.appendUnique(flowsCcy,'USD') cl.appendUnique(flowsCcy,'USDT') for ccy in flowsCcy: d=makeFlows(ccy) self.oneDayFlows += d['oneDayFlows'] if d['oneDayFlows']==0 and d['prevFlows']==0 and not ccy in ['USD','USDT']: self.flowsDict[ccy]=None else: z1 = '$' + str(round(d['oneDayFlows'])) + ' (' + str(round(d['oneDayFlowsAnnRet'] * 100)) + '% p.a.)' z2 = '$' + str(round(d['prevFlows'])) + ' (' + str(round(d['prevFlowsAnnRet'] * 100)) + '% p.a.)' self.flowsDict[ccy]=colored(('FTX 24h/prev ' + ccy + ' flows: ').rjust(37) + z1 + ' / ' + z2, 'blue') if ccy in ['USD','USDT']: # Extra info for USD/USDT zList = [] zList.append('na' if d['oneDayBorrowRate'] == 0 else str(round(d['oneDayBorrowRate'] * 100)) + '%') zList.append('na' if d['prevBorrowRate'] == 0 else str(round(d['prevBorrowRate'] * 100)) + '%') zList.append(str(round(cl.ftxGetEstBorrow(self.api, ccy) * 100)) + '%') self.flowsDict[ccy+'2'] = ('FTX ' + ccy + ' 24h/prev/est: ').rjust(37) + '/'.join(zList) + ' p.a. ($' + str(round(self.wallet.loc[ccy, 'usdValue'] / 1000)) + 'K) ' ##### self.nav = self.wallet['usdValue'].sum() ##### for ccy in self.validCcys: oneDayFunding = 0 prevFunding = 0 estFunding = 0 if CR_CONFIGS_DICT['IS_CALC_PAYMENTS']: df = pd.DataFrame(self.api.public_get_funding_rates({'future': ccy + '-PERP', 'start_time': start_time})['result']).set_index('time').sort_index() cl.dfSetFloat(df, 'rate') oneDayFunding = df['rate'].mean() * 24 * 365 prevFunding = df['rate'][-1] * 24 * 365 ##### if CR_CONFIGS_DICT['IS_CALC_ESTS']: estFunding = cl.ftxGetEstFunding(self.api, ccy) self.makeFundingStr(ccy, oneDayFunding, prevFunding, estFunding) ##### self.makeIncomesStr() ##### self.mf = float(info['marginFraction']) self.mmReq = float(info['maintenanceMarginRequirement']) self.freeCollateral = float(info['freeCollateral']) totalPositionNotional = self.nav / self.mf cushion = (self.mf - self.mmReq) * totalPositionNotional delta = self.wallet.loc[self.validCcys, 'usdValue'].sum() + self.futures['FutDeltaUSD'].sum() self.makeLiqStr(cushion=cushion,delta=delta) ##### self.isDone=True