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

Help with Volume-Weigthed Moving Average (VWMA) indicator needed



  • Have anyone done an indicator on this that they would be willing to share? Im interested in both simple and exponential smoothing.

    Alternatively, please share some tips on how to approach this given that I have not gotten my head around to indicator development yet and are not experienced in sub-classing/inheritance in Python (with some tips I could likely hack my way through it)

    in short any help would be highly appreciated. Thx in advance & Merry Christmas !



  • @søren-pallesen

    Hi, yes, among many other items I've created a rolling VWAP for backtrader which I've found useful. And as a challenge to myself I did it in four actual lines of code. If you use this you need to pay me by shipping me a box of your local delicacies (although not dried fish or lutefisk).

    Investopedia has some good entries on VWAP and MVWAP as I recall.

    '''
    Author: B. Bradford 
    
    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.
    
    
    class VolumeWeightedAveragePrice(bt.Indicator):
        plotinfo = dict(subplot=False)
    
        params = (('period', 30), )
    
        alias = ('VWAP', 'VolumeWeightedAveragePrice',)
        lines = ('VWAP',)
        plotlines = dict(VWAP=dict(alpha=0.50, linestyle='-.', linewidth=2.0))
    
    
    
        def __init__(self):
            # Before super to ensure mixins (right-hand side in subclassing)
            # can see the assignment operation and operate on the line
            cumvol = bt.ind.SumN(self.data.volume, period = self.p.period)
            typprice = ((self.data.close + self.data.high + self.data.low)/3) * self.data.volume
            cumtypprice = bt.ind.SumN(typprice, period=self.p.period)
            self.lines[0] = cumtypprice / cumvol
    
            super(VolumeWeightedAveragePrice, self).__init__()
    


  • @bigdavediode This is great work but one thing I wanted to point out, VWAP period should be current day. How do you we take that into account and use it for intraday?



  • @Armaan-Syed This routine doesn't reset at the start of a trading day and continuously adjusts based on whatever period you supply. If you wish to have it reset at the start of each day you would need to access the datetime in the indicator, which might be problematic. An easier solution might just be to change typprice and cumtypprice to lines and just calculate a temporary daily running volume.

    Or I suppose you could pass the data feed itself along with the actual data line to the indicator so you can recover the datetime information from the feed within your indicator to reset your running daily volume calculation at the start of day. I've passed those that way in the past with several indicators.

    Having said that, I suggest that a running daily VWAP is more useful than one that resets daily. But I'm open to arguments.



  • @bigdavediode Thanks for your response. Clears up quite a few things. Can you please elaborate on how to "the datetime information from the feed within your indicator to reset your running daily volume calculation at the start of day."

    If you can share a sample code it will be of great help as I am new to backtrader and working with custom data and indicators. My main focus is intraday.



  • @Armaan-Syed

    I don't have example code to do that in this particular case because I don't reset the VWAP each day, I just use a running tally.

    You may wish to consult this thread which will show you that getting datetimes in an indicator can be a challenge: https://community.backtrader.com/topic/489/getting-the-current-date-in-a-strategy-s-next-method/2

    I have done it (iirc) by passing the datafeed separately into the indicator as datas[0] and the line that I'm operating the indicator on as datas[1].

    So in my indicator I can filter (or in your case you can reset the VWAP) within the indicator based on the date:

    curdt = self.datas[0].datetime.date(ago=0)
    curtm = self.datas[0].datetime.time(ago=0)

    And my line calculation input is in datas[1]. And I call passing both the data line and the input line that I'm working on:

    x = DistPercentileSampled(d, self.inds[d]['highdiffs'], period=780, raw=True, bar_size=30, matchwkdy=False, inputdesc='SELL_disthighdiffs', subplot = False)

    d is datas[0] and self.inds[d]['highdiffs'] is datas[1] within the indicator.


Log in to reply
 

});