Breakout box strategy (tick by tick not working for entries)
Forums › ProRealTime English forum › ProOrder support › Breakout box strategy (tick by tick not working for entries)
- This topic has 8 replies, 5 voices, and was last updated 5 days ago by Watanabix.
-
-
11/29/2024 at 11:37 PM #240920
Hi everyone,
I am trying to build a simple futures strategy that will go long if the price goes above the high+buffer (measured in points as well) of a certain bar or goes short if it dips below the low-buffer of the same bar.
1 – When I run the strategy, all my entries and exits occur at 15 minutes intervals. I am using 15 minutes. Please see attached.
2 – Why can’t the system buy the price right when it crosses for example the high of the bar plus the buffer added? Is there a way to program the strategy to use the “live price”?
3 – Does it always have to wait for the close of the bar and then check if it matches any condition to enter a trade (it doesn’t seem that the tick by tick feature is working for entering or exeting a trade right when the conditions are met)?
4 – The other thing that is weird is that I set a profit target of 40 points (using the dow as an example), which should equal $200 but the gain of best trade always shows $13.80. How can a trade be a winner and have much less than the profit target (assuming no flattening is forced which was the case)? Also attached for convenience
I hope I explained this issue correctly. Thank you again everyone.
1234567891011121314151617181920212223DEFPARAM FlatAfter= 160000DEFPARAM CUMULATEORDERS=FALSEdefparam flatbefore= 100000If OpenTime=094500 and OpenTime<100000 thenHI = highLO = lowEndIfIF Opentime >= 100000 AND NOT ONMARKET THENIF close > HI THENBUY 1 CONTRACTS AT HI+buffer limitSET STOP PLOSS LOSET TARGET pPROFIt ptREM Lowest breakoutELSIF close < LO THENSELLSHORT 1 CONTRACTS AT LO-buffer limitSET STOP PLOSS HISET TARGET pPROFIT ptENDIFENDIF11/30/2024 at 6:54 AM #240928Hi Watanabix,
Your #1,2,3 questions are just about the Backtest report only showing at the intervals of your chart’s Timeframe. In Live (Demo too) you will see that this all works fine.
With Backtest, internally it also works fine. I mean, the profit and such are from the real “tick” crossings.Regarding #4 I can only say that your Set Target as well as the Set StopLoss are subject to an If condition, which is not allowed. In practice (Live/Demo) this will fail just the same (as in Backtest). Just set both at the beginning or end of your program and it will work.
Keep in mind that it is not this black and white as I or formal rules tell you;
You *can* have either under a condition, but that condition should determine the price (and then be very careful what you imply with that – e.g. a changing TP each bar). You, in this case, have the TP and SL determined only once, when you buy. This is caused by the If around it.The general rule is that such “pending” orders need to be repeated each bar (thus, each call of your code). Do notice, however, that the rules depend on the broker and that it may happen that for example, the StopLoss just needs to be given once (thus that under your condition it *would* work). But because for the real normal Pending Orders (Limit and Stop) this rule of needing to repeat for 100% sure works like that everywhere, it is best to treat the TP and SL the very same.
Notice that this is harmless, because the originating party – PRT in this case – will intelligently send a new order or not. This means : when you give such a pending order the same price as in the previous bar, nothing will be resent to the broker if only the broker will let the order stay. This is how “intelligently” comes into play, because if the broker desires to explicitly repeat the order or else it will be cancelled, then PRT will know about that, and thus repeats the order (with the same price).
All is no worry for you if yo only always repeat the orders. This means : no conditions for them.But change the price underway (like your LO and pt) and PRT will automatically send a new order with the new price, and the old pending order will be cancelled at the broker.
Literally there is another dimension, because the order may be maintained by the Broker or by the Exchange. This, with the notice that IG is both, and IB is separate (like IBKR for broker and CME for e.g. Nasdaq Futures). This relates to the rules you can’t see. The pending order for the one instrument is maintained by the Broker, while the other is maintained by the Exchange. You, working through the same broker (like IBKR) won’t know this, unless you sorted it out. This is why you should be on the safe side, and cause Backtest to behave the same as Live later (Backtest knows nothing of brokers).
Does this help ?
Peter11/30/2024 at 8:41 AM #24093011/30/2024 at 12:53 PM #240941Hi,
It’s due to incorrect use of your orders. When you use “Buy Limit” orders, you must ensure that the “Close” is above your order level (Close > HI + Buffer). If this is not the case, the “Buy Limit” will be executed as a “Market Order” (as Roberto already mentioned).
The same applies to a “Sell Short Limit” order — the price must be below the order level (Close < LO – Buffer), otherwise it will be executed as a “Market Order” as well.Additionally, there’s an incorrect use of the “Stop pLoss”. When using a “stop loss” based on a price level, such as “HI or LO” (HI and LO are price levels, not points), you cannot use “Set Stop Loss”. Instead, you should use the price level with “Set Stop Price”.
These types of orders (Limit, Stop) are executed in REAL TIME at the specified levelsHI-LO BreakOut1234567891011121314151617181920212223DEFPARAM FlatAfter= 160000DEFPARAM CUMULATEORDERS=FALSEdefparam flatbefore= 100000If OpenTime=094500 and OpenTime<100000 thenHI = highLO = lowEndIfIF Opentime >= 100000 AND NOT ONMARKET THENIF close > HI+buffer THENBUY 1 CONTRACTS AT HI+buffer limitSET STOP PRICE LOSET TARGET pProfit ptREM Lowest breakoutELSIF close < LO-buffer THENSELLSHORT 1 CONTRACTS AT LO-buffer limitSET STOP PRICE HISET TARGET pProfit ptENDIFENDIF4 users thanked author for this post.
12/02/2024 at 9:17 PM #240998Hi Peter. Thanls for taking the time for such an elaborated response, however I quite couldn’t get what you meant and why I was/am having trouble with the orders. I believe some of those issues have been addressed subsequently in this thread. Thanks again!!
12/02/2024 at 10:51 PM #241001Many thanks for the comments and edits to the code.
Let me be clear. The challenge of getting the code right is something I enjoy and I am working hard to fix and see the results before coming back to the forum. It is amazing to see things coming together.
Overall, things improved considerably thanks to all your comments&edits:
- winning trades now show the correct number of points other than they are closed (forced flat) if still open at the end of the session
- the variable “buffer” is working perfectly
- the entries occur exactly at HI+buffer or LO-buffer
Although the entries and the exits seem to be respecting the conditions of going long/short abover/below the high/low (+/- a buffer) of a certain candle, their timing seems a bit weird:
- not sure why but the bar (100000) after the breakout bar (094500) never has any trades. Obviously I have something wrong in the code and I have tried many things but just can’t find the bug. The picture attached (#1) shows the breakout bar defining the highs and lows but then nothing happens in the 100000 bar, where the low of the strategy is taken beyond the LO-buffer price point. Only in the next bar the first trade happens.
- the other thing that I have tried to code but it isn’t working is that the strategy should close for the day if the first trade is a winning trade, if not it would try a second trade (and last). I have a counter for the orders but again, I am for sure doing something wrong.
Thanks again for your help. Hopefully, this will be a quick fix and the strategy can be tested as originally intended.
Have a good week everyone
tiago
12/02/2024 at 11:47 PM #241003Forgot to add the code:
DEFPARAM FlatAfter = 160000
DEFPARAM CUMULATEORDERS =FALSE
defparam flatbefore = 094500
ordercount = 0If OpenTime >= 094500 and OpenTime < 100000 then
HI = high
LO = low
EndIfif ORDERCOUNT <=2 then
IF Opentime >= 100000 AND NOT ONMARKET THEN
IF close > HI+buffer THEN
BUY CON CONTRACTS AT HI+buffer limit
SET STOP PRICE LO
SET TARGET pProfit PT
ordercount = ordercount + 1
else
ordercount = ordercount
IF close < LO-buffer THEN
SELLSHORT CON CONTRACTS AT LO-buffer limit
SET STOP PRICE HI
SET TARGET pProfit PT
ordercount = ordercount + 1
ELSE
ordercount = ordercount
ENDIFENDIF
ENDIF
ENDIF12/03/2024 at 12:03 AM #241004Using #Js ‘s code above.
The issue regarding a (100000) trade, IMO maybe this…
The code runs only once a bar, orders are placed at the start of the next bar.
At 094500 HI and LOW are set, but testing for a entry cannot start till 100000.
At 100000, if condition is true to place an order, if set, it wont be processed until the end of this bar, and placed from the start of next bar.
At the next bar its now 101500. If triggered entry taken.
Therefore no entry is ever placed on 100000 bar, so no order can be made.
If you want a order to be placed on 100000 bar, it needs to be tested for and set on the 094500 bar.
12/03/2024 at 6:14 AM #241008Thanks Druby, it makes what you are saying, although I’d have thought that the next bar after the breakout bar, the system would be “active” to execute orders. I had already tried what you suggested (setting the starting time of the entry conditions to 094500 – please see code below) but the first order is still taking place always on the 101500 bar (and not 100000). I know I am doing something wrong…
This is one of the two things left to have the test exactly as intended… I hope someone can give me a hand here.
Pasting again from previous post for convenience:
Although the entries and the exits seem to be respecting the conditions of going long/short abover/below the high/low (+/- a buffer) of a certain candle, their timing seems a bit weird:
- not sure why but the bar (100000) after the breakout bar (094500) never has any trades. Obviously I have something wrong in the code and I have tried many things but just can’t find the bug. The 094500 breakout bar defines the highs and lows but then nothing never happens in the next (100000) bar (even if the high/low are reached), only in the following (101500) bar the first trade happens.
- the other thing that I have tried to code but it isn’t working is that the strategy should close for the day if the first trade is a winning trade, if not it would try a second trade (and last). I have a counter for the orders but again, I am for sure doing something wrong.
I am adding the code as is right now (sorry but I don’t know yet how to paste the code with the exact same format as the ProBuilder strategy editor).
Many thanks again everyone
123456789101112131415161718192021222324252627282930313233343536DEFPARAM FlatAfter = 160000DEFPARAM CUMULATEORDERS =FALSEdefparam flatbefore = 094500MaxTrades = MaxtradesONCE Tally = 0//// reset the TALLY each new day//IF IntraDayBarIndex = 0 THENTally = 0ENDIF//// enter a trade when conditions are met AND the trade to be opened doesn't exceed MaxTradesIf OpenTime >= 094500 and OpenTime < 100000 thenHI = highLO = lowEndIfIF Opentime >= 094500 AND NOT ONMARKET THENIF close > HI+buffer AND (Tally < MaxTrades) THENBUY CON CONTRACTS AT HI+buffer limitSET STOP PRICE LOSET TARGET pProfit PTTally = Tally + 1ELSEIF close < LO-buffer AND (Tally < MaxTrades) THENSELLSHORT CON CONTRACTS AT LO-buffer limitSET STOP PRICE HISET TARGET pProfit PTTally = Tally + 1ENDIFENDIFENDIF -
AuthorPosts
Find exclusive trading pro-tools on