def __update_ohlc( self): #should download ohlc soon after when it finalized if self.last_ohlc_min < datetime.now( self.JST).minute or (self.last_ohlc_min == 59 and datetime.now(self.JST).minute == 0): flg = True num_conti = 0 num_max_trial = 10 time.sleep(1) omd = TickData.get_ohlc() if omd is not None: for i in range(len(omd.unix_time)): if OneMinMarketData.ohlc.unix_time[-1] < omd.unix_time[i]: OneMinMarketData.ohlc.add_and_pop( omd.unix_time[i], omd.dt[i], omd.open[i], omd.high[i], omd.low[i], omd.close[i], omd.size[i]) flg = False LogMaster.add_log( 'updated ws ohlc at ' + str(datetime.now(self.JST)), self.prediction[0], self.ac) else: while flg: res, omd = CryptowatchDataGetter.get_data_after_specific_ut( OneMinMarketData.ohlc.unix_time[-1]) if res == 0 and omd is not None: if len(omd.unix_time) > 0: omd.cut_data( len(omd.unix_time )) #remove first data to exclude after ut for i in range(len(omd.unix_time)): if OneMinMarketData.ohlc.unix_time[ -1] < omd.unix_time[i]: OneMinMarketData.ohlc.add_and_pop( omd.unix_time[i], omd.dt[i], omd.open[i], omd.high[i], omd.low[i], omd.close[i], omd.size[i]) flg = False if flg == False: print('data download dt=' + str(datetime.now(self.JST))) LogMaster.add_log( 'updated ohlc at ' + str(datetime.now(self.JST)), self.prediction[0], self.ac) num_conti += 1 time.sleep(1) if num_conti >= num_max_trial: print('data can not be downloaded from cryptowatch.') LogMaster.add_log('ohlc download error', self.prediction[0], self.ac) break if flg == False: self.last_ohlc_min = datetime.now(self.JST).minute OneMinMarketData.update_for_bot() df = OneMinMarketData.generate_df_for_bot() pred_x = self.model.generate_bot_pred_data(df) #self.prediction = self.cbm.predict(Pool(pred_x)) self.prediction = self.model.prediction2(self.bst, pred_x) if len(self.prediction) > 1: print('prediction length error!') print(self.prediction) self.pred_side = str(int(self.prediction[0])).translate( str.maketrans({ '0': 'no', '1': 'buy', '2': 'sell', '3': 'both' })) self.ac.sync_position_order() self.ac.calc_pl(TickData.get_ltp()) print('dt={}, open={},high={},low={},close={}'.format( OneMinMarketData.ohlc.dt[-1], OneMinMarketData.ohlc.open[-1], OneMinMarketData.ohlc.high[-1], OneMinMarketData.ohlc.low[-1], OneMinMarketData.ohlc.close[-1])) print( 'total_pl={}, pl per min={}, collateral change={},num_trade={},win_rate={},prediction={},holding_side={},holding_price={},holding_size={},ltp={}' .format(self.ac.total_pl, self.ac.total_pl_per_min, self.ac.collateral_change, self.ac.num_trade, self.ac.win_rate, self.prediction[0], self.ac.holding_side, self.ac.holding_price, self.ac.holding_size, TickData.get_ltp())) print('private access per 300sec={}'.format( Trade.total_access_per_300s)) LogMaster.add_log( 'private access per 300sec = ' + str(Trade.total_access_per_300s), self.prediction[0], self.ac) LineNotification.send_notification( LogMaster.get_latest_performance())
def __update_ohlc( self): #should download ohlc soon after when it finalized if self.last_ohlc_min < datetime.now( self.JST).minute or (self.last_ohlc_min == 59 and datetime.now(self.JST).minute == 0): flg = True self.flg_loss_cut = False num_conti = 0 num_max_trial = 10 time.sleep(1) if TickData.ws_down_flg == False: omd = TickData.get_ohlc() if omd is not None: for i in range(len(omd.unix_time)): if OneMinMarketData.ohlc.unix_time[-1] < omd.unix_time[ i]: OneMinMarketData.ohlc.add_and_pop( omd.unix_time[i], omd.dt[i], omd.open[i], omd.high[i], omd.low[i], omd.close[i], omd.size[i]) flg = False LogMaster.add_log( 'updated ws ohlc at ' + str(datetime.now(self.JST)), self.prediction, self.ac) else: while flg: res, omd = CryptowatchDataGetter.get_data_after_specific_ut( OneMinMarketData.ohlc.unix_time[-1]) if res == 0 and omd is not None: if len(omd.unix_time) > 0: omd.cut_data( len(omd.unix_time) ) #remove first data to exclude after ut for i in range(len(omd.unix_time)): if OneMinMarketData.ohlc.unix_time[ -1] < omd.unix_time[i]: OneMinMarketData.ohlc.add_and_pop( omd.unix_time[i], omd.dt[i], omd.open[i], omd.high[i], omd.low[i], omd.close[i], omd.size[i]) flg = False if flg == False: print('data download dt=' + str(datetime.now(self.JST))) LogMaster.add_log( 'updated ohlc at ' + str(datetime.now(self.JST)), self.prediction, self.ac) num_conti += 1 time.sleep(1) if num_conti >= num_max_trial: print( 'data can not be downloaded from cryptowatch.') LogMaster.add_log('ohlc download error', self.prediction, self.ac) break else: TickData.ws_down_flg = False self.__overwrite_one_min_data(self.num_term, self.window_term) LogMaster.add_log('overwrite one min data', self.prediction, self.ac) print('overwrite one min data') LineNotification.send_error('overwrite one min data') if flg == False: self.last_ohlc_min = datetime.now(self.JST).minute OneMinMarketData.update_for_bot() df = OneMinMarketData.generate_df_for_bot() pred_x = self.lgb.generate_bsp_data_for_bot(df) self.prediction = self.lgb.prediction(self.model_buy, self.model_sell, pred_x, self.upper_kijun, self.lower_kijun) print('prediction=', self.prediction) self.pred_side = { 0: 'no', 1: 'buy', -1: 'sell' }[self.prediction] #self.ac.sync_position_order() self.ac.calc_holding_pnl() print('dt={}, open={},high={},low={},close={}'.format( OneMinMarketData.ohlc.dt[-1], OneMinMarketData.ohlc.open[-1], OneMinMarketData.ohlc.high[-1], OneMinMarketData.ohlc.low[-1], OneMinMarketData.ohlc.close[-1])) print( 'total_pl={}, pl per min={}, num_trade={},win_rate={},prediction={},holding_side={},holding_price={},holding_size={},ltp={}' .format(self.ac.total_pl, self.ac.total_pl_per_min, self.ac.num_trade, self.ac.win_rate, self.prediction, self.ac.holding_side, self.ac.holding_price, self.ac.holding_size, TickData.get_ltp())) print('private access per 300sec={}'.format( Trade.total_access_per_300s)) LogMaster.add_log( 'private access per 300sec = ' + str(Trade.total_access_per_300s), self.prediction, self.ac) LineNotification.send_notification( LogMaster.get_latest_performance()) while datetime.now().second >= 59: time.sleep(0.1)