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)
timeframe(15 minutes, updateonclose)
MovingAverage = average[200] (close)
closeTimeFrame=close
highTimeFrame = high
lowTimeFrame = low
IF closeTimeFrame > MovingAverage AND lowTimeFrame < MovingAverage THEN ///LONG CONDITION
ShortStopAt = highTimeFrame+EntryBuffer-closeTimeFrame
BuySellState=1//buy state
BuySellAt = highTimeFrame+EntryBuffer
LongStopAt = BuySellAt - lowTimeFrame+EntryBuffer
ENDIF
IF closeTimeFrame < MovingAverage AND highTimeFrame> MovingAverage THEN //SHORT CONDITION
LongStopAt = closeTimeFrame-lowTimeFrame+EntryBuffer
BuySellState=-1//Sell state
BuySellAt =lowTimeFrame-EntryBuffer
ShortStopAt = highTimeFrame-BuySellAt+EntryBuffer //high-low+2*buffer == buy price- SL price
ENDIF
timeframe(default)
IF ONMARKET THEN
IF SHORTONMARKET AND BuySellState=1 THEN //if int eh market prepare to get out
IF high> BuySellAt THEN
EXITSHORT AT MARKET
ENDIF
SET STOP LOSS ShortStopAt
ENDIF //not in the market, prepare to get in
IF LONGONMARKET AND BuySellState=-1 THEN
IF low < BuySellAt THEN
SELL AT MARKET
ENDIF
SET STOP LOSS LongStopAt
ENDIF
ENDIF
IF NOT ONMARKET THEN //REMIND THE NEXT BAR WHAT LEVELS TO BE ON
IF BuySellState=1 THEN //BUY TRIGGERED
BUY StakeSize CONTRACTS AT BuySellAt stop
SET STOP LOSS LongStopAt
ENDIF
IF BuySellState=-1 THEN //SELL TRIGGERED
SELLSHORT StakeSize CONTRACTS AT BuySellAt stop
SET STOP LOSS ShortStopAt
ENDIF
endif
The 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.
Thanks 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,
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
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.
OK 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!
from 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?
from 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):
timeframe(default)
ONCE EntryBuffer = 5 * pipsize
once StakeSize = 1
timeframe(15 minutes, updateonclose)
MySMA = average[200](close)
IF close > MySMA AND low < MySMA THEN ///LONG CONDITION
IF Not OnMarket THEN
MyStopLoss = abs(TradePrice - low)
ENDIF
ShortStopAt = high+EntryBuffer-close
BuySellState = 1//buy state
BuySellAt = high+EntryBuffer
LongStopAt = BuySellAt - low+EntryBuffer
ENDIF
IF close < MySMA AND high> MySMA THEN //SHORT CONDITION
IF Not OnMarket THEN
MyStopLoss = abs(TradePrice - high)
ENDIF
LongStopAt = close-low+EntryBuffer
BuySellState = -1//Sell state
BuySellAt = low-EntryBuffer
ShortStopAt = high-BuySellAt+EntryBuffer //high-low+2*buffer == buy price- SL price
ENDIF
timeframe(default)
IF ONMARKET THEN
IF SHORTONMARKET AND BuySellState = 1 THEN //if int eh market prepare to get out
IF high> BuySellAt THEN
EXITSHORT AT MARKET
ENDIF
IF PositionPerf > 0 THEN
SET STOP LOSS MyStopLoss
ENDIF
ENDIF //not in the market, prepare to get in
IF LONGONMARKET AND BuySellState = -1 THEN
IF low < BuySellAt THEN
SELL AT MARKET
ENDIF
IF PositionPerf > 0 THEN
SET STOP LOSS MyStopLoss
ENDIF
ENDIF
ENDIF
IF NOT ONMARKET THEN //REMIND THE NEXT BAR WHAT LEVELS TO BE ON
IF BuySellState = 1 THEN //BUY TRIGGERED
BUY StakeSize CONTRACTS AT BuySellAt stop
SET STOP LOSS LongStopAt
ENDIF
IF BuySellState = -1 THEN //SELL TRIGGERED
SELLSHORT StakeSize CONTRACTS AT BuySellAt stop
SET STOP LOSS ShortStopAt
ENDIF
endif