Bar by Bar Trailing Stop

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #216413 quote
    Ringo
    Participant
    Junior

    Hello,

    I am trading only stocks and I try to add a bar by bar trailing stop to my position.

    At the moment the stop managment looks like this: Entry / SL 20cents / Move SL to Break Even when position is up 25 cents / Take profit at +32 cents

    I would like to replace the Take profit with a BbB trailing stop, means when the current bar closes on a new 5 period high, the stop goes under this closed bar.

    Code:

    // Stops und Targets: Legen Sie hier schützende Stops und Profit Targets fest
    SET STOP LOSS 0.20
    SET TARGET PROFIT 0.32

    IF LONGONMARKET THEN
    IF HIGH-Tradeprice >= 0.25 THEN
    SET STOP BREAKEVEN
    ENDIF
    ENDIF

    Thanks for help

    Regards André

    #216415 quote
    JS
    Participant
    Master

    Hi, Try this trailing stop…

    StopLoss          = 23      //StopLoss size
    TrailingStopStart = 0      //Trailing Stop start at X points in profit
     
    buystoploss = close-StopLoss*pointsize
    sellstoploss = close+StopLoss*pointsize
     
    if not onmarket then
     ibuystoploss=buystoploss
     isellstoploss=sellstoploss
    endif
    if longonmarket then
     //checking and adjusting stoploss
     if close-tradeprice>=trailingstopstart*pointsize then
      ibuystoploss = max(ibuystoploss,high-(stoploss+trailingstopstart)*pointsize)
     endif
     //set the stoploss level
      sell at ibuystoploss stop
     endif
    if shortonmarket then
     //checking and adjusting stoploss
     if tradeprice-close>=trailingstopstart*pointsize then
      isellstoploss = min(isellstoploss,low+(stoploss+trailingstopstart)*pointsize)
     endif
     //set the stoploss level
     exitshort at isellstoploss stop
    endif
    Ringo thanked this post
    #216418 quote
    Ringo
    Participant
    Junior

    Thank you for help, but I guess that is not what I mean. I will post an example chart

    AMD_26_05_2023_2.png AMD_26_05_2023_2.png
    #216424 quote
    Ringo
    Participant
    Junior

    without the Breakeven Term it must be like this:

    BbB= High[0]-Low[0] + 0.01

    // Stops und Targets: Legen Sie hier schützende Stops und Profit Targets fest
    SET STOP LOSS 0.20

    IF LONGONMARKET AND Close[0] > Highest[5] (High[1]) THEN
    Set Stop Price High[0] – BbB
    ENDIF

    IF SHORTONMARKET AND Close[0] < Lowest[5] (Low[1]) THEN
    Set Stop Price Low[0] + BbB
    ENDIF

    #216429 quote
    robertogozzi
    Moderator
    Legend

    Try appending these lines to your code:

    IF Not OnMarket THEN
       StopPrice = 0
    ELSE
       IF PositionPerf > 0 THEN
          IF LongOnMarket THEN
             IF close > high[1] THEN
                StopPrice = max(StopPrice,low)
                SET STOP PRICE StopPrice
             ENDIF
          ENDIF
       ENDIF
    ENDIF
    graphonprice TradePrice
    graphonprice StopPrice  coloured("Red")
    Ringo thanked this post
    #216433 quote
    JS
    Participant
    Master
    Hi

    I’ve modified the original trailing stop to your terms…    

    StopLoss= xSL
    
    if not onmarket then
    buystoploss = close-StopLoss
    sellstoploss = close+StopLoss
    ibuystoploss=buystoploss
    isellstoploss=sellstoploss
    endif
    
    if longonmarket then
    //checking and adjusting stoploss
    if High>Highest[5](High[1]) and High>High[1] then
    ibuystoploss = (Highest[5](High[1])-StopLoss)
    endif
    
    endif
    if shortonmarket then
    //checking and adjusting stoploss
    if Low<Lowest[5](Low[1]) and Low < Low[1] then
    isellstoploss = (Lowest[5](Low[1])+StopLoss)
    endif
    endif
    
    //set the stoploss level
    sell at ibuystoploss stop
    exitshort at isellstoploss stop
    
    GraphOnPrice Highest[5](High[1]) coloured("red")
    GraphOnPrice Lowest[5](Low[1]) coloured("Blue")

    Ringo thanked this post
    #216476 quote
    Ringo
    Participant
    Junior
    Thanks all for your help, I now coded a combination of both terms and see how it will works
    #223209 quote
    Ringo
    Participant
    Junior
    Hello, following question regarding my code for a BE and then trailing stop: The code works fine so far. But I have the problem, that , if condition 2 is fullfilled and it comes to a trailing stop, the first BE +0,01 Stop also remains in the system. How can I delete the first BE +0,01 stop, after condition 2 is fullfilled? Thanks for help //Manage Long Position / BE Stop / Bar by Bar Trail Outside Candle IF LONGONMARKET THEN //Breakeven SL at +0,20 IF StopPrice = 0 AND High – TRADEPRICE >= 0.20 THEN StopPrice = TRADEPRICE +0.01 ENDIF //Bar by Bar Trail Outside Candle IF StopPrice > 0 AND Close > Highest[5] (High[1]) THEN StopPrice = Low -0.01 ENDIF ENDIF
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

Bar by Bar Trailing Stop


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Ringo @ringo Participant
Summary

This topic contains 7 replies,
has 3 voices, and was last updated by Ringo
2 years, 10 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/18/2023
Status: Active
Attachments: 1 files
Logo Logo
Loading...