Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Alain
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    A
    • Profile
    • Following 1
    • Followers 0
    • Topics 6
    • Posts 25
    • Best 0
    • Groups 0

    Alain

    @Alain

    0
    Reputation
    352
    Profile views
    25
    Posts
    0
    Followers
    1
    Following
    Joined Last Online

    Alain Unfollow Follow

    Latest posts made by Alain

    • RE: compound hourly return?

      and if i calculate the hourly non-compounded returns manually with:

          def prenext(self):
              self.last_value = self.broker.getvalue()
      
          def next(self) :
      
              self.value = self.broker.getvalue()
              self.hourly_ret += [np.floor((self.value/self.last_value - 1.) * 1e8) / 1e8]
              self.last_value = self.value
      

      i get the correct returns and my value at that candle differs from the one the observer calculates.

      posted in Indicators/Strategies/Analyzers
      A
      Alain
    • RE: compound hourly return?

      somehow I always get the difference at the end of a day.. ? probably that should give me a clue.

      posted in Indicators/Strategies/Analyzers
      A
      Alain
    • RE: compound hourly return?

      @alain I actually went through a trade and the error happens at a specific candle. Directly compounded interest (and right result) says there is a small gain, hourly return (indicator in picture) says there's a small loss:

      0_1556836546234_bb8b7882-17a3-4208-88c7-6ad3e6eeab17-image.png

      posted in Indicators/Strategies/Analyzers
      A
      Alain
    • compound hourly return?

      Hi,

      To combine 2 strategies (hourly data) I need the non-compounded hourly returns of each.

      So far for the compounded returns for every hour I've always used the benchmark observer. More precisely the benchmark.timereturn -part as can be seen in the #ret1 part of the code bellow.

      I've now tried to return the hourly returns (non-compounded) by using a specific timeframe in the TimeReturn-observer.

      This can be seen in the following code:

      #compounded returns
      cerebro1.addobserver(bt.observers.Benchmark, timeframe=bt.TimeFrame.NoTimeFrame, data=data, plot=False)
      #non-compounded hourly returns
      cerebro1.addobserver(bt.observers.TimeReturn, timeframe=bt.TimeFrame.Minutes, compression=60)
      
      #show result
      get_trade_rets(thestrat1, startdate, enddate, maxperiod, timeframe)
      
      def get_trade_rets(thestrat, startdate, enddate, maxperiod, timeframe):
      
          dt_hours = pd.date_range(start=startdate, end=enddate, freq=timeframe)
      
          #ret1
          ret1_compounded = thestrat.observers.benchmark.timereturn.lines[0].array[maxperiod:(maxperiod + len(dt_hours))]
          ret1_compounded = 1 + np.array(ret1_compounded)
          print(ret1_compounded)
      
          #ret2
          ret2_hourly = thestrat.observers.timereturn.lines[0].array[maxperiod:(maxperiod + len(dt_hours))]
          ret2_hourly = np.array(ret2_hourly)
          ret2_compounded = np.cumprod(1 + ret2_hourly)
          print(ret2_compounded)
      

      I then wanted to be sure that those non-compounded hourly returns I received in the section #ret2 are correct and tried to reproduce the compounded returns from #ret1 (ret1_compounded).
      To compound returns I use the standard equation "ret2_compounded = np.cumprod(1 + ret2_hourly)".

      What I receive is a new list that starts the same as the directly compounded list from #ret1, but at a certain point the values in the two lists are not the same anymore.

      [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.0022292 1.00308236 1.00617001 1.00507308 1.00482932 1.00036036 1.00109164 0.99816651 0.99857278 0.99629767 0.99597266 1.00149791 0.99889779 1.00511371 1.00730756 1.00470744 1.00491057 1.01888623 1.0236802 1.02766164 1.03046489 1.02676785 1.02343644 1.02372083 ...

      [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.0022292 1.00308236 1.00617001 1.00507308 1.00482932 1.00036036 1.00109164 0.99816651 0.99857278 0.99629767 0.99597266 1.00149791 0.99889779 1.00511371 1.00730756 1.00470744 1.00491057 1.01888623 1.0236802 1.02766164 1.03046489 1.02676785 1.02343644 1.02039931 ...

      I can't explain myself why that is...

      Would be great if anyone knows what the issue could be.

      Thank you!

      posted in Indicators/Strategies/Analyzers
      A
      Alain
    • RE: Question regarding Smoothed Moving Average

      how i solved the problem (just used next method to calculate the SMMA explicitly in each step):

      class RelativeVolatility(bt.Indicator):
          lines = ('rel_vol', 'signal', 'nU', 'nD',)
          plotlines = dict(nU=dict(_plotskip=True), nD=dict(_plotskip=True))
          params = (('period', 10), ('signal_line_top', 80), ('signal_line_bottom', 20),)
      
          def _plotinit(self):
              self.plotinfo.plothylines = [self.p.signal_line_top, self.p.signal_line_bottom]
      
          def __init__(self):
              std_dev = bt.ind.StdDev(self.data.close, period=self.p.period)
              self.u = bt.If(self.data.close > self.data.close(-1), std_dev, 0)
              self.d = bt.If(self.data.close < self.data.close(-1), std_dev, 0)
      
          def nextstart(self):
              self.l.nU[0] = 0
              self.l.nD[0] = 0
      
          def next(self):
      
              self.l.nU[0] = (13. * self.l.nU[-1] + self.u[0]) / 14.
              self.l.nD[0] = (13. * self.l.nD[-1] + self.d[0]) / 14.
      
              self.l.rel_vol[0] = 100. * self.l.nU[0] / (self.l.nU[0] + self.l.nD[0])
      

      I tried again to directly switch SMA to SMMA.. but it really doesn't work for me:

          def __init__(self):
              std_dev = bt.ind.StdDev(self.data.close, period=self.p.period)
              u = bt.If(self.data.close > self.data.close(-1), std_dev, 0)
              d = bt.If(self.data.close < self.data.close(-1), std_dev, 0)
              nU = btind.SMMA(u, period=14)
              nD = btind.SMMA(d, period=14)
              self.l.rel_vol = 100. * nU / (nU + nD)
      

      gives me lists full of nan's in nU and nD...

      posted in Indicators/Strategies/Analyzers
      A
      Alain
    • RE: Question regarding Smoothed Moving Average

      @alain alright, i was able to implement the smoothing moving average inside the next function. it's just a bit less elegant.

      posted in Indicators/Strategies/Analyzers
      A
      Alain
    • Question regarding Smoothed Moving Average

      Hi,

      I tried to implement a relative volatility indicator as follows:

      class RelativeVolatility(bt.Indicator):
          lines = ('rel_vol',)
          params = (('period', 10),)
      
          def __init__(self):
              std_dev = bt.ind.StdDev(self.data.close, period=self.p.period)
              self.u = bt.If(self.data.close > self.data.close(-1), std_dev, 0)
              self.d = bt.If(self.data.close < self.data.close(-1), std_dev, 0)
              self.nU = btind.SMA(self.u, period=14)
              self.nD = btind.SMA(self.d, period=14)
              self.l.rel_vol = 100. * self.nU / (self.nU + self.nD)
      

      The code works perfectly. Actually however i want to replace the simple moving average by a smoothed moving average. But as soon as i change btind.SMA to btind.SmoothedMovingAverage i get lists full or 'nan' values.

      Would be great if anyone here knows why that is ?

      Thanks!

      posted in Indicators/Strategies/Analyzers
      A
      Alain
    • RE: sharpe ratio wrong?

      If I had concluded something I wouldn't be asking here...
      Ok, gonna do some reading.

      posted in Indicators/Strategies/Analyzers
      A
      Alain
    • RE: sharpe ratio wrong?

      I didn't include any riskfree rate. Or say I set it to zero.
      So you're right, there are for sure some differences in my calculations compared to the analyzer. You think if I change these two things (# days/rate) it would give me the "right" sharpe ratio ?

      posted in Indicators/Strategies/Analyzers
      A
      Alain
    • RE: sharpe ratio wrong?

      So my guess now is that it's exactly this part of the parameters which causes the issue:

      • list itemfactor (default: None)

      If None, the conversion factor for the riskfree rate from annual to the chosen timeframe will be chosen from a predefined table

      Days: 252, Weeks: 52, Months: 12, Years: 1

      Else the specified value will be used

      I should probably specify that it should use 365 days?

      posted in Indicators/Strategies/Analyzers
      A
      Alain