# Volatility Stop Loss /need code from Metasa to Amibroker/

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

1. ### szachoolNew Member

Joined:
Aug 20, 2009
Messages:
10
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.

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&tradingideasActive Member

Joined:
Sep 13, 2009
Messages:
81
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&tradingideasActive Member

Joined:
Sep 13, 2009
Messages:
81
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&tradingideasActive Member

Joined:
Sep 13, 2009
Messages:
81
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. ### AyuravedaNew Member

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

6. ### tools&tradingideasActive Member

Joined:
Sep 13, 2009
Messages:
81
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. ### szachoolNew Member

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

8. ### cboseinActive Member

Joined:
Sep 13, 2009
Messages:
160
229
Trophy Points:
43

Good & decent chart
Thanks & regards
keerthi

9. ### rpcActive Member

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

Sell = Cross(trailarray,C);

cbosein likes this.

Joined:
May 27, 2011
Messages:
6