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

相关文章

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

使用Python实现Word文档的自动化对比方案

《使用Python实现Word文档的自动化对比方案》我们经常需要比较两个Word文档的版本差异,无论是合同修订、论文修改还是代码文档更新,人工比对不仅效率低下,还容易遗漏关键改动,下面通过一个实际案例... 目录引言一、使用python-docx库解析文档结构二、使用difflib进行差异比对三、高级对比方

Python自动化处理PDF文档的操作完整指南

《Python自动化处理PDF文档的操作完整指南》在办公自动化中,PDF文档处理是一项常见需求,本文将介绍如何使用Python实现PDF文档的自动化处理,感兴趣的小伙伴可以跟随小编一起学习一下... 目录使用pymupdf读写PDF文件基本概念安装pymupdf提取文本内容提取图像添加水印使用pdfplum

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

C#高效实现Word文档内容查找与替换的6种方法

《C#高效实现Word文档内容查找与替换的6种方法》在日常文档处理工作中,尤其是面对大型Word文档时,手动查找、替换文本往往既耗时又容易出错,本文整理了C#查找与替换Word内容的6种方法,大家可以... 目录环境准备方法一:查找文本并替换为新文本方法二:使用正则表达式查找并替换文本方法三:将文本替换为图

Python批量替换多个Word文档的多个关键字的方法

《Python批量替换多个Word文档的多个关键字的方法》有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉,所以本文给大家介绍了Py... 目录工具准备先梳理一下思路神奇代码来啦!代码详解激动人心的测试结语嘿,各位小伙伴们,大家好!有没有想

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

Python调用LibreOffice处理自动化文档的完整指南

《Python调用LibreOffice处理自动化文档的完整指南》在数字化转型的浪潮中,文档处理自动化已成为提升效率的关键,LibreOffice作为开源办公软件的佼佼者,其命令行功能结合Python... 目录引言一、环境搭建:三步构建自动化基石1. 安装LibreOffice与python2. 验证安装