Backtrader 文档学习-Strategy(下)

2024-01-09 10:36

本文主要是介绍Backtrader 文档学习-Strategy(下),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Backtrader 文档学习-Strategy(下)

1. notify_cashvalue

# 测试 #notify_cashvalue 方法特点
class Test_Strategy(bt.Strategy):  # 策略通用初始参数params = (('maperiod1', 5),('maperiod2', 20),('printlog', True),  # 写入日志标志('logfilename', 'Test_Strategy.log'), # 日志文件名('counter', 0), # 计数器('notify_trade_trigger',0),  # notify_trade触发计数        )def __init__(self):  #Open, High, Low, Close, Volumeself.dataopen = self.datas[0].openself.datahigh = self.datas[0].highself.datalow = self.datas[0].lowself.dataclose = self.datas[0].close  self.datavol = self.datas[0].volume# 5 20 日均线self.sma5 = bt.indicators.SimpleMovingAverage(self.dataclose, period=self.params.maperiod1)  self.sma20 = bt.indicators.SimpleMovingAverage(self.dataclose, period=self.params.maperiod2)  # doprint 打印日志标志def log(self, txt, dt=None, doprint=False):''' Logging function for this strategy'''if self.params.printlog or doprint:dt = dt or self.datas[0].datetime.date(0)#print('%s, %s' % (dt.isoformat(), txt))with open(self.params.logfilename, 'a') as file:file.write('%s, %s' % (dt.isoformat(), txt))file.write('\n')def start(self):    # 从0 开始#self.params.counter += 1#self.log('Test_Strategy start %s' % self.params.counter, doprint=True)passdef nextstart(self):self.params.counter += 1#self.log('Test_Strategy nextstart %s' % self.params.counter, doprint=True)def prenext(self):self.params.counter += 1#self.log('Test_Strategy prenext  %s' % self.params.counter, doprint=True)def next(self):  # 最简单的策略,观察各个属性和方法if self.sma5 > self.sma20:  self.order = self.buy()  else:  # 必须先买入,才能卖出,不能直接卖空       if self.position.size > 0 :self.order = self.sell()  self.params.counter += 1        self.order = None#self.log('Test_Strategy next %s' % self.params.counter, doprint=True)#获得订单状态变化的通知    def notify_order(self, order):pass   #通知所有开仓/更新/平仓交易    def notify_trade(self,trade) : # ,historyon=True#trade.status_names:['Created', 'Open', 'Closed']# 触发一次notify_trade 的计数器self.params.notify_trade_trigger += 1#print('self.params.notify_trade_trigger',self.params.notify_trade_trigger)# 平仓写日志if trade.isclosed:self.log("OPERATION isclosed PROFIT, GROSS %.2f, NET %.2f" % (trade.pnl, trade.pnlcomm))# 开仓写日志if trade.isopen:self.log("OPERATION isopen PROFIT, GROSS %.2f, NET %.2f" % (trade.pnl, trade.pnlcomm))def notify_fund(self,cash,value,fundvalue,shares):self.log('notify_fund:' , doprint=True)self.log('cash:%.2f' % cash, doprint=True)self.log('value:%.2f' % value, doprint=True)self.log('fundvalue:%.2f' % fundvalue, doprint=True)self.log('shares:%.2f' % shares, doprint=True)passdef notify_store(self ,msg):passdef notify_cashvalue(self,cash, value):self.log('notify_cashvalue:' , doprint=True)self.log('cash:%.2f' % cash, doprint=True)#self.log(dir(cash), doprint=True)self.log('value:%.2f' % value, doprint=True)#self.log(dir(value), doprint=True)passdef stop(self):self.params.counter += 1     #self.close()print('self.params.counter:',self.params.counter)#self.log('Test_Strategy stop  %s' % self.params.counter, doprint=True)self.log('(MA Period %2d) Ending Value %.2f' %(self.params.maperiod1, self.broker.getvalue()), doprint=True)if __name__ == '__main__':# delete log filelog_file = './Test_Strategy.log'delete_file(log_file)# 创建Cerebro引擎  导入2019-1-1 到 2019-3-31 三个月数据cerebro = declare_cerebar()# Set our desired cash startcerebro.broker.setcash(100000.0)# Set the commission - 0.1% ... divide by 100 to remove the %# 按万一的佣金 ,买卖操作都要扣除#cerebro.broker.setcommission(commission=0.0001)# Add a FixedSize sizer according to the stake#cerebro.addsizer(bt.sizers.FixedSize, stake=10)cerebro.addstrategy(Test_Strategy)  cerebro.run()print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())#cerebro.plot(iplot=False)

程序修改:

  • 1、如果仓位是0 ,不能卖出;
  • 2、为了能够清楚容易计算cash和value ,把stake 和commission 都用默认值0 。

执行结果:

# cat Test_Strategy.log 
2020-01-02, notify_cashvalue:
2020-01-02, data close :132.08,open :132.00
2020-01-02, cash:100000.00
2020-01-02, value:100000.00
2020-01-02, notify_fund:
2020-01-02, cash:100000.00
2020-01-02, value:100000.00
2020-01-02, fundvalue:100.00
2020-01-02, shares:1000.00
2020-01-03, notify_cashvalue:
2020-01-03, data close :130.55,open :131.60
2020-01-03, cash:100000.00
2020-01-03, value:100000.00
2020-01-03, notify_fund:
2020-01-03, cash:100000.00
2020-01-03, value:100000.00
2020-01-03, fundvalue:100.00
2020-01-03, shares:1000.00
2020-01-06, notify_cashvalue:
2020-01-06, data close :129.20,open :130.00
2020-01-06, cash:100000.00
2020-01-06, value:100000.00
2020-01-06, notify_fund:
2020-01-06, cash:100000.00
2020-01-06, value:100000.00
2020-01-06, fundvalue:100.00
2020-01-06, shares:1000.00
2020-01-07, notify_cashvalue:
2020-01-07, data close :129.37,open :129.50
2020-01-07, cash:100000.00
2020-01-07, value:100000.00
2020-01-07, notify_fund:
2020-01-07, cash:100000.00
2020-01-07, value:100000.00
2020-01-07, fundvalue:100.00
2020-01-07, shares:1000.00
2020-01-08, notify_cashvalue:
2020-01-08, data close :128.89,open :128.99
2020-01-08, cash:100000.00
2020-01-08, value:100000.00
2020-01-08, notify_fund:
2020-01-08, cash:100000.00
2020-01-08, value:100000.00
2020-01-08, fundvalue:100.00
2020-01-08, shares:1000.00
2020-01-09, notify_cashvalue:
2020-01-09, data close :130.77,open :129.00
2020-01-09, cash:100000.00
2020-01-09, value:100000.00
2020-01-09, notify_fund:
2020-01-09, cash:100000.00
2020-01-09, value:100000.00
2020-01-09, fundvalue:100.00
2020-01-09, shares:1000.00
2020-01-10, notify_cashvalue:
2020-01-10, data close :133.62,open :130.50
2020-01-10, cash:100000.00
2020-01-10, value:100000.00
2020-01-10, notify_fund:
2020-01-10, cash:100000.00
2020-01-10, value:100000.00
2020-01-10, fundvalue:100.00
2020-01-10, shares:1000.00
2020-01-13, notify_cashvalue:
2020-01-13, data close :139.66,open :134.26
2020-01-13, cash:100000.00
2020-01-13, value:100000.00
2020-01-13, notify_fund:
2020-01-13, cash:100000.00
2020-01-13, value:100000.00
2020-01-13, fundvalue:100.00
2020-01-13, shares:1000.00
2020-01-14, notify_cashvalue:
2020-01-14, data close :138.56,open :140.10
2020-01-14, cash:100000.00
2020-01-14, value:100000.00
2020-01-14, notify_fund:
2020-01-14, cash:100000.00
2020-01-14, value:100000.00
2020-01-14, fundvalue:100.00
2020-01-14, shares:1000.00
2020-01-15, notify_cashvalue:
2020-01-15, data close :140.80,open :138.56
2020-01-15, cash:100000.00
2020-01-15, value:100000.00
2020-01-15, notify_fund:
2020-01-15, cash:100000.00
2020-01-15, value:100000.00
2020-01-15, fundvalue:100.00
2020-01-15, shares:1000.00
2020-01-16, notify_cashvalue:
2020-01-16, data close :140.66,open :140.80
2020-01-16, cash:100000.00
2020-01-16, value:100000.00
2020-01-16, notify_fund:
2020-01-16, cash:100000.00
2020-01-16, value:100000.00
2020-01-16, fundvalue:100.00
2020-01-16, shares:1000.00
2020-01-17, notify_cashvalue:
2020-01-17, data close :138.55,open :141.26
2020-01-17, cash:100000.00
2020-01-17, value:100000.00
2020-01-17, notify_fund:
2020-01-17, cash:100000.00
2020-01-17, value:100000.00
2020-01-17, fundvalue:100.00
2020-01-17, shares:1000.00
2020-01-20, notify_cashvalue:
2020-01-20, data close :137.56,open :140.55
2020-01-20, cash:100000.00
2020-01-20, value:100000.00
2020-01-20, notify_fund:
2020-01-20, cash:100000.00
2020-01-20, value:100000.00
2020-01-20, fundvalue:100.00
2020-01-20, shares:1000.00
2020-01-21, notify_cashvalue:
2020-01-21, data close :132.62,open :136.50
2020-01-21, cash:100000.00
2020-01-21, value:100000.00
2020-01-21, notify_fund:
2020-01-21, cash:100000.00
2020-01-21, value:100000.00
2020-01-21, fundvalue:100.00
2020-01-21, shares:1000.00
2020-01-22, notify_cashvalue:
2020-01-22, data close :131.70,open :130.99
2020-01-22, cash:100000.00
2020-01-22, value:100000.00
2020-01-22, notify_fund:
2020-01-22, cash:100000.00
2020-01-22, value:100000.00
2020-01-22, fundvalue:100.00
2020-01-22, shares:1000.00
2020-01-23, notify_cashvalue:
2020-01-23, data close :126.16,open :131.77
2020-01-23, cash:100000.00
2020-01-23, value:100000.00
2020-01-23, notify_fund:
2020-01-23, cash:100000.00
2020-01-23, value:100000.00
2020-01-23, fundvalue:100.00
2020-01-23, shares:1000.00
2020-02-03, notify_cashvalue:
2020-02-03, data close :113.54,open :113.54
2020-02-03, cash:100000.00
2020-02-03, value:100000.00
2020-02-03, notify_fund:
2020-02-03, cash:100000.00
2020-02-03, value:100000.00
2020-02-03, fundvalue:100.00
2020-02-03, shares:1000.00
2020-02-04, notify_cashvalue:
2020-02-04, data close :114.50,open :109.00
2020-02-04, cash:100000.00
2020-02-04, value:100000.00
2020-02-04, notify_fund:
2020-02-04, cash:100000.00
2020-02-04, value:100000.00
2020-02-04, fundvalue:100.00
2020-02-04, shares:1000.00
2020-02-05, notify_cashvalue:
2020-02-05, data close :117.51,open :115.90
2020-02-05, cash:100000.00
2020-02-05, value:100000.00
2020-02-05, notify_fund:
2020-02-05, cash:100000.00
2020-02-05, value:100000.00
2020-02-05, fundvalue:100.00
2020-02-05, shares:1000.00
2020-02-06, notify_cashvalue:
2020-02-06, data close :119.15,open :119.00
2020-02-06, cash:100000.00
2020-02-06, value:100000.00
2020-02-06, notify_fund:
2020-02-06, cash:100000.00
2020-02-06, value:100000.00
2020-02-06, fundvalue:100.00
2020-02-06, shares:1000.00
2020-02-07, notify_cashvalue:
2020-02-07, data close :120.58,open :119.25
2020-02-07, cash:100000.00
2020-02-07, value:100000.00
2020-02-07, notify_fund:
2020-02-07, cash:100000.00
2020-02-07, value:100000.00
2020-02-07, fundvalue:100.00
2020-02-07, shares:1000.00
2020-02-10, notify_cashvalue:
2020-02-10, data close :121.13,open :119.50
2020-02-10, cash:100000.00
2020-02-10, value:100000.00
2020-02-10, notify_fund:
2020-02-10, cash:100000.00
2020-02-10, value:100000.00
2020-02-10, fundvalue:100.00
2020-02-10, shares:1000.00
2020-02-11, notify_cashvalue:
2020-02-11, data close :124.79,open :121.15
2020-02-11, cash:100000.00
2020-02-11, value:100000.00
2020-02-11, notify_fund:
2020-02-11, cash:100000.00
2020-02-11, value:100000.00
2020-02-11, fundvalue:100.00
2020-02-11, shares:1000.00
2020-02-12, notify_cashvalue:
2020-02-12, data close :124.49,open :124.50
2020-02-12, cash:100000.00
2020-02-12, value:100000.00
2020-02-12, notify_fund:
2020-02-12, cash:100000.00
2020-02-12, value:100000.00
2020-02-12, fundvalue:100.00
2020-02-12, shares:1000.00
2020-02-13, notify_cashvalue:
2020-02-13, data close :124.14,open :124.88
2020-02-13, cash:100000.00
2020-02-13, value:100000.00
2020-02-13, notify_fund:
2020-02-13, cash:100000.00
2020-02-13, value:100000.00
2020-02-13, fundvalue:100.00
2020-02-13, shares:1000.00
2020-02-14, notify_cashvalue:
2020-02-14, data close :123.43,open :124.20
2020-02-14, cash:100000.00
2020-02-14, value:100000.00
2020-02-14, notify_fund:
2020-02-14, cash:100000.00
2020-02-14, value:100000.00
2020-02-14, fundvalue:100.00
2020-02-14, shares:1000.00
2020-02-17, notify_cashvalue:
2020-02-17, data close :124.30,open :123.18
2020-02-17, cash:100000.00
2020-02-17, value:100000.00
2020-02-17, notify_fund:
2020-02-17, cash:100000.00
2020-02-17, value:100000.00
2020-02-17, fundvalue:100.00
2020-02-17, shares:1000.00
2020-02-18, notify_cashvalue:
2020-02-18, data close :123.39,open :123.80
2020-02-18, cash:100000.00
2020-02-18, value:100000.00
2020-02-18, notify_fund:
2020-02-18, cash:100000.00
2020-02-18, value:100000.00
2020-02-18, fundvalue:100.00
2020-02-18, shares:1000.00
2020-02-19, notify_cashvalue:
2020-02-19, data close :126.00,open :123.78
2020-02-19, cash:100000.00
2020-02-19, value:100000.00
2020-02-19, notify_fund:
2020-02-19, cash:100000.00
2020-02-19, value:100000.00
2020-02-19, fundvalue:100.00
2020-02-19, shares:1000.00
2020-02-20, notify_cashvalue:
2020-02-20, data close :130.15,open :127.00
2020-02-20, cash:100000.00
2020-02-20, value:100000.00
2020-02-20, notify_fund:
2020-02-20, cash:100000.00
2020-02-20, value:100000.00
2020-02-20, fundvalue:100.00
2020-02-20, shares:1000.00
2020-02-21, notify_cashvalue:
2020-02-21, data close :130.00,open :130.00
2020-02-21, cash:100000.00
2020-02-21, value:100000.00
2020-02-21, notify_fund:
2020-02-21, cash:100000.00
2020-02-21, value:100000.00
2020-02-21, fundvalue:100.00
2020-02-21, shares:1000.00
2020-02-24, OPERATION isopen PROFIT, GROSS 0.00, NET 0.00
2020-02-24, notify_cashvalue:
2020-02-24, data close :127.10,open :129.40
2020-02-24, cash:99870.60
2020-02-24, value:99997.70
2020-02-24, notify_fund:
2020-02-24, cash:99870.60
2020-02-24, value:99997.70
2020-02-24, fundvalue:100.00
2020-02-24, shares:1000.00
2020-02-25, notify_cashvalue:
2020-02-25, data close :125.30,open :124.80
2020-02-25, cash:99745.80
2020-02-25, value:99996.40
2020-02-25, notify_fund:
2020-02-25, cash:99745.80
2020-02-25, value:99996.40
2020-02-25, fundvalue:100.00
2020-02-25, shares:1000.00
2020-02-26, notify_cashvalue:
2020-02-26, data close :124.10,open :123.83
2020-02-26, cash:99621.97
2020-02-26, value:99994.27
2020-02-26, notify_fund:
2020-02-26, cash:99621.97
2020-02-26, value:99994.27
2020-02-26, fundvalue:99.99
2020-02-26, shares:1000.00
2020-02-27, notify_cashvalue:
2020-02-27, data close :126.30,open :124.80
2020-02-27, cash:99497.17
2020-02-27, value:100002.37
2020-02-27, notify_fund:
2020-02-27, cash:99497.17
2020-02-27, value:100002.37
2020-02-27, fundvalue:100.00
2020-02-27, shares:1000.00
2020-02-28, notify_cashvalue:
2020-02-28, data close :120.60,open :124.00
2020-02-28, cash:99373.17
2020-02-28, value:99976.17
2020-02-28, notify_fund:
2020-02-28, cash:99373.17
2020-02-28, value:99976.17
2020-02-28, fundvalue:99.98
2020-02-28, shares:1000.00
2020-03-02, notify_cashvalue:
2020-03-02, data close :122.65,open :120.10
2020-03-02, cash:99253.07
2020-03-02, value:99988.97
2020-03-02, notify_fund:
2020-03-02, cash:99253.07
2020-03-02, value:99988.97
2020-03-02, fundvalue:99.99
2020-03-02, shares:1000.00
2020-03-03, notify_cashvalue:
2020-03-03, data close :124.37,open :123.70
2020-03-03, cash:99129.37
2020-03-03, value:99999.96
2020-03-03, notify_fund:
2020-03-03, cash:99129.37
2020-03-03, value:99999.96
2020-03-03, fundvalue:100.00
2020-03-03, shares:1000.00
2020-03-04, notify_cashvalue:
2020-03-04, data close :125.27,open :124.00
2020-03-04, cash:99253.37
2020-03-04, value:100004.99
2020-03-04, notify_fund:
2020-03-04, cash:99253.37
2020-03-04, value:100004.99
2020-03-04, fundvalue:100.00
2020-03-04, shares:1000.00
2020-03-05, notify_cashvalue:
2020-03-05, data close :133.20,open :126.55
2020-03-05, cash:99379.92
2020-03-05, value:100045.92
2020-03-05, notify_fund:
2020-03-05, cash:99379.92
2020-03-05, value:100045.92
2020-03-05, fundvalue:100.05
2020-03-05, shares:1000.00
2020-03-06, notify_cashvalue:
2020-03-06, data close :130.07,open :132.00
2020-03-06, cash:99247.92
2020-03-06, value:100028.34
2020-03-06, notify_fund:
2020-03-06, cash:99247.92
2020-03-06, value:100028.34
2020-03-06, fundvalue:100.03
2020-03-06, shares:1000.00
2020-03-09, notify_cashvalue:
2020-03-09, data close :126.30,open :128.00
2020-03-09, cash:99119.92
2020-03-09, value:100004.02
2020-03-09, notify_fund:
2020-03-09, cash:99119.92
2020-03-09, value:100004.02
2020-03-09, fundvalue:100.00
2020-03-09, shares:1000.00
2020-03-10, notify_cashvalue:
2020-03-10, data close :130.72,open :126.30
2020-03-10, cash:98993.62
2020-03-10, value:100039.38
2020-03-10, notify_fund:
2020-03-10, cash:98993.62
2020-03-10, value:100039.38
2020-03-10, fundvalue:100.04
2020-03-10, shares:1000.00
2020-03-11, notify_cashvalue:
2020-03-11, data close :129.90,open :132.50
2020-03-11, cash:98861.12
2020-03-11, value:100030.22
2020-03-11, notify_fund:
2020-03-11, cash:98861.12
2020-03-11, value:100030.22
2020-03-11, fundvalue:100.03
2020-03-11, shares:1000.00
2020-03-12, notify_cashvalue:
2020-03-12, data close :126.04,open :126.88
2020-03-12, cash:98734.24
2020-03-12, value:99994.64
2020-03-12, notify_fund:
2020-03-12, cash:98734.24
2020-03-12, value:99994.64
2020-03-12, fundvalue:99.99
2020-03-12, shares:1000.00
2020-03-13, notify_cashvalue:
2020-03-13, data close :122.60,open :120.00
2020-03-13, cash:98614.24
2020-03-13, value:99962.84
2020-03-13, notify_fund:
2020-03-13, cash:98614.24
2020-03-13, value:99962.84
2020-03-13, fundvalue:99.96
2020-03-13, shares:1000.00
2020-03-16, notify_cashvalue:
2020-03-16, data close :115.50,open :121.40
2020-03-16, cash:98492.84
2020-03-16, value:99878.84
2020-03-16, notify_fund:
2020-03-16, cash:98492.84
2020-03-16, value:99878.84
2020-03-16, fundvalue:99.88
2020-03-16, shares:1000.00
2020-03-17, notify_cashvalue:
2020-03-17, data close :112.36,open :114.50
2020-03-17, cash:98607.34
2020-03-17, value:99843.30
2020-03-17, notify_fund:
2020-03-17, cash:98607.34
2020-03-17, value:99843.30
2020-03-17, fundvalue:99.84
2020-03-17, shares:1000.00
2020-03-18, notify_cashvalue:
2020-03-18, data close :107.59,open :113.20
2020-03-18, cash:98720.54
2020-03-18, value:99796.44
2020-03-18, notify_fund:
2020-03-18, cash:98720.54
2020-03-18, value:99796.44
2020-03-18, fundvalue:99.80
2020-03-18, shares:1000.00
2020-03-19, notify_cashvalue:
2020-03-19, data close :102.11,open :105.80
2020-03-19, cash:98826.34
2020-03-19, value:99745.33
2020-03-19, notify_fund:
2020-03-19, cash:98826.34
2020-03-19, value:99745.33
2020-03-19, fundvalue:99.75
2020-03-19, shares:1000.00
2020-03-20, notify_cashvalue:
2020-03-20, data close :108.51,open :104.32
2020-03-20, cash:98930.66
2020-03-20, value:99798.74
2020-03-20, notify_fund:
2020-03-20, cash:98930.66
2020-03-20, value:99798.74
2020-03-20, fundvalue:99.80
2020-03-20, shares:1000.00
2020-03-23, notify_cashvalue:
2020-03-23, data close :104.51,open :104.00
2020-03-23, cash:99034.66
2020-03-23, value:99766.23
2020-03-23, notify_fund:
2020-03-23, cash:99034.66
2020-03-23, value:99766.23
2020-03-23, fundvalue:99.77
2020-03-23, shares:1000.00
2020-03-24, notify_cashvalue:
2020-03-24, data close :109.05,open :107.08
2020-03-24, cash:99141.74
2020-03-24, value:99796.04
2020-03-24, notify_fund:
2020-03-24, cash:99141.74
2020-03-24, value:99796.04
2020-03-24, fundvalue:99.80
2020-03-24, shares:1000.00
2020-03-25, notify_cashvalue:
2020-03-25, data close :114.00,open :113.10
2020-03-25, cash:99254.84
2020-03-25, value:99824.84
2020-03-25, notify_fund:
2020-03-25, cash:99254.84
2020-03-25, value:99824.84
2020-03-25, fundvalue:99.82
2020-03-25, shares:1000.00
2020-03-26, notify_cashvalue:
2020-03-26, data close :113.89,open :112.98
2020-03-26, cash:99367.82
2020-03-26, value:99823.38
2020-03-26, notify_fund:
2020-03-26, cash:99367.82
2020-03-26, value:99823.38
2020-03-26, fundvalue:99.82
2020-03-26, shares:1000.00
2020-03-27, notify_cashvalue:
2020-03-27, data close :115.81,open :115.60
2020-03-27, cash:99483.42
2020-03-27, value:99830.85
2020-03-27, notify_fund:
2020-03-27, cash:99483.42
2020-03-27, value:99830.85
2020-03-27, fundvalue:99.83
2020-03-27, shares:1000.00
2020-03-30, notify_cashvalue:
2020-03-30, data close :113.23,open :112.82
2020-03-30, cash:99596.24
2020-03-30, value:99822.70
2020-03-30, notify_fund:
2020-03-30, cash:99596.24
2020-03-30, value:99822.70
2020-03-30, fundvalue:99.82
2020-03-30, shares:1000.00
2020-03-31, notify_cashvalue:
2020-03-31, data close :115.20,open :115.00
2020-03-31, cash:99711.24
2020-03-31, value:99826.44
2020-03-31, notify_fund:
2020-03-31, cash:99711.24
2020-03-31, value:99826.44
2020-03-31, fundvalue:99.83
2020-03-31, shares:1000.00
2020-03-31, (MA Period  5) Ending Value 99826.44

说明:

  • 方法引用
    是调用的analyzer._notify_cashvalue

Receives the cash/value notification before each next cycle
每次next循环中,接收cash和value 通知

self.notify_cashvalue(cash, value)
self.notify_fund(cash, value, fundvalue, fundshares)
for analyzer in itertools.chain(self.analyzers, self._slave_analyzers):analyzer._notify_cashvalue(cash, value)analyzer._notify_fund(cash, value, fundvalue, fundshares)
  • cash和value数值计算

next触发notify_cashvalue之后,计算cash和value
从2月24日开始有买入交易。
将数据导入Excel对比计算cash和value 值,数据计算原则:
在这里插入图片描述
说明:

  • 黄色是从买入转向卖出操作。
  • calc_开始的列是计算复现BT的数据计算关系
  • diff_开始的列是Excel计算后cash value 与BT的对比

经过测试比对,BT的三个数据结果计算规则:

cash:
cash[0]=cash[-1] -open[0]*(size[0]-size[-1])

value:
value[0]=close[0]*size[0]+cash[0]

fundvalue:
fundvalue[0]=(close[0]-open[0])/shares[0]*size[0]+fundvalue[-1]

fundvalue默认用股票值/基金的份额,做基金的value 。
在文档中 没有找到说明,只能是推测计算对比后,得出的结论。

2. notify_fund

见 1.notify_cashvalue
两个差别就是在于返回的参数不同而已。

3.notify_store

接收broker的通知信息

    def notify_store(self ,msg):print('msg: ',msg)pass

执行后,没有打印出任何信息,但是有买卖操作,有交易发生,broker应该是启动工作了。
难道是在broker中手工触发msg ,然后在notify_store中接收信息 ?延期到学校broker中再看看。

4.

5.

这篇关于Backtrader 文档学习-Strategy(下)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/586829

相关文章

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

浅谈Redis Key 命名规范文档

《浅谈RedisKey命名规范文档》本文介绍了Redis键名命名规范,包括命名格式、具体规范、数据类型扩展命名、时间敏感型键名、规范总结以及实际应用示例,感兴趣的可以了解一下... 目录1. 命名格式格式模板:示例:2. 具体规范2.1 小写命名2.2 使用冒号分隔层级2.3 标识符命名3. 数据类型扩展命

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen