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

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!



  • @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



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



  • 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.


Log in to reply
 

});