def do_POST(self): content_len = int(self.headers.get('content-length')) res = json.loads(self.rfile.read(content_len).decode('utf-8')) if res['boardName'] == 'BitMEX_XBTUSD': logger.debug('save response: {}'.format(dict2str(res))) self.save_response(res)
def simulate(): start_dt = '2018-11-20 00:00:00' end_dt = '2018-12-31 23:59:59' #end_dt = '2018-12-28 23:59:59' assets = 3000 params = get_params() # 過去の値を含めたローソク足を作成 start_dt_ext = (datetime.strptime(start_dt, '%Y-%m-%d %H:%M:%S') - timedelta(1)).strftime('%Y-%m-%d %H:%M:%S') messages_ext = create_message(start_dt_ext, end_dt) messages = create_message(start_dt, end_dt) tickers = create_tickers(messages) tm = TimeManager(timestamps=messages['1min'].index) rm = ResourceManager(messages_ext, tickers, assets, tm) # ResourceMangerは最終的なローソク足のみ登録する api = APISimulator(rm) logger.info('[params], ' + dict2str(params) + 'initial_assets: ' + str(assets)) #seller = ModelNaive(params['reward_upper'], params['loss_lower'], params['update_threshold'], params['step']) #buyer = ModelMa(params['pair'], params['candle_type'], params['timeperiod_s'], params['timeperiod_m'], params['timeperiod_l'], params['timeperiod_xl'], api) buyer = ModelLGB(params['pair'], api) seller = ModelLGB(params['pair'], api) trader = Trader(params['pair'], params['candle_type'], buyer, seller, params['amount'], params['order_type'], params['asset_lower'], api) PubSubSimulator(params['candle_type'], trader, api, tm).run(messages) summary(start_dt, end_dt, 'simulate/log/debug.log')
def trade(): params = get_params() api = API() try: logger.info('[params], ' + dict2str(params) + 'initial_assets: ' + str(api.get_asset())) # Listenerの追加 buyer = ModelLGBBinary(params['pair'], api, params['norm_mean'], params['norm_std'], params['lower'], params['upper'], params['loss_lower']) seller = ModelLGBBinary(params['pair'], api, params['norm_mean'], params['norm_std'], params['lower'], params['upper'], params['loss_lower']) #seller = ModelNaive(params['reward_upper'], params['loss_lower']) trader = Trader(params['pair'], params['candle_type'], buyer, seller, params['amount'], params['order_type'], params['asset_lower'], api) TimeTradeEnvironment(params['candle_type'], trader, api, params['pair']).run() except Exception: logger.error(traceback.format_exc()) logger.debug('process reboot') os.execv(sys.executable, [sys.executable] + ['trade/tr.py'])
def calc_entry_score(self, candle): end_dt = datetime.fromtimestamp(candle[5] / 1000) start_dt = (end_dt - timedelta(1)).strftime('%Y-%m-%d %H:%M:%S') end_dt = end_dt.strftime('%Y-%m-%d %H:%M:%S') features = None candle_types = ['1min', '5min', '15min', '30min'] for candle_type in candle_types: candles = self.api.get_candles(self.pair, candle_type=candle_type, start_dt=start_dt, end_dt=end_dt) candles = pd.DataFrame(candles, columns=[ 'open', 'high', 'low', 'close', 'volume', 'timestamp' ]) candles.index = candles.timestamp.map( lambda x: datetime.fromtimestamp(x / 1000)) params = get_params('1min') feature = _extract_feature(candles, params) feature = feature.fillna(method='ffill') feature.columns = [c + '_' + candle_type for c in feature.columns] if features is None: features = feature else: features = pd.concat([features, feature], axis=1) features = features.fillna(method='ffill') # 予測値を低く見積もって(25%点)さらにROCPの標準偏差も考慮した値が0より大きければロング X = features[features.index == end_dt] y_pred = self.clf.predict(X)[0] y_pred_upper = self.clf_upper.predict(X)[0] y_pred_lower = self.clf_lower.predict(X)[0] rocp_std = calc_rocp_std(self.api, self.pair, start_dt, end_dt) score = y_pred_lower - rocp_std result = { 'y_pred': y_pred, 'y_pred_upper': y_pred_upper, 'y_pred_lower': y_pred_lower, 'rocp_std': rocp_std, 'score': score } logger.debug(dict2str(result)) if score > 0: return [True, result] return [False, result]
def trade(self, candle): # 現在の資産を確認する assets = self.api.get_asset() if assets < self.asset_lower: raise InsufficientAssetsError # 板価格を確認する ticker_price_buy = self.api.get_ticker(self.pair)['buy'] ticker_price_sell = self.api.get_ticker(self.pair)['sell'] # 損切りを確認する if len(self.long_entries) > 0: price_bought = self.long_entries[sorted( self.long_entries.keys())[-1]]['average_price'] if self.seller.is_cut_loss(ticker_price_sell, price_bought): logger.debug( 'cut loss, ticker_price_buy: {}, price_bought: {}'.format( ticker_price_buy, price_bought)) active_sells = [ active_order for active_order in self.api.get_active_orders(self.pair)['orders'] if active_order['side'] == 'sell' ] # アクティブな注文はキャンセルする if len(active_sells) > 0: cancel_ids = [ active_id['order_id'] for active_id in active_sells ] self.api.cancel_orders(self.pair, cancel_ids) logger.debug('cancel order: {}'.format(cancel_ids)) # 買った数を計算する total_executed_amount_buy = 0 for order_id, entry in self.long_entries.items(): total_executed_amount_buy += float( entry['executed_amount']) # 売りの最安値で注文する logger.debug( 'order sell, pair: {}, price: {}, amount: {}, order type: {}' .format(self.pair, ticker_price_sell, total_executed_amount_buy, 'market')) res_order = self.api.order(self.pair, ticker_price_sell, total_executed_amount_buy, 'sell', 'market') logger.debug('order finished, ' + dict2str(res_order)) # 注文履歴を更新 self.update_long_entries(res_order) # 投資指標を計算 position, score = self.buyer.calc_score(candle) logger.debug('trade index, score: {}, position: {}'.format( score, position)) # 売買する if position == 'buy': # 買いの最高値で注文する logger.debug( 'order buy, pair: {}, price: {}, amount: {}, order type: {}'. format(self.pair, ticker_price_buy, self.amount, self.order_type)) res_order = self.api.order(self.pair, ticker_price_buy, self.amount, 'buy', self.order_type) logger.debug('order finished, ' + dict2str(res_order)) # 注文履歴を更新 self.update_long_entries(res_order) elif position == 'sell' and len(self.long_entries) > 0: active_sell = [ active_order for active_order in self.api.get_active_orders( self.pair)['orders'] if active_order['side'] == 'sell' ] print('{} {}'.format( len(active_sell), self.api.get_active_orders(self.pair)['orders'])) if len(active_sell) == 0: # 買った数を計算する total_executed_amount_buy = 0 for order_id, entry in self.long_entries.items(): total_executed_amount_buy += float( entry['executed_amount']) # 売りの最安値で注文する logger.debug( 'order sell, pair: {}, price: {}, amount: {}, order type: {}' .format(self.pair, ticker_price_sell, total_executed_amount_buy, self.order_type)) res_order = self.api.order(self.pair, ticker_price_sell, total_executed_amount_buy, 'sell', self.order_type) logger.debug('order finished, ' + dict2str(res_order)) # 注文履歴を更新 self.update_long_entries(res_order) ''' long_entries = self.long_entries.copy() for order_id, entry in long_entries.items(): if entry['status'] == 'ACTIVE': continue price_now = self.api.get_ticker(self.pair)['sell'] price_bought = float(entry['average_price']) is_exit, order_type = self.seller.is_exit_long(float(price_now), price_bought) if is_exit: logger.debug('order sell, pair: {}, price: {}, amount: {}, order type: {}'.format(self.pair, price_now, self.amount, order_type)) self.long_entries[order_id]['status'] = 'ACTIVE' res_order = self.api.order(self.pair, price_now, float(entry['executed_amount']), 'sell', order_type) logger.debug('order finished, ' + dict2str(res_order)) # 注文履歴を更新 self.update_long_entries(res_order, order_id) ''' # デバッグ用コメント total_executed_amount_buy = 0 for order_id, entry in self.long_entries.items(): total_executed_amount_buy += float(entry['executed_amount']) logger.debug( 'current orders, order num: {}, remained amount: {}'.format( len(self.long_entries), total_executed_amount_buy))