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/

    Hull Moving Average is not available?

    General Code/Help
    2
    8
    769
    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.
    • 黄光裕
      黄光裕 last edited by

      params = (('pfast', 40), ('pslow', 200),)

      def __init__(self):
          # To keep track of pending orders and buy price/commission
          self.is_order = None
          self.trader_price = None
          self.risk = 0.05  # risk 10%
          self.stop_dist = 0.02  # stoploss distance 5%
          self.next_runs = 0
      
          self.dataclose = self.datas[0].close
          self.datavolume = self.datas[0].volume
      
      
          self.pfast = bt.indicators.HullMovingAverage(period=self.p.pfast)
      
          self.pslow = bt.indicators.HullMovingAverage(period=self.p.pslow)
      
      1 Reply Last reply Reply Quote 0
      • 黄光裕
        黄光裕 last edited by

        When testing back, it can be operated. When trading on bitmex line, it will not be executed.

        1 Reply Last reply Reply Quote 0
        • 黄光裕
          黄光裕 last edited by

          My backtrader version is 1.9.66.122

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            Yes, it is available.

            You may want to elaborate as to what your actual problem is.

            黄光裕 1 Reply Last reply Reply Quote 0
            • 黄光裕
              黄光裕 @backtrader last edited by

              @backtrader
              If compression = 5 does not execute,
              If compression = 1 can be executed, why is that?

              1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators last edited by

                I think you overvalue the capacity of people to evaluate your problem. You complain first about 'bitmex line*, then about compression.

                Again, you need to elaborate on what your actual problem is and what errors are generated, with which code and data.

                黄光裕 1 Reply Last reply Reply Quote 1
                • 黄光裕
                  黄光裕 @backtrader last edited by

                  @backtrader
                  Sorry, I may not have described the problem clearly.

                  class TestStrategy(bt.Strategy):
                  params = (('pfast', 40), ('pslow', 150),)

                  def __init__(self):
                      # To keep track of pending orders and buy price/commission
                      self.is_order = None
                      self.trader_price = None
                      self.risk = 0.05  # risk 10%
                      self.stop_dist = 0.02  # stoploss distance 5%
                      self.next_runs = 0
                  
                      self.dataclose = self.datas[0].close
                      self.datavolume = self.datas[0].volume
                  
                      self.pfast = bt.indicators.HullMovingAverage(period=self.p.pfast)
                      self.pslow = bt.indicators.HullMovingAverage(period=self.p.pslow)
                  
                  def start(self):
                      self.counter = 0
                      print('START')
                  
                  def prenext(self):
                      self.counter += 1
                  
                  def next(self):
                      if self.live_data:
                          cash = cerebro.broker.getcash()
                          if isinstance(cash, str):
                              print("*****  get cash is faild *****")
                              return
                  
                          qty = 20  # math.ceil((cash * self.risk * 5000 * 100) / (self.data.close[0]))
                          print(bt.num2date(self.datetime[0]), "cash=", cash, "size=", qty, "close=", self.data.close[0],
                                "=", (self.data.close[0] > self.pfast and self.data.close[0] > self.pslow ),
                                "=", (self.data.close[0] < self.pfast and self.data.close[0] < self.pslow ))
                          if self.is_order == None and (
                                  self.data.close[0] > self.pfast and self.data.close[0] > self.pslow):
                              stop_price = round(self.data.close[0] * (1 - self.stop_dist))
                              qty = 20  # math.ceil((cash * self.risk * 5000 * 100) / (self.data.close[0]))
                              print(" price=", self.data.close[0], "size=", qty)
                              try:
                                  self.buy(size=qty, exectype=bt.Order.Market)
                              except IndexError:
                                  time.sleep(100)
                                  self.buy(size=qty, exectype=bt.Order.Market)
                  
                              time.sleep(100)
                              try:
                                  self.sell(exectype=bt.Order.StopLimit, stopPrice=stop_price, price=stop_price,
                                            size=qty)
                              except IndexError:
                                  time.sleep(100)
                                  self.sell(exectype=bt.Order.StopLimit, stopPrice=stop_price, price=stop_price,
                                            size=qty)
                              self.trader_price = self.data.close[0]
                              self.is_order = 'D'
                              # print("-", self.data.close[0])
                  
                          if self.is_order == None and (
                                  self.data.close[0] < self.pfast and self.data.close[0] < self.pslow ):
                              stop_price = round(self.data.close[0] * (1 + self.stop_dist))
                              qty = 20  # math.ceil((cash * self.risk * 5000 * 100) / (self.data.close[0]))
                              print(" price=", self.data.close[0], "size=", qty)
                              try:
                                  self.sell(size=qty, exectype=bt.Order.Market)
                              except IndexError:
                                  time.sleep(100)
                                  self.sell(size=qty, exectype=bt.Order.Market)
                              time.sleep(100)
                              try:
                                  self.buy(exectype=bt.Order.StopLimit, stopPrice=stop_price, price=stop_price,
                                           size=qty)
                              except IndexError:
                                  time.sleep(100)
                                  self.buy(exectype=bt.Order.StopLimit, stopPrice=stop_price, price=stop_price,
                                           size=qty)
                              self.trader_price = self.data.close[0]
                              self.is_order = 'K'
                          if self.is_order == 'D' and (self.data.close[0] > self.pfast and self.data.close[0] > self.pslow ):
                              return
                          elif self.is_order == 'D' and (self.data.close[0] < self.pfast):
                              try:
                                  self.sell(size=qty, exectype=bt.Order.Market)
                                  print(" price=", self.data.close[0], "size=", qty, "npl=",
                                        self.data.close[0] - self.trader_price)
                              except IndexError:
                                  time.sleep(100)
                                  self.sell(size=qty, exectype=bt.Order.Market)
                                  print(" price=", self.data.close[0], "size=", qty, "npl=",
                                        self.data.close[0] - self.trader_price)
                              finally:
                                  self.is_order = None
                                  print('Stopping Backtrader')
                                  self.env.runstop()
                  
                          if self.is_order == 'K' and (self.data.close[0] < self.pfast and self.data.close[0] < self.pslow ):
                              # logging.info(bt.num2date(self.datetime[0]), "pass", self.data.close[0])
                              return
                          elif self.is_order == 'K' and (self.data.close[0] > self.pfast):
                              try:
                                  self.buy(size=qty, exectype=bt.Order.Market)
                                  print(" price=", self.data.close[0], "size=", qty, "npl=",
                                        self.trader_price - self.data.close[0])
                              except IndexError:
                                  time.sleep(100)
                                  self.buy(size=qty, exectype=bt.Order.Market)
                                  print(" price=", self.data.close[0], "size=", qty, "npl=",
                                        self.trader_price - self.data.close[0])
                              finally:
                                  self.is_order = None
                                  print('Stopping Backtrader')
                                  self.env.runstop()
                  
                  def notify_data(self, data, status, *args, **kwargs):
                      dn = data._name
                      dt = datetime.now()
                      msg = 'Data Status: {}, Order Status: {}'.format(data._getstatusname(status), status)
                      print(dt, dn, msg)
                      if data._getstatusname(status) == 'LIVE':
                          self.live_data = True
                      else:
                          self.live_data = False
                  
                  黄光裕 1 Reply Last reply Reply Quote 0
                  • 黄光裕
                    黄光裕 @黄光裕 last edited by

                    When I run the following code, it's OK.

                        hist_start_date = datetime.utcnow() - timedelta(minutes=750)
                        data = store.getdata(dataname='ETH/USD', name="eth_usd_min",
                                     timeframe=bt.TimeFrame.Minutes, fromdate=hist_start_date,
                                     ohlcv_limit=750, compression=1)
                    

                    Then run the following code without any errors and stop

                              hist_start_date = datetime.utcnow() - timedelta(minutes=750)    
                              data = store.getdata(dataname='ETH/USD', name="eth_usd_min",
                                         timeframe=bt.TimeFrame.Minutes, fromdate=hist_start_date,
                                         ohlcv_limit=750, compression=5)
                    

                    The following figure shows that if Hull Moving Average is not applied and EMA is adopted, there will be no problem at all, or Hull Moving Average is used, but compression must be set to 1. Why? Thank you very much for your reply.

                    0_1555173932672_890f44c2-fe26-4e1a-9b54-d276d4ec7968-image.png

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