Backtrader Community

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

    Best posts made by Jens Halsberghe

    • retrieve backtraders information as a dataframe

      Hi,

      I'm pretty new to backtrader. I've read through the manual but haven't found the answer yet to a question I have. Is there a way to retrieve everything you feed into backtrader as a dataframe.

      for example, we feed OHLC data into backtrader, we create 2 moving averages, we add a long and short signal when crossing over etc.

      Is there a way to then also get all this information in a dataframe? eg the columns would look something like:

      Date, Open, High, Low, Close, MA1, MA2, Signal

      thanks

      posted in General Discussion
      Jens Halsberghe
      Jens Halsberghe
    • ATR difference with Metatrader? + original welles wilder version

      Hi,

      I'm using backtraders ATR. It doesn't seem like I'm getting the same ATR values as in metatrader. I have downloaded metatraders data and noticed backtrader's ATR values are different than in metatrader when comparing.

      Additionally, I'm looking for the original welles wilder version (non smoothed). on Github, it looks like an SMA was used so I thought it'd be the same as in metatrader.

      Anyone who has the original version by any chance?

      thanks

      posted in Indicators/Strategies/Analyzers
      Jens Halsberghe
      Jens Halsberghe
    • RE: Resampling issues + adding multiple timeframes

      @run-out Perfect! you always come to the rescue :) do you then also know how to refer to the datafeeds in the init / next functions?

      I have now created the three timeframes as follows which seems to work. When I log, I'm getting the closes of the 2 minute timeframe. Can't see how to refer to the other two though.

          # Add a strategy
          cerebro.addstrategy(resampling)
          # Create a Data Feed
          minute_data = bt.feeds.PandasData(dataname=df2020)
          two_minute = cerebro.resampledata(minute_data, timeframe=bt.TimeFrame.Minutes, compression=2)
          five_minute = cerebro.resampledata(minute_data, timeframe=bt.TimeFrame.Minutes, compression=5)
          ten_minute = cerebro.resampledata(minute_data, timeframe=bt.TimeFrame.Minutes, compression=10)
      
      posted in General Code/Help
      Jens Halsberghe
      Jens Halsberghe
    • RE: retrieve backtraders information as a dataframe

      @run-out thanks a lot, really appreciated! hadn't read that far yet so should have just read the full guide first.

      posted in General Discussion
      Jens Halsberghe
      Jens Halsberghe
    • RE: Resampling issues + adding multiple timeframes

      @dasch Yes. I realized that because the lowest dataframe is 2 minutes, there will never be a time ending on 5. I tried the following work around where I'm separately adding the one minute frame

          # Add a strategy
          cerebro.addstrategy(resampling)
          # Create a Data Feed
          data = bt.feeds.PandasData(dataname=df2020)
          one_minute = cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=1)
          two_minute = cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=2)
          five_minute = cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=5)
          ten_minute = cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=10)
      

      when logging the 5-minute closes I can see now it works properly

      2020-01-02 09:00, 1.12075
      2020-01-02 09:01, 1.12075
      2020-01-02 09:02, 1.12075
      2020-01-02 09:03, 1.12075
      2020-01-02 09:04, 1.12075
      2020-01-02 09:05, 1.12087
      2020-01-02 09:06, 1.12087
      2020-01-02 09:07, 1.12087
      2020-01-02 09:08, 1.12087
      2020-01-02 09:09, 1.12087
      2020-01-02 09:10, 1.12086
      2020-01-02 09:11, 1.12086
      2020-01-02 09:12, 1.12086
      2020-01-02 09:13, 1.12086
      2020-01-02 09:14, 1.12086
      2020-01-02 09:15, 1.1207799999999999
      

      thanks for the help guys

      posted in General Code/Help
      Jens Halsberghe
      Jens Halsberghe
    • RE: Trying to execute on the open, although the order executes the next day

      @ab_trader thanks!! that seems to work.

      posted in General Code/Help
      Jens Halsberghe
      Jens Halsberghe
    • resampling - first resampled bar is sampled incorrectly

      I'm trying to resample from 1 minute to 2, 5 and 10 minutes. The only problem I'm having is when resampling, the first OHLC data I'm logging of the 2,5 and 10 minute is exactly the same as the first 1 minute OHLC data. after that it resamples properly, however it then is out of sync.

      an example will make it more clear when sampling 1 minute to 2 minute data:

      Output in the log:

      Starting Portfolio Value: 1000000.00
      2020-08-12 00:00, O: 1.17383, H: 1.17391, L: 1.17372, C: 1.17391
      2020-08-12 00:02, O: 1.17391, H: 1.17408, L: 1.17384, C: 1.17384
      2020-08-12 00:04, O: 1.17384, H: 1.17384, L: 1.17377, C: 1.17377
      2020-08-12 00:06, O: 1.17377, H: 1.17389, L: 1.17376, C: 1.17386
      

      dataframe info:

                              Open    High    Low    Close   Volume
      Datetime					
      2020-08-12 00:00:00	1.17383	1.17391	1.17372	1.17391	23
      2020-08-12 00:01:00	1.17391	1.17408	1.17390	1.17390	18
      2020-08-12 00:02:00	1.17390	1.17390	1.17384	1.17384	33
      2020-08-12 00:03:00	1.17384	1.17384	1.17378	1.17378	22
      2020-08-12 00:04:00	1.17378	1.17378	1.17377	1.17377	25
      2020-08-12 00:05:00	1.17377	1.17382	1.17376	1.17382	39
      2020-08-12 00:06:00	1.17382	1.17389	1.17382	1.17386	36
      
      

      The first candle should be O:1.7383 H:1.17408 L:1.17372 C: 1.17390 but for some reason it always takes over the very first minute candle which causes all the rest of the candles to be out of sync.

      my code below:

      class Resampling_test(bt.Strategy):
       
          def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.datetime(0)
              print('%s, %s' % (dt.strftime("%Y-%m-%d %H:%M"), txt))
          
          def __init__(self):
              
              pass
             
          def next(self):
              
              self.log('O: %.5f, H: %.5f, L: %.5f, C: %.5f' %(self.datas[0].open[0], self.datas[0].high[0], self.datas[0].low[0], self.datas[0].close[0]))
      
      if __name__ == '__main__':
      
          cerebro = bt.Cerebro()
      
          # Add a strategy
          cerebro.addstrategy(Resampling_test)
          
          # Create a Data Feed
          data = bt.feeds.PandasData(dataname=df2020)
          two_minute = cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=2)
          
          # Print out the starting conditions
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
          
          cerebro.run()
          
          # Print out the final result
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      

      many thanks

      posted in General Code/Help
      Jens Halsberghe
      Jens Halsberghe
    • RE: Entering and taking profit on the same bar

      I agree and I understand the logic. I can't replay but also, it's useful to use target.activate() and it can be used to do only in certain scenarios where it looks most likely the target would have been hit. for example if the price never even reached the stoploss but got to the entry and then the target, it's perfectly fine to use it. in case of a big outside bar which would hit all three entry, target and stoploss there would be doubt but it's more an exceptional case.

      thanks again for your help Dasch

      posted in General Code/Help
      Jens Halsberghe
      Jens Halsberghe
    • 1 / 1