SL not working, what am i missing?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #229408 quote
    Cellstep
    Participant
    New

    Hi, I have created this code, however what i see is that the stop loss moves 30 points into profit, but then stays there, it does not move any more until the price hits TP or SL. I want it to continue to move with 30 points intervalls, what am i doing wrong here?

    // Step trailing stop parameters
    trailingstart = 30 // Trailing will start when in profit by 30 points
    trailingstep = 30// Trailing step to move the stop loss
    initialStopLoss = 125 // Initial stop loss in points
    takeProfit = 250 // Take profit in points
    
    // Variable to store the new stop loss level for long and short positions
    newSLlong = 0
    newSLshort = 0
    
    // Trading strategy logic based on the indicator signals
    IF LongSignal AND Not OnMarket AND not daysForbiddenEntry THEN
    BUY PositionSize CONTRACT AT MARKET
    SET STOP pLOSS initialStopLoss
    SET TARGET pPROFIT takeProfit
    newSLlong = 0 // Reset the trailing stop level for long positions
    ENDIF
    
    IF ShortSignal AND Not OnMarket AND not daysForbiddenEntry THEN
    SELLSHORT PositionSize CONTRACT AT MARKET
    SET STOP pLOSS initialStopLoss
    SET TARGET pPROFIT takeProfit
    newSLshort = 0 // Reset the trailing stop level for short positions
    ENDIF
    
    // Manage step trailing stop for long positions
    IF LONGONMARKET THEN
    IF newSLlong = 0 AND close - tradeprice(1) >= trailingstart * pipsize THEN
    newSLlong = tradeprice(1) + trailingstep * pipsize
    ELSIF newSLlong > 0 AND close - newSLlong >= trailingstep * pipsize THEN
    newSLlong = newSLlong + trailingstep * pipsize
    ENDIF
    IF newSLlong > 0 THEN
    SELL AT newSLlong STOP
    ENDIF
    ENDIF
    
    // Manage step trailing stop for short positions
    IF SHORTONMARKET THEN
    IF newSLshort = 0 AND tradeprice(1) - close >= trailingstart * pipsize THEN
    newSLshort = tradeprice(1) - trailingstep * pipsize
    ELSIF newSLshort > 0 AND newSLshort - close >= trailingstep * pipsize THEN
    newSLshort = newSLshort - trailingstep * pipsize
    ENDIF
    IF newSLshort > 0 THEN
    EXITSHORT AT newSLshort STOP
    ENDIF
    ENDIF
    
    #229416 quote
    robertogozzi
    Moderator
    Master

    Replace lines 8-9 with these ones:

    IF Not OnMarket THEN
       newSLlong = 0
       newSLshort = 0
    ENDIF

    It’s because you always reset those variables to zero, thus they always restart from scratch every bar, i.e. from 30.

    #229421 quote
    Cellstep
    Participant
    New

    Thank you so much for the fast reply! I will implement.

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

SL not working, what am i missing?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Cellstep @cellstep Participant
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by Cellstep
1 year, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/07/2024
Status: Active
Attachments: No files
Logo Logo
Loading...