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/

    Backtrader python client library Bracket orders limit is not working

    General Code/Help
    brackets limit orders backtrader
    2
    4
    40
    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.
    • Sunny P
      Sunny P last edited by

      Hello All,
      I am using Backtrader the testing framework for the stocks strategy

      I want to test my strategy for stock order placing, I want to place long(buy) order so for that I will use the bracket_order method in which I will set a limit

      1. at which price I want to buy the stock
      2. at which price I want to sell the stock after buy order limit is placed
      3. at which price I want to stop loss if buy order is placed and stock price is decreasing

      here is the code

      cerebro = bt.Cerebro()
      cerebro.addstrategy(MyStrategyWrapper)
      cerebro.adddata(data=historical_stock_data_for_the_day)
      cerebro.broker.setcash(1000)
      cerebro.run()
      

      this is the code in which I add my strategy class and the data historical_stock_data_for_the_day for the stock
      the format for the historical_stock_data_for_the_day will be like
      [{2389839283: 120}, {2389839211: 130}, ....... n]
      where the numbers like 2389839283 is the timestamp at which the stock price is 120

      when I call the cerebro.run() it will call my strategy class's method def next
      for the each dict in list

      my strategy class is

      class MyStrategyWrapper(bt.Strategy):
          params = ()
      
          def __init__(self, *args, **kwargs):
              self.is_ordered = None
      
      
          def next(self) -> None:
              """This is the first method which is executed on cerebro.run() method"""
              now = self.datas[0].datetime.datetime(0)
              last_price = self.datas[0].close[0]
              if self.is_ordered:
                  print("last price ", last_price)
                  # after placing an order here i get the last price (77.91) at which i set the limit to buy asset more than 10 times 
                  # that means after placing the order the stock reach that price
                  return
      
              # ...
              # My some logic if it fullfill all conditions i want to place an bracket order
              # ...
      
              time_before = None
              if large_enough_5_min:
                  time_before = True
      
              if time_before:
                  brackets = self.buy_bracket(limitprice=78.99, price=77.91, stopprice=77.37, size=1, exectype = bt.Order.Limit)
                  # (When i place the order from here limit is not reaching and order is buying stock at 77.62 instead of 77.91)
                  self.is_ordered = True
      
          def notify_order(self, order):
              """callback when order is sent and any order related update"""
              if order.status in (order.Canceled, order.Margin, order.Rejected):
                  return
      
              if order.status in (order.Submitted, order.Accepted):
                  return
      
              if order.status in (order.Completed,):
                  order_type = "long"
                  print("Order is placed at the given limit")
                  
      
          def notify_trade(self, trade):
              """callback when full trade is completed"""
              if not trade.isclosed:
                  return
              print("Trade is completed")
      

      i tried by changing the limit and checked on the official docs https://www.backtrader.com/docu/order-creation-execution/bracket/bracket/

      to place a bracket order, also checked by placing manually order but facing issue with the limit.

      please let me know if I missed something or doing wrong in this code

      Thank you

      B 1 Reply Last reply Reply Quote 0
      • B
        BorutF @Sunny P last edited by

        @sunny-p You said it buys at 77.62 instead of 77.91, that is correct as when you have a buy limit order you specify the worst(maximum) possible price you would want to buy at.

        Sunny P 1 Reply Last reply Reply Quote 0
        • Sunny P
          Sunny P @BorutF last edited by

          @borutf Thanks for replying but if sent an order with a limit if the order does not reach the limit I don't want to place an order

          B 1 Reply Last reply Reply Quote 0
          • B
            BorutF @Sunny P last edited by

            @sunny-p for buy a limit order limits the price upwards.

            you can do an Order.Stop that will be triggered at the price, however it will behave like a market order after that.

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