Backtrader Community

    • 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/

    how to reset the buy limit price everyday if no trade is open

    General Code/Help
    1
    3
    69
    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.
    • kian hong Tan
      kian hong Tan last edited by

      Hello community,

      this is my strategy:

      1. long only. 1 hour bar chart. Everyday at 01:00, new buy limit order with stop loss and take profit orders will be created.
      2. buy limit price is 80% of BTC open price. i added a new column in datasheet named btcopen.
      3. if at 23:00, no order is filled, all orders will be cancelled.
      4. next day, repeat from 1 to 4

      my problem:

      1. let's say on 1 Oct, BTC open price = 10,000. buy price will be 8,000. But at 23:00, price still doesn't fall to 8,000, so all orders are cancelled.
      2. on 2 Oct and 3 Oct, price didn't fall to 80% of BTC open price as well. For simplicity sake, let's say the buy price on 2 Oct is 8,100 and buy price on 3 Oct is 7,900. so all orders are void.
      3. on 4 Oct, the buy price is calculated to be 7,800. However, buy order is filled with buy price 8,100 (2 Oct). Seems the the buy price is still recorded as 8,100 until the order of this price is filled, then only it will reset.
      4. this issue always occur when no orders are filled on a particular day. the buy price on this day will be carried over to the next day until it's being filled.

      How can i reset the buyprice everyday even though no orders are filled? My code is below. Hope you guys can help. Many tks.

                  if not self.position.size and self.datas[0].datetime.time() == datetime.time(self.p.b_entrytime , 0): 
      
                      buyprice = self.data.btcopen * 0.80
                      self.order = self.buy(size=100/buyprice, exectype=bt.Order.Limit, price=buyprice, transmit=False)       
                      b_stop_price = buyprice* (1.0 - self.p.stop_loss)    
                      b_take_profit = buyprice * (1.0 + self.p.take_profit) 
                      self.b_stop_loss = self.sell(size=100/buyprice, exectype=bt.Order.Stop, price=b_stop_price, parent=self.order,transmit=False)
                      self.b_take_profit = self.sell(size=100/buyprice, exectype=bt.Order.Limit, price=b_take_profit, parent=self.order,transmit=True)
      
                  if self.datas[0].datetime.time() == datetime.time(self.p.b_exittime , 0): 
      
                      self.order = self.close()
                      self.cancel(self.b_stop_loss)
                      self.cancel(self.b_take_profit)
                      
                      return
      
      kian hong Tan 1 Reply Last reply Reply Quote 0
      • kian hong Tan
        kian hong Tan @kian hong Tan last edited by

        @kian-hong-tan

        My problems (Corrections):

        1. let's say on 1 Oct, BTC open price = 10,000. buy price will be 8,000. Price falls below 8,000 and buy order is filled. But at 23:00, price doesnt hit stop loss or take profit. All orders are closed.
        2. on 2 Oct and 3 Oct, price didn't fall to 80% of BTC open price as well. For simplicity sake, let's say the buy price on 2 Oct is 8,100 and buy price on 3 Oct is 7,900. so all orders are void.
        3. on 4 Oct, the buy price is calculated to be 7,800. However, buy order is filled with buy price 8,100 (2 Oct). Seems like the the buy price is still recorded as 8,100 until the order of this price is filled, then only it will reset.
        4. this issue always occur when no buy limit order is filled on a particular day. the buy price on this day will be carried over to the next day until it's being filled.
        kian hong Tan 1 Reply Last reply Reply Quote 0
        • kian hong Tan
          kian hong Tan @kian hong Tan last edited by

          @kian-hong-tan
          Hello @rajanprabu @ab_trader ! Are you guys able to help? much appreciated.

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