def init(): tm = stbase.TradeManager().init(context,logpath='z:/ams/main',strategy_name='main') stock = tm.addStock('1000001') stock2 = tm.addStock('1300252') stock3 = tm.addStock('1300310') stock4 = tm.addStock('1300025') stock.add_tick_handler(on_tick) stock2.add_tick_handler(on_tick) stock3.add_tick_handler(on_tick) stock4.add_tick_handler(on_tick) # stock.add_bar_handler(on_bar_m1,'m1') stock.add_bar_handler(on_bar_m5,'m5') stock.add_bar_handler(on_bar_m15,'m15') stock2.add_bar_handler(on_bar_m5, 'm5') stock2.add_bar_handler(on_bar_m15, 'm15') stock3.add_bar_handler(on_bar_m5, 'm5') stock3.add_bar_handler(on_bar_m15, 'm15') stock4.add_bar_handler(on_bar_m5, 'm5') stock4.add_bar_handler(on_bar_m15, 'm15') print_line(hash_object(context.Strategy.Product)) prd = context.Strategy.Product print_line(prd.Name) print_line(prd.Stk_UseableAmt) print_line(prd.Stk_CurrentAmt) print_line(prd.Stk_StkValue) pos_list = prd.S_Pos print_line('len:{}'.format(len(pos_list))) pos_list=[] for pos in pos_list: print_line('ServerCode:{}'.format(pos.ServerCode)) print_line('BsFlag:{}'.format(pos.BsFlag)) print_line('CurrentQty:{}'.format(pos.CurrentQty)) print_line('PositionQty:{}'.format(pos.PositionQty)) print_line('TDTotalQty:{}'.format(pos.TDTotalQty)) print_line('YdQty:{}'.format(pos.YdQty)) print_line('TdQty:{}'.format(pos.TdQty)) print_line('YdClosingqty:{}'.format(pos.YdClosingqty)) print_line('TdClosingqty:{}'.format(pos.TdClosingqty)) print_line('MarginUsedAmt:{}'.format(pos.MarginUsedAmt)) print_line('OpenAvgPrice:{}'.format(pos.OpenAvgPrice)) print_line('PostCostAmt:{}'.format(pos.PostCostAmt)) print_line('--'*20) for bar in stock.get_hist_bars(ktype='week',limit=10): print_line(stock.bar_data(bar)) print 'last_price:{} ,yesterday_close_price:{}'.format(stock.last_price , stock.yesterday_close_price)
def on_bar(context, bars, intervals, source, rcv_time): print_line(' bars info '.center(40, '='), context.barlog) print_line( '>>on_bar()' + str(bars) + ' intervals:{} source:{} {}'.format(intervals, source, rcv_time), context.barlog) for code, bar in bars.items(): print_line(code + ' ' + str(hash_object(bar)), context.barlog) save_bar_data(code, bar, intervals)
def on_tick(context, md, source, rcv_time): return print '>>on_tick()', md, source, rcv_time print_line(' md info (Ticks) '.center(40, '='), context.ticklog) print_line(hash_object(md), context.ticklog) if context.trade: last_price = md.LastPrice # rid = context.insert_limit_order(source=SOURCE.XTP, # ticker=md.InstrumentID, # exchange_id=EXCHANGE.SZE, # price=last_price, # volume=100, # direction=DIRECTION.Buy, # offset=OFFSET.Open) context.trade = False else: print 'to stop()..'
def init(): tm = stbase.TradeManager().init(context, logpath='z:/ams/test') # stock = tm.addStock('1000001') # stock = tm.addStock('0600006') stock = tm.addStock('0600000') # stock = tm.addStock('1300123') # # stock = tm.addStock('1300252') # stock.add_tick_handler(on_tick) # stock.add_bar_handler(on_bar_m1,'m1') # stock.add_bar_handler(on_bar_m5,'m5') # stock.add_bar_handler(on_bar_m15,'m15') # print '='*20 print_line(hash_object(context.Strategy.Product)) prd = context.Strategy.Product print_line(prd.Name) print_line(prd.Stk_UseableAmt) print_line(prd.Stk_CurrentAmt) print_line(prd.Stk_StkValue) pos_list = prd.S_Pos print_line('len:{}'.format(len(pos_list))) pos_list = [] for pos in pos_list: # if not pos.ServerCode == '1000001': # if not pos.ServerCode == '0600000': # if not pos.ServerCode == '1300252': # continue print_line('ServerCode:{}'.format(pos.ServerCode)) print_line('BsFlag:{}'.format(pos.BsFlag)) print_line('CurrentQty:{}'.format(pos.CurrentQty)) print_line('PositionQty:{}'.format(pos.PositionQty)) print_line('TDTotalQty:{}'.format(pos.TDTotalQty)) print_line('YdQty:{}'.format(pos.YdQty)) print_line('TdQty:{}'.format(pos.TdQty)) print_line('YdClosingqty:{}'.format(pos.YdClosingqty)) print_line('TdClosingqty:{}'.format(pos.TdClosingqty)) print_line('MarginUsedAmt:{}'.format(pos.MarginUsedAmt)) print_line('OpenAvgPrice:{}'.format(pos.OpenAvgPrice)) print_line('PostCostAmt:{}'.format(pos.PostCostAmt)) print_line('--' * 20) # for bar in stock.get_hist_bars(ktype='week',limit=10): # print_line(stock.bar_data(bar)) # # print 'last_price:{} ,yesterday_close_price:{}'.format(stock.last_price , stock.yesterday_close_price) # simple_st.strategy_ma(stock.code,5) print_line('get_stock_amount_useable:%s' % stbase.TradeManager().xy_proxy.get_stock_amount_useable()) print_line('get_stock_amount_asset:%s' % stbase.TradeManager().xy_proxy.get_stock_amount_asset()) print_line('pos detail:%s' % stock.pos) # bars = stock.get_hist_bars('m1', limit=30) # bars = stock.get_hist_bars('m5', limit=30) # close = map(lambda _: _.Close, bars) # print_line(close) # test - ordering # stbase.TradeManager().addTimer(on_timer) # stock.add_trade_handler(on_trade) # print 'code:',stock.code # print 'net_yd:',stock.pos.net_yd # print str(stock) #查询委托订单 # print_line("orders:%s"% len(context.Strategy.All_Order)) orders = stbase.TradeManager().xy_proxy.get_orders().filter(code='0600008', serial=39917) for order in orders.to_list(): println(stbase.format_order(order))
def save_bar_data(code, bar, interval): # 非交易时间段丢弃 f = open('{}-{}.txt'.format(code, interval), 'a+') data = json.dumps(hash_object(bar)) f.write(data + '\n') f.close()