Volatility Stop Loss /need code from Metasa to Amibroker/

Discussion in 'AmiBroker' started by szachool, Mar 27, 2010.

  1. szachool

    szachool New Member

    Joined:
    Aug 20, 2009
    Messages:
    10
    Likes Received:
    5
    Trophy Points:
    3
    Hi

    There is quite good Volatility Stop Loss based on Average True Range (ATR). It depends on ATR, some kind of multipiler.
    It is written in the code for Metastock, and I am looking someone, who would like to translate 3 short codes below. I thinkt that it may be very usefull for us.
    [​IMG]
    Source page

    This is the basic formula switching on stop breaks:
    Stop Long

    Stop Short
    And this Volatility Stop can also be usefull for programmers.
    Source page


    Thanks, for any help.
     
  2. tools&tradingideas

    tools&tradingideas Active Member

    Joined:
    Sep 13, 2009
    Messages:
    81
    Likes Received:
    183
    Trophy Points:
    33
    hi, this is what I get:

    // from Metastock formula, link: http://stocata.org/metastock/stop_trail_atr.html
    per = Param("per",20, 5, 150, 1);
    mult = Param("mult",2, 1, 4, 0.05);
    tr = mult * ATR(per);

    trailArray[ 0 ] = C[ 0 ]; // initialize
    for( i = 1; i < BarCount; i++ )
    {
    prev = trailArray[ i - 1 ];

    if (C[ i ] > prev AND C[ i - 1 ] > prev)
    {
    trailArray[ i ] = Max(prev,C[ i ] - tr[ i ]);
    }
    else if (C[ i ] < prev AND C[ i - 1 ] < prev)
    {
    trailArray[ i ] = Min(prev,C[ i ] + tr[ i ]);
    }
    else if (C[ i ] > prev)
    {
    trailArray[ i ] = C[ i ] - tr[ i ];
    }
    else
    {
    trailArray[ i ] = C[ i ] + tr[ i ];
    }
    }
    trailArray = Ref(trailArray,-1);

    SetChartBkColor( ParamColor("ColorBG", ColorRGB( 0, 0, 0 ) ) );
    GraphXSpace = 5;
    SetChartOptions(0, chartShowDates);
    Plot(trailArray,"\ntrailArray",ParamColor("ColorTrail",ColorRGB(255,255,255)),styleStaircase);
    Plot( C, "\nCandle",colorWhite, styleCandle );
     
  3. tools&tradingideas

    tools&tradingideas Active Member

    Joined:
    Sep 13, 2009
    Messages:
    81
    Likes Received:
    183
    Trophy Points:
    33
    put in a function and added some colours:

    // from Metastock formula, link: http://stocata.org/metastock/stop_trail_atr.html

    function vstop_func(tr)
    {
    trailArray[ 0 ] = C[ 0 ]; // initialize
    for( i = 1; i < BarCount; i++ )
    {
    prev = trailArray[ i - 1 ];

    if (C[ i ] > prev AND C[ i - 1 ] > prev)
    {
    trailArray[ i ] = Max(prev,C[ i ] - tr[ i ]);
    }
    else if (C[ i ] < prev AND C[ i - 1 ] < prev)
    {
    trailArray[ i ] = Min(prev,C[ i ] + tr[ i ]);
    }
    else if (C[ i ] > prev)
    {
    trailArray[ i ] = C[ i ] - tr[ i ];
    }
    else
    {
    trailArray[ i ] = C[ i ] + tr[ i ];
    }
    }
    return trailArray;
    }

    per = Param("per",20, 5, 150, 1);
    mult = Param("mult",2, 1, 4, 0.05);
    tr = mult * ATR(per);
    trailArray = vstop_func(tr);
    //trailArray = Ref(trailArray,-1);

    SetChartBkColor( ParamColor("ColorBG", ColorRGB( 0, 0, 0 ) ) );
    GraphXSpace = 5;
    SetChartOptions(0, chartShowDates);
    Plot(IIf(trailArray > C,trailArray,Null),"\ntrailShort",ParamColor("ColorTrailShort",ColorRGB(255,0,0)),styleStaircase);
    Plot(IIf(trailArray < C,trailArray,Null),"\ntrailLong",ParamColor("ColorTrailLong",ColorRGB(0,255,0)),styleStaircase);
    Plot( C, "\nCandle",colorWhite, styleCandle );
     
    Last edited: Jul 22, 2011
    Petom likes this.
  4. tools&tradingideas

    tools&tradingideas Active Member

    Joined:
    Sep 13, 2009
    Messages:
    81
    Likes Received:
    183
    Trophy Points:
    33
    simple reversal system can be constructed using this volatility stop. Let me know if someone has ideas how to use it
     
    Last edited: Jul 22, 2011
  5. Ayuraveda

    Ayuraveda New Member

    Joined:
    May 21, 2010
    Messages:
    10
    Likes Received:
    1
    Trophy Points:
    3
    Could you pls. write the full AFL that included B/S arrow like the picture ?
    Thanks:clapping:
     
  6. tools&tradingideas

    tools&tradingideas Active Member

    Joined:
    Sep 13, 2009
    Messages:
    81
    Likes Received:
    183
    Trophy Points:
    33
    best to try to code it yourself since you only know if it can be used when you do some backtesting. This type of system works on very volatile futures like TF, see Friday trading:
     
    Last edited: Jul 22, 2011

  7. szachool

    szachool New Member

    Joined:
    Aug 20, 2009
    Messages:
    10
    Likes Received:
    5
    Trophy Points:
    3
    Well done. Thank t&t :clapping:
     
  8. cbosein

    cbosein Active Member

    Joined:
    Sep 13, 2009
    Messages:
    160
    Likes Received:
    229
    Trophy Points:
    43
    Hi Tools&tradingideas,

    Good & decent chart
    Thanks & regards
    keerthi

     
  9. rpc

    rpc Active Member

    Joined:
    Aug 19, 2005
    Messages:
    488
    Likes Received:
    38
    Trophy Points:
    28
    @Ayurveda
    add follo 3 lines at the end of code

    Buy = Cross(C,trailarray);
    Sell = Cross(trailarray,C);
    PlotShapes(Buy*shapeUpTriangle+Sell*shapeDownTriangle,IIf(Buy,colorBlue,colorYellow),0,IIf(Buy,L,H));
     
    cbosein likes this.
  10. huss1652

    huss1652 New Member

    Joined:
    May 27, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Great formula guys. I tried this with a dual moving average crossover system and I have an issue. The trailing stop at some points is above a buy position. Is anyone good enough to add to this code stating if a buy is generated to reset the stop below the buy signal?
     
Loading...

Share This Page