def pull_ticks(): underlyings = list(gv.UNDL_POOL['total']) ticks = md.get_last_ticks(','.join(underlyings)) for tick in ticks: tempdata = [] for col in gv.POOL_COLUMNS: tempdata.append(eval(vars[col])) #UNDL_POOL_INFO[addfix(tick.sec_id)] = tempdata gv.UNDL_POOL_INFO[RawHoldingStocks.addfix( tick.sec_id)] = tempdata
#!/usr/bin/env python # -*- coding: utf-8 -*- from gmsdk import md, to_dict md.init('*****@*****.**', '123456') # 提取tick数据 r = md.get_ticks( 'SHSE.600000', '2016-01-04 09:00:00', '2016-01-04 12:00:00', ) print('get_ticks: ', len(r)) #提取tick快照 r = md.get_last_ticks('SHSE.600000') print('get_last_ticks: ', len(r)) #提取最新N笔tick数据 r = md.get_last_n_ticks( 'SHSE.600000', 10) print('get_last_n_ticks(10): ', len(r)) #提起一分钟分时数据(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))
from gmsdk import md md.init('*****@*****.**', '123456') r = md.get_ticks( 'CFFEX.IF1512,CFFEX.IF1512', '2015-03-19 11:29:00', '2015-03-19 12:00:00', ) print('get_ticks: ', len(r)) r = md.get_last_ticks('CFFEX.IF1512,') print('get_last_ticks: ', len(r)) r = md.get_last_n_ticks( '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',
def get_last_ticks(symbol_list): symbol_list = mtsymbol_list(symbol_list) var = md.get_last_ticks(symbol_list) ret = tick_topd(var,'code') return ret
def get_last_ticks(symbol_list): symbol_list = mtsymbol_list(symbol_list) var = md.get_last_ticks(symbol_list) ret = tick_topd(var, 'code') return ret
def data_subscribe(source): """ 数据源订阅 , pool_columns 提供需要订阅的字段,需要更新 undl_pool_info """ COLNUM = len(gv.POOL_COLUMNS) if source == 'wind': # 定义数据源对应 callback 函数 def wind_callback(indata): if indata.ErrorCode != 0: raise Exception('Error in callback with ErrorCode %d' % indata.ErrorCode) # 实际使用时,为防止中断可改为log输出 for dumi in range(len(indata.Codes)): fieldlen = len(indata.Fields) if fieldlen == COLNUM: # 只有在所有field都有数据的时候才存储 tempdata = [] for dumj in range(fieldlen): tempdata.append(indata.Data[dumj][dumi]) gv.UNDL_POOL_INFO[indata.Codes[dumi]] = tempdata w.start() underlyings = list(gv.UNDL_POOL['total']) w.wsq(','.join(underlyings), ','.join(gv.POOL_COLUMNS), func=wind_callback) print('data subscribed with source %s' % source) elif source == 'goldmine': vars = {'rt_last': 'tick.last_price', 'rt_time': 'tick.str_time'} def on_tick(tick): tempdata = [] for col in gv.POOL_COLUMNS: tempdata.append(eval(vars[col])) gv.UNDL_POOL_INFO[addfix(tick.sec_id)] = tempdata # 提取当前资产池中的代码,并转换为gm所需格式 underlyings = list(gv.UNDL_POOL['total']) ret = md.init(username="******", password="******", mode=3) if ret != 0: raise Exception('Error in initiation with ErrorCode %d' % ret) ret = md.subscribe(','.join(underlyings)) if ret != 0: raise Exception('Error in subscribe with ErrorCode %d' % ret) # 添加回调函数 md.ev_tick += on_tick # 初始填充POOL,确保不会出现 NAN fillundl = ','.join(underlyings).replace('.tick', '') ticks = md.get_last_ticks(fillundl) for tick in ticks: tempdata = [] for col in gv.POOL_COLUMNS: tempdata.append(eval(vars[col])) #UNDL_POOL_INFO[addfix(tick.sec_id)] = tempdata gv.UNDL_POOL_INFO[RawHoldingStocks.addfix(tick.sec_id).addfix( tick.sec_id)] = tempdata # 加入线程 data_thread = threading.Thread(target=md.run) if not gv.PRE_THREADS.get(source): # 如果是第一次建立线程则创建,否则只要重新订阅 gv.PRE_THREADS[source] = data_thread data_thread.start() else: ret = md.resubscribe(','.join(underlyings)) if ret != 0: raise Exception('Error in subscribe with ErrorCode %d' % ret) print(threading.enumerate()) print('data subscribed with source %s' % source) elif source == 'goldmine_snapshot': vars = {'rt_last': 'tick.last_price', 'rt_time': 'tick.str_time'} def pull_ticks(): underlyings = list(gv.UNDL_POOL['total']) ticks = md.get_last_ticks(','.join(underlyings)) for tick in ticks: tempdata = [] for col in gv.POOL_COLUMNS: tempdata.append(eval(vars[col])) #UNDL_POOL_INFO[addfix(tick.sec_id)] = tempdata gv.UNDL_POOL_INFO[RawHoldingStocks.addfix( tick.sec_id)] = tempdata ret = md.init(username="******", password="******", mode=1) if ret != 0: raise Exception('Error in initiation with ErrorCode %d' % ret) data_thread = NewThread(target=pull_ticks) if not gv.PRE_THREADS.get(source): # 如果是第一次建立线程则创建,否则先关闭老线程,再开启新线程 gv.PRE_THREADS[source] = data_thread else: gv.PRE_THREADS[source].stop() gv.PRE_THREADS[source] = data_thread data_thread.start() print(threading.enumerate()) print('data subscribed with source %s' % source) elif source == 'simulation': def simugen(pathtype='Brownian', step=1): """ 模拟行情数据生成器 """ colnum = len(gv.POOL_COLUMNS) holdings = gv.UNDL_POOL['total'] for undl in holdings: if undl not in gv.UNDL_POOL_INFO: gv.UNDL_POOL_INFO[undl] = np.random.rand(1, colnum)[0] else: if pathtype == 'Geometric': trend = np.random.randn(1, colnum)[0] sigma = np.random.rand(1, colnum)[0] gv.UNDL_POOL_INFO[undl] *= np.exp( trend * step + sigma * np.sqrt(step) * np.random.randn(1, colnum))[0] else: gv.UNDL_POOL_INFO[undl] += np.sqrt( step) * np.random.randn(1, colnum)[0] data_thread = NewThread(target=simugen) if not gv.PRE_THREADS.get(source): # 如果是第一次建立线程则创建,否则先关闭老线程,再开启新线程 gv.PRE_THREADS[source] = data_thread else: gv.PRE_THREADS[source].stop() gv.PRE_THREADS[source] = data_thread data_thread.start() print(threading.enumerate()) print('data subscribed with source: %s' % source) else: print('No source infomation provided, can not subscribe!')