This is code of indicator that take:
- StopLossType => Select type of stop loss trailing (integer)
- LongPosition => True if is on long position (boolean)
- ShortPosition => True if is on short position (boolean)
- EnterPrice => Last price of position (decimal)
- EnterPriceClose => Last close price of position (decimal)
- TrailingStopLoss => Trailing stop loss pips (integer)
- StartBreakeven => Breakeven pips (integer)
- PointsToKeep => Points for breakeven (integer)
SupertrendMulti = 18
SupertrendPeriod = 150
BreakevenLevel = 0
IF StopLossType = 2 THEN
SupertrendIndicator = Supertrend[SupertrendMulti,SupertrendPeriod]
ENDIF
IF NOT LongPosition AND NOT ShortPosition THEN
CurrentMaxPrice = 0
CurrentMinPrice = CLOSE * 1000
ENDIF
// --- BUY SIDE ---
IF LongPosition AND StopLossType = 1 AND NOT StopLossType = 0 THEN
CurrentMaxPrice = Max(CurrentMaxPrice,CLOSE)
IF CurrentMaxPrice - EnterPrice >= TrailingStopLoss * PointSize THEN
BreakevenLevel = CurrentMaxPrice - TrailingStopLoss * PointSize
ENDIF
ENDIF
IF LongPosition AND StopLossType = 2 AND NOT StopLossType = 0 AND EnterPriceClose >= StartBreakeven * PointSize THEN
IF BreakevenLevel = 0 THEN
BreakevenLevel = EnterPrice + PointsToKeep * PointSize
ELSE
IF CLOSE > SupertrendIndicator THEN
BreakevenLevel = Max(BreakevenLevel,SupertrendIndicator)
ENDIF
ENDIF
ENDIF
// --- SELL SIDE ---
IF ShortPosition AND StopLossType = 1 AND NOT StopLossType = 0 THEN
CurrentMinPrice = Min(CurrentMinPrice,CLOSE)
IF EnterPrice -CurrentMinPrice>=TrailingStopLoss*PointSize THEN
BreakevenLevel = CurrentMinPrice+TrailingStopLoss*PointSize
ENDIF
ENDIF
IF ShortPosition AND StopLossType = 2 AND NOT StopLossType = 0 AND EnterPriceClose > StartBreakeven*PointSize THEN
IF BreakevenLevel = 0 THEN
BreakevenLevel = EnterPrice - PointsToKeep*PointSize
ELSE
IF CLOSE < SupertrendIndicator THEN
BreakevenLevel = MIN(BreakevenLevel,SupertrendIndicator)
ENDIF
ENDIF
ENDIF
Return BreakevenLevel AS "Breakeven Level"
I can add to the charts but if I add to my strategy it is so slow.
My strategy :
DEFPARAM Preloadbars = 10000
IF NOT LongOnMarket THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
SET STOP PLOSS 10
LongPosition = 0
ShortPosition = 0
TrailingStopLoss = 15
StartBreakeven = 15
PointsToKeep = 5
EnterPrice = 0
EnterPriceClose = 0
StopLossType = 2
IF LONGONMARKET THEN
LongPosition = 1
ENDIF
IF SHORTONMARKET THEN
ShortPosition = 1
ENDIF
IF ONMARKET THEN
EnterPrice = TradePrice(1)
EnterPriceClose = TradePrice(1)-Close
BreakevenLevel = CALL "StopLossController"[StopLossType, LongPosition, ShortPosition, EnterPrice, EnterPriceClose, TrailingStopLoss, StartBreakeven, PointsToKeep]
ENDIF
IF LONGONMARKET AND BreakevenLevel > 0 THEN
SELL AT BreakevenLevel STOP
ENDIF
IF SHORTONMARKET AND BreakevenLevel > 0 THEN
EXITSHORT AT BreakevenLevel STOP
ENDIF
Why?? any suggestion?
Thanks
If you feel that the backtest is slow, it might be because of backtests servers overloading. Try to backtest at a different time of the day, are you doing optimization?
Thanks Nicolas, In this step I do not do optimization, Like more then one week I tray this indicator and always is slow, but if I put my code from indicator to my strategy is working well, why?
Actually it’s very slow. I tried embedding the indicator in the strategy and it’s fast (Dax, 4h, 200K).
I am attaching the modified version.
I was posting while you were replying. So you already experienced that. Sorry.
I can’t really know why it is slow in backtest, try to reduce the quantity of IF/ENDIF? CALL the indicator only when it is needed in the strategy?
It’s incredible, if I put my code from indicator to my strategy is working well, but if commit all code in indicator is very slow yet.
SupertrendMulti = 18
SupertrendPeriod = 150
BreakevenLevel = 0
//IF StopLossType = 2 THEN
//SupertrendIndicator = Supertrend[SupertrendMulti,SupertrendPeriod]
//ENDIF
//
//IF NOT LongPosition AND NOT ShortPosition THEN
//CurrentMaxPrice = 0
//CurrentMinPrice = CLOSE * 1000
//ENDIF
//
//// --- BUY SIDE ---
//IF LongPosition AND StopLossType = 1 AND NOT StopLossType = 0 THEN
//CurrentMaxPrice = Max(CurrentMaxPrice,CLOSE)
//IF CurrentMaxPrice - EnterPrice >= TrailingStopLoss * PointSize THEN
//BreakevenLevel = CurrentMaxPrice - TrailingStopLoss * PointSize
//ENDIF
//ENDIF
//
//IF LongPosition AND StopLossType = 2 AND NOT StopLossType = 0 AND EnterPriceClose >= StartBreakeven * PointSize THEN
//IF BreakevenLevel = 0 THEN
//BreakevenLevel = EnterPrice + PointsToKeep * PointSize
//ELSE
//IF CLOSE > SupertrendIndicator THEN
//BreakevenLevel = Max(BreakevenLevel,SupertrendIndicator)
//ENDIF
//ENDIF
//ENDIF
//
//
//// --- SELL SIDE ---
//IF ShortPosition AND StopLossType = 1 AND NOT StopLossType = 0 THEN
//CurrentMinPrice = Min(CurrentMinPrice,CLOSE)
//IF EnterPrice -CurrentMinPrice>=TrailingStopLoss*PointSize THEN
//BreakevenLevel = CurrentMinPrice+TrailingStopLoss*PointSize
//ENDIF
//ENDIF
//
//IF ShortPosition AND StopLossType = 2 AND NOT StopLossType = 0 AND EnterPriceClose > StartBreakeven*PointSize THEN
//IF BreakevenLevel = 0 THEN
//BreakevenLevel = EnterPrice - PointsToKeep*PointSize
//ELSE
//IF CLOSE < SupertrendIndicator THEN
//BreakevenLevel = MIN(BreakevenLevel,SupertrendIndicator)
//ENDIF
//ENDIF
//ENDIF
Return BreakevenLevel AS "Breakeven Level"
If you call it with constants, instead of variables, it works fine:
BreakevenLevel = CALL "StopLossController"[0,0,15,15,5,0,0,2]
Grazie Roberto, but it is not useful, in a lot of situation I need pass to indicator dynamic variable.
problem is decimal inputs,
- EnterPrice => Last price of position (decimal)
- EnterPriceClose => Last close price of position (decimal)
BreakevenLevel = CALL "StopLossController"[StopLossType,LongPosition,ShortPosition,15,15,TrailingStopLoss,StartBreakeven,PointsToKeep]
Yes, I know variables are necessary.
It’s just to know what happens in multiple different situations, in case you want to report this issue to PRT.
DANYParticipant
Senior
Dears,
please, some news about this topic? It’s real important to solve this issue, otherwise it’s not possible to public a lot of strategy on marketplace.
Thanks a lot.
The issue with using CALL was sorted out a few years ago, when CALLed indicators were automatically appended to the code, so now the only overhead is the CALL itself, not loading the indicator.
I sometimes experience slow backtesting, as well. It usually lasts one day or a few days, then the regular speed is restored.
It may be due to overloaded servers because of too many strategies running at the same time or to demo account while in maintenance mode.
Try enquiring ProRealTime.