Trailing stop loss that advances when a new swing high or low are made

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #104533 quote
    Thally
    Participant
    Average

    Hi all,

    another post from me. Thanks for all the help thus far, really appreciate it while I’m learning. Hope to be able to give back in due course.

    I am trying to code a trailing stop loss that places stops above recent swing highs (shoreside) and swing lows (longside). I have utilised some code I found that calculates recent fractals. It works ok but there is on key missing ingredient that I am not sure how to code.

    If we are following the trend then we should only move the stop up/down when a previous swing point is closed through, at which point the trailing stop would move to the closes swing high or low.

    Right now using the below code the trailing stop will simply move to the next identified fractal regardless of whether price has moved in our direction and closed above (uptrend) or below (downtrend) the current swing high/low. I only want the trailing stop to advance when price closes through the current swing high/low in our direction.

    Does that make sense? This is what I typically do manually when trend following intraday and works a treat. Would be awesome to automate it. Here’s the trailing stop code I currently have which is adapted from a few other sources.

    Any help gratefully received
    Roj

    // calculate swing points
    PivotBAR     = 2
    LookBack     = 4
    
    // Swing low
    BarLookBack  = PivotBAR + 1
    IF low[PivotBAR] < lowest[LookBack](low)[BarLookBack] THEN
    IF low[PivotBAR] = lowest[BarLookBack](low) THEN
    SupportPrice  = low[PivotBAR]
    ENDIF
    ENDIF
    
    // Swing high
    IF high[PivotBAR] > highest[LookBack](high)[BarLookBack] THEN
    IF high[PivotBAR]  = highest[BarLookBack](high) THEN
    ResistancePrice = high[PivotBAR]//high[BarIndex - MyResistance]
    ENDIF
    ENDIF
    
    // Parameters for determining when BE stop kicks in
    TrailingStart = 1*AverageTrueRange[10] * pipsize //trailing will start @trailinstart points profit
    TrailingStep = 0 * pipsize
    
    // Longside
    IF LONGONMARKET THEN
    // move to breakeven
    IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingStart THEN
    newSL = TRADEPRICE(1) + TrailingStep
    ENDIF
    // next moves
    IF newSL > 0 AND close > ResistancePrice AND SupportPrice > newSL  THEN
    newSL = SupportPrice[1]
    ENDIF
    ENDIF
     
    //Shortside
    IF SHORTONMARKET THEN
    // move to breakeven
    IF newSL = 0 AND TRADEPRICE(1) - CLOSE >= TrailingStart THEN
    newSL = TRADEPRICE(1) - TrailingStep
    ENDIF
    // next moves
    IF newSL > 0 AND close < SupportPrice AND ResistancePrice < newSL THEN
    newSL = ResistancePrice[1]
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL > 0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    #154307 quote
    ligand
    Participant
    Average

    Hi,

    have you been able to solve your problem? Im looking for exactly the same solution, but I am far less gifted regarding programming than you. Would be very thankful if you could show the complete code, if you have one.

    Greetings from the alps!

    #154312 quote
    robertogozzi
    Moderator
    Master

    Line 32 should read:

    newSL = max(newSL,SupportPrice[1])

    and line 44 should read:

    newSL = min(newSL,ResistancePrice[1])
    ligand thanked this post
    #154407 quote
    ligand
    Participant
    Average

    Thank you very much!!!

    #154445 quote
    ligand
    Participant
    Average

    Can I please ask another question which I can’t find the answer to, but I think is simple.

    I have programmed a screener and manually select the instruments it suggests. Then I start the trade with a stop below the last swing extreme.

    Afterwards the program should kick in to draw the stop automatically.

    But an automatic trading system says it needs a buy and sell order. How can I make the algorithm work on an already existing trade?

    I think if I try to make an indicator and link the stop-sell (or -buy) order to it, then this will be only pssible intra-day. But my trades normally last longer.

    I’d be very thankful for a hint, how to solve this problem.

    #154449 quote
    ligand
    Participant
    Average

    Perhaps it would work if the last lines are changed from:

    IF newSL > 0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF

     

    to

    IF newSL > 0 THEN
    SELLSHORT AT newSL STOP
    BUY AT newSL STOP
    ENDIF

     

    ?

    MODERATORS EDIT: Your post has been edited. please use the ‘Insert PRT Code’ button when putting code in your posts.

    #154454 quote
    robertogozzi
    Moderator
    Master

    Your solution would do a Stop & Reverse OR accumulate positions.

    Manual and Automatic trading cannot be combined.

    You can only alter trades opened automatically, though this will shut your strategy.

    ligand thanked this post
    #154474 quote
    ligand
    Participant
    Average

    Thanks again Roberto,

    and it wouldn’t be possible to construct an indicator based on the swing-extreme trailing stop (which would exceed my abilities) and then link this indicator to an alarm and this alarm to a sell trigger?

    #154482 quote
    robertogozzi
    Moderator
    Master

    Indicators can only return numeric values.

    Alerts can only be set manually according to what indicators return.

    ligand thanked this post
    #154535 quote
    ligand
    Participant
    Average

    Thanks again, Roberto. But I don’t really understand your sentence: Indicators can only return numeric values.

    Is it not the case, that this indicator would return numeric, albeit distinct stop-loss levels?

    I’m really thankful for your time and knowledge but I would really love to solve this problem As an amateur I would think that there has to be a way to automate this, since it doesn’t seem to be to complex. Do you please have any idea if I could ask somewhere or if there any other way?

    Sorry for bothering you, best regards, Michael

    #154550 quote
    robertogozzi
    Moderator
    Master

    I mean it cannot set nor trigger alerts.

    ligand thanked this post
    #154553 quote
    GraHal
    Participant
    Master

    Try what you want to do using the Alert Function with it’s integral Trade Open function.

    Try above using a stnadard Indcator … e.g Bollinger using the Close Crosses Over BollDown on the Alert Function.

    If above does what you want then you are half way there?

    ligand thanked this post
    #154622 quote
    ligand
    Participant
    Average

    Hello Grahal, thank you! I think your suggestion may be the same I mentioned above (to construct an indicator based on the swing-extreme trailing stop (which would exceed my abilities) and then link this indicator to an alarm and this alarm to a sell trigger)?

    If this works, the code has to be changed from trading advice to definition of a indicator and I must say that above my abilities. Could someone of you perhaps help – if this way makes sense?

    Another different solution might be to do it completely via an automated trading approach using a simple trade entry advice after the screener suggested a good candidate at bar “[1]”, i.g. for a long trade:

    defparam cumulateorder=false
    
    once nbbarlimit=1
    
    if close>close[1] and close< close[1]+0.3*atr[10] then 
    buy x shares at market
    endif
    #154624 quote
    Vonasi
    Moderator
    Master

    ligand  – Please always use the ‘Insert PRT Code’ button when putting code in your posts. I have edited your last post.

    ligand thanked this post
    #154923 quote
    ligand
    Participant
    Average
    1. The code line starting with defparam gives an error , but why?

    2. Roberto, I thought you gave a hint to another error in the code from Thally besides “max(newSL,SupportPrice[1])”, but I can’t find it anymore. Did you erase this post?

    Thanks, Michael

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

Trailing stop loss that advances when a new swing high or low are made


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Thally @rodger_dodger Participant
Summary

This topic contains 19 replies,
has 5 voices, and was last updated by ligand
5 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/10/2019
Status: Active
Attachments: No files
Logo Logo
Loading...