Fave This

Sunday, 19 September 2010

Re: [Komunitas AmiBroker] AFL Linear Regression + Slope + Line + Exploration



Terima kasih Christopher Tahir ,semoga selalu sukses.

ini saya surfing dapat artikel
semoga berguna


Raff Regression Channel

Introduction

Developed by Gilbert Raff, the Raff Regression Channel is a linear regression with evenly spaced trendline above and below. The width of the channel is based on the high or low that is the furthest from the linear regression. The trend is up as long as prices rise within this channel. An uptrend reverses when price breaks below the channel extension. The trend is down as long as prices decline within the channel. Similarly, a downtrend reverses when price breaks above the channel extension.

Formula

The Raff Regression Channel (RRC) is based on a linear regression, which is the least-squares line-of-best-fit for a price series. Even though the formula is beyond the scope of this article, linear regressions are easy to understand with a visual example. Chart 1 shows the Nasdaq 100 ETF (QQQQ) with the Raff Regression Channel in red. The middle line is the linear regression extending from the July closing low to the January closing high. Note that the linear regression is based on closing prices. This makes the linear regression the line-of-best-fit for the closing prices from the July low to the January high. Next, the width is set by determining the high or low that is the furthest from the linear regression (early July to mid January). In this case, the next day's low is the furthest and it is used to set the lower channel trendline. The upper trendline is then set the same distance from the linear regression as the lower trendline.

 Chart 1 - Raff Regression Channel

Chart 2 shows an example of QQQQ in a downtrend. The Raff Regression Channel extends from the April (closing) high to the July (closing) low. The late June high defines the width of the channel because it is the furthest high or low from the linear regression. This means the lower trendline is set the same distance from the linear regression as the upper trendline.

 Chart 2 - Raff Regression Channel

Drawing and Signals

The Raff Regression Channel can be drawn to cover the existing trend and subsequently define the trend. Once established, extension lines can be drawn to identify the suport resistance or reversal points. An uptrend extends from the lowest closing low to the highest closing high for a move. A downtrend extends from the highest closing high to the lowest closing low. Keep in mind that closing prices are used when drawing the Raff Regression Channel, but intraday highs and lows are used to set the channel trendlines.

Chart 3 shows Urban Outfitters (URBN) with the Raff Regression Channel drawn from the July 2007 low to the September 2008 high (weekly closes). This covers the uptrend so far. Had URBN moved to a new closing high in October, the Raff Regression Channel would have extended to that high. Instead of moving to a new high, URBN broke below the regression channel extension to reverse the uptrend. Notice that the lower trendline was extended to extrapolate the channel.

 Chart 3 - Raff Regression Channel

Chart 4 shows Nvidia (NVDA) with a downtrend extending from the October 2007 high to the November 2008 low. The Raff Regression Channel did not extend further because the stock traded flat and held above its November low into 2009. The red dotted line shows the channel extension or the regression channel top. NVDA broke this extension in February-March to start an uptrend.


Conclusions

As a channel based on a linear regression, the Raff Regression Channel is well suited for trend identification. The width of the channel depends on the furthest high or low from the linear regression. As such, spike highs and lows will result in very wide channels that may not capture the true range. When an uptrend starts with a sharp surge, the low a few days after this initial surge is often the furthest high-low from the linear regression. By extension, when a downtrend starts with a sharp decline, the high of this initial decline often the furthest high-low from the linear regression. Sharp initial moves create wide channels with few, if any, reaction highs or lows touching the upper and lower trendlines. Such was the case with the surge off the March 2009 lows (see the QQQQ chart further below). Even though this article only focused on trend identification, the Raff Regression Channel can be drawn early in the trend and extended to forecast future support or resistance levels as well as overbought or oversold levels. Channel extensions can act as support or resistance. Moves outside the channel extensions can also denote overbought or oversold conditions.

Using with SharpCharts

The Raff Regression Channel tool can be found when annotating a SharpChart. The icon is a gray zigzag with a green line through it. Left click on the icon to select it. Left click on the starting point and drag the indicator to the ending point. To rearrange an existing Raff Regression Channel, click on the start or end of the linear regression (middle line) and move accordingly. Extensions can be added manually using the trendline tool in SharpCharts.

Chart 5 - Raff Regression Channel



2010/9/19 Christopher Tahir <chris_tahir@ymail.com>
 

Sorry sy lupa attach...



AFL ini fungsi utamanya adalah melihat trend, tapi penentuan trend hanya berdasarkan apa yg saya pake saja, tdk begitu valid apabila dipake di saham US bs dibantu temen2 kalo berminat....
Krn ada saham2 Bullish, tetapi hanya dibuat hanya biasa2 saja. bahkan sideways.
Btw, sy kasi sdikit info pemakaiannya:
Sy ada attach Parameternya, boleh dihajar coba2:

Ada terdapat yg namanya Parameter Start, itu hanya dipake utk catatan sekaligus utk BarCount itung2 Bar dari tgl tertentu.
Parameter Trend, dipake buat Exploration, ada beberapa sy buat.
Price From & To adalah Harga saham yg dikehendaki.

Silahkan dioprek bagi yg berminat, lagipula coding yg sy pake lumayan simple, maklum nubie....
Hehehhehe

Langsung liat aja uda nampak itu mana yg bs di fix sama temen2....




// ========== Dedicated to Amibroker 4 BEI ========= //

_SECTION_BEGIN("R-Squared+LinReg+Slope+Exploration");
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot( Volume,", Volume",colorBlack,styleHidden);

MaxGraph=5;

Periods = Optimize("Periods",Param("Periods",126,2,300,1),2,500,1);
shift = Param("Look back period",0,0,500,1);
P = ParamField("Price field",3);

x = Cum(1);
lastx = LastValue( x ) - shift;
aa = LastValue( Ref(LinRegIntercept(P, Periods), -shift) );
bb = LastValue( Ref(LinRegSlope( P, Periods), -shift) );
y = Aa + bb * ( x - (Lastx - Periods+1 ) );

R2=(Correlation(P,y,Periods)^2);
R2Style=ParamStyle("R2 Style");
R2Color=ParamColor("R2 Color",colorBlack);
Rdm = 1-R2;

LRColor = ParamColor("LR Color", colorCycle );
LRStyle = ParamStyle("LR Style");
LRLine =  IIf( x > (lastx - Periods) AND BarIndex() < Lastx, y, Null );
Plot( LRLine , "LinReg", LRCOLOR, LRSTYLE);

// =========================== LinReg1 =========================== //

SDP = Param("Standard Deviation", 1.0, 0, 6, 0.1);
SD = SDP/2;

width = LastValue( Ref(SD*StDev(p, Periods),-shift) );SDU = IIf( x > (lastx - Periods) AND BarIndex() < Lastx, y+width , Null ) ;
SDL = IIf( x > (lastx - Periods) AND BarIndex() < Lastx, y-width , Null ) ;

SDColor = ParamColor("SD Color", colorCycle );
SDStyle = ParamStyle("SD Style");

Plot( SDU , "Upper Lin Reg", SDColor,SDStyle );
Plot( SDL , "Lower Lin Reg", SDColor,SDStyle );

// =========================== LinReg2 =========================== //

SDP2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1);
SD2 = SDP2/2;

width2 = LastValue( Ref(SD2*StDev(p, Periods),-shift) );SDU2 = IIf( x > (lastx - Periods) AND BarIndex() < Lastx, y+width2 , Null ) ;
SDL2 = IIf( x > (lastx - Periods) AND BarIndex() < Lastx, y-width2 , Null ) ;

SDColor2 = ParamColor("2 SD Color", colorCycle );
SDStyle2 = ParamStyle("2 SD Style");

Plot( SDU2 , "Upper Lin Reg", SDColor2,SDStyle2 );
Plot( SDL2 , "Lower Lin Reg", SDColor2,SDStyle2 );

slope = LinRegSlope(P,Periods);

// === BarCount ===//

Start = ParamDate("Start","Default");
X = BarsSince(DateNum()== Start)+1;

YY = int(Start / 10000) + 1900;
M = int((Start-((YY-1900)*10000))/100);
DD = (Start-((YY-1900)*10000))-(M*100);


// =========================== Exploration =========================== //
Degree = LinRegSlope(MA(P,Periods),2);

Bu1 = Degree > 0.05 AND Degree <= 0.25;
Bu2 = Degree > 0.25 AND Degree <= 0.5;
Bu3 = Degree > 0.5 AND Degree <= 0.75;
Bu4 = Degree > 0.75;

Si1 = Degree > 0.0175 AND Degree <= 0.05;
Si2 = Degree >= -0.0175 AND Degree <= 0.0175;
Si3 = Degree < -0.0175 AND Degree >= -0.05;

Be1 = Degree < -0.05 AND Degree >= -0.25;
Be2 = Degree < -0.25 AND Degree >= -0.5;
Be3 = Degree < -0.5 AND Degree >= -0.75;
Be4 = Degree < -0.75;

Result =
WriteIf( Bu1 , "Weak Bullish" ,
WriteIf( Bu2 , "Medium Bullish" ,
WriteIf( Bu3 , "Strong Bullish" ,
WriteIf( Bu4 , "Extreme Bullish" ,
WriteIf( Si1 , "Sideways-Bullish" ,
WriteIf( Si2 , "Sideways" ,
WriteIf( Si3 , "Sideways-Bearish" ,
WriteIf( Be1 , "Weak Bearish" ,
WriteIf( Be2 , "Medium Bearish" ,
WriteIf( Be3 , "Strong Bearish" ,
WriteIf( Be4 , "Extreme Bearish" , "")))))))))));

FGCol =
IIf(Bu1 , colorBlack  ,
IIf(Bu2 , colorBlack ,
IIf(Bu3 , colorLightYellow ,
IIf(Bu4 , colorLightYellow ,
IIf(Si1 , colorBlack  ,
IIf(Si2 , colorBlack  ,
IIf(Si3 , colorBlack ,
IIf(Be1 , colorBlack ,
IIf(Be2 , colorBlack ,
IIf(Be3 , colorLightYellow ,
IIf(Be4 , colorLightYellow , colorDefault)))))))))));

BGCol =
IIf(Bu1 , colorBrightGreen ,
IIf(Bu2 , colorLime ,
IIf(Bu3 , colorGreen ,
IIf(Bu4 , colorDarkGreen ,
IIf(Si1 , colorSkyblue ,
IIf(Si2 , colorYellow ,
IIf(Si3 , colorRose ,
IIf(Be1 , colorPink ,
IIf(Be2 , ColorRGB(255,100,100) ,
IIf(Be3 , colorRed ,
IIf(Be4 , colorDarkRed , colorDefault)))))))))));

R2FGCol =
IIf(R2 >= 0 AND R2 < 0.25 , colorBlack ,
IIf(R2 >= 0.25 AND R2 < 0.5 , colorBlack ,
IIf(R2 >= 0.5 AND R2 < 0.75 , colorBlack ,
IIf(R2 >= 0.75 AND R2 < 0.85 , colorLightYellow ,
IIf(R2 >= 0.85 , colorLightYellow , colorDefault)))));

R2BGCol =
IIf(R2 >= 0 AND R2 < 0.25 , colorRose ,
IIf(R2 >= 0.25 AND R2 < 0.5 , colorLime ,
IIf(R2 >= 0.5 AND R2 < 0.75 , colorBrightGreen ,
IIf(R2 >= 0.75 AND R2 < 0.85 , colorGreen ,
IIf(R2 >= 0.85 , colorDarkGreen , colorDefault)))));

Bullish = Bu1 OR Bu2 OR Bu3 OR Bu4;
Bullish1 = Bu1 OR Bu2;
Bullish2 = Bu3 OR Bu4;
Sideways = Si1 OR Si2 OR Si3;
Bearish = Be1 OR Be2 OR Be3 OR Be4;
Bearish1 = Be1 OR Be2;
Bearish2 = Be3 OR Be4;
All = Bu1 OR Bu2 OR Bu3 OR Bu4 OR Si1 OR Si2 OR Si3 OR Be1 OR Be2 OR Be3 OR Be4;

Q = ParamList("Trend","Bullish|Good Bullish|Mild Bullish|Sideways|Bearish|Mild Bearish|Bad Bearish|All");
QQ =
IIf( Q == "Bullish" , Bullish ,
IIf( Q == "Mild Bullish" , Bullish1 ,
IIf( Q == "Good Bullish" , Bullish2 ,
IIf( Q == "Sideways" , Sideways ,
IIf( Q == "Bearish" , Bearish ,
IIf( Q == "Mild Bearish" , Bearish1 ,
IIf( Q == "Bad Bearish" , Bearish2 ,
IIf( Q == "All" , All , 0))))))));


PriceFrom = Param("Price From:", 1, 50 , 1000000, 1);
PriceTo   = Param("Price To:",   100000, 50, 1000000, 1);
Filter = (QQ) AND (C >= PriceFrom AND C <= PriceTo);
AddColumn(C,"Close" , 1.2 , colorDefault , colorDefault , 65);
AddTextColumn( Result , "Trend" , 1.0 , FGCol , BGCol , 96);
AddColumn(R2 , "R-Sq" , 1.3 , R2FGCol , R2BGCol);
AddColumn(Degree , "Slope" , 1.2);
AddColumn(SDU2 , "Upper LinReg2" , 1.2 , colorDefault , SDColor2 , 65);
AddColumn(SDU , "Upper LinReg" , 1.2 , colorLightYellow , SDColor , 65);
AddColumn(LRLine , "LinReg" , 1.2 , colorDefault , LRColor , 65);
AddColumn(SDL , "Lower LinReg" , 1.2 , colorLightYellow , SDColor , 65);
AddColumn(SDL2 , "Lower LinReg2" , 1.2 , colorDefault , SDColor2 , 65);

GfxSetOverlayMode(1);
GfxSelectFont("Kunstler Script", Status("pxheight")*6/100 );
GfxSetBkMode(0);
GfxSetTextColor(colorBlue);
GfxTextOut("Christopher Tahir",Status("pxwidth")/300, Status("pxheight")/2.1 );


Title = "  •  "+Name()+"  •  ||  "+Interval(2)+"  ||  •  "+Date()+"  •  Open "+Open+"  •  Hi "+ H+"  •  Lo "+L+"  •  Close "+Close+"("+WriteVal(ROC(C,1),1.1)+"%)"
+EncodeColor(colorRed)+"\n----------------------------------------------------------------------------"
+EncodeColor(R2Color)+"\n  •  R-Squared ("+Periods+") = "+WriteVal(R2,1.4)+"  •  Random ("+Periods+") = "+WriteVal(Rdm,1.4)+"  •  "
+EncodeColor(colorRed)+"\n----------------------------------------------------------------------------"
+EncodeColor(colorGreen)+"\n  •  Slope ("+Periods+") = "+WriteVal(slope,1.4)+"  •  "
+EncodeColor(colorRed)+"\n----------------------------------------------------"
+EncodeColor(colorBlue)+"\n  •  Since = "+M+"/"+DD+"/"+YY+EncodeColor(colorDarkGrey)+"  •  BarCount = "+X+"  •  "
+EncodeColor(colorRed)+"\n----------------------------------------------------"
+EncodeColor(colorBlack)+"\n  •  Trend ("+Periods+") = "+Result+"  •  "
+EncodeColor(colorRed)+"\n------------------------------------------------";
_SECTION_END();

// ========= END ========== //





__._,_.___


Apabila membutuhkan software AmiBroker, Realtime Intraday Data & Pelatihan silahkan kontak : Dendo Valentino | Cell : 08159304868 | e-mail: amibrokerfreak{at}yahoo.co.id | YM id : dendov | http://www.facebook.com/dendo.amibrokerfreak | http://www.amibroker-4-bei.org





Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___

No comments:

Post a Comment