def test_local_data(self): from quantdigger.datasource.data import LocalData from quantdigger.util import pcontract """ 测试本地数据接口 """ db = LocalData() target = db.load_data(pcontract('BB.SHFE', '1.Minute')) fname = os.path.join(os.getcwd(), 'data', 'CC.SHFE-1.Minute.csv') source = pd.read_csv(fname, parse_dates='datetime', index_col='datetime') self.assertFalse(source.equals(target), '本地数据接口负测试失败!') fname = os.path.join(os.getcwd(), 'data', 'BB.SHFE-1.Minute.csv') source = pd.read_csv(fname, parse_dates='datetime', index_col='datetime') self.assertTrue(source.equals(target), '本地数据接口正测试失败!') logger.info('----------------- 本地数据接口测试成功 -------------------')
def on_bar(self): """ 策略函数,对每根Bar运行一次。""" #self.ma2.update(average(self.open, 10)) if self.ma10[1] < self.ma20[1] and self.ma10 > self.ma20: self.buy('long', self.open, 1, contract = 'IF000.SHFE') elif self.position() > 0 and self.ma10[1] > self.ma20[1] and self.ma10 < self.ma20: self.sell('long', self.open, 1) # 夸品种数据引用 print self.open_(1)[1], self.open #print self.position(), self.cash() #print self.datetime, self.b_upper, self.b_middler, self.b_lower if __name__ == '__main__': try: pcon = pcontract('IF000.SHFE', '10.Minute') begin_dt, end_dt = None, None #begin_dt, end_dt = '2015-05-25', '2015-06-01' #pcon = stock('600848','10.Minute') # 通过tushare下载股票数据 simulator = ExecuteUnit([pcon, pcon], begin_dt, end_dt) algo = DemoStrategy(simulator) #algo1 = DemoStrategy(simulator) #algo2 = DemoStrategy(simulator) simulator.run() #for deal in algo.blotter.deal_positions: ## code... #print("----------------") #print("开仓时间: %s;成交价格: %f;买卖方向: %s;成交量: %d;") % \ #(deal.open_datetime, deal.open_price, Direction.type_to_str(deal.direction), deal.quantity) #print("平仓时间: %s;成交价格: %f;买卖方向: %s;成交量: %d;盈亏: %f;") % \
def on_bar(self): """ 策略函数,对每根Bar运行一次。""" #self.ma2.update(average(self.open, 10)) if self.ma10[1] < self.ma20[1] and self.ma10 > self.ma20: self.buy('long', self.open, 1, contract = 'IF000.SHFE') elif self.position() > 0 and self.ma10[1] > self.ma20[1] and self.ma10 < self.ma20: self.sell('long', self.open, 1) # 夸品种数据引用 #print self.position(), self.cash() #print self.datetime, self.b_upper, self.b_middler, self.b_lower #print self.datetime[0] if __name__ == '__main__': try: pcon = pcontract('BB.SHFE', '1.Minute') #begin_dt, end_dt = '2015-05-25', '2015-06-01' #pcon = stock('600848','10.Minute') # 通过tushare下载股票数据 simulator = ExecuteUnit([pcon, pcon], '2013-12-12', '2013-12-25') algo = DemoStrategy(simulator) #algo1 = DemoStrategy(simulator) #algo2 = DemoStrategy(simulator) simulator.run() #for deal in algo.blotter.deal_positions: ## code... #print("----------------") #print("开仓时间: %s;成交价格: %f;买卖方向: %s;成交量: %d;") % \ #(deal.open_datetime, deal.open_price, Direction.type_to_str(deal.direction), deal.quantity) #print("平仓时间: %s;成交价格: %f;买卖方向: %s;成交量: %d;盈亏: %f;") % \ #(deal.close_datetime, deal.close_price, Direction.type_to_str(deal.direction), deal.quantity, deal.profit())
def on_bar(self): """ 策略函数,对每根Bar运行一次。""" #self.ma2.update(average(self.open, 10)) if self.ma10[1] < self.ma20[1] and self.ma10 > self.ma20: self.buy('long', self.open, 1, contract = 'IF000.SHFE') elif self.position() > 0 and self.ma10[1] > self.ma20[1] and self.ma10 < self.ma20: self.sell('long', self.open, 1) # 夸品种数据引用 #print self.position(), self.cash() #print self.datetime, self.b_upper, self.b_middler, self.b_lower #print self.datetime[0] if __name__ == '__main__': try: pcon = pcontract('BB.SHFE', '1.Minute') #begin_dt, end_dt = '2015-05-25', '2015-06-01' #pcon = stock('600848','10.Minute') # 通过tushare下载股票数据 simulator = ExecuteUnit([pcon, pcon]) algo = DemoStrategy(simulator) #algo1 = DemoStrategy(simulator) #algo2 = DemoStrategy(simulator) simulator.run() # 显示回测结果 from quantdigger.datastruct import TradeSide ping = 0 kai = 0 for t in algo.blotter.transactions: if t.side == TradeSide.PING:
if self.ma10[1] < self.ma20[1] and self.ma10 > self.ma20: self.buy('long', self.open, 1, contract='IF000.SHFE') elif self.position( ) > 0 and self.ma10[1] > self.ma20[1] and self.ma10 < self.ma20: self.sell('long', self.open, 1) # 夸品种数据引用 print self.open_(1)[1], self.open #print self.position(), self.cash() #print self.datetime, self.b_upper, self.b_middler, self.b_lower if __name__ == '__main__': try: begin_dt, end_dt = None, None pcon = pcontract('IF000.SHFE', '10.Minute') pcon = stock('600848') # 通过tushare下载股票数据 simulator = ExecuteUnit([pcon, pcon], begin_dt, end_dt) algo = DemoStrategy(simulator) algo1 = DemoStrategy(simulator) algo2 = DemoStrategy(simulator) simulator.run() for deal in algo.blotter.deal_positions: # code... print("----------------") print("开仓时间: %s;成交价格: %f;买卖方向: %s;成交量: %d;") % \ (deal.open_datetime, deal.open_price, Direction.type_to_str(deal.direction), deal.quantity) print("平仓时间: %s;成交价格: %f;买卖方向: %s;成交量: %d;盈亏: %f;") % \ (deal.close_datetime, deal.close_price, Direction.type_to_str(deal.direction), deal.quantity, deal.profit())
if self.ma10[1] < self.ma20[1] and self.ma10 > self.ma20: self.buy('long', self.open, 1, contract = 'IF000.SHFE') elif self.position() > 0 and self.ma10[1] > self.ma20[1] and self.ma10 < self.ma20: self.sell('long', self.open, 1) # 输出pcon1的当前K线开盘价格。 print(self.open) # 夸品种数据引用 # pcon2的前一根K线开盘价格。 print(self.open_(1)[1]) if __name__ == '__main__': try: # 策略的运行对象周期合约 pcon1 = pcontract('IF000.SHFE', '10.Minute') pcon2 = pcontract('IF000.SHFE', '10.Minute') # 创建模拟器,这里假设策略要用到两个不同的数据,比如套利。 simulator = ExecuteUnit([pcon1, pcon2]); # 创建策略。 algo = DemoStrategy(simulator) # 运行模拟器,这里会开始事件循环。 simulator.run() # 显示回测结果 plotting.plot_result(simulator.data[pcon], algo._indicators, algo.blotter.deal_positions, algo.blotter) except Exception, e: print(e)
# self.ma2.update(average(self.open, 10)) if self.ma10[1] < self.ma20[1] and self.ma10 > self.ma20: self.buy("long", self.open, 1, contract="IF000.SHFE") elif self.position() > 0 and self.ma10[1] > self.ma20[1] and self.ma10 < self.ma20: self.sell("long", self.open, 1) # 夸品种数据引用 print self.open_(1)[1], self.open # print self.position(), self.cash() # print self.datetime, self.b_upper, self.b_middler, self.b_lower if __name__ == "__main__": try: begin_dt, end_dt = None, None pcon = pcontract("IF000.SHFE", "10.Minute") # pcon = stock('600848') # 通过tushare下载股票数据 simulator = ExecuteUnit([pcon, pcon], begin_dt, end_dt) algo = DemoStrategy(simulator) # algo1 = DemoStrategy(simulator) # algo2 = DemoStrategy(simulator) simulator.run() # for deal in algo.blotter.deal_positions: ## code... # print("----------------") # print("开仓时间: %s;成交价格: %f;买卖方向: %s;成交量: %d;") % \ # (deal.open_datetime, deal.open_price, Direction.type_to_str(deal.direction), deal.quantity) # print("平仓时间: %s;成交价格: %f;买卖方向: %s;成交量: %d;盈亏: %f;") % \ # (deal.close_datetime, deal.close_price, Direction.type_to_str(deal.direction), deal.quantity, deal.profit())
if self.ma10[1] < self.ma20[1] and self.ma10 > self.ma20: self.buy("long", self.open, 1, contract="IF000.SHFE") elif self.position() > 0 and self.ma10[1] > self.ma20[1] and self.ma10 < self.ma20: self.sell("long", self.open, 1) # 输出pcon1的当前K线开盘价格。 print(self.open) # 夸品种数据引用 # pcon2的前一根K线开盘价格。 print(self.open_(1)[1]) if __name__ == "__main__": try: # 策略的运行对象周期合约 pcon1 = pcontract("IF000.SHFE", "10.Minute") pcon2 = pcontract("IF000.SHFE", "10.Minute") # 创建模拟器,这里假设策略要用到两个不同的数据,比如套利。 simulator = ExecuteUnit([pcon1, pcon2]) # 创建策略。 algo = DemoStrategy(simulator) # 运行模拟器,这里会开始事件循环。 simulator.run() # 显示回测结果 plotting.plot_result(simulator.data[pcon], algo._indicators, algo.blotter.deal_positions, algo.blotter) except Exception, e: print(e)