Coding help needed – flexible stop loss

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #245405 quote
    OggyFluff77OggyFluff77
    Participant
    New

    Good morning,

    i have a piece of code for a strategy which has a moving profit target but a fixed stop loss.

    i would like some help  please that would keep the original target in place but in addition, add a parameter that when a different $profit is hit, the stop loss is moved to ensure i lock in a profit.

    example: target profit is 20*AverageTrueRange[15](close)

    however, if the trade is in profit say £150, then move the stop loss to £100 in profit and close trade on that pull back.

    thanks in advance, hope the above makes sense

    #245406 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    There you go:

    ONCE TP           = 150      //150 currency unit (€, $, etc...)
    ONCE SL           = 450      //450 currency unit (€, $, etc...)
    ONCE SLflag       = 1
    Sma100            = average[100,0](close)
    myLongConditions  = close CROSSES OVER  Sma100
    myShortConditions = close CROSSES UNDER Sma100
    IF Not OnMarket THEN
       IF myLongConditions THEN
          BUY 1 CONTRACT AT MARKET
       ELSIF myShortConditions THEN
          SELLSHORT 1 CONTRACT AT MARKET
       ENDIF
       SET TARGET PROFIT 50*AverageTrueRange[15](close)
       SET STOP   $LOSS  SL
       SLflag = 1
       tempProfit = 0
    ENDIF
    SET TARGET PROFIT 50*AverageTrueRange[15](close)
    IF OnMarket THEN
       tempProfit = TradePrice * PositionPerf * PipValue * abs(CountOfPosition)
       IF (tempProfit >= TP) AND SLflag THEN
          SLflag = 0
          SET STOP $LOSS 100
       ENDIF
    ENDIF
    //graph tempProfit
    Iván González and Schizophunk thanked this post
    #245481 quote
    OggyFluff77OggyFluff77
    Participant
    New

    many thanks for coming back to me, as always, much appreciated.

    however, the code moves the stop loss to a 100 loss, but still a loss – i would like the stop loss moved to a target that is positive so the trade at least makes some money – so in essence once a target is hit (the code you kindly provided), i then have 2 take profit targets, the original one as set out in initial trade and one which is lower than the current profit – ie, replacement of the original stop loss.

    in my earlier example, if my running profit hits £150 then my old stop loss which is naturally negative is replaced by a number which is positive so i lock in a definite gain – like £100 in the event of a pull back. and i also have my original target still in place so i win either way.

    i dont want a trailing stop loss per se as i am happy with 2 distinct take profit targets

    hope thats clear

    thanks in advance for your help

    #245499 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    My fault, here’s the correct one:

    ONCE TP           = 150      //150 currency unit (€, $, etc...)
    ONCE SL           = 450      //450 currency unit (€, $, etc...)
    ONCE SLflag       = 1
    Sma100            = average[100,0](close)
    myLongConditions  = close CROSSES OVER  Sma100
    myShortConditions = close CROSSES UNDER Sma100
    IF Not OnMarket THEN
    IF myLongConditions THEN
    BUY 1 CONTRACT AT MARKET
    ELSIF myShortConditions THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    SET TARGET PROFIT 50*AverageTrueRange[15](close)
    SET STOP   $LOSS  SL
    SLflag = 1
    tempProfit = 0
    ENDIF
    SET TARGET PROFIT 50*AverageTrueRange[15](close)
    IF OnMarket THEN
    tempProfit = TradePrice * PositionPerf * PipValue * abs(CountOfPosition)
    IF (tempProfit >= TP) AND SLflag THEN
    SLflag = 0
    IF LongOnMarket THEN
    NewSL  = TradePrice + (100 / abs(CountOfPosition) / PipValue)
    ELSE
    NewSL  = TradePrice - (100 / abs(CountOfPosition) / PipValue)
    ENDIF
    SET STOP PRICE NewSL
    ENDIF
    ENDIF
    //graph tempProfit
    //graph NewSL
    //graph TradePrice
    OggyFluff77 thanked this post
    #245556 quote
    OggyFluff77OggyFluff77
    Participant
    New

    many thanks Roberto that is amazing – really appreciate your help – that works perfectly.

    However, the structure of the second take profit/movable stop loss has given me an idea.

    can a second one be added so it looks something like:

    if temp profit goes to 100, move stop loss/new take profit to +50

    if temp profit goes to 150, move stop loss/new take profit to +100

    make sense? the numbers dont really matter at this stage i am just trying to limit losses and maximise the gain but allow flexibility for some volatility

    thank you so much in advance

    #245562 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    This version support 2 profitable SLs, the first one is triggered at a $100 gain and sets a SL to +$50, the second one is triggered at a $150 gain and sets a SL to +$100.

    When a major Gain  occurs and the first profitable trigger is the SECOND trigger, obviously there will not be a FIRST (worse) SL1.

    ONCE Trigger1     = 100      //100 $ gain is second trigger for SL 2
    ONCE TP1          = 50       //50  currency unit (€, $, etc...)
    ONCE Trigger2     = 150      //150 $ gain is second trigger for SL 2
    ONCE TP2          = 100      //100 currency unit (€, $, etc...)
    ONCE SL           = 450      //450 currency unit (€, $, etc...)
    ONCE SLflag1      = 1
    ONCE SLflag2      = 1
    Sma100            = average[100,0](close)
    myLongConditions  = close CROSSES OVER  Sma100
    myShortConditions = close CROSSES UNDER Sma100
    IF Not OnMarket THEN
    IF myLongConditions THEN
    BUY 1 CONTRACT AT MARKET
    ELSIF myShortConditions THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    SET TARGET PROFIT 50*AverageTrueRange[15](close)
    SET STOP   $LOSS  SL
    SLflag1 = 1
    SLflag2 = 1
    tempProfit = 0
    ENDIF
    SET TARGET PROFIT 50*AverageTrueRange[15](close)
    IF OnMarket THEN
    tempProfit = TradePrice * PositionPerf * PipValue * abs(CountOfPosition)
    IF (tempProfit >= Trigger2) AND SLflag2 THEN
    SLflag1 = 0
    SLflag2 = 0
    IF LongOnMarket THEN
    NewSL  = TradePrice + (TP2 / abs(CountOfPosition) / PipValue)
    ELSE
    NewSL  = TradePrice - (TP2 / abs(CountOfPosition) / PipValue)
    ENDIF
    SET STOP PRICE NewSL
    ENDIF
    IF (tempProfit >= Trigger1) AND SLflag1 THEN
    SLflag1 = 0
    IF LongOnMarket THEN
    NewSL  = TradePrice + (TP1 / abs(CountOfPosition) / PipValue)
    ELSE
    NewSL  = TradePrice - (TP1 / abs(CountOfPosition) / PipValue)
    ENDIF
    SET STOP PRICE NewSL
    ENDIF
    ENDIF
    //graph tempProfit
    //graph NewSL
    //graph TradePrice
    //graph SLflag1
    //graph SLflag2 coloured("Red")
    OggyFluff77 and NeoTrader thanked this post
    #245596 quote
    OggyFluff77OggyFluff77
    Participant
    New

    thank you once again Roberto for the code – works perfectly well

    much appreciated as always for your excellent help

    robertogozzi thanked this post
    #245612 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    Sorry for the incorrect comment in line 1, I should have written SL 1.

Viewing 8 posts - 1 through 8 (of 8 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

Coding help needed – flexible stop loss


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 7 replies,
has 2 voices, and was last updated by robertogozzirobertogozzi
1 year, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/29/2025
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...