ProRealCode - Trading & Coding with ProRealTime™
Hi guys I am using this trailing stop code and it is working but when the trade is closed it is stopping running the system with this error message : The stop was closer than the minimum required distance from the market
Have been using this code before and this was not happening so not sure why it is now, the code works but when the limit creeps up and position is closed in profit the system stops running and I have to turn back on, any ideas would be much appreciated.
trailingstart = 25 //trailing will start @trailinstart points profit
trailingstep = 1 //trailing step to move the “stoploss”
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
set target pprofit 100
Have been using this code before and this was not happening so not sure why it is now,
Loads of Instruments are at or near ‘All Time Highs’ and so this results in loads of volatility which means that brokers push out / widen the minimum distance to price at which trades can be executed. If we are within the minimum distance then the trade is not executed and the System is Stopped.
Try ticking the box ‘Auto-Adjust Stops‘ when you start your System … it may help?
… And it could be due to the (short?) time frame in which you use the trailing stop now… with a short time frame, in combination with the small “trailing step”, you will soon be in the “forbidden zone” that GraHal describes…
Have been using this code before and this was not happening so not sure why it is now,Since when exactly does this happen ? (date) Did you change anything ? (anything) The solution GraHal describes should help. But a reason that this suddenly would be necessary without changes on your side, I don’t see (including GraHal’s reason 😉 )
Loads of Instruments are at or near ‘All Time Highs’ and so this results in loads of volatility which means that brokers push out / widen the minimum distance to price at which trades can be executed. If we are within the minimum distance then the trade is not executed and the System is Stopped. Try ticking the box ‘Auto-Adjust Stops‘ when you start your System … it may help?Does this burn pending stop orders being placed as well? Sounds like it might. So you end up with open positions (potentially other resting orders as well) and no system as it’s been Stopped? Strewth! So is? the logic here if you’re pending (resting either entry/stp/lmt or exit/stp/lmt) prior to any event (entries earlier before)(how early is early enough, ha) you should be okay BUT if moving orders around (system or otherwise too maybe) you could be caught out by broker imposed min. limits – so req. the use of market orders, which come with there own issues (slip etc). That said, there’s also perhaps the issue of how your ‘execution’ is manhandled in the event your ‘tp/stp’ is reached during news events etc. which is what you said above Gra. Is the logic here (if any by me) given some systems are aligned to shorter term gains and trigger on lower TF that here is where you get caught as your trade-logic has you so close, and infact w/in the spread (due to it *widening* or *whatever-intervention*) which is impossible with a market maker and so you take a bath?
I’ve never traded with systems or indicators of any significance before and am on an EDU journey with a younger family member looking to perhaps bundle a little bit of IP from generations past and systems present.All you (and your younger family member) need to know is that it happens and that you can’t do anything about it, except for being prepared to restart your system(s). Yes, only use Market orders helps. This world, regardless the broker, is not prepared for your pending orders and PRT is not prepared to counteract them. There has really been said enough about this, or, there really has been done enough about it. There is nothing left. Only one yesterday (see below, Amsterdam time) because it is about the only one left with pending Limit and they will be thrown out when IG permits. So don’t tempt them, use Market orders, don’t even think that it will cost you a bit extra, but earn a bit more for compensation. That is what I do, and it brings peace of mind. There’s no more lyrics.
Automated trading with Interactive Broker and ProRealTime – NOW AVAILABLE
trailingstart = 25 //trailing will start @trailinstart points profit
trailingstep = 1 //trailing step to move the "stoploss"
distance = 10 //distance required for pending orders
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
IF LongOnMarket THEN
IF close > NewSL + distance THEN
SELL AT newSL STOP
ELSIF close < NewSL - distance THEN
SELL AT newSL LIMIT
ELSE
SELL AT MARKET
ENDIF
ELSIF ShortOnMarket THEN
IF close > NewSL + distance THEN
EXITSHORT AT newSL LIMIT
ELSIF close < NewSL - distance THEN
EXITSHORT AT newSL STOP
ELSE
EXITSHORT AT MARKET
ENDIF
ENDIF
ENDIF
set target pprofit 100
you may want to comment out the AT MARKET exit lines in case you prefer to wait for the next candle when there’s not enough distance from the current price.
Help please Stop distance orders being rejected?
This topic contains 12 replies,
has 7 voices, and was last updated by Tbone6060
1 year, 10 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 03/29/2024 |
| Status: | Active |
| Attachments: | 1 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.