Standard Deviations of VWAP

Hello. Trying to calculate standard deviations of VWAP and running into issues using the md.L1.daily_vwap float as the calculation would require the set of what vwap was for each bar... is there any way to have this with out having to re-invent the wheel and have it store vwap on each minute bar then some how pass it back? For example, take the array of vwap at each minute thus far then take the std dev of it using the python statistics library?


  • ptunneyptunney Posts: 246
    edited October 2020

    I would write a separate script for all your symbols run as a single multidays script (so it does not try to run in parallel and can write out to the file one day at a time), with something like this... Then you can write a second script to read the file in and construct long lists of vwaps for each symbol.

    Minute bars have VWAP but this is the VWAP at this point in the day so far.
    There is MD.L1.minute_vwap but it is not stored in the minute bars so the user would have to capture at the end of each minute.

    from cloudquant.interfaces import Strategy
    class _VWAP(Strategy):
        def is_symbol_qualified(cls, symbol, md, service, account):
            return symbol in ["AAPL","MSFT"]
        def on_start(self, md, order, service, account): #
            self.minvwap = md.L1.last
            self.minvwapold = md.L1.last
            self.vwaplist = []
        def on_minute_bar(self, event, md, order, service, account, bar):
        def on_trade(self, event, md, order, service, account):
            self.minvwapold = self.minvwap
            self.minvwap = md.L1.minute_vwap
        def on_finish(self, md, order, service, account):
            temp_string = service.time_to_string(md.L1.timestamp,'%Y-%m-%d')+','+self.symbol+','+str(self.vwaplist) # .replace(",", " ")
            service.write_file('vwaps.csv', temp_string)
Sign In or Register to comment.