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/

    Questions about EMA

    Indicators/Strategies/Analyzers
    2
    9
    161
    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.
    • A
      Anthony Chow last edited by

      Hello everyone, i am new to BT, and trying to use EMA for backtesting,
      i am going to use EMA(period=60)(green line in the plot).

      My question :
      I want to start the backtest at 2012-01-1 (The starting point of green line is dated on 2012-01-01,), however, i have setup some calculations about the year high and year low in next(self), but it seems that the calculations is started from the beginning from the data that i input(2011-10-01).

      Is there anyway i can do so that the calculations on next will be starting from 2012-01-01 ? i have tried to change the data input starting from 2012-01-01, however its not enough preload data for ema(60) to generate.

      c8d9d5ee-97bb-431d-a8aa-0b93d4f7e607-image.png

      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @Anthony Chow last edited by

        @Anthony-Chow said in Questions about EMA:

        Is there anyway i can do so that the calculations on next will be starting from 2012-01-01 ?
        Use datetime

        from datetime import date
        

        Place a conditional statement at the beginning of next.

            def next(self):
                if self.datas[0].datetime.date() < date(2012, 1, 1):
                    return
        

        RunBacktest.com

        A 1 Reply Last reply Reply Quote 0
        • A
          Anthony Chow @run-out last edited by

          @run-out Thanks for the reply, i tried and there are still some problems that,

          0e71da0d-e140-4efc-8b9c-f591cde1e707-image.png
          e934d821-61c6-49c6-ab41-ee6d2e88a157-image.png

          print(self.data.num2date(), Dayhigh, Daylow)
          

          by printing this, it show the following, it seems that the calculations(Dayhigh) still focusing on the data before date(2012, 1, 1) and what i want is the Green One on the below drawing which is calculated starting from (2012, 1, 1) and the (Dayhigh)value should be 195.
          Really thanks for the help!!!

          fa9bdb18-215b-4a24-a8ff-fdcf1bade3fc-image.png

          84d9fe4c-d89a-42dd-bc6b-5663da03a9cd-image.png

          1 Reply Last reply Reply Quote 0
          • run-out
            run-out last edited by

            Your dayhigh and daylow code is still grabing the max for len(self) with is the whole length of the data. You need to capture the length of self once the date condition is met. Add in a new variable in the if statment that will capture the len when the date condition is met, then subtract this from lenght of self.

            I would have made this for you but you are using code in images which prevents from copying and pasting. See instructions at the top of the page regarding how to add code to your quetsions. Thanks

            RunBacktest.com

            A 1 Reply Last reply Reply Quote 1
            • A
              Anthony Chow @run-out last edited by

              @run-out Thank you sir, really appreciated

                  def __init__(self):
              
                      self.dataclose = self.datas[0].close
                      self.order = None
                      self.buyprice = None
                      self.buycomm = None
                      self.datahigh = self.datas[0].high
                      self.datalow = self.datas[0].low
                      self.datatime = self.datas[0].datetime
              
                  def next(self):
                      if self.datas[0].datetime.date() < date(2012, 1, 4):
                          return
                      
                      #Find Range
                      Dayhigh = max(self.datahigh.get(size=(len(self)),ago=0))
                      Daylow = min(self.datalow.get(size=(len(self)),ago=0))
                      Range = (Dayhigh - Daylow)
                      
                      print(self.data.num2date(), Dayhigh, Daylow)
              
              1 Reply Last reply Reply Quote 1
              • run-out
                run-out last edited by

                I've made a few changes to your code. Thanks for posting like that. In my code below, startdate would more likely be an input parameter but I left it like this for the illustration. You may wish to add an exception if startlength is None after the start date, just in case.

                from datetime import date
                
                def __init__(self):
                    self.start_length = None
                    # Your other stuff here.
                
                
                def next(self):
                    startdate = date(2012, 1, 4)  # This would probably actually be an input parameter.    
                    date = self.datas[0].datetime.date()
                    if date < startdate:
                        return
                    elif date == startdate:
                        self.start_length = len(self)
                    else:
                        if self.start_length is None: 
                                    raise ValueError("Error: start_length not properly set.")
                        
                
                    # Find Range
                    num_bars = len(self) - start_length
                
                    Dayhigh = max(self.datahigh.get(size=num_bars, ago=0))
                    Daylow = min(self.datalow.get(size=num_bars, ago=0))
                    
                    Range = (Dayhigh - Daylow)
                
                    print(date, Dayhigh, Daylow, Range)
                

                RunBacktest.com

                A 2 Replies Last reply Reply Quote 1
                • A
                  Anthony Chow @run-out last edited by

                  @run-out Thank you SIR, Its work=]

                  1 Reply Last reply Reply Quote 0
                  • A
                    Anthony Chow @run-out last edited by

                    @run-out Sorry to keep bothering you, i am really a newbie.
                    There is another problem for the first Bar(num_bars = 0), max cannot be found
                    c8757b5a-d969-4859-9954-d031b29ceb85-image.png

                    so i tried to fix it like this, but i failed, the first bar (high and low cannot be added to the Dayhigh and Daylow.

                            num_bars = len(self) - self.start_length
                            if num_bars == 0 :
                                Dayhigh = self.datahigh[0]
                                Daylow = self.datalow[0]
                                
                            else:
                                Dayhigh = max(self.datahigh.get(size=num_bars, ago=0))
                                Daylow = min(self.datalow.get(size=num_bars, ago=0))
                    
                            Range = (Dayhigh - Daylow)
                    
                            print(date, Dayhigh, Daylow, Range)
                    

                    It should be 192.77

                    824b557e-bad3-45a8-bd2e-39dc76999c25-image.png

                    run-out 1 Reply Last reply Reply Quote 0
                    • run-out
                      run-out @Anthony Chow last edited by

                      @Anthony-Chow said in Questions about EMA:

                      Dayhigh = max(self.datahigh.get(size=num_bars, ago=0))

                      Try using bt.Max() as discussed here. This will evaluate the max at each bar in the line object.

                      RunBacktest.com

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