def bbtInit(self): self.api = cl.bbCCXTInit(n=self.n) riskDf = cl.bbtGetRiskDf(self.api, self.spotDict) for ccy in self.validCcys: if ccy in riskDf.index: self.futures.loc[ccy, 'FutDelta']=cl.bbtGetFutPos(self.api,ccy) self.liqDict[ccy] = riskDf.loc[ccy,'liq'] else: self.liqDict[ccy] = 0 self.calcFuturesDeltaUSD() ##### if self.n>=2: # trim list for auxiliary BBTs self.validCcys=list(self.futures.index[self.futures['FutDelta'] != 0]) ##### self.oneDayIncome = 0 self.prevIncome = 0 if CR_CONFIGS_DICT['IS_CALC_PAYMENTS']: pmts=pd.DataFrame() for ccy in self.validCcys: data = cl.bbtGetTradeExecutionList(self.api,ccy) if not data is None: pmts = pmts.append(pd.DataFrame(data).set_index('symbol', drop=False)) if len(pmts)>0: cl.dfSetFloat(pmts, ['fee_rate', 'exec_fee']) pmts.loc[['Sell' in z for z in pmts['order_id']],'fee_rate']*=-1 # Correction for fee_rate signs pmts['incomeUSD'] = -pmts['exec_fee'] * self.spotDict['USDT'] pmts['date'] = [datetime.datetime.fromtimestamp(int(ts) / 1000) for ts in pmts['trade_time_ms']] pmts = pmts.set_index('date').sort_index() ##### self.oneDayIncome = pmts['incomeUSD'].sum() self.prevIncome = pmts.loc[pmts.index[-1]]['incomeUSD'].sum() self.setAnnRets() ##### usdtDict=self.api.v2_private_get_wallet_balance({'coin': 'USDT'})['result']['USDT'] equity = float(usdtDict['equity']) self.nav = equity * self.spotDict['USDT'] self.spots.loc['USDT', 'SpotDelta'] = equity self.calcSpotDeltaUSD() ##### for ccy in self.validCcys: oneDayFunding = 0 prevFunding = 0 estFunding=0 estFunding2=0 if CR_CONFIGS_DICT['IS_CALC_PAYMENTS']: if len(pmts)>0: df=pmts.loc[pmts['symbol']==ccy+'USDT','fee_rate'] if len(df) > 0: oneDayFunding = df.mean() * 3 * 365 prevFunding = df[df.index[-1]].mean() * 3 * 365 ##### if CR_CONFIGS_DICT['IS_CALC_ESTS']: estFunding, estFunding2 = self.bbtGetEstFundings_fast(ccy) self.makeFundingStr(ccy, oneDayFunding, prevFunding, estFunding, estFunding2) ##### self.makeIncomesStr() self.makeLiqStr(riskDf=riskDf,availableBalance=float(usdtDict['available_balance'])) self.isDone = True
if lev[side] < select_lev: api.private_linear_post_position_set_leverage({ 'symbol': sym, side.lower() + '_leverage': select_lev, opp_side.lower() + '_leverage': lev[opp_side] }) isMod = True if isMod: print( f'{sym} {position_value:,.0f} -> Risk limit ({max_size:,.0f}) Leverage ({select_lev})' ) break ###### # Init ###### cl.printHeader('BBTAutoRiskLimit - ' + cl.getCurrentTime()) apiDict = cl.getApiDict() spotDict = dict() spotDict['USDT'] = cl.ftxGetMid(apiDict['ftx'], 'USDT/USD') # Traditional for i in range(SHARED_EXCH_DICT['bbt']): cl.printHeader('BBT' + str(i + 1)) bb = cl.bbCCXTInit(i + 1) df = cl.bbtGetRiskDf(bb, spotDict) autoRiskLimit(bb, df.index)