HELP >> Function CLOSE not working (it does not check price levels properly)
Forums › ProRealTime English forum › ProOrder support › HELP >> Function CLOSE not working (it does not check price levels properly)
- This topic has 7 replies, 4 voices, and was last updated 2 weeks ago by
Watanabix.
-
-
01/15/2025 at 10:06 PM #242663
Hi Traders,
I am starting this thread as a continuation from the previous one here, where I got your help to classify and count trades as a WIN or a LOSS, although where there is still an important issue left to resolve based on the following summary below:
- This is a bracket order system. It should LONG if price crosses ABOVE the HIGH of the 0945-1000 bar and go SHORT BELOW the LOW of the 0945-1000 bar (I use a BUFFER to add/subtract to the HIGH/LOW but this variable works really well)
- I am using a 15m interval
- The code is using the function CLOSE to compare/check the price with 2 other price levels (the 2 entry points above/below the 0945 bar)
- Although it looks like a simple calculation to make, its behaviour is very erratic
- It seems that it only does that check at the end (CLOSE) of the 1000 bar and onwards, essentially not doing any check for the first 15 minutes.
- On top of that, there are several circumstances where the CLOSE of a bar is above the pre-defined HIGH and the trade isn’t placed. (example attached)
- Is there a way to compare the current price with the entry points right from the 1000 start?
- Would the only “fix” be to change the interval to 1m but still define the entry points based on the 0945-1000 bar?
As always, I really appreciate your comments and suggestions. I have researched this topic on other threads but wasn’t able to find something that suits my needs.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647DEFPARAM CUMULATEORDERS = FALSE // to prevent adding to positionsdefparam FLATBEFORE = 094500DEFPARAM FLATAFTER = 160000Once MaxTrades = 1//Once BUFFER = 0//Once PT = 40IF IntraDayBarIndex=0 THENTradeCount=0TradeOn=1HI =0LO =0ENDIFIf StrategyProfit>StrategyProfit[1] thenTradeOn=0EndIfIf LongTriggered or ShortTriggered thenTradeCount=TradeCount+1EndIfIf OpenTime=094500 thenHI=HighLO=LowCandleLength=HI-LOEndIfIf NOT ONMARKET and TradeOn=1 and TradeCount<MaxTrades and CandleLength <= MaxCandleLength THENIf Close>HI+Buffer thenBUY CON CONTRACTS AT HI+Buffer limitSET STOP PRICE LO-CandleLength *stopadjusterSET TARGET pProfit PTEndIfIF Close<LO-Buffer thenSELLSHORT CON CONTRACTS AT LO-Buffer limitSET STOP PRICE HI+CandleLength *stopadjusterSET TARGET pProfit PTEndIfEndIfGraph TradeCount as "TradeCount"Graph TradeOn as "TradeOn"GraphOnPrice HI as "HI"GraphOnPrice LO as "LO"Many thanks
tiago
01/15/2025 at 11:00 PM #2426651- Your variable : MaxCandleLength is not defined
2- if you want the code to trigger the buy when price go above HI+Buffer, then you should use High instead of Close
if you code like this : If Close>HI+Buffer then, it means the Close of the candle at THE END OF THE CANDLE
So the candle that triggers the buy is the one that really CLOSE above, and the buy order is executed at the opening of the next bar…
Everything is normal here!
1 user thanked author for this post.
01/15/2025 at 11:08 PM #242666From my point of view, if you want to have accurate breakout/breakdown, the best is to have 2 Closes above or under your limits…
It is better than using a buffer that you add there… The horizontal supports and resistances are often checked with the wicks and sometimes broken by one close and then the next candle go reversal. So 2 best test for a true breakout is to have 2 bars that close above
01/29/2025 at 7:55 PM #243204Hi Lucas. Thank you for your input.
So I changed the formula to use HIGH and LOW instead of CLOSE but it still yields the same results. I honestly don’t know what I am doing wrong here and I don’t want to give up.
I am attaching the code and one example where the LO-Buffer was crossed down in the 100000 candle but it only placed an order in the next candle 101500, exactly as it had done when using the function CLOSE.
Thank you again all for trying to help.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647DEFPARAM CUMULATEORDERS = FALSE // to prevent adding to positionsdefparam FLATBEFORE = 094500DEFPARAM FLATAFTER = 160000Once MaxTrades = 2//Once BUFFER = 0//Once PT = 40IF IntraDayBarIndex=0 THENTradeCount=0TradeOn=1HI =0LO =0ENDIFIf StrategyProfit>StrategyProfit[1] thenTradeOn=0EndIfIf LongTriggered or ShortTriggered thenTradeCount=TradeCount+1EndIfIf OpenTime=094500 thenHI=HighLO=LowCandleLength=HI-LOEndIfIf NOT ONMARKET and TradeOn=1 and TradeCount<MaxTrades and CandleLength <= MaxCandleLength THENIf High >HI+Buffer thenBUY CON CONTRACTS AT HI+Buffer limitSET STOP PRICE LO-CandleLength *stopadjusterSET TARGET pProfit PTEndIfIF Low <LO-Buffer thenSELLSHORT CON CONTRACTS AT LO-Buffer limitSET STOP PRICE HI+CandleLength *stopadjusterSET TARGET pProfit PTEndIfEndIfGraph TradeCount as "TradeCount"Graph TradeOn as "TradeOn"GraphOnPrice HI as "HI"GraphOnPrice LO as "LO"01/29/2025 at 9:04 PM #24321001/30/2025 at 8:22 PM #243248Thanks for trying to help. For the sake of simplicity, I removed the maxcandle length condition (although I had set it to extremely high, or 500 points for the YM). Nothing has changed… Still places random orders… I am about to give up unfortunately.
- attaching one picture where we see it placed orders inside the breakout bar (0945-1000), not respecting the under the low of above the high of that bar
- attaching another picture where it placed, in the 1015-1030 bar, a short order correctly below the low of the breakout bar, but like it always does, it missed valid entries in the previous bar (1000-1015). I haven’t figure it out why it NEVER places an order in the bar right after the breakout bar, ie the 1000-1015
Pasting the code to see if anyone can solve this enigma… I am very frustrated with the fact that I totally believe I am aiming at something simple yet there is still no answer to this issue…
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647DEFPARAM CUMULATEORDERS = FALSE // to prevent adding to positionsdefparam FLATBEFORE = 094500DEFPARAM FLATAFTER = 160000Once MaxTrades = 2//Once BUFFER = 0//Once PT = 40IF IntraDayBarIndex=0 THENTradeCount=0TradeOn=1HI =0LO =0ENDIFIf StrategyProfit>StrategyProfit[1] thenTradeOn=0EndIfIf LongTriggered or ShortTriggered thenTradeCount=TradeCount+1EndIfIf OpenTime=094500 thenHI=HighLO=LowCandleLength=HI-LOEndIfIf NOT ONMARKET and TradeOn=1 and TradeCount<MaxTrades THENIf High >HI+Buffer thenBUY CON CONTRACTS AT HI+Buffer limitSET STOP PRICE LO-CandleLength *stopadjusterSET TARGET pProfit PTEndIfIF Low <LO-Buffer thenSELLSHORT CON CONTRACTS AT LO-Buffer limitSET STOP PRICE HI+CandleLength *stopadjusterSET TARGET pProfit PTEndIfEndIfGraph TradeCount as "TradeCount"Graph TradeOn as "TradeOn"GraphOnPrice HI as "HI"GraphOnPrice LO as "LO"01/31/2025 at 3:10 AM #243255Using the initial image, I think this is what’s happening:
The orders are only evaluated at the end of a bar, which I think here is every 15min, and placed on the next bar.
The 094500-100000 bar updates the HI and LO levels at the open of the bar, and since time is always going forward, the condition for that is true at the end of the bar, so levels are set.
Also, that candle closes below the HI level, since the close is below the high with this candle.
The 100000-101500 bar, opens below HI but wicks up through HI, and here your expecting a order because the trigger condition has been met.
However, the break of HI condition is true while the price is above HI, but then it falls to the close of the bar.
At the end of this bar, the break HI condition is false, so when the order is evaluated, the condition is false, so no order can be made.
The 101500-103000 bar, plays out a similar story as the prior bar, break in the HI level, but below it at the end of the bar, so no order.
The 103000-104500 bar, opens below the HI and then breaks through and closes above HI.
Here the trigger condition is still true at the end of the bar and the order is placed as required at the start of the next bar.
The 104500-110000 bar, note that the order is placed at the right level but its below the close of the prior bar which triggered it.
If the order is a limit order, the price needs to cross the order level from below, but its initially above.
I’m guessing here since the only way to get definitive proof is to look at the ticks, but
when the price falls back below the order level, it probably went back above with the swing of the price and triggered the order in the process.
Since the order was sent to broker, the bar end is not in the equation any more since it was triggered in the bar.
The order is executed the trade continue till the stop level is hit, several bars later with a loss.
If this is all correct, then the problems is the waiting till the end of the bar before order is evaluated.
An initial solution, is to run the code at a lower timeframe for the faster orders but, the hi/lo levels etc at the 15min timeframe, or some combination etc.
1 user thanked author for this post.
01/31/2025 at 10:56 PM #243298Hi Druby. Many thanks for taking the time to help me.
In another thread regarding this same issue, but where I included the basic conditions needed for the strategy, I got 2 responses suggesting using STOP instead of LIMIT, which I think it addresses the (good) points you raised. Unfortunately, the solutions fixed a few things but messed up with others… so still a uphill battle from here.
-
AuthorPosts
Find exclusive trading pro-tools on