Calculation Confusion

The calculation of self.onedayvolper = self.sumadvvol / self.sumvol at the end of my code should yield a result of .63154. However I'm getting 0 returned as the answer?

def on_start(self, md, order, service, account):
self.advvol = []
self.decvol = []
mb4 = md.bar.minute(start=-390,end=None,include_empty=False,include_extended=False,bar_size=1,today_only=False)
opn = mb4.open
close = mb4.close
volume = mb4.volume
for x in range(len(close)) :
bar = close[x] - opn[x]
print x,"Open",opn[x],"Close",close[x],"Net",bar,"Volume",volume[x]
if bar >= 0 :
self.advvol.append(volume[x])
continue
if bar < 0 :
self.decvol.append(volume[x])
continue
print "Advancing Volume",self.advvol
self.sumadvvol = sum(self.advvol)
print "Sum Adv Vol",self.sumadvvol
print "Declining Volume",self.decvol
self.sumdecvol = sum(self.decvol)
print "Sum Dec Vol",self.sumdecvol
print "Volume",volume
self.sumvol = sum(volume)
print "Sum Volume",self.sumvol
self.onedayvolper = self.sumadvvol / self.sumvol
print type(self.onedayvolper)
print "1 Day Volume Percentage",self.onedayvolper

Best Answers

  • ptunneyptunney Posts: 246
    Answer ✓

    JL

    Looks like you got yourself 99% of the way there by the fact that you printed out the "type" of the variable at the end and it was an INT.
    This is a pythonic thing... if you divide an int by an int you will get an int and if the result is less than 0.5 you will get 0.

    The simple solution is to multiply your numerator by 1.0 thus converting it and the result to a float.
    So instead of
    self.onedayvolper = self.sumadvvol / self.sumvol

    use
    self.onedayvolper = self.sumadvvol*1.0 / self.sumvol

    Note also, there is an icon in the toolbar that lets you insert code and it formats it, I won't say it formats it perfectly.. but it is better!

    from cloudquant.interfaces import Strategy
    class forum_Q_06_10_2018(Strategy):
        @classmethod
        def is_symbol_qualified(cls, symbol, md, service, account):
                return symbol=="SPY"
        def on_start(self, md, order, service, account):
            self.advvol = []
            self.decvol = []
            mb4 = md.bar.minute(start=-390,end=None,include_empty=False,include_extended=False,bar_size=1,today_only=False)
            opn = mb4.open
            close = mb4.close
            volume = mb4.volume
            for x in range(len(close)) :
                bar = close[x] - opn[x]
                print x,"Open",opn[x],"Close",close[x],"Net",bar,"Volume",volume[x]
                if bar >= 0 : 
                    self.advvol.append(volume[x])
                    continue
                if bar < 0 :
                    self.decvol.append(volume[x])
                    continue
            print "Advancing Volume",self.advvol
            self.sumadvvol = sum(self.advvol)
            print "Sum Adv Vol",self.sumadvvol
            print "Declining Volume",self.decvol
            self.sumdecvol = sum(self.decvol)
            print "Sum Dec Vol",self.sumdecvol
            print "Volume",volume
            self.sumvol = sum(volume)
            print "Sum Volume",self.sumvol
            self.onedayvolper = self.sumadvvol*1.0 / self.sumvol
            print type(self.onedayvolper)
            print "1 Day Volume Percentage",self.onedayvolper
  • ptunneyptunney Posts: 246
    Answer ✓

    On what date. for what symbol?

Answers

  • jl371304jl371304 Posts: 6

    Note: run the code with mb4 = md.bar.minute(start=-10,end=None,include_empty=False,include_extended=False,bar_size=1,today_only=False)

  • jl371304jl371304 Posts: 6

    Apologies: SPY for 060818. Your response works, thank you again for your help. Much appreciated!

Sign In or Register to comment.