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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能