Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Using Backtrader indicators outside init()

    Indicators/Strategies/Analyzers
    2
    3
    76
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Pierre Cilliers 0
      Pierre Cilliers 0 last edited by

      Hi all.

      Essentially, I am wanting to use backtrader indicators outside the backtesting environment (bt.Strategy base class).

      A bit more detail... when backtesting my strategy, I am correctly using the built in technical indicators as follows (for example):

      import backtrader as bt
      
      def __init__(self):
      
          #computing technical indicators (5-day weighted moving average)
          self.wma = bt.ind.WMA(self.data1, period=5)
      
      def next(self):
      
         self.wma_value = self.wma[0]
      
         if data1.close[0] > self.wma_value :
              self.buy()
         elif data1.close[0] < self.wma_value :
              self.sell()
      
      

      I have developed a custom API with MT4 and therefore want to use the same strategy and identical technical indicators when live trading. Therefore, in my live trading code, I would like to do something as follows (however it currently is not working as shown):

      def logic(self):
           self.historical_data = self.MT4.historical_data    # past 10 days OHLC prices pulled from MT4
           self.close = self.MT4.close    # live close price pulled from MT4
           self.wma_value = bt.ind.WMA(self.historical_data, period=5)
      
           if self.close > self.wma_value :
                   self.buy()
           elif self.close < self.wma_value :
                   self.sell()
      

      It is important to illustrate how my historical data looks which is pulled from MT4 and attempted to be used in the bt.ind library:

      {'2021.12.14 00:00': {'open': 1.12837, 'high': 1.13237, 'low': 1.12536, 'close': 1.12577, 'tick_volume': 72543}, '2021.12.15 00:00': {'open': 1.12572, 'high': 1.12991, 'low': 1.12218, 'close': 1.12937, 'tick_volume': 92187}, '2021.12.16 00:00': {'open': 1.12877, 'high': 1.13602, 'low': 1.12811, 'close': 1.13302, 'tick_volume': 94873}, '2021.12.17 00:00': {'open': 1.1329, 'high': 1.13488, 'low': 1.12348, 'close': 1.12376, 'tick_volume': 83128}, '2021.12.20 00:00': {'open': 1.12391, 'high': 1.13039, 'low': 1.12345, 'close': 1.12762, 'tick_volume': 80407}, '2021.12.21 00:00': {'open': 1.12773, 'high': 1.13026, 'low': 1.12608, 'close': 1.12854, 'tick_volume': 68844}, '2021.12.22 00:00': {'open': 1.12845, 'high': 1.13422, 'low': 1.12642, 'close': 1.13265, 'tick_volume': 66443}, '2021.12.23 00:00': {'open': 1.1323, 'high': 1.13421, 'low': 1.129, 'close': 1.13266, 'tick_volume': 62267}, '2021.12.24 00:00': {'open': 1.13173, 'high': 1.13436, 'low': 1.13033, 'close': 1.131, 'tick_volume': 80564}
      

      More specifically, I would want to call the bt.ind library in my live trading (which is NOT in the init() method). I therefore want to use my historical data as input to the bt.ind library to compute a technical indicator value (for example, the 5-day WMA).

      1 Reply Last reply Reply Quote 0
      • Pierre Cilliers 0
        Pierre Cilliers 0 last edited by

        Problem solved

        Tong Ge 1 Reply Last reply Reply Quote 0
        • Tong Ge
          Tong Ge @Pierre Cilliers 0 last edited by

          @pierre-cilliers-0 I have come to the similar issue, can you pls share your solution?

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors