def prepare_data_201109(): dates2 = [20110817,20110818,20110819,20110822,20110823,20110824,20110825,20110826,20110829,20110830,20110831,20110901,20110902,20110909,20110913,20110914] tickss = [] for idate in dates2: cur_ticks = hreader.read_ticks('IF1109',idate) prepare_index(cur_ticks) tickss.append(cur_ticks) return tickss
def prepare_data_201110(): dates = [20110914,20110915,20110916,20110919,20110920,20110921,20110922,20110923,20110926,20110927,20110928,20110929,20110930] tickss = [] for idate in dates: cur_ticks = hreader.read_ticks('IF1110',idate) prepare_index(cur_ticks) tickss.append(cur_ticks) return tickss
def prepare_data_201108(): dates = [20110714,20110715,20110718,20110719,20110720,20110721,20110722,20110725,20110726,20110727,20110728,20110729,20110801,20110802,20110803,20110804,20110805,20110808,20110809,20110810,20110811,20110812,20110815,20110816,20110817,20110818] tickss = [] for idate in dates: cur_ticks = hreader.read_ticks('IF1108',idate) prepare_index(cur_ticks) tickss.append(cur_ticks) return tickss
def testv1(capacity=200): ticks = hreader.read_ticks('IF1110',20110927) #不加载当日数据 results = [] for llen in range(5,500,2): trades=make_trades(lvama_opener(llen=llen,capacity=capacity),long_trailing_stop(),ticks,2,4) results.append((llen,sum([trade.profit for trade in trades]),len(trades))) if results[-1][1]>0: print results[-1] return results
def testv1(capacity=200): ticks = hreader.read_ticks('IF1110', 20110927) #不加载当日数据 results = [] for llen in range(5, 500, 2): trades = make_trades(lvama_opener(llen=llen, capacity=capacity), long_trailing_stop(), ticks, 2, 4) results.append( (llen, sum([trade.profit for trade in trades]), len(trades))) if results[-1][1] > 0: print results[-1] return results
def prepare_data_201110(): dates = [ 20110914, 20110915, 20110916, 20110919, 20110920, 20110921, 20110922, 20110923, 20110926, 20110927, 20110928, 20110929, 20110930 ] tickss = [] for idate in dates: cur_ticks = hreader.read_ticks('IF1110', idate) prepare_index(cur_ticks) tickss.append(cur_ticks) return tickss
def load(self): ''' 按顺序加载fname下的ticks数据. fname必须为IF1201这种格式 ''' sub_path = '%s%s' % (self.data_path,self.fname) tdates = sorted([int(os.path.splitext(name)[0][:8]) for name in os.listdir(sub_path) if self.rpattern.search(name) and name[0]!='.' and name[-1]!='~']) for tdate in tdates: cticks = hreader.read_ticks(self.fname,tdate) self.mtickss[tdate] = DTicks(self.fname,tdate,cticks) self.stickss.append(self.mtickss[tdate])
def prepare_data_201109(): dates2 = [ 20110817, 20110818, 20110819, 20110822, 20110823, 20110824, 20110825, 20110826, 20110829, 20110830, 20110831, 20110901, 20110902, 20110909, 20110913, 20110914 ] tickss = [] for idate in dates2: cur_ticks = hreader.read_ticks('IF1109', idate) prepare_index(cur_ticks) tickss.append(cur_ticks) return tickss
def trade_mock(instrument='IF1108'): #logging.basicConfig(filename="ctp_trade_mock.log",level=logging.DEBUG,format='%(name)s:%(funcName)s:%(lineno)d:%(asctime)s %(levelname)s %(message)s') tday = 20110726 myagent = create_agent_with_mocktrader(instrument,-1) #不需要tday的当日数据 myagent.scur_day = tday myagent.save_flag = True ticks = hreader.read_ticks(instrument,tday) #不加载当日数据 #for tick in ticks:myagent.inc_tick(),myagent.RtnTick(tick) #for tick in ticks: # myagent.inc_tick() # myagent.RtnTick(tick) run_ticks(ticks,myagent)
def prepare_data_201108(): dates = [ 20110714, 20110715, 20110718, 20110719, 20110720, 20110721, 20110722, 20110725, 20110726, 20110727, 20110728, 20110729, 20110801, 20110802, 20110803, 20110804, 20110805, 20110808, 20110809, 20110810, 20110811, 20110812, 20110815, 20110816, 20110817, 20110818 ] tickss = [] for idate in dates: cur_ticks = hreader.read_ticks('IF1108', idate) prepare_index(cur_ticks) tickss.append(cur_ticks) return tickss
def trade_mock(instrument='IF1108'): #logging.basicConfig(filename="ctp_trade_mock.log",level=logging.DEBUG,format='%(name)s:%(funcName)s:%(lineno)d:%(asctime)s %(levelname)s %(message)s') tday = 20110726 myagent = create_agent_with_mocktrader(instrument, -1) #不需要tday的当日数据 myagent.scur_day = tday myagent.save_flag = True ticks = hreader.read_ticks(instrument, tday) #不加载当日数据 #for tick in ticks:myagent.inc_tick(),myagent.RtnTick(tick) #for tick in ticks: # myagent.inc_tick() # myagent.RtnTick(tick) run_ticks(ticks, myagent)
def load(self): ''' 按顺序加载fname下的ticks数据. fname必须为IF1201这种格式 ''' sub_path = '%s%s' % (self.data_path, self.fname) tdates = sorted([ int(os.path.splitext(name)[0][:8]) for name in os.listdir(sub_path) if self.rpattern.search(name) and name[0] != '.' and name[-1] != '~' ]) for tdate in tdates: cticks = hreader.read_ticks(self.fname, tdate) self.mtickss[tdate] = DTicks(self.fname, tdate, cticks) self.stickss.append(self.mtickss[tdate])
def trade1day(opener,closer,instrument_name,tday,base_unit=2,fee=4): ''' opener: 开仓策略 closer: 平仓策略 instrument_name: 合约名 tday: 交易时间 必须保证在当前data目录下存在 instrument_name/tday.tick文件 base_unit: tick的最小变化单位 fee:手续费 返回 trades. 结构见make_trades ''' ticks = hreader.read_ticks(instrument_name,tday) #不加载当日数据 prepare_index(ticks) trades = make_trades(opener,closer,ticks,base_unit,fee) return trades
def trade1day(opener, closer, instrument_name, tday, base_unit=2, fee=4): ''' opener: 开仓策略 closer: 平仓策略 instrument_name: 合约名 tday: 交易时间 必须保证在当前data目录下存在 instrument_name/tday.tick文件 base_unit: tick的最小变化单位 fee:手续费 返回 trades. 结构见make_trades ''' ticks = hreader.read_ticks(instrument_name, tday) #不加载当日数据 prepare_index(ticks) trades = make_trades(opener, closer, ticks, base_unit, fee) return trades
def play(self,tday=0): ticks = hreader.read_ticks(self.instrument,tday) for tick in ticks: self.agent.RtnTick(tick)
def play(self, tday=0): ticks = hreader.read_ticks(self.instrument, tday) for tick in ticks: self.agent.RtnTick(tick) self.agent.RtnTick(tick)