Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    VWAP indicator - session/day based

    Indicators/Strategies/Analyzers
    2
    4
    409
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      rajanprabu last edited by

      Hello Everyone,

      Backtrader is a wonderful platform, I'm am very grateful to the author and the community.

      I was looking for vwap indicator and some implementations are already posted in this forum by @bigdavediode. Its a nice and crisp four line code, which calculates the Volume weighted average price for a given period.

      Problem

      I wanted to have a vwap which was specific to particular session ( I believe this is what al charting platforms are using ). Meaning when the session starts in the morning vwap period has to be 1 and increased there after for every new candle. ( For example 15 minutes candle data

      Time period
      9:00 1
      9:15 2
      9:30 3
      ..

      and so on.. and when next day/session starts period is reset and starts with 1 again. One need to calculate cumulative and volume and price.

      Approach

      First we need a flag to reset the period every session. This can be done using Timers. We also need to change the period within the indicator so that its dynamic. Luckily backtrader already had a framework dynamic indicators. We have to use renounce=False in cerebro otherwise it wont work.

      Working Code
      Im very new to Backtrader, please be kind if I made mistakes and Im sure code can be improved so much better

      '''
      Author: B. Bradford 
      Dynamic vwap: Rajan Prabu
      
      MIT License
      
      Copyright (c) B. Bradford
      
      Permission is hereby granted, free of charge, to any person obtaining a copy
      of this software and associated documentation files (the "Software"), to deal
      in the Software without restriction, including without limitation the rights
      to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      copies of the Software, and to permit persons to whom the Software is
      furnished to do so, subject to the following conditions:
      
      The above copyright notice and this permission notice shall be included in all
      copies or substantial portions of the Software.
      
      That they contact me for shipping information for the purpose of sending a 
      local delicacy of their choice native to whatever region they are domiciled.
      
      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      SOFTWARE.
      
      
      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      import datetime  # For datetime objects
      import pandas as pd  # for reading data
      import backtrader as bt
      import backtrader.indicators as btind
      
      class vwap(bt.Indicator):
      
          ''' This indicator needs a timer to reset the period to 1 at every session start
              also it needs a flag in next section of strategy to increment the self._vwap_period 
              run cerebro with runonce=False as we need dynamic indicator'''
      
          plotinfo = dict(subplot=False)
      
          alias = ('VWAP', 'VolumeWeightedAveragePrice','vwap',)
          lines = ('VWAP','typprice','cumprice', 'cumtypprice',)
          plotlines = dict(VWAP=dict(alpha=1.0, linestyle='-', linewidth=2.0, color = 'magenta'))
      
          def __init__(self):
              self._vwap_period = 1
              
          def vwap_period(self, period):
              self._vwap_period = period
              
      
          def next(self):
              
              self.l.typprice[0] = ((self.data.close + self.data.high + self.data.low)/3) * self.data.volume
              self.l.cumtypprice[0] = sum(self.l.typprice.get(size=self._vwap_period), self._vwap_period)
              self.cumvol = sum(self.data.volume.get(size=self._vwap_period), self._vwap_period)
              self.lines.VWAP[0] = self.l.cumtypprice[0] / self.cumvol
      
              #super(vwap, self).__init__()
      
      # Create a Stratey
      class strat(bt.Strategy):
          params = dict( 
              vwap_period = 1,
              when = bt.timer.SESSION_START,
          )
      
          def __init__(self):
          
              self.vwap = vwap(self.data) # get VWAP 
      
              self.add_timer(
                      when=self.p.when,
                  )
      
          def notify_timer(self, timer, when, *args, **kwargs):
              self.vwap_period = 1
              
          def next(self):
              
              ## Use the method insread of changing param
              self.vwap.vwap_period (self.vwap_period) 
      
              ##  print values for diagnostics
              txt = list()
              txt.append('{}'.format(len(self.data0)))
              txt.append('{}'.format(self.data.datetime.datetime(0)))
              txt.append('{}'.format(self.data.close[0]))
              txt.append('{}'.format(self.vwap[0]))
              txt.append('{}'.format(self.vwap_period))
              #print(', '.join(txt))  
      
              # Increment the Period
              self.vwap_period += 1
             
            
      ## Setup cerebro 
      cerebro = bt.Cerebro() 
      
      ## Add a strat
      cerebro.addstrategy (strat)
      
      ## input data file 
      data_file = 'Nifty_combined_contracts_10_min.csv'
      
      # Create a Data Feed and add to cerebro
      df = pd.read_csv(data_file, header=0,  index_col=0, parse_dates=True)
      data0 = bt.feeds.PandasData(dataname=df, \
          name = "nifty", \
          timeframe=bt.TimeFrame.Minutes, \
          compression=5, \
          sessionstart=datetime.time(9, 15), \
          sessionend=datetime.time(15, 30), \
          openinterest=-1, \
          fromdate=datetime.datetime(2020, 1, 1),\
          todate=datetime.datetime(2020, 1, 28)
          ) 
      
      # add data
      cerebro.adddata(data0)
      
      ## Run it with runonce=False 
      cerebro.run(runonce=False)
      
      # Plot the data
      cerebro.plot(style="candlestick")
      
      

      I have used some from @bigdavediode and modified it for the purpose ( I have included his license agreement on top ). In case you like and use the code please buy me a beer ;-) ( @Munich-DE )

      It would be very helpful to get comments and improvements. Thanks

      arrabyte 1 Reply Last reply Reply Quote 0
      • arrabyte
        arrabyte @rajanprabu last edited by

        @rajanprabu Hi, I think you could keep all the logic inside the indicator and just reset vwap_period when current date of the day is different from date of previous candle.
        Something like this:

        def next(self):
            if self.data.datetime.date(0) != self.data.datetime.date(-1):
              self._vwap_period = 1
        
            self.l.typprice[0] = ((self.data.close + self.data.high + self.data.low)/3) * self.data.volume
        1 Reply Last reply Reply Quote 1
        • R
          rajanprabu last edited by

          Thanks @arrabyte yes thats possible instead of using the timers. For my strategy day open was crucial and I wanted to skip days that didnt have the opening tick, hence used the timers.

          1 Reply Last reply Reply Quote 0
          • R
            rajanprabu last edited by

            Thanks for looking in to it. Very much appreciate it. I hope it was useful.

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
            $(document).ready(function () { app.coldLoad(); }); }