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/

    Order Price is different from issued price

    Indicators/Strategies/Analyzers
    4
    14
    698
    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.
    • N
      new_trader last edited by

      Hello everyone,

      I am really new at backtrader so I don't know how to create stops and targets. I saw this post on the docs that used manual bracket orders and I copied my code from this post

      I'm issuing my trades like this

      p1 = 100+d.close[0] *0.99
                  p2 = p1 - 2* std
                  p3 = p1 + 2* std
                  self.log("Buying {} at price {} Stop {} Target {} CurrentPrice {}".format(d._name,p1,p2,p3,d.close[0]))
                  valid=self.datas[0].datetime.datetime(0) + datetime.timedelta(minutes = 2)
      
      mainside = self.buy(d,size=1,price=p1, valid=valid, exectype=bt.Order.Limit, transmit=False)
                  lowside  = self.sell(d,price=p2, size=mainside.size, valid=valid, exectype=bt.Order.Stop,
                                      transmit=False, parent=mainside)
                  highside = self.sell(d,price=p3, size=mainside.size, valid=valid, exectype=bt.Order.Limit,
                                      transmit=True, parent=mainside)
      

      but this is leading to

      2019-07-08T19:08:00, Buying AAPL at price 149.5 Stop 149.47220847072128 Target 149.52779152927872 CurrentPrice 50.0
      2019-07-08T19:09:00, BUY ORDER SUBMITTED for AAPL size 1 Created on 2019-07-08 19:08:00
      2019-07-08T19:09:00, SELL ORDER SUBMITTED for AAPL size -1 Created on 2019-07-08 19:08:00
      2019-07-08T19:09:00, SELL ORDER SUBMITTED for AAPL size -1 Created on 2019-07-08 19:08:00
      2019-07-08T19:09:00, BUY ORDER ACCEPTED for AAPL size 1 Created on 2019-07-08 19:08:00
      2019-07-08T19:09:00, SELL ORDER ACCEPTED for AAPL size -1 Created on 2019-07-08 19:08:00
      2019-07-08T19:09:00, SELL ORDER ACCEPTED for AAPL size -1 Created on 2019-07-08 19:08:00
      2019-07-08T19:09:00, BUY EXECUTED for AAPL, Size 1.000000 Price: 49.997500, Cost: 49.997500, Comm 0.049998
      2019-07-08T19:10:00, SELL EXECUTED for AAPL, Size -1 , Price: 49.992500, Cost: 49.997500, Comm 0.049993
      

      Please help.

      D 1 Reply Last reply Reply Quote 0
      • D
        davidavr @new_trader last edited by

        @new_trader I'm not sure what your question is, but it seems that BT is doing what I'd expect:

        1. You put a buy-limit order in with a 149.5 limit when the market price was 50.0 and you got filled at 49.9975.
        2. Your sell stop order was activated because the price is below the stop level of 149.4722 so it became a market order and was then executed at 49.9925.
        3. Your sell limit order was not executed because you have a limit price of 149.5278.

        But I don't understand whey you are adding 100 to the prices at the top: p1 = 100+d.close[0] *0.99. You will always be way off the market with that approach.

        N 1 Reply Last reply Reply Quote 1
        • N
          new_trader @davidavr last edited by

          @davidavr Thanks for the reply.
          I think that I'm misunderstanding something. I thought that limit order was supposed to execute when the desired price p1 was reached and the stop loss gets executed when the stoploss price is reached. I added 100 in p1 because I wanted to set an unachievable price so that it won't get executed but it is still getting executed.

          N 1 Reply Last reply Reply Quote 0
          • N
            new_trader @new_trader last edited by

            @new_trader I want the trade to get executed just above the current price but if I use Limit order, the given thing happens. What type of order can I issue then?

            D 1 Reply Last reply Reply Quote 0
            • D
              davidavr @new_trader last edited by

              @new_trader Here is how limit orders work:

              • A buy limit order will only get filled at or below the limit price. The limit is the maximum you are willing to pay. You put a 149.5 limit on it and got filled below your limit. That's good.
              • A sell limit order will only get filled at or above the limit price. The limit is the minimum you are willing to receive. Your sell limit was way above the market so you did not get filled.
              • A sell stop order is an order that gets activated once the stop price is reached. You always put sell stops below the current market price or they will be immediately activated. Once they are activated, they become market orders and will be executed at the current market price. You put a sell stop order way above the market so it was immediately active and it got filled.
              • A sell stop limit order has two prices associated with it: the stop price is the price at which it becomes activated at which time it becomes a sell limit order at the limit price. And like any sell limit order, it is the minimum price you are will to receive. You did not use this order type. You used a sell stop order but not a sell stop limit order.

              Hope that helps.

              N 1 Reply Last reply Reply Quote 2
              • N
                new_trader @davidavr last edited by

                @davidavr Hey thanks for the reply. I really appreciate your help.
                I understand what's happening here now. Can you tell me what type of order should I use if I want to buy at a price when the price reaches higher than the current price? For example, issuing the buy order when the price reaches 149, not 49.

                run-out D A 3 Replies Last reply Reply Quote 0
                • run-out
                  run-out @new_trader last edited by

                  @new_trader Have a look here

                  RunBacktest.com

                  1 Reply Last reply Reply Quote 0
                  • D
                    davidavr @new_trader last edited by

                    @new_trader You could just wait until the price reaches your threshold in next() before you place an order. Right now you are placing an order regardless of the current price.

                    But what you are describing is a very odd strategy: you want to wait until the price is much higher before you buy it. Most strategies boil down to "buy low, sell high" in one form or another (but not necessarily in that order).

                    N A 2 Replies Last reply Reply Quote 1
                    • A
                      ab_trader @new_trader last edited by

                      @new_trader use buy stop order.

                      • If my answer helped, hit reputation up arrow at lower right corner of the post.
                      • Python Debugging With Pdb
                      • New to python and bt - check this out
                      1 Reply Last reply Reply Quote 1
                      • N
                        new_trader @davidavr last edited by

                        @davidavr Yeah, I know it's weird. My strategy requires this functionality. Thanks for your help.

                        1 Reply Last reply Reply Quote 0
                        • A
                          ab_trader @davidavr last edited by

                          @davidavr said in Order Price is different from issued price:

                          But what you are describing is a very odd strategy: you want to wait until the price is much higher before you buy it. Most strategies boil down to "buy low, sell high" in one form or another (but not necessarily in that order).

                          this is what typically trend following strategies are doing: wait for the price to make a move in the necessary direction and only after this move the position in that direction is open. Buy high, sell much higher.

                          TS (probably) tries to simulate price channel breakout strategy.

                          • If my answer helped, hit reputation up arrow at lower right corner of the post.
                          • Python Debugging With Pdb
                          • New to python and bt - check this out
                          D 1 Reply Last reply Reply Quote 1
                          • D
                            davidavr @ab_trader last edited by

                            @ab_trader Yes, I'm familiar with momentum strategies, but I just have never seen one that is waiting for the price to triple (from 50 to 150) before it thinks there is enough momentum to jump in!

                            A 1 Reply Last reply Reply Quote 1
                            • A
                              ab_trader @davidavr last edited by

                              @davidavr I think it is due to testing or debugging reasons. From one of the posts above:

                              I added 100 in p1 because I wanted to set an unachievable price so that it won't get executed but it is still getting executed.

                              • If my answer helped, hit reputation up arrow at lower right corner of the post.
                              • Python Debugging With Pdb
                              • New to python and bt - check this out
                              D 1 Reply Last reply Reply Quote 1
                              • D
                                davidavr @ab_trader last edited by

                                @ab_trader Ah. My bad for missing that comment.

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