How do i normalize volume on resampled data?



  • I used Python's MinMaxScaler to normalize volume data before calling

    cerebro.adddata(base_data)
    

    When I add the same data via resample

    cerebro.resampledata(base_data, timeframe=bt.TimeFrame.Days, compression=1)
    

    .. the reported volume seems to be summed when I call

    higher_tf_vol = self.datas[1].volume.get(0, 10).tolist()
    

    in strategy, but i would like to normalize this (0 to 1 scale, just like minmaxscaler)

    How do I achieve this?


  • administrators

    .. the reported volume seems to be summed when I call

    That's how resampling works for volume. All bars being part of a period contribute to the total of the resampled bar.

    in strategy, but i would like to normalize this (0 to 1 scale, just like minmaxscaler)

    How do I achieve this?

    The resampler would need to know in advance the upper limit of the volume to normalize between 0 and 1. The upper limit cannot be known until the last bar is delivered.

    By the time the bar is delivered you cannot normalize.

    If backtrader only worked with preloaded data and additional pass over the data could be considered, but the data can also be read in next steps.

    Option 1

    You resample outside of the platform and normalize

    Option 2

    • You are only working with preloaded data (only backtesting)

    • You use resampledata with a name=xxxx parameter

      Unfortunately resampledata doesn't return the data stream it creates. Will be added.

    • Until it is added you find get the data in data = cerebro.databynames[xxxx]

    • You monkey patch the method preload with your own version which will

      • Call the original preload via super
      • Go over the self.volume.array iterable and normalize the volume

    You probably want to go forward with Option 1.



  • Uhh yeah.. option #1 seems a lot easier. ;)

    Thank you


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.