Using MA to trigger a trailing stop

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #150043 quote
    nonetheless
    Participant
    Master

    Is there any reason the following shouldn’t work with a moving average value (or any other defined indicator) as the trigger for a trailing stop?

    Seems to work as expected in initial tests (where HULLa is defined further up the page)

    once trailingstop = 1
    if trailingstop then
    
    trailingpercentlong  = tsl // %
    trailingpercentshort = tss // %
    stepPercentlong = stl
    stepPercentshort = sts
    if onmarket then
    trailingstartlong = tradeprice(1)*(trailingpercentlong/100) //trailing will start @trailingstart points profit
    trailingstartshort = tradeprice(1)*(trailingpercentshort/100) //trailing will start @trailingstart points profit
    trailingsteplong = tradeprice(1)*(stepPercentlong/100) //% step to move the stoploss
    trailingstepshort = tradeprice(1)*(stepPercentshort/100) //% step to move the stoploss
    endif
    
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
     
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND HULLa-tradeprice(1)>=trailingstartlong THEN
    newSL = tradeprice(1)+trailingsteplong
    ENDIF
    //next moves
    IF newSL>0 AND HULLa-newSL>trailingsteplong THEN
    newSL = newSL+trailingsteplong
    ENDIF
    ENDIF
     
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartshort THEN
    newSL = tradeprice(1)-trailingstepshort
    ENDIF
    //next moves
    IF newSL>0 AND newSL-HULLa>trailingstepshort THEN
    newSL = newSL-trailingstepshort
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    endif
    Monochrome and Zigo thanked this post
    #150045 quote
    robertogozzi
    Moderator
    Master
    tradeprice(1)-HULLa

    Because at line 23 and the others you make a difference with inverted operators, write it as above.

    #150046 quote
    nonetheless
    Participant
    Master

    Do you mean that line 23 should be

    IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartlong THEN

    isn’t that effectively the same as line 35 for short positions?

    Kovit thanked this post
    #150050 quote
    robertogozzi
    Moderator
    Master

    Yes, in line 23 and the other ones those two operators need to be swapped.

    #150051 quote
    nonetheless
    Participant
    Master

    Like so?

    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartlong THEN
    newSL = tradeprice(1)+trailingsteplong
    ENDIF
    //next moves
    IF newSL>0 AND newSL-HULLa>trailingsteplong THEN
    newSL = newSL+trailingsteplong
    ENDIF
    ENDIF
     
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND HULLa-tradeprice(1)>=trailingstartshort THEN
    newSL = tradeprice(1)-trailingstepshort
    ENDIF
    //next moves
    IF newSL>0 AND HULLa-newSL>trailingstepshort THEN
    newSL = newSL-trailingstepshort
    ENDIF
    ENDIF

    or

    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartlong THEN
    newSL = tradeprice(1)+trailingsteplong
    ENDIF
    //next moves
    IF newSL>0 AND HULLa-newSL>trailingsteplong THEN
    newSL = newSL+trailingsteplong
    ENDIF
    ENDIF
     
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND HULLa-tradeprice(1)>=trailingstartshort THEN
    newSL = tradeprice(1)-trailingstepshort
    ENDIF
    //next moves
    IF newSL>0 AND newSL-HULLa>trailingstepshort THEN
    newSL = newSL-trailingstepshort
    ENDIF
    ENDIF
    #150055 quote
    robertogozzi
    Moderator
    Master

    Yes, but I did not spot it before, you have to also replace TRADEPRICE(1)  with CLOSE.

    #150056 quote
    robertogozzi
    Moderator
    Master

    This is the original Nicolas’code and the modified version for you:

    Close-tradeprice(1)   //original
    Close-Hulla           //modified for Hull

    the opposite for Short trades.

    Zigo thanked this post
    #150057 quote
    nonetheless
    Participant
    Master

    Roberto, the original TS that I had been using was like this:

    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstartlong THEN
    newSL = tradeprice(1)+trailingsteplong
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>trailingsteplong THEN
    newSL = newSL+trailingsteplong
    ENDIF
    ENDIF
     
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-close>=trailingstartshort THEN
    newSL = tradeprice(1)-trailingstepshort
    ENDIF
    //next moves
    IF newSL>0 AND newSL-close>trailingstepshort THEN
    newSL = newSL-trailingstepshort
    ENDIF
    ENDIF
     
    //stop order to exit the positions
    IF newSL>0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    endif

    Then without changing anything else, I exchanged ‘close’ for ‘typicalprice’. This also works fine, but then I thought, why have the TS triggered by the performance of just one candle? why not use an MA value?

    It is precisely the use of ‘close’ that I am trying to get away from.

    I don’t understand why ‘close’ shouldn’t simply be replaced by ‘HULLa’ as in my first post.

    #150078 quote
    robertogozzi
    Moderator
    Master

    Tradeprice(1) needs to be replaced by HULLa.

    If profitable CLOSE needs to be above the MA for Long trades and below for Short trades.

    nonetheless thanked this post
    #150079 quote
    nonetheless
    Participant
    Master

    Ok, I’ll try that, thanks!

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

Using MA to trigger a trailing stop


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

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

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