Stop loss not working??
Forums › ProRealTime English forum › ProOrder support › Stop loss not working??
- This topic has 13 replies, 4 voices, and was last updated 2 years ago by robertogozzi.
-
-
01/06/2022 at 9:51 AM #184713
Morning all.
Please see attached / below code.
After backtesting for what seems like ages (Over a year), i went live testing on a small account a few months ago, however – The stoploss does not show on either the ProRealTime charts or on my brokers web-charts.
The stop never gets hit and the losses are mounting up –
Could anyone tell me whats wrong with the code please?
Also – Have a play with it and let me know if you are getting the same issue?
Thanks
defparam cumulateorders=false
defparam preloadbars=200000
n=1.5
m=1
FEMA=ExponentialAverage[40](close)
LEMA=ExponentialAverage[200](close)
MEMA=ExponentialAverage[300](close)
SEMA=ExponentialAverage[950](close)
atr=Averagetruerange[14](close)//AcceptableTradingHours
//This trades monday through friday between Midnight am and 5pm
TimeandDay=(dayofweek>1 and dayofweek<6)and(time>=000500 and time<=170000)//FirstOrder
if TimeandDay then
if not longonmarket and FEMA crosses over SEMA then
buy n shares at close limit
endif
if not shortonmarket and FEMA crosses under SEMA then
sellshort n shares at close limit
endifendif
//FirstOrderExit
if longonmarket and FEMA crosses under MEMA then
sell n shares at market
endif
if shortonmarket and FEMA crosses over MEMA then
exitshort n shares at market
endif//SecondOrder
if TimeandDay then
if not longonmarket and not shortonmarket and FEMA>SEMA and FEMA crosses over LEMA then
buy m shares at market
endif
if not longonmarket and not shortonmarket and FEMA<SEMA and FEMA crosses under LEMA then
sellshort m shares at market
endif//Stoploss
SET STOP pLOSS 40
SET STOP pTRAILING 0
SET TARGET pPROFIT 65ENDIF
01/06/2022 at 10:22 AM #184721Hi lisamtitch,
Are you sure that the wheel above the mouse pointer is still there ?
If not, the automatic trading stopped, the StopLoss and TakeProfit orders have been thrown out, and your code is as a loose running train now. But in that case you can deal with it as a manual trade (put Limits, kill the position).If this is indeed your case … it is a Setting which allows for the position to stay after something goes odd with the autotrading. Flip that setting, and the position will be killed right away as well.
Peter
01/06/2022 at 10:39 AM #184726Thanks Peter,
The code has been working fine on backtest, set from 01/01/2021 and have been running it on my demo for the whole year, making small adjustments to the stop and so on as i go.
Since moving it over to live account (Small) the stop does show on the charts, either on PRT or my brokers web charts – See attached file if i can add one.
Not sure about the setting you mean – The code is the code, and if you mean inside PRT then i havent chanegd anything else and my other automated strategies are working…
Thanks again for any help..
01/06/2022 at 10:42 AM #18472801/06/2022 at 10:45 AM #184730go Settings>Trading>Automatic Trading and you’ll find options for ‘Position and order status after stop of systems’
if ‘Remain open’ is selected then it’s poss that your system stopped but the position stayed open, so the stop instructions won’t apply.
01/06/2022 at 11:07 AM #184737Thanks nonetheless,
Yeah screenshot shows not changed and is still default – Close.
Just to confirm, i have changed a few of the strategy parameters before i popped it on this thread, if you were to back test it, it would show a major loss.
The real EMA’s and times have a demo account return showing amazing results, the fact that when on the live account the stop never shows and does not get hit is annoying me, in fact after so much time looking at the code and running around 30 more tests on different markets, i am down to 3 possible reasons why its not working, but getting to the actual bottom of why is out of my reach.
Its not even hard code, its childs play code. Simple is the best way ahead – Especially over time.
The code runs great on the demo account PRT and web charts, but as soon as its on a live account the stop doesn’t show…
01/06/2022 at 12:24 PM #184750could be a problem with
SET STOP pTRAILING 0
I never use that sort of trail so I’m not familiar with it but a 0 value looks wrong to me ???
Maybe try replacing it with this (includes breakeven)
1234567891011121314151617181920212223242526272829303132333435363738//points trailing stop functiontrailingstart = tst //trailing will start @trailinstart points profittrailingstep = st //trailing step to move the "stoploss"//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF01/06/2022 at 1:51 PM #184758Thanks nonetheless,
See attached – not letting me backtest or add to code –
01/06/2022 at 2:11 PM #184762tst and st are variables, you have to replace them with some value
try 25 and 1 as a test or (better) run the optimizer
01/06/2022 at 8:33 PM #184783Hey again,
Yeah did that, and had to remove a few other bits for it to work –
Had to remove all the STOPLOSS info, then it worked. However, there is no stop if it opens and the trade and goes against it. Full account loss in less than a year. Makes a fortune (Unbelievable) profits at 1tst/1st, but drawdown is a cow.
The code looks great and thanks for helping, but i still need the stoploss to be in place as soon as the trade opens.
Have since sent the code to a freelance chap (Quite a lot of cash) and ill see what he says, but it looks like it works on my partners account…… Seems a little suspect to me.
Thanks again, any more info would be great.
Mitch
01/06/2022 at 9:05 PM #184784Try it like this. I removed ATR (not used) and changed Dayofweek >0 (>1 won’t trade on monday)
Run SL, TP, SLS, TPS through the optimizer, you might find better results with different values for long and short.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990defparam cumulateorders=falsedefparam preloadbars=10000n=1.5m=1FEMA=ExponentialAverage[40](close)LEMA=ExponentialAverage[200](close)MEMA=ExponentialAverage[300](close)SEMA=ExponentialAverage[950](close)//AcceptableTradingHours//This trades monday through friday between Midnight am and 5pmTimeandDay=(dayofweek>0 and dayofweek<6)and(time>=000500 and time<=170000)//FirstOrderif TimeandDay thenif not longonmarket and FEMA crosses over SEMA thenbuy n shares at close limitSET STOP pLOSS slSET TARGET pPROFIT tpendifif not shortonmarket and FEMA crosses under SEMA thensellshort n shares at close limitSET STOP pLOSS slsSET TARGET pPROFIT tpsendifendif//FirstOrderExitif longonmarket and FEMA crosses under MEMA thensell at marketendifif shortonmarket and FEMA crosses over MEMA thenexitshort at marketendif//SecondOrderif TimeandDay thenif not longonmarket and not shortonmarket and FEMA>SEMA and FEMA crosses over LEMA thenbuy m shares at marketSET STOP pLOSS slSET TARGET pPROFIT tpendifif not longonmarket and not shortonmarket and FEMA<SEMA and FEMA crosses under LEMA thensellshort m shares at marketSET STOP pLOSS slsSET TARGET pPROFIT tpsendifENDIF//points trailing stop functiontrailingstart = tst //trailing will start @trailinstart points profittrailingstep = st //trailing step to move the "stoploss"//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF01/06/2022 at 9:19 PM #184785You can’t have tst = 1 (if that’s what you meant) due to minimum stop distance.
01/07/2022 at 1:46 AM #184793Hey again nonetheless.
Yeah i know dude, but after a little jiggery tests, there is no fixed SL. If the price goes immediately against the trade when it opens, there is no stop..
My fixed 40point SL worked so well in backtesting on demo.
I shall wait and see what the expensive “Coder” thinks.
Thanks again.
01/07/2022 at 9:26 AM #184799In your code (first post), remove the pTRAILING line and move the last ENDIF to just after the previous ENDIF (before//Stoploss).
If you need a trailing stop add a code snippet as already suggested.
Max PreLoadBars are 10000, any higher value is ignored.
-
AuthorPosts
Find exclusive trading pro-tools on