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__()
    

Log in to reply
 

});