Stoploss beyond high/low previous bar
Forums › ProRealTime English forum › ProOrder support › Stoploss beyond high/low previous bar
- This topic has 14 replies, 3 voices, and was last updated 1 year ago by RichardVeen.
-
-
01/05/2022 at 8:57 PM #184677
Hi,
I found the code below on this forum it works fine.
Sample code123456789101112131415161718Defparam cumulateorders = false//order launch (example) would be set to any other entry conditionsc1 = close>close[1]c2 = close<close[1]if c1 thenBUY 1 LOT AT MARKETset target pprofit 30set stop loss abs((close-low))+2*pointsizeendifif c2 thenSELLSHORT 1 LOT AT MARKETset target pprofit 30set stop loss abs((close-high))+2*pointsizeendifBut I want to change code a little bit to cover the following situation:
Buy LONG: set the stoploss 1 point beyond the low of the previous bar.
Sell SHORT: set the stoploss 1 point beyond the high of the previous bar
Applicable for both situations: as long as the trade is active, the stoploss moves to the following bar. So it is a trailing stop beyond the high/low of each previous bar as long as the trade is active.
Is that possible?
01/05/2022 at 11:41 PM #184684There you go (not tested):
123456789101112131415161718192021222324252627Defparam cumulateorders = false//order launch (example) would be set to any other entry conditionsc1 = close>close[1]c2 = close<close[1]if c1 thenBUY 1 LOT AT MARKETset target pprofit 30sl = low[1] - 1*pointsizeSell at sl STOPendifif c2 thenSELLSHORT 1 LOT AT MARKETset target pprofit 30sl = high[1] + 1*pointsizeExitshort at sl STOPendifIf LongOnMarket Thensl = max(sl,low[1] - 1*pointsize)Sell at sl STOPElsif ShortOnMarket thensl = min(sl,high[1] + 1*pointsize)Exitshort at sl STOPEndif01/06/2022 at 10:20 AM #184719It’s working!
Thanks again Roberto.
06/15/2023 at 4:13 PM #216294Hi,
I received the code below from @robertogozzi
Defparam cumulateorders = false//order launch (example) would be set to any other entry conditionsc1 = close>close[1]c2 = close<close[1]if c1 thenBUY 1 LOT AT MARKETset target pprofit 30sl = low[1] – 1*pointsizeSell at sl STOPendifif c2 thenSELLSHORT 1 LOT AT MARKETset target pprofit 30sl = high[1] + 1*pointsizeExitshort at sl STOPendifIf LongOnMarket Thensl = max(sl,low[1] – 1*pointsize)Sell at sl STOPElsif ShortOnMarket thensl = min(sl,high[1] + 1*pointsize)Exitshort at sl STOPEndifAnd this code is working correctly, it sets the stop below the previous candle, when the trade starts.But when the current candle closes (the candle where the trade started) and a new candle open, then my stop disappear and I have no stop.So I have 2 questions:1. Is it possible to leave the stop at the initials previous candle, also when a new candle starts?2. Is it possible that the stop moves to the next candle when a new candle begins? So it will a stop above/below a ‘new previous’ candle06/15/2023 at 5:16 PM #216297It’s just the two of us, it’s useless to use the “@” sign, as it’s quite easy to know who your are talking to! 🙂
Are there any differences from the code I posted and the one you posted?
06/15/2023 at 5:23 PM #21629806/15/2023 at 6:23 PM #216304Ok, the two code snippets are the same.
It seems to be working fine. Try appending this line to your code:
1graphonprice sl coloured("Red")to sport any incorrect value.
If you find incorrect values, please point a few out by letting me know the instrument used, time frame, date and time of the entry candle.
06/15/2023 at 7:57 PM #216311Hi Roberto,
When I test with the ‘graphonprice line’, I see that it is working but when I test my code with automatic trading, it doesn’t.
Micro Nasdaq – 1 minute
Enter short: 15.391 -> Bar Open: 15.393.25 ….Close:15.392,75…Low: 15.382,50
Time: 20:46h (today) – I just did it
SL = 15.400 above the high of previous bar
Next bar: Open: 15.392,50….Close: 15.394,50
Time: 20.47
The SL disappear
06/16/2023 at 1:11 AM #216316The code above closes a trade at 20:46 and opens the nexct one at 20:47, but prices are different from those you posted.
It must be a different instrument or a different time.
Attach a screenshot of the whole chart.
06/21/2023 at 6:51 PM #216559Hi Roberto,
I did a trade with a simple code as stated below:
If not onmarket and hora1 and hora2 and Close>Open and Close[1]<Open[1] then
Buy possize contract at high + 1 * pointsize stop
SET TARGET PPROFIT 15
sl = low[1] – 1*pointsize
Sell at sl STOPelsif not onmarket and hora1 and hora2 and Close<Open and Close[1]>Open[1]then
Sellshort possize contract at low + 1 * pointsize stop
set target pprofit 15
sl = high[1] + 1*pointsizeIf LongOnMarket Then
sl = max(sl,low[1] – 1*pointsize)
Sell at sl STOP
Elsif ShortOnMarket then
sl = min(sl,high[1] + 1*pointsize)
Exitshort at sl STOP
Endif
endifWhen I enter the trade the ‘Stop’ is visible but after 1 candle, the ‘Stop’ disappear.
I have attached screenshots of the trade. The 1st screenshot shows the entry of the trade and the Stop. The 2nd screenshot, the trade is still active but the Stop is not there anymore.
Can you please check why the ‘Stop’ disappear when the next candle appears?
06/21/2023 at 11:03 PM #216579The whole chart, not just a small part. I need to read all info.
06/22/2023 at 12:22 AM #21658812345678910111213141516171819If not onmarket and hora1 and hora2 and Close>Open and Close[1]<Open[1] thenBuy possize contract at high + 1 * pointsize stopSET TARGET PPROFIT 15sl = low[1] – 1*pointsizeSell at sl STOPelsif not onmarket and hora1 and hora2 and Close<Open and Close[1]>Open[1]thenSellshort possize contract at low + 1 * pointsize stopset target pprofit 15sl = high[1] + 1*pointsizeIf LongOnMarket Thensl = max(sl,low[1] – 1*pointsize)Sell at sl STOPElsif ShortOnMarket thensl = min(sl,high[1] + 1*pointsize)Exitshort at sl STOPEndifendifAs you can see, your structure is not correct. The main ElsIf is executed only once, when not OnMarket. After that you are OnMarket and the Sell at sl Stop (or ExitShort at sl Stop) is executed only once.
Notice that these “Pending Stop” orders need to be executed each new bar. Thus it needs to be something like this :12345678910111213141516171819If not onmarket and hora1 and hora2 and Close>Open and Close[1]<Open[1] thenBuy possize contract at high + 1 * pointsize stopSET TARGET PPROFIT 15sl = low[1] – 1*pointsize//Sell at sl STOPelsif not onmarket and hora1 and hora2 and Close<Open and Close[1]>Open[1]thenSellshort possize contract at low + 1 * pointsize stopset target pprofit 15sl = high[1] + 1*pointsizeendifIf LongOnMarket Thensl = max(sl,low[1] – 1*pointsize)Sell at sl STOP // Must be repeated at each bar.Elsif ShortOnMarket thensl = min(sl,high[1] + 1*pointsize)Exitshort at sl STOP // Must be repeated at each bar.Endif2 users thanked author for this post.
06/22/2023 at 9:01 AM #216605Hi guys,
It’s working! That’s really great!
See the screenshots 3 & 4.
The only thing what I see, is that sometime the ‘Stop’ is not immediately active with the first candle but the ‘Stop’ appears with the 2nd candle.
See Screenshots 5 & 6
Do you why it happens?
06/22/2023 at 12:40 PM #2166151234567If LongOnMarket Thensl = max(sl,low[1] – 1*pointsize)Sell at sl STOP // Must be repeated at each bar.Elsif ShortOnMarket thensl = min(sl,high[1] + 1*pointsize)Exitshort at sl STOP // Must be repeated at each bar.EndifYou say “sometime not” but I would attest “always not”. 😉
After your code has executed and a new position is taken – which is visible in the next bar – the code needs to be called again before it can see that LongOnMarket is true. So only then (see code snippet) the pending order can emerge. Not on the same bar as where the position was taken. And so it runs one bar behind.
Notice that a normal StopLoss (Set Stop Loss) order would become visible at the same time as the position itself – at the first bar after your code was executed.1 user thanked author for this post.
06/22/2023 at 4:01 PM #216625Ok, now I understand.
Thanks Roberto & Peter for your help!
Really appreciated.
-
AuthorPosts
Find exclusive trading pro-tools on