问题描述
我刚开始使用backTrader。
我已经从pandas数据框中获取了1分钟的蜡烛数据。
运行backTrader时,我在每个购买订单后收到以下日志消息:“订单已取消/保证金/已拒绝”
我正在使用jupyter笔记本
这是我的交易策略:
import backTrader
class TestStrategy(backTrader.Strategy):
def log(self,txt,dt=None):
''' Logging function for this strategy'''
dt = dt or self.datas[0].datetime.date(0)
print('%s,%s' % (dt.isoformat(),txt))
def __init__(self):
# Keep a reference to the "close" line in the data[0] dataseries
self.dataclose = self.datas[0].close
self.order = None
def notify_order(self,order):
# if order.status in [order.Submitted,order.Accepted]:
# return
# if order.status in [order.Completed]:
# if order.isbuy():
# self.log("BUY EXECUTED {}".format(order.executed.price))
# elif order.issell():
# self.log("SELL EXECUTED {}".format(order.executed.price))
# self.bar_executed = len(self)
if order.status in [order.Submitted,order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - nothing to do
return
# Check if an order has been completed
# Attention: broker Could reject order if not enough cash
if order.status in [order.Completed]:
if order.isbuy():
self.log('BUY EXECUTED,%.2f' % order.executed.price)
elif order.issell():
self.log('SELL EXECUTED,%.2f' % order.executed.price)
self.bar_executed = len(self)
elif order.status in [order.Canceled,order.Margin,order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
self.order = None
def next(self):
# Simply log the closing price of the series from the reference
self.log('Close,%.2f' % self.dataclose[0])
# print(len(self))
# print(self.order)
# print(self.position)
# print("self.bar_executed: ")
# print(self.bar_executed)
#if in order do nothing
if self.order:
return
#if NOT in order (do buy or sell)
#video tutorial https://www.youtube.com/watch?v=K8buXUxEfMc&ab_channel=PartTimeLarry
if not self.position:
if self.dataclose[0] < self.dataclose[-1]:
self.log('BUY CREATE %.2f' % self.dataclose[0])
self.order = self.buy()
else:
if len(self) >= (self.bar_executed + 2):
#self.log('SELL CREATE %.2f' % self.dataclose[0])
self.log("SELL CREATED {}".format(self.dataclosed[0]))
self.order = self.sell()
这是我的主要代码
import backTrader
class PandasData_Custom(bt.Feeds.PandasData):
# params = (
# ('datetime',0),# ('open',1),# ('high',None),# ('low',# ('close',2),# ('volume',# )
params = (
# Possible values for datetime (must always be present)
# None : datetime is the "index" in the Pandas Dataframe
# -1 : autodetect position or case-wise equal name
# >= 0 : numeric index to the colum in the pandas dataframe
# string : column name (as index) in the pandas dataframe
('datetime',# Possible values below:
# None : column not present
# -1 : autodetect position or case-wise equal name
# >= 0 : numeric index to the colum in the pandas dataframe
# string : column name (as index) in the pandas dataframe
('open',-1),('high',('low',('close',('volume',# ('openinterest',)
Feed2 = PandasData_Custom(dataname=bt_Feed)
cerebro = backTrader.Cerebro()
cerebro.broker.setcash(1000000)
#for Feed data look here https://stackoverflow.com/questions/62301378/backTrader-error-dataframe-object-has-no-attribute-setenvironment
# Feed = backTrader.Feeds.PandasData(dataname=bt_Feed)
# cerebro.adddata(Feed)
cerebro.adddata(Feed2)
cerebro.addstrategy(TestStrategy)
# cerebro.addsizer(backTrader.sizers.FixedSize,stake = 100)
# cerebro.broker.setcash(10000)
# cerebro.broker.setcommission(0.1/100)
cerebro.broker.setcash(100000.0)
cerebro.addsizer(bt.sizers.FixedSize,stake=10)
cerebro.broker.setcommission(commission=0.001)
# cerebro.broker.setcommission(commission=0.5,margin=0.5,mult=2.0)
print("Starting portfolio value: " + str(cerebro.broker.getvalue()))
cerebro.run()
print("Final portfolio value: " + str(cerebro.broker.getvalue()))
这是我的数据框数据
open high low close volume openinterest
datetime
2020-09-10 00:19:00 10309.5 10309.5 10294.5 10300.5 1 1
2020-09-10 00:20:00 10300.5 10308.5 10300.0 10302.5 1 1
2020-09-10 00:21:00 10302.5 10303.0 10302.5 10302.5 1 1
2020-09-10 00:22:00 10302.5 10312.0 10302.5 10311.5 1 1
2020-09-10 00:23:00 10311.5 10312.0 10300.0 10300.0 1 1
... ... ... ... ... ... ...
2020-09-29 23:55:00 10839.5 10839.5 10839.0 10839.5 1 1
2020-09-29 23:56:00 10839.5 10839.5 10839.0 10839.0 1 1
2020-09-29 23:57:00 10839.0 10839.5 10839.0 10839.0 1 1
2020-09-29 23:58:00 10839.0 10839.5 10839.0 10839.0 1 1
2020-09-29 23:59:00 10839.0 10839.5 10839.0 10839.5 1 1
这些是日志,加上测试开始后的最终余额是相同的。
2020-09-29,BUY CREATE 10840.00
2020-09-29,Order Canceled/Margin/Rejected
2020-09-29,Close,10839.00
2020-09-29,BUY CREATE 10839.00
2020-09-29,10839.50
2020-09-29,10839.50
Final portfolio value: 100000.0
解决方法
本金是10,所以在2020-09-29,你买入10840 * 10 = 108400。比你分配的资本大= 100,000