Order Price is different from issued price
-
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.
-
@new_trader I'm not sure what your question is, but it seems that BT is doing what I'd expect:
- 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.
- 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.
- 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. -
@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. -
@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?
-
@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.
-
@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. -
@new_trader Have a look here
-
@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).
-
@new_trader use buy stop order.
-
@davidavr Yeah, I know it's weird. My strategy requires this functionality. Thanks for your help.
-
@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.
-
@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!
-
@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.
-
@ab_trader Ah. My bad for missing that comment.