Trailing Stoploss from Breakeven

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #198063 quote
    borsnasan
    Participant
    New

    Hi,

    Could someone please help me with my request regarding trailing stoploss from breakeven?

    I want to exit half my position when I’ve met my first profit target. I then want to set a trailing stoploss for the rest of my position which begin at breakeven with 5 steps. Could anyone please explain how I can do that?

    IF c2  and Flag THEN
    EXITSHORT 1 CONTRACT AT MARKET
    Flag = 0
    SET STOP pTRAILING 
    ENDIF
    #198066 quote
    robertogozzi
    Moderator
    Master

    Try this one (I used a modified version ofNicolas’trailing stop snippet), not tested:

    IF Not OnMarket OR (ShortOnMarket AND LongOnMarket[1]) OR (ShortOnMarket[1] AND LongOnMarket) THEN
       StartTrail = 0
       StartPrice = 0
    ENDIF
    IF c2  and Flag THEN
       EXITSHORT 1 CONTRACT AT MARKET
       Flag       = 0
       //set the flags forthe start of the Trailing Stop
       StartTrail = 1
       StartPrice = tradeprice(1)
    ENDIF
    //
    IF StartTrail THEN
    //trailing stop function
    trailingstart = 0     //trailing will start @trailinstart points profit
    trailingstep  = 5     //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-StartPrice>=trailingstart*pipsize THEN
      newSL = StartPrice+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 StartPrice-close>=trailingstart*pipsize THEN
      newSL = StartPrice-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
    ENDIF
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Trailing Stoploss from Breakeven


General Trading: Market Analysis & Manual Trading

New Reply
Author
author-avatar
borsnasan @borsnasan Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by robertogozzi
3 years, 6 months ago.

Topic Details
Forum: General Trading: Market Analysis & Manual Trading
Language: English
Started: 07/29/2022
Status: Active
Attachments: No files
Logo Logo
Loading...