Trailing stop loss code

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

    Hello everybody!

    I am abit new to this software and i need helpwriting a trailing stop loss code.

    I want it to do as the standard in trailing stop loss in pro order.

    “Creates a trailing stop which takes as a reference the price level that created the highest profit since the position was opened” And i want it to be in $

    How do i right that in code?

    I have tried searching everywhere but never found a code that does exacly that.

    Thank you

    #114808 quote
    GraHalGraHal
    Participant
    Legend

    I have tried searching everywhere but never found a code that does exacly that.

    Any of attached Trailing Stop codes do the job?

    Below is a link to the sources (on this website) of the Trail codes.

    Snippet Link Library

    Noch.jpg Noch.jpg
    #114881 quote
    nonethelessnonetheless
    Participant
    Master

    This is the one I use, written by Nicolas. It assumes a fixed stop loss to start with, so you have to add SET STOP PLOSS x after the line of entry code.

    //trailing stop function
    trailingstart = 10 //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-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+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 tradeprice(1)-close>=trailingstart*pipsize THEN
    newSL = tradeprice(1)-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
    //************************************************************************

    You might also try this MFE code, also by Nicolas:

    //trailing stop
    trailingstop = 20
     
    //resetting variables when no trades are on market
    if not onmarket then
     MAXPRICE = 0
     MINPRICE = close
     priceexit = 0
    endif
     
    //case SHORT order
    if shortonmarket then
     MINPRICE = MIN(MINPRICE,close) //saving the MFE of the current trade
     if tradeprice(1)-MINPRICE>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then  
      priceexit = MINPRICE+trailingstop*pointsize //set the exit price at the MFE + trailing stop price level
     endif
    endif
     
    //case LONG order
    if longonmarket then
     MAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current trade
     if MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
      priceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price level
     endif
    endif
     
    //exit on trailing stop price levels
    if onmarket and priceexit>0 then
     EXITSHORT AT priceexit STOP
     SELL AT priceexit STOP
    endif
    
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Trailing stop loss code


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 2 replies,
has 3 voices, and was last updated by nonethelessnonetheless
6 years, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 12/13/2019
Status: Active
Attachments: 1 files
ProRealCode ProRealCode
Loading...