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, Volume
self.dataopen = self.datas[0].open
self.datahigh = self.datas[0].high
self.datalow = self.datas[0].low
self.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)
pass
def 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)
pass
def notify_store(self ,msg):
pass
def 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)
pass
def 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 file
log_file = './Test_Strategy.log'
delete_file(log_file)
# 创建Cerebro引擎 导入2019-1-1 到 2019-3-31 三个月数据
cerebro = declare_cerebar()
# Set our desired cash start
cerebro.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中再看看。