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, 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中再看看。

4.

5.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/304496.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Vue-8、Vue事件处理

1、点击事件 <!DOCTYPE html> <html lang"en" xmlns:v-model"http://www.w3.org/1999/xhtml" xmlns:v-bind"http://www.w3.org/1999/xhtml"xmlns:v-on"http://www.w3.org/1999/xhtml"> <head><meta charset&quo…

计算机网络—— 概述

概述 1.1 因特网概述 网络、互联网和因特网 网络由若干结点和连接这些结点的链路组成多个网络还可以通过路由器互联起来&#xff0c;这样就构成了一个覆盖范围更大的网络&#xff0c;即互联网&#xff08;或互连网&#xff09;。因特网&#xff08;Internet&#xff09;是世…

react输入框检索树形(tree)结构

input搜索框搜索树形子级内容1. input框输入搜索内容2. 获取tree结构数据3. 与tree匹配输入的内容&#xff0c;tree是多维数组&#xff0c;一级一级的对比输入的内容是否匹配&#xff0c;用forEach循环遍历数据&#xff0c;匹配不到在往下找&#xff0c;直到找到为null &#x…

求求你,别再乱用@Transactional了

求求你&#xff0c;别再乱用Transactional了 文章目录 &#x1f50a;先看个问题&#x1f4d5;情况1情况1结果 &#x1f5a5;️情况2情况2结果 &#x1f4dc; 情况三情况3结果 &#x1f4d8;情况4情况4结果 &#x1f516;先说结论情况1结果情况2结果情况3结果情况4结果&#x1f…

oracle 12c pdb expdp/impdp 数据导入导出

环境 (源)rac 环境 byoradbrac 系统版本&#xff1a;Red Hat Enterprise Linux Server release 6.5 软件版本&#xff1a;Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit byoradb1&#xff1a;172.17.38.44 byoradb2&#xff1a;172.17.38.45 (目的&am…

2024年中职网络安全——Windows操作系统渗透测试(Server2105)

Windows操作系统渗透测试 任务环境说明&#xff1a; 服务器场景&#xff1a;Server2105服务器场景操作系统&#xff1a;Windows&#xff08;版本不详&#xff09;&#xff08;封闭靶机&#xff09;需要环境加Q 目录 1.通过本地PC中渗透测试平台Kali对服务器场景进行系统服务…

区块链金融科技:技术融合与挑战应对【文末送书-16】

文章目录 前言一.区块链与金融科技的融合&#xff1a;革新金融格局的技术之光1.1区块链技术简介1.2 区块链在金融科技中的应用 二.智能合约2.1 去中心化金融&#xff08;DeFi&#xff09;2.2区块链对金融科技的影响2.3数据安全性 三.区块链与金融科技【文末送书-16】3.1 粉丝福…

spring Security源码讲解-Sevlet过滤器调用springSecurty过滤器的流程

承接上文 上一节 http://t.csdnimg.cn/ueSAl 最后讲到了过滤器收集完成注入容器&#xff0c;这节我们来讲Security的Filter是怎么被Spring调用的。 我们先看webSecurity的performBuild方法(), ![在这里插入图片描述](https://img-b 也就是说&#xff0c;最终返回的过滤器对象…

如何利用大语言模型(LLM)打造定制化的Embedding模型

一、前言 在探索大语言模型&#xff08;LLM&#xff09;应用的新架构时&#xff0c;知名投资公司 Andreessen Horowitz 提出了一个观点&#xff1a;向量数据库是预处理流程中系统层面上最关键的部分。它能够高效地存储、比较和检索高达数十亿个嵌入&#xff08;也就是向量&…

解码 Elasticsearch 查询 DSL:利用 Elasticsearch 中的 has_child 和 has_parent 查询进行父子文档搜索

今天&#xff0c;让我们深入研究 has_child 查询和 has_parent 查询&#xff0c;这将帮助我们将 2 个不同的文档组合到一个索引中&#xff0c;从而使我们能够将它们与关系关联起来。 这样做会对我们搜索相关文档时有很大帮助。 在使用 has_child 及 has_parent 这种关系时&…

解决 rasa 中 slot 不能为中文的问题

解决 rasa 中 slot 不能为中文的问题 定位问题解决办法 定位问题 slots:姓名:type: textmappings:- type: custom如上的 slot 配置&#xff0c;在 rasa train 时会报以下错误&#xff1a; YamlValidationException: Failed to validate D:\project\python\rasa_test\y\domain…

Ansys Zemax | 如何使用 ZPL 创建用户自定义求解

附件下载 联系工作人员获取附件 本文使用两个示例演示了如何使用 ZPL 创建用户自定义解。第一个示例介绍了如何创建 ZPL 解以确保序列文件中像面的曲率半径等于系统的 Petzval 曲率。第二个示例介绍了如何在非序列元件编辑器 ( Non-Sequential Component Editor ) 中基于其他…

实战:使用docker容器化服务与文件挂载-2

接着上文&#xff0c;演示Elasticsearch 和 Kibana 的安装&#xff0c;并讲解文件挂载 Elasticsearch of Docker &#xff08;Kibana&#xff09; 1、Elasticsearch 安装 ElasticSearch 使用 Docker 安装&#xff1a;https://www.yuque.com/zhangshuaiyin/guli-mall/dwrp5b 1.…

C++ 实现十大排序算法

教你手撕排序&#xff0c;这里有一个概念就是稳定排序。假定在待排序的记录序列中&#xff0c;存在多个具有相同的关键字的记录&#xff0c;若经过排序&#xff0c;这些记录的相对次序保持不变&#xff0c;即在原序列中&#xff0c;r[i]r[j]&#xff0c;且r[i]在r[j]之前&#…

vue2 +Html + css 实现房间状态图,酒店前台入住管理系统的设计与开发

一、需求分析 酒店管理系统是指一种可以提高酒店管理效率的软件或平台。其面向酒店前台工作人员和酒店管理员&#xff0c;界面美观大方、操作方便。系统强化以客源为中心的信息完整性、长久性、可操作性&#xff0c;突出以预订、房源、房价等对营销具有影响力的信息处理。 系统…

微服务-sentinel-基本案例,持久化

sentinel 功能 限流 限流文档 直接拒绝&#xff1a;触发阀值直接抛弃。冷启动&#xff1a;在一段时间内针对突发流量缓慢增长处理数量。 3&#xff09;匀速器&#xff1a;请求以均匀的速度通过。 降级降级文档 1&#xff09;RT 统计时间内&#xff0c;大于预设请求数量&…

Spring - 配置支持多数据源

目录 SpringBoot整合多数据源整合步骤具体整合步骤如下&#xff1a;1、在application.properties中配置出多个数据源2、在代码中创建出mapper目录&#xff0c;在mapper目录下创建出不同数据源的目录创建出目录MySQL数据源的MapperSQL Server数据源的Mapper 3、创建config packa…

数据结构实验2:队列的应用

目录 一、实验目的 二、实验原理 1.1 队列的基本操作 1.1.1 队列的定义 1.1.2 队列的初始化 1.1.3 入队操作 1.1.4 出队操作 1.1.5 检查队列是否为空 1.1.6 返回队列的长度 2.1队列的运用 三、实验内容 问题描述 代码 截图 分析 一、实验目的 1、理解并掌握队列…

Pod的亲和性和反亲和性

如何部署pod是重要的集群的调度机制&#xff0c;合理的配置pod调度机制可以实现资源最大化利用。 调度策略匹配标签操作符拓扑域调度目标node的亲和性主机标签In、NotIn、Exists、DoesNotExist、Gt、Lt不支持指定主机pod的亲和性pod的标签In、NotIn、Exists、DoesNotExist支持…

【DNS Server Spoofed Request Amplification DDoS漏洞修复】

文章目录 前言 之前对公司服务器做漏洞扫描&#xff0c;发现扫描工具提示存在这样一个漏洞&#xff0c;本来觉得漏洞利用率低&#xff0c;而且扫描百度&#xff0c;QQ等网站也会有此漏洞&#xff0c;主要是找了很久资料也不知道如何修复&#xff0c;所以暂未处理。但是近期由于…