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

 Discuss Volatility Stop Loss /need code from Metasa to Amibroker/ at the AmiBroker within the Traderji.com; Hi There is quite good Volatility Stop Loss based on Average True Range (ATR). It ...

 Traderji.com Volatility Stop Loss /need code from Metasa to Amibroker/
 Register FAQ Calendar Search Today's Posts Mark Forums Read

#1
27th March 2010, 05:41 PM
 Member Join Date: Aug 2009 Posts: 10 Thanks: 10 Thanked 5 Times in 2 Posts
Volatility Stop Loss /need code from Metasa to Amibroker/

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:
Quote:
 SVE_Stop_Trail_ATR atrper:=Input("ATR period :",1,100,5); atrfact:=Input("ATR multiplication :",1,10,3.5); loss:=atrfact*ATR(atrper); trail:= If(C>PREV AND Ref(C,-1)>PREV, Max(PREV,C-loss), If(CPREV,C-loss,C+loss))); Trail
Stop Long
Quote:
 SVE_StopLong_Trail_ATR_Date {SVE_StopLong_Trail_ATR_Date - ATR trailing stop Long from date} InpMonth:=Input("Month",1,12,1); InpDay:=Input("Day",1,31,2); InpYear:=Input("Year",1800,2050,2009); InitStop:=Input("Initial Stop Price",0.1,10000,10); atrper:=Input("ATR period :",1,100,5); atrfact:=Input("ATR multiplication :",1,10,3.5); loss:=atrfact*ATR(atrper); EntryLong:= InpYear=Year() AND InpMonth=Month() AND InpDay=DayOfMonth(); EntryLock:=If(Ref(EntryLong,-1)=0 AND EntryLong=1,1,PREV); support:=C-loss; TrailStopLong:= If(EntryLock=0 OR EntryLong=1,InitStop, If(support>Ref(Support,-1),Max(support,PREV),PREV)); TrailStopLong

Stop Short
Quote:
 SVE_StopShort_Trail_ATR_Date {SVE_StopShort_Trail_ATR_Date - ATR trailing stop Short from date} InpMonth:=Input("Month",1,12,1); InpDay:=Input("Day",1,31,2); InpYear:=Input("Year",1800,2050,2009); InitStop:=Input("Initial Stop Price",0.1,10000,10); atrper:=Input("ATR period :",1,100,5); atrfact:=Input("ATR multiplication :",1,10,3.5); loss:=atrfact*ATR(atrper); EntryLong:= InpYear=Year() AND InpMonth=Month() AND InpDay=DayOfMonth(); EntryLock:=If(Ref(EntryLong,-1)=0 AND EntryLong=1,1,PREV); support:=C+loss; TrailStopShort:= If(EntryLock=0 OR EntryLong=1,InitStop, If(support>Ref(Support,-1),Min(support,PREV),PREV)); TrailStopShort
And this Volatility Stop can also be usefull for programmers.
Source page

Thanks, for any help.
 Sorry your browser does not support IFRAME

## Looking for a share broker?

#2
13th July 2010, 04:30 PM
 Member Join Date: Sep 2009 Posts: 81 Thanks: 6 Thanked 182 Times in 58 Posts
Re: Volatility Stop Loss /need code from Metasa to Amibroker/

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("ColorTr ail",ColorRGB(255,255,255)),styleStaircase);
Plot( C, "\nCandle",colorWhite, styleCandle );
#3
13th July 2010, 04:52 PM
 Member Join Date: Sep 2009 Posts: 81 Thanks: 6 Thanked 182 Times in 58 Posts
Re: Volatility Stop Loss /need code from Metasa to Amibroker/

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("Colo rTrailShort",ColorRGB(255,0,0)),styleStaircase);
Plot(IIf(trailArray < C,trailArray,Null),"\ntrailLong",ParamColor("Color TrailLong",ColorRGB(0,255,0)),styleStaircase);
Plot( C, "\nCandle",colorWhite, styleCandle );

Last edited by tools&tradingideas; 22nd July 2011 at 03:09 AM.
 The Following User Says Thank You to tools&tradingideas For This Useful Post: Petom (18th July 2010)
#4
14th July 2010, 01:35 AM
 Member Join Date: Sep 2009 Posts: 81 Thanks: 6 Thanked 182 Times in 58 Posts
Re: Volatility Stop Loss /need code from Metasa to Amibroker/

simple reversal system can be constructed using this volatility stop. Let me know if someone has ideas how to use it

Last edited by tools&tradingideas; 22nd July 2011 at 03:09 AM.
#5
17th July 2010, 06:52 PM
 Member Join Date: May 2010 Posts: 10 Thanks: 0 Thanked 1 Time in 1 Post
Re: Volatility Stop Loss /need code from Metasa to Amibroker/

Could you pls. write the full AFL that included B/S arrow like the picture ?
Thanks
#6
18th July 2010, 02:22 AM
 Member Join Date: Sep 2009 Posts: 81 Thanks: 6 Thanked 182 Times in 58 Posts
Re: Volatility Stop Loss /need code from Metasa to Amibroker/

Quote:
 Originally Posted by Ayuraveda Could you pls. write the full AFL that included B/S arrow like the picture ? Thanks
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 by tools&tradingideas; 22nd July 2011 at 03:09 AM.
#7
26th March 2011, 02:16 AM
 Member Join Date: Aug 2009 Posts: 10 Thanks: 10 Thanked 5 Times in 2 Posts
Re: Volatility Stop Loss /need code from Metasa to Amibroker/

Well done. Thank t&t
#8
27th March 2011, 08:06 PM
 Member Join Date: Sep 2009 Posts: 160 Thanks: 1,007 Thanked 229 Times in 85 Posts
Re: Volatility Stop Loss /need code from Metasa to Amibroker/

Good & decent chart
Thanks & regards
keerthi

Quote:
 Originally Posted by tools&tradingideas 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("Colo rTrailShort",ColorRGB(255,0,0)),styleStaircase); Plot(IIf(trailArray < C,trailArray,Null),"\ntrailLong",ParamColor("Color TrailLong",ColorRGB(0,255,0)),styleStaircase); Plot( C, "\nCandle",colorWhite, styleCandle );
#9
27th March 2011, 10:05 PM
 Member Join Date: Aug 2005 Posts: 488 Thanks: 171 Thanked 38 Times in 27 Posts
Re: Volatility Stop Loss /need code from Metasa to Amibroker/

@Ayurveda
add follo 3 lines at the end of code

Sell = Cross(trailarray,C);
 The Following User Says Thank You to rpc For This Useful Post: cbosein (28th March 2011)
#10
27th May 2011, 02:45 AM
 Member Join Date: May 2011 Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts
Re: Volatility Stop Loss /need code from Metasa to Amibroker/

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?

 Bookmarks

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are Off Pingbacks are Off Refbacks are Off Forum Rules

All times are GMT +5.5. The time now is 03:38 AM.

Indemnity, Disclaimer & Disclosure Notice:
* By visiting Traderji.com you automatically indicate that you agree to our Forum Rules, Indemnity, Disclaimer & Disclosure Notice and General Content Disclaimer Notice and indemnify Traderji.com, its associates and related parties of all claims howsoever resulting from the usage of the forum/site.