Stoploss not responding with Time Frames
Forums › ProRealTime English forum › ProOrder support › Stoploss not responding with Time Frames
- This topic has 6 replies, 2 voices, and was last updated 3 years ago by robertogozzi.
-
-
04/15/2020 at 9:24 PM #126335
Hi Everyone,
I have a simple code that I am testing and for some reason it doesn’t do what I expect it to do:
here is the scenario: it is a classic “When bar crosses moving average do…something”
BUT if the market goes against me I want to change the StopLoss to the current bar (for example if I was in a short and the market moved long I want to move the SL to the high of the bar that crossed the moving average and to stop me out if it goes higher). This did not work even when I tried adding a TF and when I checked every second. at one point the SL would be 10 and then it goes to -1 and never stops. so I had to add lines 29-31 and 35-37 (SELL and SELLSHORT )
Could you help me understand why is the stoploss not activated as I would expect it to?
P.S Here is anotehr problem I had: I was not able to use crossover/under function effectively as any bar higher than the the MA would trigger the bar Crosses MA so I had to create this lower condition in line 7 (AND lowTimeFrame < MovingAverage)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253timeframe(15 minutes, updateonclose)MovingAverage = average[200] (close)closeTimeFrame=closehighTimeFrame = highlowTimeFrame = lowIF closeTimeFrame > MovingAverage AND lowTimeFrame < MovingAverage THEN ///LONG CONDITIONShortStopAt = highTimeFrame+EntryBuffer-closeTimeFrameBuySellState=1//buy stateBuySellAt = highTimeFrame+EntryBufferLongStopAt = BuySellAt - lowTimeFrame+EntryBufferENDIFIF closeTimeFrame < MovingAverage AND highTimeFrame> MovingAverage THEN //SHORT CONDITIONLongStopAt = closeTimeFrame-lowTimeFrame+EntryBufferBuySellState=-1//Sell stateBuySellAt =lowTimeFrame-EntryBufferShortStopAt = highTimeFrame-BuySellAt+EntryBuffer //high-low+2*buffer == buy price- SL priceENDIFtimeframe(default)IF ONMARKET THENIF SHORTONMARKET AND BuySellState=1 THEN //if int eh market prepare to get outIF high> BuySellAt THENEXITSHORT AT MARKETENDIFSET STOP LOSS ShortStopAtENDIF //not in the market, prepare to get inIF LONGONMARKET AND BuySellState=-1 THENIF low < BuySellAt THENSELL AT MARKETENDIFSET STOP LOSS LongStopAtENDIFENDIFIF NOT ONMARKET THEN //REMIND THE NEXT BAR WHAT LEVELS TO BE ONIF BuySellState=1 THEN //BUY TRIGGEREDBUY StakeSize CONTRACTS AT BuySellAt stopSET STOP LOSS LongStopAtENDIFIF BuySellState=-1 THEN //SELL TRIGGEREDSELLSHORT StakeSize CONTRACTS AT BuySellAt stopSET STOP LOSS ShortStopAtENDIFendif04/15/2020 at 10:44 PM #126338The code is not complete to be able to test it, since I can’t see how EntryBuffer is defined, hower:
- a crossover is detected with CROSSES OVER which means that the previous CLOSING price was < to previous MA and that the current CLOSING price is > current MA, while you defined it mixing HIGH and CLOSE
- a crossunder is just the other way round using CROSSES UNDER, instead
- the Stop Loss is used to make sure your account is not wiped out in case the market goes against you, so why would you need to change it when it DOES go against you? Keep the one you set when your trade was entered and change it only if you want to trail it
- at line 6 I think (but I could not test it) you should add IF Not OnMarket, not to change values when a trade is open
- at line 22 you should add ENDIF to match line 6
- remove lines 32 and 38, as there’s no need to set a stop loss when exiting a trade.
1 user thanked author for this post.
04/16/2020 at 12:05 AM #126343Thanks for the quick relpy,
I simplified the code before loading it here, sorry that I missed a parameter. for EntryBuffer I used 5 or 10, but it can be any number.
I guess I was not clear in my question.
the code in the 15 time frame should be executed even if the trade is active. so if the trade is active and goes against me I don’t want to wait till it reaches the old SL but I want to reset the SL to a lower level then it was originally (for short trades for example) and if the market continues to go against me then the trade should exit on the new SL and re-enter to the opposite direction (in this case go long)
the “SET STOP LOSS LongStopAt” is ignored (It seems) which is why I added a hack of exiting.
Yes Lines 32 and 38 were commented out. but I do want to remove the IF clause 29-31 and 35-37 as they are just a hack since re-setting of the SL did not work for me
I hope it is clearer now
Thanks again,
04/16/2020 at 11:24 AM #126382if the trade is active and goes against me I don’t want to wait till it reaches the old SL but I want to reset the SL to a lower level then it was originally
that’s exactly what the Stop Loss hasn’t been built for! It was built as a buffer to allow for retracements to happen when you enter a position. If, as soon as the market goes againt you, you move it closer, well… why didn’t you put it there from start?
Stop Loss should be moved to trail price when it goes IN YOUR FAVOUR, not against!
That’s what I don’t understand.
1 user thanked author for this post.
04/17/2020 at 1:07 AM #126460OK I get it,
Yes I mean for it to “trail” but to a specific place.
I will give an example I went in short and the market went down and now the MA has curved down as well.
I want to change the SL to be nearer the new MA location. so yes, trailing. but not by using percentage but to a very specific location.
In reality if the market will THEN go against me the NEW/LOWER SL would be triggered to close the trade and it will switch to a new trade which is a long one.
Maybe my code is not written well, but that is what I am attempting to do.
Since it didn’t work I had to exit the trade with a condition as a hack, which I prefer not to do
Thanks again!
04/17/2020 at 1:11 AM #12646104/17/2020 at 4:40 PM #126610from your answer I am assuming the re calling stop loss will not work.
SET STOP LOSS LongStopAt
should I be calling ploss instead?
SET STOP pLOSS LongStopAt (points from entry)?
would that make a difference?
If you trade DAX, SP or similar instruments there’s no difference, actually the correct form should be used, though. LOSS requires a difference in price (40 pips would be 0.0040 in Forex), pLOSS requires a difference in pips (40, not 0.0040).
Try this one (I replaced some variable names to make it easier for me to read):
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152timeframe(default)ONCE EntryBuffer = 5 * pipsizeonce StakeSize = 1timeframe(15 minutes, updateonclose)MySMA = average[200](close)IF close > MySMA AND low < MySMA THEN ///LONG CONDITIONIF Not OnMarket THENMyStopLoss = abs(TradePrice - low)ENDIFShortStopAt = high+EntryBuffer-closeBuySellState = 1//buy stateBuySellAt = high+EntryBufferLongStopAt = BuySellAt - low+EntryBufferENDIFIF close < MySMA AND high> MySMA THEN //SHORT CONDITIONIF Not OnMarket THENMyStopLoss = abs(TradePrice - high)ENDIFLongStopAt = close-low+EntryBufferBuySellState = -1//Sell stateBuySellAt = low-EntryBufferShortStopAt = high-BuySellAt+EntryBuffer //high-low+2*buffer == buy price- SL priceENDIFtimeframe(default)IF ONMARKET THENIF SHORTONMARKET AND BuySellState = 1 THEN //if int eh market prepare to get outIF high> BuySellAt THENEXITSHORT AT MARKETENDIFIF PositionPerf > 0 THENSET STOP LOSS MyStopLossENDIFENDIF //not in the market, prepare to get inIF LONGONMARKET AND BuySellState = -1 THENIF low < BuySellAt THENSELL AT MARKETENDIFIF PositionPerf > 0 THENSET STOP LOSS MyStopLossENDIFENDIFENDIFIF NOT ONMARKET THEN //REMIND THE NEXT BAR WHAT LEVELS TO BE ONIF BuySellState = 1 THEN //BUY TRIGGEREDBUY StakeSize CONTRACTS AT BuySellAt stopSET STOP LOSS LongStopAtENDIFIF BuySellState = -1 THEN //SELL TRIGGEREDSELLSHORT StakeSize CONTRACTS AT BuySellAt stopSET STOP LOSS ShortStopAtENDIFendif1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on