Yes, in backtesting everything works fine and looks great. But when it comes to live trading some parameters change.
So my strategy enters a trade using STOP orders. And I am struggling at the moment with the situation, that somethings the algo calculates the levels of the STOPs to close to the current price, so that IG rejects the STOP orders and the strategy stops.
Any idea how to prevent this? How can I identify what the minimal distance of points to the current price must be on financial instrument level?
Thanks
There’s no way to know what that distance is at runtime and it changes when there’s high volatility or news.
You should check what it is with IG, then use this code to make sure your SL is not below:
Once Distance = 10
.
.
MyStopLoss = max(MyStopLoss, Distance)
this will not guarantee your strategy will never be stopped again when exceptions occur, but most of the times it will.
Thats a helpful snipped of code – Grazie
BCParticipant
Master
Thanks Robertogozzi
Some of my algo which use pending stop order also affect by IG conditions (too close to market price).
Any command can read the current price so that I use your syntax?
You need to apply that code whenever you place an order, be it a SET STOP LOSS or a PENDING LIMIT/STOP order (not AT MARKET).
Before you place the order make a difference between the current price (CLOSE) and your desired entry price, then check that it’s not less than the required distance:
ONCE Distance = 10 * pipsize
.
.
IF MyLongConditions AND close > close[1] THEN //your long conditions on a raising market
EntryPrice = min(Close - Distance,low[1]) //enter al the lowest price between LOW[1] and CLOSE-Distance to make sure your order is not rejected
BUY 1 CONTRACT AT EntryPrice LIMIT
ENDIF
in this case say you want to enter LIMIT at LOW[1] on a raising market, so you want to make sure that the entry price is at least within DISTANCE pips from the current price (CLOSE) at the very moment you place the order.
Obviously you will have to check with IG what is the correct value for DISTANCE (if any).
BCParticipant
Master
Thanks for your suggestion, Robertogozzi.
But actual syntax is as below:-
ONCE Distance = 10 * pipsize
InitialTargetPrice = DClose(1) + AverageTrueRange[20](Close)
If LongCondition then
FinalTargetPrice = Max(InitialTargetPrice, CurrentPrice + Distance)
Buy 1 Contract at FinalTargetPrice Stop
EndIf
So I wondering what command can replace the CurrentPrice?
CLOSE is the current price.
In strategies it’s the last price of the most recently closed candle since currently forming candles cannot be accessed, while in indicators and screeners it’s the last price of the candle being formed.
BCParticipant
Master
Yes, I should use Close to detect current price.
Million thanks Robertogozzi.🙏🙏