def get_daily_bars(stock_list, begin_time, end_time): # 连接本地终端时,td_addr为localhost:8001, if (td.init('*****@*****.**', 'zyj2590@1109', 'strategy_1') == 0): try: bars = md.get_dailybars(stock_list, begin_time, end_time) return bars except: pass
def read_last_n_kline(symbol_list, weeks_in_seconds, count, end_time): # 连接本地终端时,td_addr为localhost:8001, if (td.init('*****@*****.**', 'zyj2590@1109', 'strategy_1') == 0): # 类结构体转成dataframe columns = [ 'endtime', 'open', 'high', 'low', 'close', 'volume', 'amount' ] bars = 0 is_daily = (weeks_in_seconds == 240 * 60) data_list = [] # pd.DataFrame(None, columns=columns) ''' todo 整批股票读取有问题,数据取不全,放弃 stocks = '' for x in symbol_list: stocks+=','+x read_days=int(count*weeks_in_seconds/240/60)+1 start_date=md.get_calendar('SZSE', datetime.datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S') -datetime.timedelta(days=read_days),end_time)[0].strtime start_date=start_date[:10] +' 09:30:00' while start_date<end_time: bars=md.get_bars(stocks[1:], weeks_in_seconds, start_date, end_time) ''' for stock in symbol_list: #now = '[{0}] read k line'.format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) #print(now,stock) kdata = [] # 返回结果是bar类数组 if is_daily: bars = md.get_last_n_dailybars(stock, count, end_time) else: bars = md.get_last_n_bars(stock, weeks_in_seconds, count, end_time) for bar in bars: if is_daily: kdata.append([ int(bar.utc_time), bar.open, bar.high, bar.low, bar.close, bar.volume, bar.amount ]) else: kdata.append([ int(bar.utc_endtime), bar.open, bar.high, bar.low, bar.close, bar.volume, bar.amount ]) if len(bars) > 0: kdata = pd.DataFrame(kdata, columns=columns) kdata = kdata.sort_values(by='endtime', ascending=False) data_list.append({'code': stock, 'kdata': kdata}) return data_list
def get_stock_by_market(exchange, sec_type, is_active, return_list=True): # 连接本地终端时,td_addr为localhost:8001, if (td.init('*****@*****.**', 'zyj2590@1109', 'strategy_1') == 0): try: stock_list = "" css = md.get_instruments(exchange, sec_type, is_active) if return_list: stock_list = [cs.symbol for cs in css] else: for cs in css: stock_list += "," + cs.symbol return stock_list[1:] except: pass
def get_index_stock(index_symbol, return_list=True): # 连接本地终端时,td_addr为localhost:8001, if (td.init('*****@*****.**', 'zyj2590@1109', 'strategy_1') == 0): try: stock_list = "" css = md.get_constituents(index_symbol) if return_list: stock_list = [cs.symbol for cs in css] else: for cs in css: stock_list += "," + cs.symbol return stock_list[1:] except: pass
orders = td.get_unfinished_orders() print('query unfinished orders: ', orders) # 策略已配置实盘账户,查询策略关联账户的相关信息 # 获取柜台交易账号列表 # r = td.get_broker_accounts() # print ('get_broker_accounts', [to_dict(i) for i in r]) # 获取柜台交易账号资金 # r = td.get_broker_cash() # print ('get_broker_cash', [to_dict(i) for i in r]) # 获取柜台交易账号持仓 # r = td.get_broker_positions() # print ('get_broker_positions', [to_dict(i) for i in r]) # register callbacks td.ev_execrpt += on_execrpt td.ev_error += on_error td.ev_login += on_login td.ev_order_status += on_order_status #连接本地终端时,td_addr为localhost:8001, #ret = td.init('*****@*****.**', '123456', 'strategy_1', 'localhost:8001') ret = td.init('*****@*****.**', '123456', 'strategy_1') print('td init result: ', ret) td.run()
orders = td.get_unfinished_orders() print('query unfinished orders: ', orders) # 策略已配置实盘账户,查询策略关联账户的相关信息 # 获取柜台交易账号列表 # r = td.get_broker_accounts() # print ('get_broker_accounts', [to_dict(i) for i in r]) # 获取柜台交易账号资金 # r = td.get_broker_cash() # print ('get_broker_cash', [to_dict(i) for i in r]) # 获取柜台交易账号持仓 # r = td.get_broker_positions() # print ('get_broker_positions', [to_dict(i) for i in r]) # register callbacks td.ev_execrpt += on_execrpt td.ev_error += on_error td.ev_login += on_login td.ev_order_status += on_order_status #连接本地终端时,td_addr为localhost:8001, #ret = td.init('*****@*****.**', '123456', 'strategy_1', 'localhost:8001') ret = td.init('*****@*****.**', '123456', 'strategy_1') print('td init result: ', ret) td.run()
def read_kline(symbol_list, weeks_in_seconds, begin_time, end_time, max_record=50000): #连接本地终端时,td_addr为localhost:8001, if (td.init('*****@*****.**', 'zyj2590@1109', 'strategy_1') == 0): #类结构体转成dataframe kdata = [] columns = [ 'endtime', 'open', 'high', 'low', 'close', 'volume', 'amount' ] bars = 0 is_daily = (weeks_in_seconds == 240 * 60) while (True): # 返回结果是bar类数组 if is_daily: bars = md.get_dailybars(symbol_list, begin_time, end_time) else: bars = md.get_bars(symbol_list, weeks_in_seconds, begin_time, end_time) for bar in bars: if is_daily: kdata.append([ int(bar.utc_time), bar.open, bar.high, bar.close, bar.low, bar.volume, bar.amount ]) else: kdata.append([ int(bar.utc_endtime), bar.open, bar.high, bar.close, bar.low, bar.volume, bar.amount ]) count = len(bars) #TODO 一次最多处理10000项以内数据,超出应有所提示 if (count==0 or len(kdata)>max_record) \ or (not is_daily and bars[count - 1].strendtime >= end_time) \ or (is_daily and bars[count - 1].strtime >= end_time) : break #print("read [%s] k line:%s count=%d" % (symbol_list, # bars[0].strtime[:10] + ' ' + bars[0].strtime[11:19], count)) if is_daily: if count <= 10: break else: begin_time = bars[count - 1].strtime[:10] \ + ' ' + bars[count - 1].strtime[11:19] else: begin_time=bars[count-1].strendtime[:10]\ +' '+bars[count-1].strendtime[11:19] ''' count=len(kdata) if count>0: print("total count:%d,end date:%s " % (count,timestamp_datetime(kdata[count-1][0]))) else: print("No data" ) ''' return pd.DataFrame(kdata, columns=columns)
# query order order = td.get_order(order2.cl_ord_id) print('query order: ', order_to_dict(order) if order else '') # query cash cash = td.get_cash() print('query cash: ', cash_to_dict(cash) if cash else '') # query position position = td.get_position('CFFEX', 'IF1512', OrderSide_Bid) print('query position: ', position_to_dict(position) if position else '') # query all positions positions = td.get_positions() print('query all positions: ', positions) # get unfinished orders orders = td.get_unfinished_orders() print('query unfinished orders: ', orders) # register callbacks td.ev_execrpt += on_execrpt td.ev_error += on_error td.ev_login += on_login td.ev_order_status += on_order_status # init td ret = td.init('*****@*****.**', '123456', 'strategy_2', 'localhost:8001') print('td init result: ', ret) td.run()