def update_context_with_context(context): db = bs.SharedDatabase.tradeDatabase # 序列化 context serialize_ctx = pkl.dumps(context) ctx = { 'context': serialize_ctx, 'user_id': context.user_id, 'broker_id': context.broker_id, 'strategy_id': context.strategy_id, 'strategy_name': context.strategy_name, } ctx = bs.insertTime(ctx) if db.contexts.find({ 'user_id': ctx['user_id'], 'broker_id': ctx['broker_id'], 'strategy_id': ctx['strategy_id'], 'strategy_name': ctx['strategy_name'] }).count() > 0: db.contexts.update( { 'user_id': ctx['user_id'], 'broker_id': ctx['broker_id'], 'strategy_id': ctx['strategy_id'], 'strategy_name': ctx['strategy_name'] }, {"$set": ctx}) else: db.contexts.insert(ctx)
def updateContext(event): db = bs.SharedDatabase.tradeDatabase ctxs = event.dict # contexts 永远只保存正在运行的策略的context if len(ctxs) != 0: for ctx in ctxs: # 如果是日内清仓的 clear = True 删除context # 否则更新context if ctx['clear']: db.contexts.remove({ 'user_id': ctx['user_id'], 'broker_id': ctx['broker_id'] }) else: ctx = bs.insertTime(ctx) if db.contexts.find({ 'user_id': ctx['user_id'], 'broker_id': ctx['broker_id'] }).count() > 0: db.contexts.update( { 'user_id': ctx['user_id'], 'broker_id': ctx['broker_id'] }, {"$set": ctx}) else: db.contexts.insert(ctx)
def getStrategyInfo(dict): db = bs.SharedDatabase.tradeDatabase dict = bs.insertTime(dict) # result = list(db.send_order.find({'order_ref':int(dict['OrderRef']),'user_id':dict['InvestorID'],'broker_id':dict['BrokerID']})) # if len(result) > 0: # result = result[0] # dict['strategy_name'] = result['strategy_name'] # dict['strategy_id'] = result['strategy_id'] # else: # dict['strategy_name'] = '未知' # dict['strategy_id'] = '未知' return dict
def adjust_context(): db = bs.SharedDatabase.tradeDatabase result = db.contexts.find({ 'user_id': '00305188', 'broker_id': '6000', 'strategy_id': 'boll_00305188', 'strategy_name': '布林策略' }).limit(1) res = result.next()['context'] ctx = pkl.loads(res) ctx.portfolio.static_total_value = 101126 ctx.portfolio.daily_pnl = 28.6 ctx.portfolio.daily_commission = 28.6 serialize_ctx = pkl.dumps(ctx) dict = { 'context': serialize_ctx, 'user_id': ctx.user_id, 'broker_id': ctx.broker_id, 'strategy_id': ctx.strategy_id, 'strategy_name': ctx.strategy_name, } print(dict) ctxs = [dict] if len(ctxs) != 0: for ctx in ctxs: # 如果是日内清仓的 clear = True 删除context # 否则更新context # if ctx['clear']: # db.contexts.remove({'user_id': ctx['user_id'], 'broker_id': ctx['broker_id'], 'strategy_id':ctx['strategy_id'], 'strategy_name':ctx['strategy_name']}) # else: ctx = bs.insertTime(ctx) if db.contexts.find({ 'user_id': ctx['user_id'], 'broker_id': ctx['broker_id'], 'strategy_id': ctx['strategy_id'], 'strategy_name': ctx['strategy_name'] }).count() > 0: db.contexts.update( { 'user_id': ctx['user_id'], 'broker_id': ctx['broker_id'], 'strategy_id': ctx['strategy_id'], 'strategy_name': ctx['strategy_name'] }, {"$set": ctx}) else: db.contexts.insert(ctx)
def updatePosition(event): db = bs.SharedDatabase.tradeDatabase dict = bs.insertTime(event.dict) if db.position.find({ 'user_id': dict['user_id'], 'broker_id': dict['broker_id'], 'trading_day': dict['trading_day'] }).count() > 0: db.position.update( { 'user_id': dict['user_id'], 'broker_id': dict['broker_id'], 'trading_day': dict['trading_day'] }, {"$set": dict}) else: db.position.insert(dict)
def insertSendOrderArgs(event): db = bs.SharedDatabase.tradeDatabase # 枚举类型要转字符串保存 direction = str(event.dict['direction']).split('.')[-1] event.dict['direction'] = direction price_type = str(event.dict['price_type']).split('.')[-1] event.dict['price_type'] = price_type stop_price = str(event.dict['stop_price']).split('.')[-1] event.dict['stop_price'] = stop_price contingent_condition = str( event.dict['contingent_condition']).split('.')[-1] event.dict['contingent_condition'] = contingent_condition event.dict = bs.insertTime(event.dict) db.send_order.insert(event.dict)
def updateAccount(event): db = bs.SharedDatabase.tradeDatabase dict = bs.insertTime(event.dict) if db.account.find({ 'AccountID': dict['AccountID'], 'BrokerID': dict['BrokerID'], 'TradingDay': dict['TradingDay'] }).count() > 0: db.account.update( { 'AccountID': dict['AccountID'], 'BrokerID': dict['BrokerID'], 'TradingDay': dict['TradingDay'] }, {"$set": dict}) else: db.account.insert(dict)
def insertTick(tick): db = bs.SharedDatabase.futuresDatabase # db = bs.SharedDatabase.testDatabase tk = bs.insertTime(tick) collection_name = tl.symbol_classify(tk['InstrumentID']) db[collection_name].insert(tk)
def insertCancelOrderArgs(event): db = bs.SharedDatabase.tradeDatabase event.dict = bs.insertTime(event.dict) db.cancel_order.insert(event.dict)
def insertLog(event): db = bs.SharedDatabase.tradeDatabase dict = bs.insertTime(event.dict) db.log.insert(dict)
def insertSendOrderArgs(event): db = bs.SharedDatabase.tradeDatabase event.dict = bs.insertTime(event.dict) db.send_order.insert(event.dict)