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

how to setup a standard deviation in def __init__ (self)?

  • this is what I have for now, which is working fine outside of btr system. I have to be honest that I am pretty shady with class writing.

        def __init__(self):
            # Keep a reference to the "close" line in the data[0] dataseries
            self.dataclose = self.datas[0].close
            # To keep track of pending orders
            self.order = None
            self.buyprice = None
            self.buycomm = None
            # Add a MovingAverageSimple indicator
            self.sma5 = bt.talib.SMA(self.datas[0], timeperiod=self.p.ma5period)
            self.sma20 = bt.talib.SMA(self.datas[0], timeperiod=self.p.ma20period)
            self.sma60 = bt.talib.SMA(self.datas[0], timeperiod=self.p.ma60period)
            self.stdev = stats.pstdev((self.sma5, self.sma20, self.sma60))
            self.MACDhisto = bt.indicators.MACDHisto(self.datas[0])


      File "P:/LEIGH PYTHON/Codes/Quant/", line 67, in __init__
        self.stdev = stats.pstdev((self.sma5, self.sma20, self.sma60))
      File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\", line 666, in pstdev
        var = pvariance(data, mu)
      File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\", line 637, in pvariance
        T, ss = _ss(data, mu)
      File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\", line 535, in _ss
        c = mean(data)
      File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\", line 312, in mean
        T, total, count = _sum(data)
      File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\", line 148, in _sum
        for n,d in map(_exact_ratio, values):
      File "C:\Users\Mike_Leigh\.conda\envs\LEIGH\lib\", line 230, in _exact_ratio
        raise TypeError(msg.format(type(x).__name__))
    TypeError: can't convert type 'SMA' to numerator/denominator

    would anyone please give me a direction on this? much appreciated!

  • @Mike-Leighton said in how to setup a standard deviation in def __init__ (self)?:

    self.stdev = stats.pstdev((self.sma5, self.sma20, self.sma60))

    stats.pstdev requires a list of numbers. You are supplying a tuple of line objects, which is considerably different. What are you trying to accomplish with this formula? Are you trying to find the pstdev for the three at any given time/bar?

  • @run-out yes indeed, when the stdev is small, which means that the three MAs are very close, which could be a good buy point or breakup point. I am testing my theory. and thanks for the help with the codes, chief. =)

Log in to reply