def get_last_n_bars(symbol, bar_type, n): end_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") VAR = md.get_last_n_bars(symbol, bar_type, n, end_time) z = len(VAR) var = [] for i in range(z): var.append(VAR[z - 1 - i]) ret = bar_topd(var, 'date') return ret
def get_last_n_bars(symbol, bar_type, n): end_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") VAR = md.get_last_n_bars(symbol, bar_type, n, end_time) z = len(VAR) var = [] for i in range(z): var.append(VAR[z-1-i]) ret = bar_topd(var,'date') return ret
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
#提起一分钟分时数据(freq=60 seconds) r = md.get_bars( 'SHSE.600000', 60, '2015-12-01 09:30:00', '2015-12-10 09:31:00', ) print('get_bars: ', len(r)) #提取bar快照 r = md.get_last_bars('SHSE.600000,', 60) print('get_last_bars: ', len(r)) #提取最新N笔bar数据 r = md.get_last_n_bars('SHSE.600000',60,10) print('get_last_n_bars(10): ', len(r)) #提取日频数据 r = md.get_dailybars( 'SHSE.600000', '2015-05-01 00:00:00', '2015-05-20 23:59:59') print('get_dailybars: ', len(r)) #提取dailybar快照 r = md.get_last_dailybars('SHSE.600000,') print('get_last_dailybars: ', len(r)) #提取最新N笔dailybar数据 r = md.get_last_n_dailybars('SHSE.600000', 10)
'CFFEX.IF1512', 10) print('get_last_n_ticks(10): ', len(r)) r = md.get_bars( 'CFFEX.IF1512', 60, '2015-05-01 09:30:00', '2015-05-10 09:31:00', ) print('get_bars: ', len(r)) r = md.get_last_bars('CFFEX.IF1512,', 60) print('get_last_bars: ', len(r)) r = md.get_last_n_bars( 'CFFEX.IF1512', 60, 10) print('get_last_n_bars(10): ', len(r)) r = md.get_dailybars( 'CFFEX.IF1512', '2015-05-01 00:00:00', '2015-05-20 23:59:59') print('get_dailybars: ', len(r)) r = md.get_last_dailybars('CFFEX.IF1512,') print('get_last_dailybars: ', len(r)) r = md.get_last_n_dailybars('CFFEX.IF1512', 10) print('get_last_n_dailybars(10): ', len(r))