For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

How is the getvalue() function calculated



  • Hi, I've started using backtrader for a while now but I still haven't figured out how the self.broker.getvalue() is calculated (the forth line in each block), it's very different from cash value, as shown in my code. The order.execute.value is also unknown to me how it's calculated.

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    import argparse
    import datetime
    import csv
    import datetime  # For datetime objects
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    # The above could be sent to an independent module
    import backtrader as bt
    import backtrader.feeds as btfeeds
    import backtrader.indicators as btind
    pd.set_option('display.width', 500)
    pd.set_option('display.max_columns', 100)
    pd.set_option('display.notebook_repr_html', True)
    import seaborn as sns
    sns.set_style("whitegrid")
    sns.set_context("poster")
    
    count = 0
    count1 = 0
    mon = 1000000
    value = []
    class PairTradingStrategy(bt.Strategy):
        params = dict(
            signal = [0 for i in range(100000)],
            TMOM = 24, #TMOM has to be even 
            printout = True
        )
    
        def log(self, txt, dt=None):
            if self.p.printout:
                dt = dt or self.datas[0].datetime.datetime(0)
                
                print('%s, %s' % (dt.isoformat(), txt))
        def notify_order(self, order):
            global count
            if order.status in [bt.Order.Submitted, bt.Order.Accepted]:
                return  # Await further notifications
    
            if order.status == order.Completed:
                if order.isbuy():
                    buytxt = 'BUY COMPLETE, %.8f %.8f %.8f' % (order.executed.value,
                                                               order.executed.size,order.executed.price)
                    self.log(buytxt)
                    print(self.broker.getcash(),self.broker.getcash()+abs(order.executed.size*order.executed.price))
                else:
                    selltxt = 'SELL COMPLETE, %.8f %.8f %.8f' % (order.executed.value,
                                                               order.executed.size,order.executed.price)
                    self.log(selltxt)
                    print(self.broker.getcash(),self.broker.getcash()-abs(order.executed.size*order.executed.price))
                print(self.broker.getvalue())
                print()
            elif order.status in [order.Expired, order.Canceled, order.Margin]:
                self.log('%s ,' % order.Status[order.status])
                pass  # Simply log
    
            # Allow new orders
            self.orderid = None
    
        def __init__(self):
            # To control operation entries
            self.orderid = None
            self.signal = self.p.signal
            self.TMOM = self.p.TMOM
            self.count = 0
    
        def next(self):
            global value 
            value.append(self.broker.getvalue())
            if self.orderid or self.count < self.TMOM:
                self.count += 1 
                return  # if an order is active, no new orders are allowed
            
            if self.datas[0].close[0] >= self.datas[0].close[-self.TMOM]:
                self.signal[self.count] = 1
            else:
                self.signal[self.count] = -1
            
            if self.signal[self.count] == 1 and (self.count % (2*self.TMOM) == self.TMOM or self.count % (2*self.TMOM) == self.TMOM+self.TMOM//2):
                self.log('BUY CREATE , price = %.8f, qty = %.8f' % ( self.datas[0].close[0], 1000/self.datas[0].close[0]))
                self.buy(data=self.datas[0], size=1000/self.datas[0].close[0])                
    
            if self.signal[self.count] == -1 and (self.count % (2*self.TMOM) == self.TMOM or self.count % (2*self.TMOM) == self.TMOM+self.TMOM//2):
                self.log('SELL CREATE , price = %.8f, qty = %.8f' % (self.datas[0].close[0], 1000/self.datas[0].close[0]))
                self.sell(data=self.datas[0], size=1000/self.datas[0].close[0])
    
            if self.signal[self.count-self.TMOM] == 1 and (self.count % (2*self.TMOM) == 0 or self.count % (2*self.TMOM) == self.TMOM//2):
                self.log('SELL CREATE , price = %.8f, qty = %.8f' % (self.datas[0].close[0], 1000/self.datas[0].close[-self.TMOM]))
                self.sell(data=self.datas[0], size=1000/self.datas[0].close[-self.TMOM])
    
            if self.signal[self.count-self.TMOM] == -1 and (self.count % (2*self.TMOM) == 0 or self.count % (2*self.TMOM) == self.TMOM//2):
                self.log('BUY CREATE , price = %.8f, qty = %.8f' % (self.datas[0].close[0], 1000/self.datas[0].close[-self.TMOM]))
                self.buy(data=self.datas[0], size=1000/self.datas[0].close[-self.TMOM])
            
            self.count += 1
    
        def stop(self):
            print('==================================================')
            print('Starting Value - %.2f' % self.broker.startingcash)
            print('Ending   Value - %.2f' % self.broker.getvalue())
            print('==================================================')
    
    if __name__ == '__main__':
            # Create a cerebro
        cerebro = bt.Cerebro()
    
        data1 = bt.feeds.GenericCSVData(
        dataname='Downloads/Bitfinex/IOTAUSDT1h.csv',
    
        fromdate=datetime.datetime(2018, 5, 1, 0, 0, 0),
        todate=datetime.datetime(2018, 6, 1, 0, 0, 0),
    
        nullvalue=0.0,
    
        dtformat=('%Y-%m-%d %H:%M:%S'),
        timeframe = bt.TimeFrame.Minutes, 
        compression = 60,
        
        datetime=0, # count index
        open=1,
        high=2,
        low=3,
        close=4,
        volume=5,
        openinterest = None
    )
    
        cerebro.adddata(data1)
        # Add the strategy
        cerebro.addstrategy(PairTradingStrategy)
    
        # Add the commission - only stocks like a for each operation
        cerebro.broker.setcash(10000.0)
    
        # Add the commission - only stocks like a for each operation
        cerebro.broker.setcommission(commission=0.0)
        
        # And run it
        cerebro.run()
        plt.plot(value)
        plt.show()
        cerebro.plot(iplot=True, volume=False)
    

    The results show

    2018-05-02T00:00:00, SELL CREATE , price = 1.86840000, qty = 535.21729822
    2018-05-02T01:00:00, SELL COMPLETE, -999.94647827 -535.21729822 1.86830000
    10999.946478270178 10000.0
    9999.5183044316
    
    2018-05-02T12:00:00, BUY CREATE , price = 1.99720000, qty = 500.70098137
    2018-05-02T13:00:00, BUY COMPLETE, -935.45964350 500.70098137 1.99980000
    9998.644655718606 10999.946478270178
    9928.921695683313
    
    2018-05-03T00:00:00, BUY CREATE , price = 2.15340000, qty = 535.21729822
    2018-05-03T01:00:00, BUY COMPLETE, 1013.62251833 535.21729822 2.15320000
    8846.214769184673 9998.644655718606
    9952.213166941532
    
    2018-05-03T12:00:00, SELL CREATE , price = 2.23000000, qty = 500.70098137
    2018-05-03T13:00:00, SELL COMPLETE, 1078.10935309 -500.70098137 2.23000000
    9962.777957648523 8846.214769184673
    9962.777957648523
    
    2018-05-04T00:00:00, BUY CREATE , price = 2.58970000, qty = 386.14511333
    2018-05-04T01:00:00, BUY COMPLETE, 996.75638105 386.14511333 2.58130000
    8966.021576600524 9962.777957648523
    9961.967164892605
    
    2018-05-04T12:00:00, BUY CREATE , price = 2.58880000, qty = 386.27935723
    2018-05-04T13:00:00, BUY COMPLETE, 1000.15451174 386.27935723 2.58920000
    7965.867064857632 8966.021576600524
    9916.470580374771
    
    2018-05-05T00:00:00, SELL CREATE , price = 2.38190000, qty = 386.14511333
    2018-05-05T01:00:00, SELL COMPLETE, 998.28191933 -386.14511333 2.37700000
    8883.733999251577 7965.867064857632
    9818.53004375096
    
    2018-05-05T12:00:00, SELL CREATE , price = 2.38860000, qty = 386.27935723
    2018-05-05T13:00:00, SELL COMPLETE, 998.62897346 -386.27935723 2.38860000
    9806.400871933902 8883.733999251577
    9806.400871933902
    
    2018-05-06T00:00:00, SELL CREATE , price = 2.33150000, qty = 428.90842805
    2018-05-06T01:00:00, SELL COMPLETE, -1000.04289084 -428.90842805 2.33160000
    10806.443762776707 9806.400871933902
    9804.213438950845
    
    2018-05-06T12:00:00, BUY CREATE , price = 2.39230000, qty = 418.00777494
    2018-05-06T13:00:00, BUY COMPLETE, -974.62692806 418.00777494 2.38940000
    9807.655985324047 10806.443762776707
    9782.224761627755
    
    2018-05-07T00:00:00, BUY CREATE , price = 2.31220000, qty = 428.90842805
    2018-05-07T01:00:00, BUY COMPLETE, 941.10161444 428.90842805 2.31220000
    8815.933917985425 9807.655985324049
    9772.12670317123
    
    2018-05-07T12:00:00, SELL CREATE , price = 2.14400000, qty = 418.00777494
    2018-05-07T13:00:00, SELL COMPLETE, 966.51757723 -418.00777494 2.14400000
    9712.142587466678 8815.933917985425
    9712.142587466678
    
    2018-05-08T00:00:00, SELL CREATE , price = 2.21500000, qty = 451.46726862
    2018-05-08T01:00:00, SELL COMPLETE, -1000.00000000 -451.46726862 2.21500000
    10712.142587466678 9712.142587466678
    9707.627914780449
    
    2018-05-08T12:00:00, BUY CREATE , price = 2.25200000, qty = 444.04973357
    2018-05-08T13:00:00, BUY COMPLETE, -983.57015986 444.04973357 2.25900000
    9709.034239331688 10712.142587466678
    9692.301763759435
    
    2018-05-09T00:00:00, BUY CREATE , price = 2.36000000, qty = 451.46726862
    2018-05-09T01:00:00, BUY COMPLETE, 1031.52753108 451.46726862 2.36000000
    8643.57148538135 9709.034239331688
    9711.111449857372
    
    2018-05-09T12:00:00, SELL CREATE , price = 2.30740000, qty = 444.04973357
    2018-05-09T13:00:00, SELL COMPLETE, 1047.95737123 -444.04973357 2.30750000
    9668.216245594494 8643.57148538135
    9668.216245594494
    
    2018-05-10T00:00:00, SELL CREATE , price = 2.31950000, qty = 431.12739815
    2018-05-10T01:00:00, SELL COMPLETE, -999.56887260 -431.12739815 2.31850000
    10667.785118196349 9668.216245594494
    9671.104799162074
    
    2018-05-10T12:00:00, SELL CREATE , price = 2.29280000, qty = 436.14794138
    2018-05-10T13:00:00, SELL COMPLETE, -1000.00000000 -436.14794138 2.29280000
    11667.785118196349 10667.785118196349
    9701.585195952717
    
    2018-05-11T00:00:00, BUY CREATE , price = 2.21110000, qty = 431.12739815
    2018-05-11T01:00:00, BUY COMPLETE, -993.99681528 431.12739815 2.21030000
    10714.864230073908 11667.785118196349
    9756.778047240692
    
    2018-05-11T12:00:00, BUY CREATE , price = 2.13150000, qty = 436.14794138
    2018-05-11T13:00:00, BUY COMPLETE, -1005.57205732 436.14794138 2.13140000
    9785.258507812918 10714.864230073908
    9785.258507812918
    
    2018-05-12T00:00:00, SELL CREATE , price = 1.86750000, qty = 535.47523427
    2018-05-12T01:00:00, SELL COMPLETE, -999.19678715 -535.47523427 1.86600000
    10784.455294961512 9785.258507812918
    9779.207637665662
    
    2018-05-12T12:00:00, SELL CREATE , price = 1.74940000, qty = 571.62455699
    2018-05-12T13:00:00, SELL COMPLETE, -1000.05716246 -571.62455699 1.74950000
    11784.512457417211 10784.455294961512
    9848.637762417557
    
    2018-05-13T00:00:00, BUY CREATE , price = 1.83870000, qty = 535.47523427
    2018-05-13T01:00:00, BUY COMPLETE, -966.98688364 535.47523427 1.83600000
    10801.37992729673 11784.512457417211
    9748.447493319367
    
    2018-05-13T12:00:00, BUY CREATE , price = 1.83490000, qty = 571.62455699
    2018-05-13T13:00:00, BUY COMPLETE, -1032.26706596 571.62455699 1.83840000
    9750.505341724534 10801.37992729673
    9750.505341724534
    
    2018-05-14T00:00:00, BUY CREATE , price = 1.94630000, qty = 513.79540667
    2018-05-14T01:00:00, BUY COMPLETE, 1001.54138622 513.79540667 1.94930000
    8748.963955504527 9750.505341724534
    9768.693699120618
    
    2018-05-14T12:00:00, BUY CREATE , price = 1.87300000, qty = 533.90282968
    2018-05-14T13:00:00, BUY COMPLETE, 997.54404698 533.90282968 1.86840000
    7751.419908521078 8748.963955504527
    9754.199857135502
    
    2018-05-15T00:00:00, SELL CREATE , price = 1.95190000, qty = 513.79540667
    2018-05-15T01:00:00, SELL COMPLETE, 980.35949425 -513.79540667 1.95590000
    8756.3523444251 7751.419908521078
    9796.448446934444
    
    2018-05-15T12:00:00, SELL CREATE , price = 1.92640000, qty = 533.90282968
    2018-05-15T13:00:00, SELL COMPLETE, 1018.72593895 -533.90282968 1.92640000
    9784.86275553028 8756.3523444251
    9784.86275553028
    
    2018-05-16T00:00:00, BUY CREATE , price = 1.96650000, qty = 508.51767099
    2018-05-16T01:00:00, BUY COMPLETE, 999.94914823 508.51767099 1.96640000
    8784.913607297378 9784.86275553028
    9766.251008772078
    
    2018-05-16T12:00:00, SELL CREATE , price = 1.87230000, qty = 534.10244085
    2018-05-16T13:00:00, SELL COMPLETE, 952.10307012 -534.10244085 1.87010000
    9783.738581927511 8784.913607297378
    9735.529200082032
    
    2018-05-17T00:00:00, SELL CREATE , price = 1.82180000, qty = 508.51767099
    2018-05-17T01:00:00, SELL COMPLETE, -928.70582253 -508.51767099 1.82630000
    10712.444404454844 9783.738581927511
    9728.627708412543
    
    2018-05-17T12:00:00, BUY CREATE , price = 1.91860000, qty = 534.10244085
    2018-05-17T13:00:00, BUY COMPLETE, -976.55190064 534.10244085 1.91710000
    9688.516615104847 10712.444404454844
    9688.516615104847
    
    2018-05-18T00:00:00, BUY CREATE , price = 1.82600000, qty = 547.64512596
    2018-05-18T01:00:00, BUY COMPLETE, 1000.00000000 547.64512596 1.82600000
    8688.516615104847 9688.516615104847
    9673.730196703971
    
    2018-05-18T12:00:00, SELL CREATE , price = 1.71550000, qty = 582.92043136
    2018-05-18T13:00:00, SELL COMPLETE, 939.48521358 -582.92043136 1.71550000
    9688.516615104847 8688.516615104847
    9629.430478555258
    
    2018-05-19T00:00:00, SELL CREATE , price = 1.71180000, qty = 547.64512596
    2018-05-19T01:00:00, SELL COMPLETE, -938.99233297 -547.64512596 1.71460000
    10627.508948073084 9688.516615104847
    9601.568988877514
    
    2018-05-19T12:00:00, BUY CREATE , price = 1.74820000, qty = 582.92043136
    2018-05-19T13:00:00, BUY COMPLETE, -999.50711939 582.92043136 1.74820000
    9608.447449967576 10627.508948073084
    9608.447449967576
    
    2018-05-20T00:00:00, BUY CREATE , price = 1.78190000, qty = 561.19872047
    2018-05-20T01:00:00, BUY COMPLETE, 1000.72955834 561.19872047 1.78320000
    8607.71789163097 9608.447449967576
    9611.141203825817
    
    2018-05-20T12:00:00, BUY CREATE , price = 1.77980000, qty = 561.86088325
    2018-05-20T13:00:00, BUY COMPLETE, 1000.05618609 561.86088325 1.77990000
    7607.661705542645 8607.71789163097
    9599.295606765907
    
    2018-05-21T00:00:00, SELL CREATE , price = 1.83500000, qty = 561.19872047
    2018-05-21T01:00:00, SELL COMPLETE, 999.80303449 -561.19872047 1.83600621
    8638.026041363959 7607.661705542645
    9671.288205652081
    
    2018-05-21T12:00:00, SELL CREATE , price = 1.82210000, qty = 561.86088325
    2018-05-21T13:00:00, SELL COMPLETE, 1000.98270994 -561.86088325 1.82140000
    9661.399454106964 8638.026041363959
    9661.399454106964
    
    2018-05-22T00:00:00, SELL CREATE , price = 1.72830000, qty = 578.60325175
    2018-05-22T01:00:00, SELL COMPLETE, -1000.69432390 -578.60325175 1.72950000
    10662.093778009064 9661.399454106964
    9671.728152828251
    
    2018-05-22T12:00:00, SELL CREATE , price = 1.70800000, qty = 585.48009368
    2018-05-22T13:00:00, SELL COMPLETE, -998.24355972 -585.48009368 1.70500000
    11660.337337728033 10662.093778009064
    9662.304683636976
    
    2018-05-23T00:00:00, BUY CREATE , price = 1.72510000, qty = 578.60325175
    2018-05-23T01:00:00, BUY COMPLETE, -993.56456224 578.60325175 1.72510000
    10662.188868133635 11660.337337728033
    9649.249758063377
    
    2018-05-23T12:00:00, BUY CREATE , price = 1.60620000, qty = 585.48009368
    2018-05-23T13:00:00, BUY COMPLETE, -1005.37332138 585.48009368 1.60620000
    9721.790741669935 10662.188868133635
    9721.790741669935
    
    2018-05-24T00:00:00, SELL CREATE , price = 1.42770000, qty = 700.42726063
    2018-05-24T01:00:00, SELL COMPLETE, -1002.17132451 -700.42726063 1.43080000
    10723.962066177884 9721.790741669935
    9713.245529090262
    
    2018-05-24T12:00:00, SELL CREATE , price = 1.51700000, qty = 659.19578115
    2018-05-24T13:00:00, SELL COMPLETE, -999.86816084 -659.19578115 1.51680000
    11723.830227021655 10723.962066177884
    9684.395664357678
    
    2018-05-25T00:00:00, BUY CREATE , price = 1.50730000, qty = 700.42726063
    2018-05-25T01:00:00, BUY COMPLETE, -1031.37633690 700.42726063 1.50830000
    10667.37578981496 11723.830227021655
    9669.48521631463
    
    2018-05-25T12:00:00, BUY CREATE , price = 1.57270000, qty = 659.19578115
    2018-05-25T13:00:00, BUY COMPLETE, -970.66314845 659.19578115 1.57290000
    9630.526745648842 10667.37578981496
    9630.526745648842
    
    2018-05-26T00:00:00, SELL CREATE , price = 1.49030000, qty = 671.00583775
    2018-05-26T01:00:00, SELL COMPLETE, -1003.42212977 -671.00583775 1.49540000
    10633.94887542137 9630.526745648842
    9638.109111615426
    
    2018-05-26T12:00:00, SELL CREATE , price = 1.47670000, qty = 677.18561658
    2018-05-26T13:00:00, SELL COMPLETE, -999.32281438 -677.18561658 1.47570000
    11633.271689804793 10633.94887542137
    9631.207380127278
    
    2018-05-27T00:00:00, BUY CREATE , price = 1.54170000, qty = 671.00583775
    2018-05-27T01:00:00, BUY COMPLETE, -996.78242637 671.00583775 1.54130000
    10599.050392079502 11633.271689804791
    9556.31997967346
    
    2018-05-27T12:00:00, BUY CREATE , price = 1.42050000, qty = 677.18561658
    2018-05-27T13:00:00, BUY COMPLETE, -1005.96251779 677.18561658 1.42050000
    9637.108223731157 10599.050392079502
    9637.108223731157
    
    2018-05-28T00:00:00, SELL CREATE , price = 1.45970000, qty = 685.07227513
    2018-05-28T01:00:00, SELL COMPLETE, -1000.20552168 -685.07227513 1.46000000
    10637.313745413694 9637.108223731157
    9658.688000397595
    
    2018-05-28T12:00:00, SELL CREATE , price = 1.39770000, qty = 715.46111469
    2018-05-28T13:00:00, SELL COMPLETE, -997.28124776 -715.46111469 1.39390000
    11634.594993177878 10637.313745413694
    9651.579766541025
    
    2018-05-29T00:00:00, BUY CREATE , price = 1.39670000, qty = 685.07227513
    2018-05-29T01:00:00, BUY COMPLETE, -977.07260365 685.07227513 1.39680000
    10677.686039283242 11634.594993177878
    9684.55446598425
    
    2018-05-29T12:00:00, BUY CREATE , price = 1.35200000, qty = 715.46111469
    2018-05-29T13:00:00, BUY COMPLETE, -1020.41416580 715.46111469 1.35120000
    9710.954981116254 10677.686039283242
    9710.954981116254
    
    2018-05-30T00:00:00, BUY CREATE , price = 1.64500000, qty = 607.90273556
    2018-05-30T01:00:00, BUY COMPLETE, 1000.00000000 607.90273556 1.64500000
    8710.954981116254 9710.954981116254
    9675.635832180084
    
    2018-05-30T12:00:00, BUY CREATE , price = 1.60780000, qty = 621.96790646
    2018-05-30T13:00:00, BUY COMPLETE, 1001.30613260 621.96790646 1.60990000
    7709.6488485126965 8710.954981116254
    9666.004078771266
    
    2018-05-31T00:00:00, SELL CREATE , price = 1.54140000, qty = 607.90273556
    2018-05-31T01:00:00, SELL COMPLETE, 989.20929661 -607.90273556 1.54140000
    8646.670125108441 7709.6488485126965
    9600.14692570553
    
    2018-05-31T12:00:00, SELL CREATE , price = 1.67610000, qty = 621.96790646
    2018-05-31T13:00:00, SELL COMPLETE, 1012.09683600 -621.96790646 1.66830000
    9684.299183449031 8646.670125108441
    9684.299183449031
    
    2018-06-01T00:00:00, BUY CREATE , price = 1.80000000, qty = 555.55555556
    ==================================================
    Starting Value - 10000.00
    Ending   Value - 9684.30
    ==================================================
    

    Thanks


  • administrators

    This has been answered in your other thread: Community - How is the value calculated?

    The question here is the same: what is what you are expecting?