def show_create_trader_page(): # trader list 가져오기 trader_list = dao.query(Trader.name).filter(Trader.use_yn == 'Y').all() coin_list = dao.query(CoinoneTick.currency).group_by( CoinoneTick.currency).all() return render_template('trader_create.html', trader_list=trader_list, coin_list=coin_list)
def stop_trader(): data = request.json Log.info(data) end_dt = strftime('%Y/%m/%d %H:%M:%S', localtime()) g = RUNNING_TRADER.pop(data['run_no']) g.kill() # update dao.query(SimulTraderRunHist.end_dt)\ .filter(SimulTraderRunHist.run_no == data['run_no'])\ .update({SimulTraderRunHist.end_dt: end_dt}) dao.commit() return json.dumps({'status': 'OK', 'end_dt': end_dt})
def rsi_trader_alarm(run_no): run_info = dao.query(SimulTraderRunHist).filter( SimulTraderRunHist.run_no == run_no).first() trader = rsi_trader_v01(dao, run_info) return_buy, volume, rsi = trader.buy() return_sell, revenue_rate = trader.sell() log_info = '[TRADER %s Called] RSI: %s 수익률: %s' % (run_info.run_no, rsi, revenue_rate) Log.info(log_info) if return_buy is not None: Log.info( '[buy order alarm] run_no: %s buy_price: %s volume: %s rsi: %s' % (run_info.run_no, return_buy, volume, rsi)) if return_sell is not None: Log.info( '[sell order alarm] run_no: %s sell_price: %s revenue_rate: %s' % (run_info.run_no, return_sell, revenue_rate)) dst_namespace = '/trader/log/' + str(run_no) socketio.emit('message', log_info, broadcast=True, namespace=dst_namespace) dao.remove()
def create_trader(): try: data = request.form.to_dict() Log.info(data) trader_name = data.pop('trader') interval = is_positive_number(int(data.pop('interval'))) balance = is_positive_number(int(data.pop('balance'))) currency = data.pop('currency') trader_parm = data except Exception as e: Log.info(e) return redirect(url_for('.show_create_trader_page')) trader_no = dao.query( Trader.trader_no).filter(Trader.name == trader_name).all()[0][0] # run_no: auto_increment dao.add( SimulTraderRunHist(trader_no=trader_no, currency=currency, time_interval=interval, init_balance=balance, trader_parm=trader_parm)) dao.commit() return redirect(url_for('.show_traders'))
def get_seq(): run_hist = request.json Log.info(run_hist) show_time_unit = '%Y,%m,%d' yesterday = datetime.now() tick_data = dao.query(func.date_format(CoinoneTick.create_dt, '%Y,%m,%d,%H'), func.avg(CoinoneTick.last)) \ .filter_by(currency=run_hist['currency']) \ .filter(func.date_format(CoinoneTick.create_dt, '%Y,%m,%d,%H') >= yesterday.strftime(show_time_unit))\ .group_by(func.date_format(CoinoneTick.create_dt, '%Y,%m,%d,%H')) \ .order_by(func.date_format(CoinoneTick.create_dt, '%Y,%m,%d,%H').asc()) \ .limit(24).all() Log.debug(tick_data) tick_list = [(time.split(','), float(tick)) for (time, tick) in tick_data] tick_last = [float(tick) for time, tick in tick_data] y_max = round(np.max(tick_last)) + round(np.max(tick_last) * 0.1) y_min = round(np.min(tick_last)) - round(np.min(tick_last) * 0.1) return json.dumps({ 'status': 'OK', 'currency': run_hist['currency'], 'ticks': tick_list, 'max': y_max, 'min': y_min })
def run_trader(): data = request.json Log.info(data) # start trader start_dt = strftime('%Y/%m/%d %H:%M:%S', localtime()) Log.debug(start_dt) interval = dao.query(SimulTraderRunHist.time_interval).filter( SimulTraderRunHist.run_no == data['run_no']).first()[0] g = eventlet.spawn(enrollment_trader, rsi_trader_alarm, data['run_no'], interval) RUNNING_TRADER[data['run_no']] = g # update dao.query(SimulTraderRunHist.start_dt) \ .filter(SimulTraderRunHist.run_no == data['run_no']) \ .update({SimulTraderRunHist.start_dt: start_dt}) dao.commit() return json.dumps({'status': 'OK', 'start_dt': start_dt})
def show_traders(): trader_list = dao.query(SimulTraderRunHist, Trader)\ .filter(SimulTraderRunHist.end_dt.is_(None))\ .filter(Trader.trader_no == SimulTraderRunHist.trader_no)\ .all() is_alive = dict() for run_no in RUNNING_TRADER.keys(): is_alive[run_no] = True return render_template('traders.html', trader_list=trader_list, is_alive=is_alive)
def update_last_tick(currency='btc'): queries = dao.query(CoinoneTick) tick = queries.filter_by(currency=currency).order_by( CoinoneTick.timestamp.desc()).first() tick_dict = dict() tick_dict['create_dt'] = tick.create_dt.strftime("%Y-%m-%d %H:%M:%S") tick_dict['currency'] = tick.currency tick_dict['last'] = tick.last tick_dict['rise_fall_rate'] = round( ((tick.last - tick.yesterday_last) / tick.yesterday_last) * 100, 2) Log.info(tick_dict) socketio.emit('message', tick_dict, broadcast=True, namespace='/tick') eventlet.sleep(0) dao.remove()
def show_trader_order_hist(): data = request.json Log.info(data) orders = dao.query(SimulTraderOrder)\ .filter(SimulTraderOrder.run_no == data['run_no'])\ .order_by(SimulTraderOrder.create_dt.desc())\ .all() orders = [order.__dict__ for order in orders] order_list = [] for order in orders: order.pop('_sa_instance_state') order.pop('update_dt') order['create_dt'] = order['create_dt'].strftime('%Y/%m/%d %H:%M:%S') order_list.append(order) return json.dumps({'status': 'OK', 'order_list': order_list})
# # def __init__(self, trader_no, init_balance, invoke_interval): # self.trader_no = trader_no # self.cur_balance = init_balance # self.init_balance = init_balance # self.invoke_interval = invoke_interval # # self.num_of_order = 0 # self.revenue_rate = 0 # # self.create_dt = strftime('%Y/%m/%d %H:%M:%S', localtime()) # self.update_dt = self.create_dt if __name__ == '__main__': from coincast.database import DBManager db_url = 'mysql://*****:*****@49.142.50.199/CC_REAL?charset=utf8' DBManager.init(db_url) DBManager.init_db() from coincast.database import dao queries = dao.query(SimulTraderRunHist) entries = [ dict(trader_no=q.trader_no, run_no=q.run_no, interval=q.time_interval) for q in queries ] dao.commit() print(entries[:10])
def get_parm_list(): trader = request.json trader_parm = dao.query( Trader.trader_parm).filter(Trader.name == trader['trader']).first() return json.dumps({'status': 'OK', 'trader_parm': trader_parm})
self.run_no = run_no self.type = _type self.price = price self.volume = volume self.success_yn = 'Y' self.create_dt = strftime('%Y/%m/%d %H:%M:%S', localtime()) self.update_dt = self.create_dt # class RealTraderOrder(Base): # __tablename__ = 'real_trader_order_hist' # pass if __name__ == '__main__': from coincast.database import DBManager db_url = 'mysql://*****:*****@49.142.50.199/CC_REAL?charset=utf8' DBManager.init(db_url) DBManager.init_db() from coincast.database import dao queries = dao.query(SimulTraderOrder) entries = [ dict(order_no=q.order_no, run_no=q.run_no, type=q.type) for q in queries ] dao.commit() print(entries[:10])