Why is my indicator so slow when I use it in a strategy?

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #170406 quote
    Kurosh
    Participant
    Junior

    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

    StopLossController.itf Test-StopLossController.itf
    #170429 quote
    Nicolas
    Keymaster
    Master

    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?

    #170433 quote
    Kurosh
    Participant
    Junior

    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?

    #170434 quote
    robertogozzi
    Moderator
    Master

    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.

    Test-StopLossController-MODIF.itf
    #170436 quote
    robertogozzi
    Moderator
    Master

    I was posting while you were replying. So you already experienced that. Sorry.

    #170457 quote
    Nicolas
    Keymaster
    Master

    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?

    #170471 quote
    Kurosh
    Participant
    Junior

    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"
    
    #170488 quote
    robertogozzi
    Moderator
    Master

    If you call it with constants, instead of variables, it works fine:

    BreakevenLevel = CALL "StopLossController"[0,0,15,15,5,0,0,2]
    #170496 quote
    Kurosh
    Participant
    Junior

    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]
    #170504 quote
    robertogozzi
    Moderator
    Master

    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.

    Kurosh thanked this post
    #237302 quote
    DANY
    Participant
    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.

    #237303 quote
    robertogozzi
    Moderator
    Master

    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.

    DANY thanked this post
Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

Why is my indicator so slow when I use it in a strategy?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Kurosh @bazrafkan Participant
Summary

This topic contains 11 replies,
has 4 voices, and was last updated by robertogozzi
1 year, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 05/25/2021
Status: Active
Attachments: 3 files
Logo Logo
Loading...