@ab_trader that works perfectly. Thank you very much!
Posts made by kr
RE: Limit order trade entry not correct
I was reading this post:
and it said this:
Stop Entry, Stop Loss and Take Profit
Sometimes we may want to enter a position at a price which is worse than what we can have right now. We might choose to do this when looking for confirmation that price will continue moving in a certain direction. Another reason might be to try and catch a breakout. To enter with a stop order we just change the exectype like so: self.sell_bracket(limitprice=short_tp, price=entry, stopprice=short_stop, exectype=bt.Order.Limit)
I guess this is incorrect. You are saying this is not how to do entry at a worse price. Is this even possible then?
Limit order trade entry not correct
Hello, I am in need of help to understand limit orders and why the following code doesn't execute like what the docs says.
Here is my strategy:
class backtestCSV(bt.feeds.GenericCSVData): params = ( ('datetime', 0), ('open', 1), ('high', 2), ('low', 3), ('close', 4), ('volume', 5), ('openinterest', -1), ('timeframe', bt.TimeFrame.Ticks), ('compression', 1), ('dtformat', '%Y-%m-%d %H:%M:%S.%f'), ) class testStrategy(bt.Strategy): def __init__(self): self.bought = False def notify_order(self, order): print("ORDER:", self.datetime.datetime(ago=0), order.exectype, order.status, self.data0.close) def notify_trade(self, trade): print("TRADE:", self.datetime.datetime(ago=0), trade.status, self.data0.close) def next(self): if self.bought == False: entry_price = 2.0 self.buy(exectype=bt.Order.Limit, price=entry_price) self.bought = True print("BUY:", self.datetime.datetime(ago=0), entry_price)
and here is the data:
2019-01-01 00:00:00.950,1.00368,1.00368,1.00368,1.00368,29000 2019-01-01 00:00:01.176,1.00368,1.00368,1.00368,1.00368,81000 2019-01-01 00:00:02.248,1.00367,1.00367,1.00367,1.00367,50000 2019-01-01 00:00:02.506,1.00368,1.00368,1.00368,1.00368,94000 2019-01-01 00:00:02.608,1.00369,1.00369,1.00369,1.00369,56000 2019-01-01 00:00:02.709,1.00370,1.00370,1.00370,1.00370,57000 2019-01-01 00:00:05.864,1.00370,1.00370,1.00370,1.00370,93000 2019-01-01 00:00:05.966,1.00372,1.00372,1.00372,1.00372,64000 2019-01-01 00:00:06.068,1.00374,1.00374,1.00374,1.00374,57000 2019-01-01 00:00:06.173,1.00374,1.00374,1.00374,1.00374,95000 2019-01-01 00:00:06.274,1.00375,1.00375,1.00375,1.00375,38000 2019-01-01 00:00:06.434,1.00375,1.00375,1.00375,1.00375,45000 2019-01-01 00:00:07.360,1.00375,1.00375,1.00375,1.00375,93000 2019-01-01 00:00:09.092,1.00374,1.00374,1.00374,1.00374,01000 2019-01-01 00:00:09.143,1.00372,1.00372,1.00372,1.00372,63000 2019-01-01 00:00:09.296,1.00372,1.00372,1.00372,1.00372,76000 2019-01-01 00:00:09.499,1.00372,1.00372,1.00372,1.00372,28000 2019-01-01 00:00:09.601,1.00372,1.00372,1.00372,1.00372,56000 2019-01-01 00:00:09.982,1.00372,1.00372,1.00372,1.00372,56000 2019-01-01 00:00:10.084,1.00372,1.00372,1.00372,1.00372,58000 2019-01-01 00:00:10.186,1.00373,1.00373,1.00373,1.00373,68000 2019-01-01 00:00:10.287,1.00373,1.00373,1.00373,1.00373,19000 2019-01-01 00:00:12.072,1.00372,1.00372,1.00372,1.00372,65000 2019-01-01 00:00:12.174,1.00370,1.00370,1.00370,1.00370,25000 2019-01-01 00:00:13.738,1.00371,1.00371,1.00371,1.00371,76000 2019-01-01 00:00:13.960,1.00370,1.00370,1.00370,1.00370,92000 2019-01-01 00:00:14.076,1.00371,1.00371,1.00371,1.00371,76000 2019-01-01 00:00:14.372,1.00372,1.00372,1.00372,1.00372,93000 2019-01-01 00:00:14.474,1.00371,1.00371,1.00371,1.00371,39000 2019-01-01 00:00:14.575,1.00371,1.00371,1.00371,1.00371,12000 2019-01-01 00:00:14.802,1.00370,1.00370,1.00370,1.00370,25000 2019-01-01 00:00:15.886,1.00371,1.00371,1.00371,1.00371,94000 2019-01-01 00:00:15.987,1.00372,1.00372,1.00372,1.00372,85000 2019-01-01 00:00:16.088,1.00371,1.00371,1.00371,1.00371,94000 2019-01-01 00:00:16.804,1.00373,1.00373,1.00373,1.00373,62000 2019-01-01 00:00:16.905,1.00374,1.00374,1.00374,1.00374,69000 2019-01-01 00:00:17.007,2.00375,2.00375,2.00375,2.00375,86000 2019-01-01 00:00:17.136,2.00374,2.00374,2.00374,2.00374,47000 2019-01-01 00:00:17.237,2.00374,2.00374,2.00374,2.00374,94000 2019-01-01 00:00:17.795,2.00375,2.00375,2.00375,2.00375,87000 2019-01-01 00:00:18.226,2.00375,2.00375,2.00375,2.00375,44000
And here is STDOUT
BUY: 2019-01-01 00:00:01.176004 2.0 ORDER: 2019-01-01 00:00:02.247996 2 1 1.00367 ORDER: 2019-01-01 00:00:02.247996 2 2 1.00367 ORDER: 2019-01-01 00:00:02.247996 2 4 1.00367 TRADE: 2019-01-01 00:00:02.247996 1 1.00367
Can somebody tell me why the trade happens at 00:00:02 when the price is still at 1.00. Shouldn't happen at 00:00:17 when the price has reached 2.0, which is the price I set for the limit order?
RE: Bracket Orders Behavior
P.S. I have gotten around it by keeping previous datetime of the daily timeframe and skipping unless it's a new datetime:
for _, d in enumerate(self.datas): if d._timeframe == 4 or (d in prev_dt.keys() and d.datetime == prev_dt[d]): continue prev_dt[d] = d.datetime # trading logic here
Please let me know if there is a better way to do this. Thanks!
RE: Bracket Orders Behavior
@backtrader got it. It would be an awesome feature if we could give backtrader finer granularity data just for it to use in the background for smart execution of bracket orders, and give it a main data (daily in my case) to operate as it does right now, i.e. call next() with it, etc. Anyway, thank you for the information.
RE: Bracket Orders Behavior
@backtrader I may be using the wrong type of order. I basically want to simulate this real world scenario: I trade Forex on daily time frame. As you know, unlike the stock market which gives me the ability to place order before market open, etc, I just have to place order at a certain time in day to operate on daily. I want to simulate a real world scenario where I place an order at midnight (the daily time frame starting point in data bars) for a pair with takeprofit at +%1 and stoploss at -%0.5. In real world this pair will hit +%1 around noon and it will automatically be sold at a profit but goes down and will end up being at -%2 the midnight of the following day. So you see in Backtrader this would end up as a loss, since it only daily granularity, where as in real world this would have been a profit. I am trying to figure out if there is an easy way to do this in Backtrader without doing extra custom coding. Thank you in advance for your time.