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

What's wrong with my VWAP indicator?



  • Hi, I've tried to find out what's wrong with my indicator for 5 hours now and I can't seem to find what's wrong with it. I think I have a clue but I'm not sure how to solve it. When I print the the cumulative values of the lists for my calculations, they are wrong but I can't see where I went wrong. Also, when I print the volume variable, i get <backtrader.linebuffer.LineBuffer object at 0x0000027FA2DF3080> as the output which is an object but if i print the price variable I get the right price (float variable). I'm not sure why these are different since the data is being imported properly. My volume data is at the correct index in my .csv file. Also, I've noticed the when I initiate my VWAP indicator in my initializing function for my strategy, it alters the output of my trades and results although I havn't incorporated the VWAP in my trading logic yet. Also, I'm not sure if I'm I'm using the plotting function to plot my indicator properly or it's because of the errors I'm describing above.

    Any help would be greatly appreciated. Thanks.

    I'm initializing using this:

    self.vwap = bt.indicators.VWAP(period=5)
    

    And my vwap function is:

     '''        # There are five steps in calculating VWAP:
     
             #
     
             # Calculate the Typical Price for the period. [(High + Low + Close)/3)]
     
             #
     
             # Multiply the Typical Price by the period Volume (Typical Price x Volume)
     
             #
     
             # Create a Cumulative Total of Typical Price. Cumulative(Typical Price x Volume)
     
             #
     
             # Create a Cumulative Total of Volume. Cumulative(Volume)
     
             #
     
             # Divide the Cumulative Totals.
     
             #
     
             # VWAP = Cumulative(Typical Price x Volume) / Cumulative(Volume)
     
     '''
    
    
    class VolumeWeightedAveragePrice(Indicator):
    
        alias = ('VWAP', 'VolumeWeightedAveragePrice',)
        lines = ('vwap',)
        params = (('period', 5),)
    
        plotinfo = dict(subplot=False)
    
    
        def __init__(self):
    
            self.addminperiod(self.p.period)
            self.cum_price_by_Volume = list()
            self.cum_volume = list()
    
    
        def _plotlabel(self):
    
            # This method returns a list of labels that will be displayed
            # behind the name of the indicator on the plot
    
            # The period must always be there
            
            plabels = [self.p.period]
    
            plabels += [self.lines.vwap]
            return plabels
    
        def next(self):
    
    
    
            price = (self.data.close + self.data.high + self.data.low) / 3
    
            #check price
            print(price)
    
            volume = self.data.volume
            #check price
            print(volume)
    
            price_by_Volume = price * volume
    
    
            self.cum_price_by_Volume.append(price_by_Volume)
            self.cum_volume.append(volume)
    
            if len(self.cum_price_by_Volume) and len(self.cum_volume) >= self.p.period:
    
                #Check cumulative values for accuracy
    
                print(sum(self.cum_price_by_Volume[-1*(self.p.period):]))
                print(sum(self.cum_volume[-1*(self.p.period):]))
    
                # get last n'th (period) values in list and perform calculations for VWAP. This is because
                # the latest values go at the end of the list due to the way data is imported (earliest to latest)
    
                self.lines.vwap[0] = sum(self.cum_price_by_Volume[-1*(self.p.period):]) / sum(self.cum_volume[-1*(self.p.period):])


  • @hamid-zargar Just solved it!


  • administrators

    @hamid-zargar said in What's wrong with my VWAP indicator?:

    self.vwap = bt.indicators.VWAP(period=5)
    

    Let me argue, for future reference of others ... that your indicator is not inside the backtrader package, and such an import does of course fail.


  • administrators

    @hamid-zargar said in What's wrong with my VWAP indicator?:

    Also, I've noticed the when I initiate my VWAP indicator in my initializing function for my strategy, it alters the output of my trades and results although I havn't incorporated the VWAP in my trading logic yet

    Yes you have. Because you change the minimum warm-up period.

    Please read: Docs - Operating the Platform