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/

    Buy and Sell Orders Execution order

    General Code/Help
    execution order margin
    3
    6
    569
    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.
    • S
      sebs29 last edited by

      Hi everyone,
      I'm starting using backtrader, and I am running into an issue with orders execution. Here's my strategy:

      • I define a set of stocks I want to invest on
      • I select the two best according to some indicators, and invest 50% of my available cash on each. Everything works well until this point.
      • At some point, I determine that the stocks I have in positions are not the right ones anymore, and I want to change my position. So I send sell orders to close the 2 existing positions, and then 2 buy orders to invest on 2 new stocks. These orders are setup to be executed on next day close.

      My issue is sometimes, one of the buy order is treated before the sell orders. Then the order gets rejected because I don't have the cash available.

      Is there a way to work around this please ?

      Thank you !
      Seb

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

        Orders are executed (and evaluated for admission simulating pseudo-execution) on a FIFO basis, just like in a regular exchange.

        You have to properly schedule the order. If your sell orders are sent before the buy orders and are Market orders, they will ALWAYS be executed before the buy orders.

        1 Reply Last reply Reply Quote 0
        • S
          sebs29 last edited by

          Thanks for your reply. But I would like these orders to be executed on the close, next day. So I currently send my orders as "CLOSE". Can we achieve the same behavior ?

          B 1 Reply Last reply Reply Quote 0
          • B
            Batman last edited by

            Perhaps the FIFO order queue is maintained for each stock? And because you have more than one stock, the order of execution is defined by the list of stocks and not by the placement of buy and sell orders (the FIFO queue is valid only for 1 stock, not for a list of stocks)?

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

              The FIFO order (in backtesting) is a general one for good reasons

              1. If there were independent FIFO queues ... which one would have the preference? Alphabetic ordering?
              2. General FIFO ordering ensures that selling something ensures funds are freed for buying something then.
              1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators @sebs29 last edited by

                @sebs29 said in Buy and Sell Orders Execution order:

                But I would like these orders to be executed on the close, next day. So I currently send my orders as "CLOSE". Can we achieve the same behavior ?

                The question as formulated makes no sense. The only thing that matters in what you previously described is ordering.

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