Professional Documents
Culture Documents
//Linear Regression Line with 2 Standard Deviation Channels Plotted Above and
Below
//The original was written by Patrick Hargus, with critical hints from Marcin
Gorzynski, Amibroker.com Technical Support
//Wysiwyg coded the angle in degrees part
//I modified the original Linear Regression code so that the line will change color
based on the degree of the Linear Regression slope.
//I combine this with my TRADING SYSTEM.
//When my system gives an entry signal I look at the Linear Regression Line and I
will only take long positions if the Linear Regression line is green and the entry price
is below the LR line.
//When my system gives an entry signal I look at the Linear Regression Line and I
will only take short positions if the Linear Regression line is red and the entry price
is above the LR line.
//It is usefull for filtering out lower probability trades.
//================================================Sta
rt Chart
Configuration==========================================
==================================
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo
%g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C,
SelectedValue( ROC( C, 1 )) ));
SetChartBkGradientFill(ParamColor("Top", colorTeal), ParamColor("Bottom",
colorLightGrey), ParamColor("Title", colorTeal));
SetChartBkColor(colorTeal);
//================================================En
d Chart
Configuration==========================================
=====================================
//====================================Start of Linear
Regression
Code===============================================
===================================
P = ParamField("Price field",-1);
Length = 150;
x = Cum(1);
lastx = LastValue( x ) - shift;
aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) );
bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) );
y = Aa + bb * ( x - (Lastx - DaysBack +1 ) );
LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );
LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );
Pi = 3.14159265 * atan(1); // Pi
SlopeAngle = atan(bb)*(180/Pi);
if(LineUp)
{
Plot(LRLine, "Lin. Reg. Line Up", IIf(LineUp, colorBrightGreen, colorWhite), LRStyle);
}
else
{
Plot(LRLine, "Lin. Reg. Line Down", IIf(LineDn, colorDarkRed, colorWhite), LRStyle);
}
SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ;
SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ;
_SECTION_BEGIN("MAMA");
SetBarsRequired( 10000, 10000 );
SetChartOptions(0,chartShowArrows|chartShowDates);
prc = ( High + Low ) / 2;
fastlimit = 0.5;
slowlimit = 0.05;
pi=4*atan(1);
RTD=180/pi;
DTR=pi/180;
Cyclepart=Param("Alpha",0.5,0.1,1,0.1);
Detrender[i] = ( 0.0962 * Smooth[i] + 0.5769 * Smooth[i-2] - 0.5769 * Smooth[i-4] 0.0962 * Smooth[i-6] ) * AmpCorr[i];
Q1[i] = ( 0.0962 * Detrender[i] + 0.5769 * Detrender[i-2] - 0.5769 * Detrender[i-4] 0.0962 * Detrender[i-6] ) * AmpCorr[i];
I1[i] = Detrender[i-3];
if (I1[i] != 0) phase[i] = DTR*360/atan(q1[i]/I1[i]);
deltaphase[i]=phase[i-1]-phase[i];
if (deltaphase[i] <1) deltaphase[i]=1;
alpha[i]=fastlimit[i]/deltaphase[i];
if (alpha[i] < slowlimit[i]) alpha[i]=slowlimit[i];
MAMA[i]=alpha[i] * prc [i] +(1-alpha[i])*MAMA[i-1];
FAMA[i]=Cyclepart*alpha[i] * prc [i] +(1-Cyclepart*alpha[i])*FAMA[i-1];
}
_SECTION_END();
_SECTION_BEGIN("SYSTEM");
BuySetupValue=ValueWhen(Cross(MAMA,FAMA),H,1);
SellsetupValue=ValueWhen(Cross(FAMA,MAMA),L,1);
Buysetup =Cross(MAMA,FAMA) ;
Sellsetup = Cross(FAMA,MAMA);
Longa = Flip(Buysetup,Sellsetup);
shrta = Flip(Sellsetup,Buysetup);
Buy=Longa AND Cross(C,BuySetupValue);
Sell=shrta AND Cross(SellsetupValue,C);
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
t1= Flip(Buy,Sell);
t2= Flip(Sell,Buy);
BPrice=ValueWhen(t1 AND Ref(t1,-1)==0,C,1);
SPrice=ValueWhen(t2 AND Ref(t2,-1)==0,C,1);
GraphXSpace = 5;
dist = 1.5*ATR(20);
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] ) PlotText( "" + C[ i ], i, L[ i ]-dist[i], colorLime );
if( Sell[i] ) PlotText( "" + C[ i ], i, H[ i ]+dist[i], colorYellow );
}
_SECTION_END();
//=================TITLE=============================
==================================================
=================
_SECTION_BEGIN("Title");
if( Status("action") == actionIndicator )
(
Title = EncodeColor(colorWhite)+ "MAMA Poistional System" + " - " + Name() + " " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorYellow) +"Op-"+O+" "+"Hi-"+H+"
"+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+
EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+" ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+" ","")
+"\n"+EncodeColor(colorWhite)+
WriteIf(Sell , "Total Profit/Loss for the Last TRADE Rs."+(C-BPrice)+"","")+
GfxSetBkMode(0); // transparent
GfxTextOut( Name(), Status("pxwidth")/2, Status("pxheight")/12 );
GfxSelectFont("Tahoma", Status("pxheight")/12 );
GfxTextOut( "", Status("pxwidth")/2, Status("pxheight")/3 );
GfxSelectFont("Tahoma", Status("pxheight")/20 );
GfxTextOut( "", Status("pxwidth")/2, Status("pxheight")/2 );
GfxSelectFont("Tahoma", Status("pxheight")/40);
GfxTextOut( "", Status("pxwidth")/7, Status("pxheight")/6);
_SECTION_END();