Tuesday 29 October 2019

Sunday 27 October 2019

WayToWin Trading System


WayToWin Trading System



If you wants This Trading System
Email: asfatul.alam@yahoo.com
 

Compare fit for Amibroker (AFL)

 Least Squares Polynomial Fit test
 comparing a least squares fit using matrix calculations
 with one that does not

Button Style Trading Control’s in Amibroker

Button Style Trading Control’s in Amibroker 




If any one wants to get This AFL Email: asfatul.alam@yahoo.com

Saturday 26 October 2019

Fisher transform indicator AFL Amibroker



Ehlers formulas from Ehlers, John F. Cybernetic Analysis for Stocks and Futures. Wiley. 2004. Chapter 1, p. 1. Code on p. 7.

Thursday 24 October 2019

Controlled Trailing Stop-Loss AFL Amibroker

Controlled Trailing Stop-Loss (TSL): It is necessary to ensure that TSL always goes up for Long positions and goes down for Short positions or remains constant to the previous value. "Percentage or Points" based TSLs does not account market Volatility, so it is not dynamic. "Standard Deviation" based TSL or "ATR" based TSLs are ideal. For demo using ATR based TSL here:


Here is one way to deal with daily pivots AFL Amibroker


Here is one way to deal with daily pivots:


https://drive.google.com/file/d/1_KQNmw2_RwEuPKwATKbs1hA0Nc83zwG3/view?usp=sharing

Mean band for Amibroker (AFL)



You can buy a stock when candle stick is above the band and in similar way u can sell when the candle stick is below the band…it is really a helpful AFL….


Bollinger band with entry signals AFL for Amibroker



Trading system, Amibroker, Axploration, Bands


The formula use macd and stoch astick signals with the bollinger band and moving averages for entry and exit signals


All in one AFL for Amibroker



All in one has Patterns Wave, MACD, Candle Details, Guppy Movement, Support-Resistance, BB, Pivot indicator.

One can easily determine the trend.

https://drive.google.com/file/d/1HtAZaKWbP84k_nMXecHPxAmHR3rjwZjT/view?usp=sharing


Wednesday 23 October 2019

NICK MA Swing + heikin pivots DoubleTop for Amibroker (AFL)



NICK MA Swing + heikin pivots







_SECTION_BEGIN("NICK MA Swing + heikin pivots");
SetBarsRequired(200,0);

GraphXSpace = 5;
SetChartOptions(0,chartShowArrows|chartShowDates);
k = Optimize("K",Param("K",1,0.25,5,0.25),0.25,5,0.25);
Per= Optimize("atr",Param("atr",4,3,20,1),3,20,1);
HACLOSE=(O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "" + Name(), colorWhite, styleCandle | styleNoLabel );
j=Haclose;

f=ATR(14);

rfsctor = WMA(H-L, Per);

revers = k * rfsctor;

Trend = 1;
NW[0] = 0;


for(i = 1; i < BarCount; i++)
{
if(Trend[i-1] == 1)
{
if(j[i] < NW[i-1])
{
Trend[i] = -1;
NW[i] = j[i] + Revers[i];
}
else
{
Trend[i] = 1;
if((j[i] - Revers[i]) > NW[i-1])
{
NW[i] = j[i] - Revers[i];
}
else
{
NW[i] = NW[i-1];
}
}
}
if(Trend[i-1] == -1)
{
if(j[i] > NW[i-1])
{
Trend[i] = 1;
NW[i] = j[i] - Revers[i];
}
else
{
Trend[i] = -1;
if((j[i] + Revers[i]) < NW[i-1])
{
NW[i] = j[i] + Revers[i];
}
else
{
NW[i] = NW[i-1];
}
}
}
}



_SECTION_BEGIN("ema5,13sound");
x = EMA(Close,5);
y = EMA(Close,13);
Plot(EMA(Close,5),"",colorBrightGreen,styleLine, styleThick);
Plot(EMA(Close,13),"",colorOrange,styleLine, styleThick);
XR=(EMA(Close,5) * (2 / 6 - 1) - EMA(Close,13) * (2 / 11 - 1)) / (2 / 6 - 2 / 11);


Buy=Cross(j,nw);
Cover=Cross(j,nw);
Sell=Cross(nw,j);
Short=Cross(nw,j);
SellPrice=ValueWhen(Sell,C,1);
BuyPrice=ValueWhen(Buy,C,1);
Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );
_SECTION_END();
_SECTION_BEGIN("Title");
if( Status("action") == actionIndicator )
(
Title = EncodeColor(colorWhite)+ "NICK MA Swing 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(colorRed)+
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-BuyPrice)+"","")+
WriteIf(Buy , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","")+
WriteIf(Long AND NOT Buy, "Trade : Long - Entry price Rs."+(BuyPrice),"")+
WriteIf(shrt AND NOT Sell, "Trade : Short - Entry price Rs."+(SellPrice),"")+"\n"+
WriteIf(Long AND NOT Buy, "Current Profit/Loss Rs."+(C-BuyPrice)+"","")+
WriteIf(shrt AND NOT Sell, "Current Profit/Loss Rs."+(SellPrice-C)+"",""));
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-30);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-40);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-35);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-30);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-40);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-35);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
AlertIf( Ref(Buy,-1), "SOUND C:\\Windows\\Media\\Windows XP Startup.wav", "Nick MA Buy", 2 );
AlertIf( Ref(Sell,-1), "SOUND C:\\Windows\\Media\\Ringin.wav", "Nick MA Sell", 2 );
_SECTION_END();

_SECTION_BEGIN("MA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 8, 2, 200, 1 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") | styleNoRescale );
_SECTION_END();

_SECTION_BEGIN("Mid MA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 34, 2, 300, 1 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") | styleNoRescale );
_SECTION_END();

_SECTION_BEGIN("Long MA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 200, 2, 400, 1 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") | styleNoRescale );
_SECTION_END();
_SECTION_END();

_SECTION_BEGIN("SAR");
acc = Param("Acceleration", 0.02, 0, 1, 0.001 );
accm = Param("Max. acceleration", 0.2, 0, 1, 0.001 );
Plot( SAR( acc, accm ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );
_SECTION_END();
_SECTION_BEGIN("Sup / Res Lines");
RSIperiod = 15; // Param("RSI p",3,14,30,1);
Percent = 5; // Param("ZIG %",8,9,15,1);
EMAperiod = 2; //Param("EMA p",4,5,10,1);
HHVperiod = 8; //Param("HHV p",3,5,10,1);
NumLine = 2; //Param("Num Lines",3,1,20,1);

Base = DEMA(RSI(RSIperiod),EMAperiod);


for( i = 1; i <= numline; i++ )
{
ResBase = LastValue(Peak(Base,Percent,i));
SupBase = LastValue(Trough(Base,Percent,i));
Plot(ValueWhen( ResBase==Base, HHV(H,HHVperiod) ), "Resist Level", colorRed, styleLine);
Plot(ValueWhen( supbase==Base, LLV(L,HHVperiod) ), "Support Level", colorGreen, styleLine);

}_SECTION_END();


_SECTION_BEGIN("heikin");

// Calculate Moving Average
MAPeriod = Param("MA Period", 15, 1, 100);
MAOpen = EMA(Open, MAPeriod);
MAHigh = EMA(High, MAPeriod);
MALow = EMA(Low, MAPeriod);
MAClose = EMA(Close, MAPeriod);

HaClose = (MAOpen + MAHigh + MALow + MAClose) / 4;
HaOpen = AMA(Ref(HaClose, -1), 0.5);


for(i = 0; i <= MAPeriod; i++) {

HaClose[i] = Null;


HaHigh = Max(MAHigh, Max(HaClose, HaOpen));
HaLow = Min(MALow, Min(HaClose, HaOpen));


"BarIndex = " + BarIndex();
"Open = " + Open;
"High = " + High;
"Low = " + Low;
"Close = "+ Close;
"HaOpen = " + HaOpen;
"HaHigh = " + HaHigh;
"HaLow = " + HaLow;
"HaClose = "+ HaClose;


farback=Param("How Far back to go",100,50,5000,10);
nBars = Param("Number of bars", 12, 5, 40);



aHPivs = H - H;

aLPivs = L - L;


aHPivHighs = H - H;

aLPivLows = L - L;

aHPivIdxs = H - H;

aLPivIdxs = L - L;

nHPivs = 0;

nLPivs = 0;

lastHPIdx = 0;

lastLPIdx = 0;

lastHPH = 0;

lastLPL = 0;

curPivBarIdx = 0;


aHHVBars = HHVBars(H, nBars);

aLLVBars = LLVBars(L, nBars);

aHHV = HHV(H, nBars);

aLLV = LLV(L, nBars);



aVisBars = Status("barvisible");

nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));

_TRACE("Last visible bar: " + nLastVisBar);


curBar = (BarCount-1);

curTrend = "";

if (aLLVBars[curBar] <

aHHVBars[curBar]) {

curTrend = "D";

}

else {

curTrend = "U";

}



for (i=0; i<farback; i++) {

curBar = (BarCount - 1) - i;



if (curTrend == "U") {

curTrend = "D";



aLPivs[curPivBarIdx] = 1;



nLPivs++;

}

// -- or current trend is up

//}
else {

if (curTrend == "D") {

curTrend = "U";

//curPivBarIdx = curBar - aHHVBars[curBar];

aHPivs[curPivBarIdx] = 1;

//aHPivHighs[nHPivs] = H[curPivBarIdx];

//aHPivIdxs[nHPivs] = curPivBarIdx;

nHPivs++;

}

// -- If curTrend is up...else...

}

// -- loop through bars

}



curBar = (BarCount-1);

candIdx = 0;

candPrc = 0;

lastLPIdx = aLPivIdxs[0];

lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];

lastHPH = aHPivHighs[0];

if (lastLPIdx > lastHPIdx) {

// -- Bar and price info for candidate pivot

candIdx = curBar - aHHVBars[curBar];

candPrc = aHHV[curBar];

if (

lastHPH < candPrc AND

candIdx > lastLPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aHPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nHPivs; j++) {

aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-

(j+1)];

aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];

}

aHPivHighs[0] = candPrc ;

aHPivIdxs[0] = candIdx;

nHPivs++;

}

} else {


// -- Bar and price info for candidate pivot

candIdx = curBar - aLLVBars[curBar];

candPrc = aLLV[curBar];

if (

lastLPL > candPrc AND

candIdx > lastHPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aLPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nLPivs; j++) {

aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];

aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];

}

aLPivLows[0] = candPrc;

aLPivIdxs[0] = candIdx;

nLPivs++;

}

}



PlotShapes(

IIf(aHPivs==1, shapeDownArrow, shapeNone), colorRed, 0,

High, Offset=-15);

PlotShapes(

IIf(aLPivs==1, shapeUpArrow , shapeNone), colorBrightGreen, 0,

Low, Offset=-15);

AlertIf( Ref(aLPivs==1,-1), "SOUND C:\\Windows\\Media\\Windows XP Startup.wav", "Heikin Buy ", 2 );
AlertIf( Ref(aHPivs==1,-1), "SOUND C:\\Windows\\Media\\Ringin.wav", "Heikin Sell", 2 );

Buy=(aLPivs==1);
Cover=(aLPivs==1);
Sell=(aHPivs==1);
Short=(aHPivs==1);
SellPrice=ValueWhen(Sell,C,1);
BuyPrice=ValueWhen(Buy,C,1);
Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );
_SECTION_END();
//=================TITLE================================================================================================
_SECTION_BEGIN("Title");
if( Status("action") == actionIndicator )
(
Title = EncodeColor(colorLightBlue)+ "Heikin-Ashi pivot + NMA Swing system - Boxed arrows NMA buy-sell - Normal Arrows Heikin Pivot Buy-Sell" + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorYellow) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+
EncodeColor(colorRed)+
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-BuyPrice)+"","")+
WriteIf(Buy , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","")+
WriteIf(Long AND NOT Buy, "Trade : Long - Entry price Rs."+(BuyPrice),"")+
WriteIf(shrt AND NOT Sell, "Trade : Short - Entry price Rs."+(SellPrice),"")+"\n"+
WriteIf(Long AND NOT Buy, "Current Profit/Loss Rs."+(C-BuyPrice)+"","")+
WriteIf(shrt AND NOT Sell, "Current Profit/Loss Rs."+(SellPrice-C)+"",""));
AlertIf( Ref(Buy, -1), "SOUND C:\\Windows\\Media\\Windows XP Startup.wav", "ActionIndicator Buy", 2 );
AlertIf( Ref(Sell, -1), "SOUND C:\\Windows\\Media\\Ringin.wav", "Action Indicator Sell", 2 );
_SECTION_END();

_SECTION_BEGIN("Shiree_Hanging Man bullish +bearish with volume");

BT = BBandTop( C, 20, 2 );
BB = BBandBot( C, 20, 2 );
X=(BT-BB);

RBuy=EMA(C,3)<EMA(C,10) AND C<EMA(C,3) AND ((O+C)/2)>L+0.7*(H-L) AND abs(O-C)<0.4*(H-L) AND V>MA(V,10) AND (H-L)>=0.8*MA((H-L),10) AND X>1.2*MA(X,10);
Rsell=EMA(C,3)>EMA(C,10) AND C>EMA(C,3) AND ((O+C)/2)<H-0.7*(H-L) AND abs(O-C)<0.4*(H-L) AND V>MA(V,10) AND (H-L)>=0.8*MA((H-L),10) AND X>1.2*MA(X,10);


PlotShapes(Rsell*shapeDownTriangle,colorCustom12, 0, High, Offset =-40);
PlotShapes(Rsell*shapeDigit5,colorCustom12, 0, High, Offset =55);

PlotShapes(RBuy*shapeDigit5,colorYellow, 0, High, Offset =-70);
PlotShapes(RBuy*shapeUpTriangle,colorYellow, 0, Low, Offset =-10);


Filter =Rbuy OR Rsell;

AddColumn( IIf(RBuy,1,IIf(Rsell,-1,Null)) ,"RBS",1.0,colorWhite,IIf(RBuy,colorDarkGreen,IIf( Rsell,colorRed,Null)));

_SECTION_END();

_SECTION_BEGIN("Double top detection");

percdiff = 5;
fwdcheck = 5;
mindistance = 10;
validdiff = percdiff/400;
PK= Peak( H, percdiff, 1 ) == High;

x = Cum( 1 );
XPK1 = ValueWhen( PK, x, 1 );
XPK2 = ValueWhen( PK, x, 2 );

peakdiff = ValueWhen( PK, H, 1 )/ValueWhen( PK, H, 2 );
doubletop = PK AND abs( peakdiff - 1 ) < validdiff AND (XPK1 - XPK2)>mindistance
AND High > HHV( Ref( H, fwdcheck ), fwdcheck - 1 );
Buy = doubletop;
Sell = 0;

WriteIf( Highest( doubletop ) == 1, "AmiBroker has detected some possible double top patterns for " + Name() + "\nLook for green arrows on the price chart.", "There are no double top patterns for " + Name() );
_SECTION_END();

}

FOREX INTRADAY HEIKIN ASHI + PIVOT POINTS for Amibroker (AFL)

FOREX INTRADAY HEIKIN ASHI + PIVOT POINTS for Amibroker (AFL)



_SECTION_BEGIN("The_Beast_2");
SetBarsRequired(10000,10000); 

Prd1=Param("ATR Period 1-20",4,1,20,1);
Prd2=Param("LookBack Period 1-20",7,1,20,1);
Green=HHV(LLV(L,Prd1)+ATR(Prd1),Prd2);      

RED=LLV(HHV(H,Prd1)-ATR(Prd1),Prd2);

HaClose =EMA((O+H+L+C)/4,3);
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
Color = IIf(C>Green ,colorBrightGreen,IIf(C < RED,colorRed,colorBlue));

PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", color, styleCandle,styleThick );


Odd=13;
CoefOdd=round(Odd/2);

Even=12;
Coefeven=Even/2;
Coefeven2=Coefeven+1;

CongestionPercent=2.8;

TriangularOdd=MA(MA(C,CoefOdd),CoefOdd);
TriangularEven=MA(MA(C,Coefeven),Coefeven2);

finalMov_avg=IIf(Odd > even,triangularOdd,TriangularEven);

Color=colorBrightGreen;
tickercolor=colorBlack;

Plot(finalMov_avg,"",IIf(C < finalmov_avg,colorRed,Color),styleLine|styleThick);


LB= Param("Look Back Periods",10,1,30,1);
R=ValueWhen(Cross(MA(C,LB),C),HHV(H,LB),1);
S=ValueWhen(Cross(C,MA(C,LB)),LLV(L,LB),1);


UpClose = Close - Ref(Close,-1);
Color = IIf(UpClose > 0, colorBrightGreen, colorRed);


Plot (R,"Resz",ParamColor("R Color",colorRed),8+16);
Plot (S,"Supp",ParamColor("S Color",colorGreen),8+16);


GraphXSpace=4;


_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} -                       "+ EncodeColor(colorYellow)+"Open = "+ EncodeColor(colorYellow) +"%g    "+ EncodeColor(colorBrightGreen)+"High = "+ EncodeColor(colorBrightGreen) +"%g - "+ EncodeColor(colorRed)+"Low = "+ EncodeColor(colorRed) +"%g    "+ EncodeColor(colorYellow) +"Close = "+ EncodeColor(colorYellow) +" %g                                                                                                                                                                                                                                                                                                                                         (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));







DayH = TimeFrameGetPrice("H", inDaily, -1);     DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1);     DayLI = LastValue (DayL,1);    // yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1);         // yesterdays close
DayO = TimeFrameGetPrice("O", inDaily);            // current day open
DayH2= TimeFrameGetPrice("H", inDaily, -2);  DayH2I = LastValue (DayH2,1); // Two days before high
DayL2= TimeFrameGetPrice("L", inDaily, -2);  DayL2I = LastValue (DayL2,1);  // Two days before low
DayH3= TimeFrameGetPrice("H", inDaily, -3);  DayH3I = LastValue (DayH3,1);  // Three days before high
DayL3= TimeFrameGetPrice("L", inDaily, -3);  DayL3I = LastValue (DayL3,1);  // Three days before low

numbars = LastValue(Cum(Status("barvisible")));
hts  = -33.5;

YHL = ParamToggle("Yesterday HI LO","Show|Hide",1);
if(YHL==1) {
  Plot(DayL,"YL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayH,"YH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  PlotText(" YH " ,     LastValue(BarIndex())-(numbars/Hts), DayHI, colorTurquoise);
  PlotText(" YL " ,     LastValue(BarIndex())-(numbars/Hts), DayLI, colorTurquoise);
}

TDBHL = ParamToggle("2/3Days before HI LO","Show|Hide",0);
if(TDBHL==1) {
  Plot(DayL2,"2DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayH2,"2DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayL3,"3DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayH3,"3DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  PlotText(" 2DBH " ,     LastValue(BarIndex())-(numbars/Hts), DayH2I, colorTurquoise);
  PlotText(" 2DBL " ,     LastValue(BarIndex())-(numbars/Hts), DayL2I, colorTurquoise);
  PlotText(" 3DBH " ,     LastValue(BarIndex())-(numbars/Hts), DayH3I, colorTurquoise);
  PlotText(" 3DBL " ,     LastValue(BarIndex())-(numbars/Hts), DayL3I, colorTurquoise);
}

// Pivot Levels //
PP = (DayL + DayH + DayC)/3;  PPI = LastValue (PP,1);   // Pivot
R1  =  (PP * 2) - DayL;       R1I = LastValue (R1,1);   // Resistance 1
S1  =  (PP * 2)  - DayH;      S1I = LastValue (S1,1);   // Support 1
R2  =  PP + R1 - S1;          R2I = LastValue (R2,1);   // Resistance 2
S2  =  PP - R1 + S1;          S2I = LastValue (S2,1);   // Support 2
R3  =  PP + R2 - S1;          R3I = LastValue (R3,1);   // Resistance 3
S3  =  PP - R2 + S1;          S3I = LastValue (S3,1);   // Support 3

ppl = ParamToggle("Pivot Levels","Show|Hide",1);
if(ppl==1)  {
  Plot(PP, "PP",colorYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(R1, "R1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(S1, "S1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(R2, "R2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(S2, "S2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(R3, "R3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(S3, "S3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);

  PlotText(" Pivot ",  LastValue(BarIndex())-(numbars/Hts), PPI, colorYellow);
  PlotText(" R1 " ,    LastValue(BarIndex())-(numbars/Hts), R1I, colorViolet);
  PlotText(" S1 " ,    LastValue(BarIndex())-(numbars/Hts), S1I, colorViolet);
  PlotText(" R2 " ,    LastValue(BarIndex())-(numbars/Hts), R2I, colorViolet);
  PlotText(" S2 " ,    LastValue(BarIndex())-(numbars/Hts), S2I, colorViolet);
  PlotText(" R3 " ,    LastValue(BarIndex())-(numbars/Hts), R3I, colorViolet);
  PlotText(" S3 " ,    LastValue(BarIndex())-(numbars/Hts), S3I, colorViolet);
}
// Camerilla Levels //

rg = (DayH - DayL);

H5=DayC+1.1*rg;     H5I = LastValue (H5,1);
H4=DayC+1.1*rg/2;   H4I = LastValue (H4,1);
H3=DayC+1.1*rg/4;   H3I = LastValue (H3,1);
H2=DayC+1.1*rg/6;   H2I = LastValue (H2,1);
H1=DayC+1.1*rg/12;  H1I = LastValue (H1,1);
L1=DayC-1.1*rg/12;  L1I = LastValue (L1,1);
L2=DayC-1.1*rg/6;   L2I = LastValue (L2,1);
L3=DayC-1.1*rg/4;   L3I = LastValue (L3,1);
L4=DayC-1.1*rg/2;   L4I = LastValue (L4,1);
L5=DayC-1.1*rg;     L5I = LastValue (L5,1);

pcl = ParamToggle("Camerilla Levels","Show|Hide",0);
if(pcl==1)  {
   Plot(H5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   PlotText(" H5 = " ,     LastValue(BarIndex())-(numbars/Hts), H5I  +0.05, colorRose);
   PlotText(" H4 = " ,     LastValue(BarIndex())-(numbars/Hts), H4I  +0.05, colorRose);
   PlotText(" H3 = " ,     LastValue(BarIndex())-(numbars/Hts), H3I  +0.05, colorRose);
   PlotText(" H2 = " ,     LastValue(BarIndex())-(numbars/Hts), H2I  +0.05, colorRose);
   PlotText(" H1 = " ,     LastValue(BarIndex())-(numbars/Hts), H1I  +0.05, colorRose);
   PlotText(" L1 = " ,     LastValue(BarIndex())-(numbars/Hts), L1I  +0.05, colorRose);
   PlotText(" L2 = " ,     LastValue(BarIndex())-(numbars/Hts), L2I  +0.05, colorRose);
   PlotText(" L3 = " ,     LastValue(BarIndex())-(numbars/Hts), L3I  +0.05, colorRose);
   PlotText(" L4 = " ,     LastValue(BarIndex())-(numbars/Hts), L4I  +0.05, colorRose);
   PlotText(" L5 = " ,     LastValue(BarIndex())-(numbars/Hts), L5I  +0.05, colorRose);
}

THL = ParamToggle("Todays Hi Lo","Show|Hide",1);
if(THL==1)  {
  isRth = TimeNum() >= 084500 & TimeNum() <= 085959;
  isdRth = TimeNum() >= 084500 & TimeNum() <= 160000;
  aRthL = IIf(isRth, L, 1000000);
  aRthH = IIf(isdRth, H, Null);
  aRthLd = IIf(isdRth, L, 1000000);
  DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
  DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
  DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
  DayL = TimeFrameExpand( DayL, inDaily, expandFirst );
  Bars = BarsSince(TimeNum() >= 94500 AND TimeNum() < 095959);
  x0 = BarCount-LastValue(Bars);
  x1 = BarCount-1;
  DayHline=LineArray(x0,LastValue(DayH),x1,LastValue (DayH),0);
  DayLline=LineArray(x0,LastValue(DayL),x1,LastValue (DayL),0);
  DayHlineI = LastValue (DayHline,1);
  DayLlineI = LastValue (DayLline,1);
  Plot(DayHline,"DayH",colorYellow,styleBar|styleNoRescale|styleNoTitle);
  Plot(DayLline,"DayL",colorYellow,styleBar|styleNoRescale|styleNoTitle);
  PlotText(" Day Hi " ,     LastValue(BarIndex())-(numbars/Hts), DayHlineI  +0.05, colorYellow);
  PlotText(" Day Lo " ,     LastValue(BarIndex())-(numbars/Hts), DayLlineI  +0.05, colorYellow);
 }


_SECTION_BEGIN("Pivot_Finder");


farback=Param("How Far back to go",200,0,5000,10);
nBars = Param("Number of bars", 12, 5, 40);


aHPivs = H - H;

aLPivs = L - L;


aHPivHighs = H - H;

aLPivLows = L - L;

aHPivIdxs = H - H;

aLPivIdxs = L - L;

nHPivs = 0;

nLPivs = 0;

lastHPIdx = 0;

lastLPIdx = 0;

lastHPH = 0;

lastLPL = 0;

curPivBarIdx = 0;


aHHVBars = HHVBars(H, nBars);

aLLVBars = LLVBars(L, nBars);

aHHV = HHV(H, nBars);

aLLV = LLV(L, nBars);



aVisBars = Status("barvisible");

nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));

_TRACE("Last visible bar: " + nLastVisBar);



curBar = (BarCount-1);

curTrend = "";

if (aLLVBars[curBar] <

aHHVBars[curBar]) {

curTrend = "D";

}

else {

curTrend = "U";

}



for (i=0; i<farback; i++) {

curBar = (BarCount - 1) - i;



if (aLLVBars[curBar] < aHHVBars[curBar]) {



if (curTrend == "U") {

curTrend = "D";



curPivBarIdx = curBar - aLLVBars[curBar];

aLPivs[curPivBarIdx] = 1;

aLPivLows[nLPivs] = L[curPivBarIdx];

aLPivIdxs[nLPivs] = curPivBarIdx;

nLPivs++;

}



} else {

if (curTrend == "D") {

curTrend = "U";

curPivBarIdx = curBar - aHHVBars[curBar];

aHPivs[curPivBarIdx] = 1;

aHPivHighs[nHPivs] = H[curPivBarIdx];

aHPivIdxs[nHPivs] = curPivBarIdx;

nHPivs++;

}



}



}



curBar = (BarCount-1);

candIdx = 0;

candPrc = 0;

lastLPIdx = aLPivIdxs[0];

lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];

lastHPH = aHPivHighs[0];

if (lastLPIdx > lastHPIdx) {



candIdx = curBar - aHHVBars[curBar];

candPrc = aHHV[curBar];

if (

lastHPH < candPrc AND

candIdx > lastLPIdx AND

candIdx < curBar) {




aHPivs[candIdx] = 1;



for (j=0; j<nHPivs; j++) {

aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-

(j+1)];

aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];

}

aHPivHighs[0] = candPrc ;

aHPivIdxs[0] = candIdx;

nHPivs++;

}

} else {




candIdx = curBar - aLLVBars[curBar];

candPrc = aLLV[curBar];

if (

lastLPL > candPrc AND

candIdx > lastHPIdx AND

candIdx < curBar) {




aLPivs[candIdx] = 1;



for (j=0; j<nLPivs; j++) {

aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];

aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];

}

aLPivLows[0] = candPrc;

aLPivIdxs[0] = candIdx;

nLPivs++;

}

}

 



for (k=0; k<nHPivs; k++) {

_TRACE("High pivot no. " + k

+ " at barindex: " + aHPivIdxs[k] + ", "

+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],

DateTime(), 1), formatDateTime)

+ ", " + aHPivHighs[k]);

}



PlotShapes(

IIf(aHPivs==1, shapeStar, shapeNone), colorBrightGreen, 0, H, 25);

PlotShapes(

IIf(aLPivs==1, shapeStar , shapeNone), colorRed, 0, L, -20);


farback=Param("How Far back to go",200,12,30,1);
nBars = Param("Number of bars", 12, 1, 30, 1);

aHPivs = H - H;
aLPivs = L - L;

aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
nHPivs = 0;
nLPivs = 0;
lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;

aHHVBars = HHVBars(H, nBars);
aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);
aLLV = LLV(L, nBars);

aVisBars = Status("barvisible");
nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));
_TRACE("Last visible bar: " + nLastVisBar);

curBar = (BarCount-1);
curTrend = "";
if (aLLVBars[curBar] <
aHHVBars[curBar]) {
curTrend = "D";
}
else {
curTrend = "U";
}

for (i=0; i<farback; i++) {
curBar = (BarCount - 1) - i;

if (aLLVBars[curBar] < aHHVBars[curBar]) {

if (curTrend == "U") {
curTrend = "D";

curPivBarIdx = curBar - aLLVBars[curBar];
aLPivs[curPivBarIdx] = 1;
aLPivLows[nLPivs] = L[curPivBarIdx];
aLPivIdxs[nLPivs] = curPivBarIdx;
nLPivs++;
}

} else {
if (curTrend == "D") {
curTrend = "U";
curPivBarIdx = curBar - aHHVBars[curBar];
aHPivs[curPivBarIdx] = 1;
aHPivHighs[nHPivs] = H[curPivBarIdx];
aHPivIdxs[nHPivs] = curPivBarIdx;
nHPivs++;
}

}

}

curBar = (BarCount-1);
candIdx = 0;
candPrc = 0;
lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];
lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];
if (lastLPIdx > lastHPIdx) {

candIdx = curBar - aHHVBars[curBar];
candPrc = aHHV[curBar];
if (
lastHPH < candPrc AND
candIdx > lastLPIdx AND
candIdx < curBar) {

aHPivs[candIdx] = 1;

for (j=0; j<nHPivs; j++) {
aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-
(j+1)];
aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];
}
aHPivHighs[0] = candPrc ;
aHPivIdxs[0] = candIdx;
nHPivs++;
}
} else {

candIdx = curBar - aLLVBars[curBar];
candPrc = aLLV[curBar];
if (
lastLPL > candPrc AND
candIdx > lastHPIdx AND
candIdx < curBar) {

aLPivs[candIdx] = 1;

for (j=0; j<nLPivs; j++) {
aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];
aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];
}
aLPivLows[0] = candPrc;
aLPivIdxs[0] = candIdx;
nLPivs++;
}
}

PlotShapes(IIf(aHPivs==1, shapeSmallDownTriangle, shapeNone), colorCustom12, 0, High, Offset=-5);
PlotShapes(IIf(aLPivs==1, shapeSmallUpTriangle , shapeNone), colorCustom11, 0, Low, Offset=-5);

Sell = aHPivs == 1 ;
Buy = aLPivs == 1 ;
Filter=Buy OR Sell;
Sell=ExRem(Sell,Buy);
Buy=ExRem(Buy,Sell);



R=3;
m=0.015*(HHV(H,159)-LLV(L,159));
x=Cum(1);
per = 2.5;
pR = PeakBars( H, per, 1 ) == 0;
x01= LastValue(ValueWhen( pR, x, 1 ));
x02=LastValue(ValueWhen( pR, x, 2 ));
x03=LastValue(ValueWhen( pR, x, 3 ));
y01 = LastValue(ValueWhen( pR, H, 1 ) );
y02=LastValue(ValueWhen( pR, H, 2 ) );
y03 = LastValue( ValueWhen( pR, H, 3 ));

y0=y01;x0=x01;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y02;x0=x02;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y03;x0=x03;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
TpR = TroughBars( L, per, 1 ) == 0;//Trough condition
x01T= LastValue(ValueWhen( TpR, x, 1 ));
x02T=LastValue(ValueWhen( TpR, x, 2 ));
x03T=LastValue(ValueWhen( TpR, x, 3 ));
y01T = LastValue(ValueWhen( TpR, L, 1 ) );
y02T=LastValue(ValueWhen( TpR, L, 2 ) );
y03T = LastValue( ValueWhen( TpR, L, 3 ));
y0T=y01T;x0T=x01T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(Y,"",5,1);Plot(Y1,"",5,1);
y0T=y02T;x0T=x02T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);
y0T=y03T;x0T=x03T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);

R=2;//Marking circle radius
m=0.015*(HHV(H,159)-LLV(L,159));//scaling factor
x=Cum(1);
per = 1.5;//Sensitivity Calibration
pR = PeakBars( H, per, 1 ) == 0;//Peak condition
x01= LastValue(ValueWhen( pR, x, 1 ));
x02=LastValue(ValueWhen( pR, x, 2 ));
x03=LastValue(ValueWhen( pR, x, 3 ));
y01 = LastValue(ValueWhen( pR, H, 1 ) );
y02=LastValue(ValueWhen( pR, H, 2 ) );
y03 = LastValue( ValueWhen( pR, H, 3 ));

y0=y01;x0=x01;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y02;x0=x02;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y03;x0=x03;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
TpR = TroughBars( L, per, 1 ) == 0;//Trough condition
x01T= LastValue(ValueWhen( TpR, x, 1 ));
x02T=LastValue(ValueWhen( TpR, x, 2 ));
x03T=LastValue(ValueWhen( TpR, x, 3 ));
y01T = LastValue(ValueWhen( TpR, L, 1 ) );
y02T=LastValue(ValueWhen( TpR, L, 2 ) );
y03T = LastValue( ValueWhen( TpR, L, 3 ));
y0T=y01T;x0T=x01T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(Y,"",5,1);Plot(Y1,"",5,1);
y0T=y02T;x0T=x02T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);
y0T=y03T;x0T=x03T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);


R=1;
m=0.015*(HHV(H,159)-LLV(L,159));//scaling factor
x=Cum(1);
per = 0.325;//Sensitivity Calibration
pR = PeakBars( H, per, 1 ) == 0;//Peak condition
x01= LastValue(ValueWhen( pR, x, 1 ));
x02=LastValue(ValueWhen( pR, x, 2 ));
x03=LastValue(ValueWhen( pR, x, 3 ));
y01 = LastValue(ValueWhen( pR, H, 1 ) );
y02=LastValue(ValueWhen( pR, H, 2 ) );
y03 = LastValue( ValueWhen( pR, H, 3 ));

y0=y01;x0=x01;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y02;x0=x02;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y03;x0=x03;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
TpR = TroughBars( L, per, 1 ) == 0;//Trough condition
x01T= LastValue(ValueWhen( TpR, x, 1 ));
x02T=LastValue(ValueWhen( TpR, x, 2 ));
x03T=LastValue(ValueWhen( TpR, x, 3 ));
y01T = LastValue(ValueWhen( TpR, L, 1 ) );
y02T=LastValue(ValueWhen( TpR, L, 2 ) );
y03T = LastValue( ValueWhen( TpR, L, 3 ));
y0T=y01T;x0T=x01T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(Y,"",5,1);Plot(Y1,"",5,1);
y0T=y02T;x0T=x02T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);
y0T=y03T;x0T=x03T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);

EMA34 = EMA(C,34);
LSMA = LinearReg(C,23);


_SECTION_BEGIN("Price");

SetChartOptions(0,chartShowArrows|chartShowDates);

_N(Title = EncodeColor(colorCustom12) +StrFormat(" {{NAME}} - {{INTERVAL}}     {{DATE}} "+ EncodeColor(colorWhite) +"                       Open  = "+ EncodeColor(colorWhite) +"%g  "+ EncodeColor(colorBrightGreen) +"    High : "+ EncodeColor(colorBrightGreen) +" %g "+ EncodeColor(colorRed) +"  -   Low : "+ EncodeColor(colorRed) +" %g "+ EncodeColor(colorWhite) +"     Close  = "+ EncodeColor(colorWhite) +"%g                                                                                                                                                                                                                                                                              
{{VALUES}}",O,H,L,C ));

_SECTION_END();


_SECTION_BEGIN("Break Outs");
//{Buy}
x1=5; x2=4; x3=3; x4=2; x5=1; x6=.5;
//{Lowest price stock <5}
AA=IIf(C<5,Cross(C,ValueWhen(Peak(H,x1,1) !=Ref(Peak(H,x1,1),-1),H,1)),
//{Price between 5 AND 20}
IIf(C>5 AND C<20,Cross(C,ValueWhen(Peak(H,x2,1) !=Ref(Peak(H,x2,1),-1),H,1)),
//{Price between 20 AND 70}
IIf(C>20 AND C<70,Cross(C,ValueWhen(Peak(H,x3,1) !=Ref(Peak(H,x3,1),-1),H,1)),
//{Price between 70 AND 150}
IIf(C>70 AND C<150,Cross(C,ValueWhen(Peak(H,x4,1) !=Ref(Peak(H,x4,1),-1),H,1)),
//{Price between 150 AND 300}
IIf(C>150 AND C<300,Cross(C,ValueWhen(Peak(H,x5,1) !=Ref(Peak(H,x5,1),-1),H,1)),
//{Price over 300}
Cross(C,ValueWhen(Peak(H,x6,1)!=Ref(Peak(H,x6,1),-1),H,1)))))));

//{Sell }
x1=5; x2=4; x3=3; x4=2; x5=1; x6=.5;
//{Lowest price stock <5}
BB=IIf(C<5,Cross(ValueWhen(Trough(L,x1,1) !=Ref(Trough(L,x1,1),-1),L,1),C),
//{Price between 5 AND 20}
IIf(C>5 AND C<20,Cross(ValueWhen(Trough(L,x2,1) !=Ref(Trough(L,x2,1),-1),L,1),C),
//{Price between 20 AND 70}
IIf(C>20 AND C<70,Cross(ValueWhen(Trough(L,x3,1) !=Ref(Trough(L,x3,1),-1),L,1),C),
//{Price between 70 AND 150}
IIf(C>70 AND C<150,Cross(ValueWhen(Trough(L,x4,1) !=Ref(Trough(L,x4,1),-1),L,1),C),
//{Price between 150 AND 300}
IIf(C>150 AND C<300,Cross(ValueWhen(Trough(L,x5,1) !=Ref(Trough(L,x5,1),-1),L,1),C),
//{Price over 300}
Cross(ValueWhen(Trough(L,x6,1)!=Ref(Trough(L,x6,1),-1),L,1),C))))));

Color=IIf(BarsSince(AA)>BarsSince(BB),colorRed,IIf(RSI()>70,colorCustom11,colorDarkGreen));



P6=Param("Trailing Stop Risk",2.5,2,3.5,0.1);
P7=Param("Trailing Stop LookBack",2,5,25,1);
P8=Param("Trailing Stop PrevLow Switch",0,0,1,1);
PrevLow=IIf(P8==1, Ref(C,-TroughBars(C,3,1)) ,Null);

MyTotalPort = Param("MyTotalPort",1000000,10000,10000000,100000);
AcceptableRisk = Param("AcceptableRisk",0.5,0.1,3,0.1);
BarsFromStart = BarsSince(Cross(AA,BB)AND Ref(Color,-1)==colorRed) ;
InitialStopLoss =Ref( H - P6*ATR(P7),-BarsFromStart);
PositionSizing = 0.01*AcceptableRisk*MyTotalPort/( C - InitialStopLoss );

PPP = IIf( HHV(H - P6*ATR(P7),BarsFromStart+1) <C ,HHV(H - P6*ATR(P7),BarsFromStart+1),Null);
Plot( PPP ,"",colorCustom12);



_SECTION_BEGIN("Magnified Market Price");

FS=Param("Font Size",30,30,100,1);
GfxSelectFont("Arial", FS, 900, italic = False, underline = False, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor( ParamColor("Color",colorYellow) );
Hor=Param("Horizontal Position",1350,1350,1350,1350);
Ver=Param("Vertical Position",12,14,16,18);
GfxTextOut(""+C,Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Arial", 12, 700, italic = False, underline = False, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor(ParamColor("Color",colorYellow) );
GfxTextOut(""+DD+" ("+xx+"%)", Hor+5, Ver+45 );
_SECTION_END();



_SECTION_BEGIN("Ribbon");
uptrend=PDI()>MDI()AND Signal()<MACD();
downtrend=MDI()>PDI()AND Signal()>MACD();
Plot( 1, "ribbon",
IIf( uptrend, colorLime, IIf( downtrend, colorRed,IIf(Signal()<MACD(), colorBlack, colorBlack ))), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -01, 50 );
_SECTION_END();



GraphXSpace=10;

 

ANDREWS Peak Trough for Amibroker (AFL)


 ANDREWS Peak Trough for Amibroker (AFL)




bi=BarIndex();
sbi=SelectedValue(bi);

// Pct threshhold for peak() & trough()

Zigpct=Param("Zigpct",4.6,1.0,30.0,0.1,0);


zema=EMA(C,14);
zpr=IIf(ROC(zema,1) > 0,H,L);
zag=Zig(zpr,zigpct);
tr=Ref(zag,-1) > zag AND zag < Ref(zag,1);
pk=Ref(zag,-1) < zag AND zag > Ref(zag,1);
pkprice=ValueWhen(pk,zpr);
trprice=ValueWhen(tr,zpr);


pklast=IIf(BarsSince(pk) < BarsSince(tr),1,0);
trlast=IIf(BarsSince(tr) < BarsSince(pk),1,0);


pk_id=((pklast OR tr) AND pkprice*(1-(zigpct*0.01)) > L ) AND NOT ( pk AND (H - L)/L >= 0.01 * zigpct);
tr_id=((trlast OR pk) AND H > trprice*(1+(zigpct*0.01)) ) AND NOT ( tr AND (H - L)/L >= 0.01 * zigpct);


pk_id=ExRem(pk_id,tr_id);
tr_id=ExRem(tr_id,pk_id);

pk_idlast=IIf(BarsSince(pk_id) < BarsSince(tr_id),1,0);
tr_idlast=IIf(BarsSince(tr_id) < BarsSince(pk_id),1,0);

Lookbk=IIf(pk_idlast,BarsSince(pk),BarsSince(tr));


xtsn=Param("Extension",62,1,2500,1);


shift=Param("Shift",0,-2000,2000,0.01);


alimit=Param("Angle Limit",40,1,180,1);


bkgrndcolor=ParamColor("Background Color",14);
pitchforkcolor=ParamColor("Pitchfork Color",10);


pline1=pkprice;
tline1=trprice;


pzag1=pline1 != Ref(pline1,-1);
tzag1=tline1 != Ref(tline1,-1);



zagx1=0;
zagx2=0;
zagx3=0;
zagy1=0;
zagy2=0;
zagy3=0;

for ( i = sbi - Lookbk[sbi], zagfnd = 0, pzagfnd = 0, tzagfnd = 0 ; i >= 0 && zagfnd < 3; i-- ) {
    if ( pzag1[i] || tzag1[i] ) {
        if ( pzag1[i] && NOT pzagfnd ) {
             zagfnd=zagfnd+1;
             pzagfnd=1;
             tzagfnd=0;
             if ( zagfnd == 1 ) {
                 zagx1[sbi]=i;
                 zagy1[sbi]=pline1[i];
             } else if (zagfnd == 2) {
                 zagx2[sbi]=i;
                 zagy2[sbi]=pline1[i];
             } else if (zagfnd == 3) {
                 zagx3[sbi]=i;
                 zagy3[sbi]=pline1[i];
             }
        } else if ( tzag1[i] && NOT tzagfnd ) {
             zagfnd=zagfnd+1;
             tzagfnd=1;
             pzagfnd=0;
             if ( zagfnd == 1 ) {
                 zagx1[sbi]=i;
                 zagy1[sbi]=tline1[i];
             } else if (zagfnd == 2) {
                 zagx2[sbi]=i;
                 zagy2[sbi]=tline1[i];
             } else if (zagfnd == 3) {
                 zagx3[sbi]=i;
                 zagy3[sbi]=tline1[i];
             }
        }
    }

    if ( zagfnd == 3 ) {  // Got 3 candidate peak/trough points

        echng=0;
        midx=0;
        midy=0;
        Handle=0;
        Top=0;
        Bot=0;

       

        Midx[sbi]=zagx2[sbi] + (zagx1[sbi]-zagx2[sbi]) / 2;
        Midy[sbi]=exp( log(zagy1[sbi]) - ( log(zagy1[sbi])-log(zagy2[sbi]) ) / 2);
        echng=(log(midy[sbi])-log(zagy3[sbi]))/(midx[sbi]-zagx3[sbi]);

       

        angle_rad = atan(echng);//radians
        angle_deg = 100 * angle_rad * 180/3.1416;//degrees

        if ( angle_deg < -alimit || angle_deg > alimit ) { // Too steep, reset the search
                                                           // to begin from the 2nd pivot found
            if ( tzagfnd == 1 ) {  // was tr,pk,tr so switch to pk,tr,pk
                tzagfnd = 0;
                pzagfnd = 1;
                zagfnd = 1;
                zagx1[sbi]=zagx2[sbi];
                zagy1[sbi]=zagy2[sbi];
                i = zagx1[sbi];
                zagx2=0;
                zagx3=0;
                zagy2=0;
                zagy3=0;
            } else {  // was pk,tr,pk so switch to tr,pk,tr
                tzagfnd = 1;
                pzagfnd = 0;
                zagfnd = 1;
                zagx1[sbi]=zagx2[sbi];
                zagy1[sbi]=zagy2[sbi];
                i = zagx1[sbi];
                zagx2=0;
                zagx3=0;
                zagy2=0;
                zagy3=0;
            }
       }
    }
}

// Calculate the Pitchfork itself

// Handle first

for ( j=zagx3[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
   Handle[j]=exp(log(zagy3[sbi]) + n*echng) + shift;
}

// High & low median lines.
if ( (exp(log(zagy2[sbi]) + (sbi-zagx2[sbi])*echng))
     > (exp(log(zagy1[sbi]) + (sbi-zagx1[sbi])*echng)) ) {  // Which one is top?
   for ( j=zagx2[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      top[j]=exp(log(zagy2[sbi]) + n*echng) + shift;
   }
   for ( j=zagx1[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      bot[j]=exp(log(zagy1[sbi]) + n*echng) + shift;
   }
} else {
   for ( j=zagx2[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      bot[j]=exp(log(zagy2[sbi]) + n*echng) + shift;
   }
   for ( j=zagx1[sbi],n=0 ; j < Min(sbi+xtsn,BarCount) ; j++,n++ ) {
      top[j]=exp(log(zagy1[sbi]) + n*echng) + shift;
   }
}

Hcolor=IIf(Handle==0,bkgrndcolor,IIf(Ref(handle,-1)== 0 AND handle != 0, bkgrndcolor,pitchforkcolor));
Tcolor=IIf(Top==0,bkgrndcolor,IIf(Ref(top,-1)== 0 AND top != 0, bkgrndcolor,pitchforkcolor));
Bcolor=IIf(Bot==0,bkgrndcolor,IIf(Ref(bot,-1)== 0 AND bot != 0, bkgrndcolor,pitchforkcolor));
Htitle=EncodeColor(pitchforkcolor)
      + StrFormat("\nAndrews: pct=%g lkbk=%g shft=%g alimit=%g Angle=%3.2f Handle=",
        Zigpct, Lookbk, shift, alimit, angle_deg);
Plot(Handle,Htitle,Hcolor,styleLine+styleDashed+styleNoRescale);
Plot(Bot,EncodeColor(pitchforkcolor)+" Bot=",Bcolor,styleLine+styleDashed+styleNoRescale);
Plot(Top,EncodeColor(pitchforkcolor)+" Top=",Tcolor,styleLine+styleDashed+styleNoRescale);

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

Polynomial curves for Amibroker (AFL)

Polynomial curves for Amibroker (AFL)
 
_SECTION_BEGIN("Chart Settings");
 SetChartOptions(0,chartShowArrows|chartShowDates);
 SetChartBkColor(ParamColor("Outer Panel",colorPaleBlue)); 
 SetChartBkGradientFill(ParamColor("Upper Chart",colorBlack),ParamColor("Lower Chart",colorBlack));  
_SECTION_END(); 


_SECTION_BEGIN("Katsayı Optimizeli 6.ncı Dereceden Polynom'un WMA()'sı Alınmış Deklem");
PG = ParamToggle(" Polynom Göstermek ","hide|show",0);

ACID=Param("Süper İnce Ayar",1,1,180,0.001);
Derece3 = ParamToggle("3.ncü Derece Polynom ","hide|show",0);
Derece4 = ParamToggle("4.ncü Derece Polynom ","hide|show",0);
Derece5 = ParamToggle("5.ncü Derece Polynom ","hide|show",0);
Derece6 = ParamToggle("6.ncü Derece Polynom ","hide|show",0);
Shape = ParamToggle(" Okları Göstermek ","hide|show",0);

Katsayi43=(Param("3.Katsayı1",0,-2,30,0.001));
Katsayi53=Param("3.Katsayı2",0,-2,30,0.001);
Katsayi63=Param("3.Katsayı3",0,-2,30,0.001);

Katsayi34=Param("4.Katsayı1",0,-30,30,0.001);
Katsayi44=Param("4.Katsayı2",0,-30,30,0.001);
Katsayi54=Param("4.Katsayı3",0,-30,30,0.001);
Katsayi64=Param("4.Katsayı4",0,-30,30,0.001);

Katsayi25=Param("5.Katsayı1",0,-30,30,0.001);
Katsayi35=Param("5.Katsayı2",0,-30,30,0.001);
Katsayi45=Param("5.Katsayı3",0,-30,30,0.001);
Katsayi55=Param("5.Katsayı4",0,-30,30,0.001);
Katsayi65=Param("5.Katsayı5",0,-30,30,0.001);

Katsayi16=Param("6.Katsayı1",0,-30,30,0.001);
Katsayi26=Param("6.Katsayı2",0,-30,30,0.001);
Katsayi36=Param("6.Katsayı3",0,-30,30,0.001);
Katsayi46=Param("6.Katsayı4",0,-30,30,0.001);
Katsayi56=Param("6.Katsayı5",0,-30,30,0.001);
Katsayi66=Param("6.Katsayı6",0,-30,30,0.001);

Moveto=Param("Sağa Sola Kaydır",1,1,1456,2);
Moveto1=Param("Yukarı Aşağı Kaydır",1,1,1456,2);
if(PG)
{

GfxSelectPen( colorGreen, 4 ); 
GfxSelectSolidBrush( colorYellow ); 

GfxRoundRect( 20+Moveto, 30+Moveto1, 300+Moveto, 430+Moveto1, 15, 15 ); 

GfxSelectPen( colorRed);
GfxSetTextColor( colorBlue );
GfxSetTextAlign( 0 ); 
GfxSetBkColor( colorYellow );
GfxTextOut( "Süper İnce Ayar               :   "+ StrFormat("%-3.000d",ACID), 50+Moveto, 35+Moveto1 );


PlotOHLC(O, H, L, C, "Data", colorWhite, styleBar | styleThick); 
pi=22/7;

Rtd = 180 / Pi;
Dtr = 1 / Rtd;
x=(O+C+H+L)/4;


if(Derece3)
{

Katsayi43=Katsayi43*(ACID*DTR);
Katsayi53=Katsayi53*(ACID*DTR);
Katsayi63=Katsayi63*(ACID*DTR);

y3=Katsayi43*x*exp(3) + Katsayi53*x*exp(2) + Katsayi63*x;
y3m=WMA(x,y3);

for( i = 1 ; i < BarCount-2; i++ ) 
{
if (y3m[i] <y3m[i-1] && y3m[i] <y3m[i+1]) Lpml[i]=1; 
else
Lpml[i] =0;

if (y3m[i] >y3m[i-1] && y3m[i] >y3m[i+1]) Lpmh[i]=1;
else
Lpmh[i] =0;
}

GR =ExRem(LpmH,Lpmh);
RD =ExRem(Lpml,Lpml);

if(Shape)
{
PlotShapes(IIf(GR!=0,shapeDownArrow,shapeNone),colorRed,H,y3m,-30);
PlotShapes(IIf(RD!=0,shapeUpArrow,shapeNone),colorBrightGreen,L,y3m,-30);
}



Plot(y3m,"",IIf(y3m>Ref(y3m,-1),colorLime,colorRed),styleLine|styleThick);

GfxTextOut( "3.ncü Derece Katsayı-1 :   "+ StrFormat("%-3.000d",Katsayi43), 50+Moveto, 60+Moveto1);
GfxTextOut( "3.ncü Derece Katsayı-2 :   "+ StrFormat("%-3.000d",Katsayi53), 50+Moveto, 80+Moveto1); 
GfxTextOut( "3.ncü Derece Katsayı-3 :   "+ StrFormat("%-3.000d",Katsayi63), 50+Moveto, 100+Moveto1); 

}

if(Derece4)
{

Katsayi34=Katsayi34*(ACID*DTR);
Katsayi44=Katsayi44*(ACID*DTR);
Katsayi54=Katsayi54*(ACID*DTR);
Katsayi64=Katsayi64*(ACID*DTR);

y4=Katsayi34*x*exp(4) +Katsayi44*x*exp(3) + Katsayi54*x*exp(2) + Katsayi64*x;
y4m=WMA(x,y4);


for( i = 1 ; i < BarCount-2; i++ ) 
{
if (y4m[i] <y4m[i-1] && y4m[i] <y4m[i+1]) Lpml[i]=1; 
else
Lpml[i] =0;

if (y4m[i] >y4m[i-1] && y4m[i] >y4m[i+1]) Lpmh[i]=1;
else
Lpmh[i] =0;
}

GR =ExRem(LpmH,Lpmh);
RD =ExRem(Lpml,Lpml);

if(Shape)
{

PlotShapes(IIf(GR!=0,shapeDownArrow,shapeNone),colorRed,H,y4m,-40);
PlotShapes(IIf(RD!=0,shapeUpArrow,shapeNone),colorBrightGreen,L,y4m,-40);
}


Plot(y4m,"",IIf(y4m>Ref(y4m,-1),colorLime,colorRed),styleLine|styleThick);


  
GfxTextOut( "4.ncü Derece Katsayı-1 :   "+ StrFormat("%-3.000d",Katsayi34), 50+Moveto, 120+Moveto1); 
GfxTextOut( "4.ncü Derece Katsayı-2 :   "+ StrFormat("%-3.000d",Katsayi44), 50+Moveto, 140+Moveto1); 
GfxTextOut( "4.ncü Derece Katsayı-3 :   "+ StrFormat("%-3.000d",Katsayi54), 50+Moveto, 160+Moveto1); 
GfxTextOut( "4.ncü Derece Katsayı-4 :   "+ StrFormat("%-3.000d",Katsayi64), 50+Moveto, 180+Moveto1); 



}

if(Derece5)
{

Katsayi25=Katsayi25*(ACID*DTR);
Katsayi35=Katsayi35*(ACID*DTR);
Katsayi45=Katsayi45*(ACID*DTR);
Katsayi55=Katsayi55*(ACID*DTR);
Katsayi65=Katsayi65*(ACID*DTR);

y5=Katsayi25*x*exp(5) + Katsayi35*x*exp(4) +Katsayi45*x*exp(3) + Katsayi55*x*exp(2) + Katsayi65*x; 
y5m=WMA(x,y5);

for( i = 1 ; i < BarCount-2; i++ ) 
{
if (y5m[i] <y5m[i-1] && y5m[i] <y5m[i+1]) Lpml[i]=1; 
else
Lpml[i] =0;

if (y5m[i] >y5m[i-1] && y5m[i] >y5m[i+1]) Lpmh[i]=1;
else
Lpmh[i] =0;
}

GR =ExRem(LpmH,Lpmh);
RD =ExRem(Lpml,Lpml);

if(Shape)
{

PlotShapes(IIf(GR!=0,shapeDownArrow,shapeNone),colorRed,H,y5m,-50);
PlotShapes(IIf(RD!=0,shapeUpArrow,shapeNone),colorBrightGreen,L,y5m,-50);
}


Plot(y5m,"",IIf(y5m>Ref(y5m,-1),colorLime,colorRed),styleLine|styleThick);


GfxTextOut( "5.ncü Derece Katsayı-1 :   "+ StrFormat("%-3.000d",Katsayi25), 50+Moveto, 200+Moveto1); 
GfxTextOut( "5.ncü Derece Katsayı-2 :   "+ StrFormat("%-3.000d",Katsayi35), 50+Moveto, 220+Moveto1); 
GfxTextOut( "5.ncü Derece Katsayı-3 :   "+ StrFormat("%-3.000d",Katsayi45), 50+Moveto, 240+Moveto1); 
GfxTextOut( "5.ncü Derece Katsayı-4 :   "+ StrFormat("%-3.000d",Katsayi55), 50+Moveto, 260+Moveto1); 
GfxTextOut( "5.ncü Derece Katsayı-5 :   "+ StrFormat("%-3.000d",Katsayi65), 50+Moveto, 280+Moveto1); 


}

if(Derece6)
{


Katsayi16=Katsayi16*(ACID*DTR);
Katsayi26=Katsayi26*(ACID*DTR);
Katsayi36=Katsayi36*(ACID*DTR);
Katsayi46=Katsayi46*(ACID*DTR);
Katsayi56=Katsayi56*(ACID*DTR);
Katsayi66=Katsayi66*(ACID*DTR);

y6 =Katsayi16*x*exp(6)+Katsayi26*x*exp(5) +Katsayi36*x*exp(4) + Katsayi46*x*exp(3) + Katsayi56*x*exp(2) + Katsayi66*x; 
y6m=WMA(x,y6);

for( i = 1 ; i < BarCount-2; i++ ) 
{
if (y6m[i] <y6m[i-1] && y6m[i] <y6m[i+1]) Lpml[i]=1; 
else
Lpml[i] =0;

if (y6m[i] >y6m[i-1] && y6m[i] >y6m[i+1]) Lpmh[i]=1;
else
Lpmh[i] =0;
}

GR =ExRem(LpmH,Lpmh);
RD =ExRem(Lpml,Lpml);

if(Shape)
{

PlotShapes(IIf(GR!=0,shapeHollowCircle,shapeNone),colorRed,H,y6m,0);
PlotShapes(IIf(RD!=0,shapeHollowCircle,shapeNone),colorBrightGreen,L,y6m,0);
}




Plot(y6m,"",IIf(y6m>Ref(y6m,-1),colorYellow,colorRed),styleLine|styleThick);


GfxTextOut( "6.ncü Derece Katsayı-1 :   "+ StrFormat("%-3.000d",Katsayi16), 50+Moveto, 300+Moveto1); 
GfxTextOut( "6.ncü Derece Katsayı-2 :   "+ StrFormat("%-3.000d",Katsayi26), 50+Moveto, 320+Moveto1); 
GfxTextOut( "6.ncü Derece Katsayı-3 :   "+ StrFormat("%-3.000d",Katsayi36), 50+Moveto, 340+Moveto1); 
GfxTextOut( "6.ncü Derece Katsayı-4 :   "+ StrFormat("%-3.000d",Katsayi46), 50+Moveto, 360+Moveto1); 
GfxTextOut( "6.ncü Derece Katsayı-5 :   "+ StrFormat("%-3.000d",Katsayi56), 50+Moveto, 380+Moveto1); 
GfxTextOut( "6.ncü Derece Katsayı-6 :   "+ StrFormat("%-3.000d",Katsayi66), 50+Moveto, 400+Moveto1); 

}


 }

_SECTION_END();

 
_SECTION_BEGIN("Colored Bollinger Bands");

Bg = ParamToggle(" Bollinger göster ","hide|show",0);
pr=Param( "Period",20,0,100,1);
P = ParamField("Price field",-1);
Style = ParamStyle("Style") | styleNoRescale | styleNoLabel |styleDashed;


if(Bg)
{

BBT=BBandTop(P,pr,2);
BBB=BBandBot(P,pr,2);
topcond = (Cross(BBT,C)OR C>BBT)AND Ref(C, -1)> BBT;
botcond = ( Cross(C,BBB))AND Ref(C, -1 < BBB);
bbtcolor = IIf( BBT> Ref (BBT, -1), colorDarkGreen,colorYellow );
bbbcolor = IIf( BBB > Ref (BBB, -1), colorDarkGreen,colorYellow );
PlotOHLC(O, H, L, C, "Data", colorWhite, styleBar | styleThick); 

Plot( BBT, "BBTop" + _PARAM_VALUES(), bbtcolor, styleLine|styleThick); 
Plot( BBB, "BBBot" + _PARAM_VALUES(), bbbcolor, styleLine|styleThick ); 

Buy=botcond;
Sell=topcond ;

//PlotShapes(shapeUpArrow * Buy, colorGreen, 0, L, - 10);
//PlotShapes(shapeDownArrow * Sell, colorRed, 0, H, - 10);

}
 _SECTION_END();


_SECTION_BEGIN("Hurst Cycle");
#include <PolyFit.afl>

p1    = Param("CMA Period 1",  13, 0, 999, 1);
p2    = Param("CMA Period 2",  25, 0, 999, 1);
p3    = Param("CMA Period 3",  50, 0, 999, 1);
p4    = Param("CMA Period 4", 100, 0, 999, 1);
p5    = Param("CMA Period 5", 200, 0, 999, 1);
p6    = Param("CMA Period 6", 600, 0, 999, 1);

BType = ParamList("Band Type", "Percent|Amount");

Pct1  = Param("Pct or Amt 1", 1.6, 0, 33, 0.05);
Pct2  = Param("Pct or Amt 2", 2.4, 0, 33, 0.05);
Pct3  = Param("Pct or Amt 3", 3.6, 0, 33, 0.05);
Pct4  = Param("Pct or Amt 4", 5.4, 0, 33, 0.05);
Pct5  = Param("Pct or Amt 5", 8.1, 0, 33, 0.05);
Pct6  = Param("Pct or Amt 6",12.0, 0, 33, 0.05);

ePFac = Param("Extrapolation Period Factor",      0.35, 0.10, 5.00, 0.05);
ePOrd = Param("Extrapolation PolyFit Order",      2,    1,    8,    1);
eAuto = Param("Extrapolation Auto Fixup Passes",  5,    0,    9,    1); 
eLFac = Param("Extrapolation AF Lag Factor",      0.33, 0,    1,    0.01);
eFFac = Param("Extrapolation AF Factor",          0.25, 0,    1,    0.01);
eFLen = Param("Extrapolation Future Length",      0,    0,   10,    1);
HT = ParamToggle("Hurst Cycle Göstermek ","hide|show",0);
BI    = BarIndex();
SVBI  = SelectedValue(BI);
LVBI  = LastValue(BI);
Data  = (H + L) / 2;

Title = EncodeColor(colorWhite) + " Hurst / Millard DE " +
EncodeColor(colorDefault) + " - BI = " + NumToStr(BI, 1.0) + " - CMA Periods =
";



GraphXSpace = 5;




function CMA_DE(Period, Percent, Red, Green, Blue)
{
    Pm = int(Period * 2 / 3);
    if (Pm % 2 == 0)
        Pm = Pm + 1;
    Pn = Period - Pm;
    if (Pn < 3) 
        Pn = 3;
    if (Pm <= Pn)
        Pm = Pn + 2;
    if (Pn % 2 == 0)
        Pn = Pn + 1;

    if (SVBI - (Pm + Pn) * (1 + eLFac + 0.02) > 0)
    {
        Lag    = (Pm - 1) / 2 + (Pn - 1) / 2;
        ExtraF = Lag + eFLen;
        CMA    = Ref(MA(MA(Data, Pm), Pn), Lag);
        EndBar = SVBI - Lag;
        BegBar = EndBar - round(Period * ePFac) + 1;
        if (EndBar - BegBar < 2)
            BegBar = EndBar - 2;

        CMA = IIf(BI <= SVBI - Lag, CMA, -1e10);

        if (BType == "Amount")
        {
            UBIS = CMA + Percent;
            LBIS = CMA - Percent;
        }
        else
        if (BType == "Percent")
        {
            UBIS = CMA * (1 + Percent / 100);
            LBIS = CMA * (1 - Percent / 100);
        }

        Plot(UBIS, "", ColorRGB(Red, Green, Blue), styleThick);
        Plot(LBIS, "", ColorRGB(Red, Green, Blue), styleThick);

        if (Red   > 0) Red   = 254;
        if (Green > 0) Green = 254;
        if (Blue  > 0) Blue  = 254;

        CMAx = PolyFit(CMA, BegBar, EndBar, ePOrd, 0, ExtraF);
        CMAx = IIf(BI >= SVBI - Lag - ExtraF, CMAx, -1e10);

        if (BType == "Amount")
        {
            CMAx = CMAx + CMA[SVBI - Lag] - CMAx[SVBI - Lag - ExtraF];
            UBOS = CMAx + Percent; 
            LBOS = CMAx - Percent;
        }
        else
        {
            CMAx = CMAx * CMA[SVBI - Lag] / CMAx[SVBI - Lag - ExtraF];
            UBOS = CMAx * (1 + Percent / 100); 
            LBOS = CMAx * (1 - Percent / 100); 
        }

        k = 0;

        for (j = 1; j <= eAuto; j++)
        {   
            k = j;
            AdjBar = 0;

            for (i = SVBI - ExtraF; i >= SVBI - ExtraF - Lag * eLFac; i--)
            { 
                if (i + ExtraF <= LVBI) 
                { 
                    if (UBOS[i] < (H[i + ExtraF] + L[i + ExtraF]) / 2)
                    { 
                        AdjBar = i + ExtraF;
                        Target = CMAx[i] + (C[i + ExtraF] - CMAx[i]) * eFFac;
                    } 
                    if (LBOS[i] > (H[i + ExtraF] + L[i + ExtraF]) / 2)
                    { 
                        AdjBar = i + ExtraF;
                        Target = CMAx[i] - (CMAx[i] - C[i + ExtraF]) * eFFac;
                    } 
                    if (AdjBar > 0)
                        i = 0;
                } 
            }

            if (AdjBar > 0)
            {
                ExtraB = AdjBar - EndBar;
                ExtraF = ExtraF - ExtraB;
                EndBar = EndBar + ExtraB;

                StepSize = (Target - CMA[SVBI - Lag]) / (EndBar - (SVBI -
Lag));

                CMA = IIf(BI <= SVBI - Lag, CMA, 0);

                for (i = SVBI - Lag + 1; i <= EndBar; i++)
                {
                    CMA[i] = CMA[i - 1] + StepSize;
                }

                CMAx = PolyFit(CMA, BegBar, EndBar, ePOrd, 0, ExtraF);
                CMAx = IIf(BI >= SVBI - Lag - ExtraF, CMAx, -1e10);

                if (BType == "Amount")
                {
                    CMAx = CMAx + CMA[SVBI - Lag] - CMAx[SVBI - Lag - ExtraF];
                    UBOS = CMAx + Percent; 
                    LBOS = CMAx - Percent;
                }
                else
                {
                    CMAx = CMAx * CMA[SVBI - Lag] / CMAx[SVBI - Lag - ExtraF];
                    UBOS = CMAx * (1 + Percent / 100); 
                    LBOS = CMAx * (1 - Percent / 100); 
                }
            }
            else 
                j = 999;
        }

        k = k - 1;

        Plot(UBOS, "", ColorRGB(Red, Green, Blue), styleThick, Null, Null,
ExtraF);
        Plot(LBOS, "", ColorRGB(Red, Green, Blue), styleThick, Null, Null,
ExtraF);

        Title = Title + EncodeColor(ColorRGB(Red, Green, Blue)) +
NumToStr(Period, 1.0) + " (" + NumToStr(Pm, 1.0) + "/" + NumToStr(Pn, 1.0) + ")
" + NumToStr(k, 1.0) + "     ";

        PlotShapes((BI == SVBI - Lag) * shapeSmallUpTriangle,   ColorRGB(Red,
Green, Blue), 0, CMA * (1 - Percent / 100), -15); 
        PlotShapes((BI == SVBI - Lag) * shapeSmallDownTriangle, ColorRGB(Red,
Green, Blue), 0, CMA * (1 + Percent / 100), -15); 

    } 
    return;
}
if(HT)
{
PlotOHLC(O, H, L, C, "Data", colorWhite, styleBar | styleThick); 
if (p1 > 0)
    CMA_DE(p1, Pct1, 000, 160, 000);
if (p2 > 0)
    CMA_DE(p2, Pct2, 160, 112, 000);
if (p3 > 0)
    CMA_DE(p3, Pct3, 160, 000, 000);
if (p4 > 0)
    CMA_DE(p4, Pct4, 160, 000, 160);
if (p5 > 0)
    CMA_DE(p5, Pct5, 000, 000, 160);
if (p6 > 0)
    CMA_DE(p6, Pct6,   0, 160, 160);
}
_SECTION_END();

_SECTION_BEGIN(" DESTEK DİRENÇ");

DDG = ParamToggle("Destek Direnç Göstermek ","hide|show",0);
chartflag = ParamToggle("Heikin Ashi","show|hide",1);

xx = Cum(1);
nbar = Param("nbar",5,2,50,1); 
if(DDG)
{
PHigh = H > Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar) <= H;
PHighPrice = ValueWhen(PHigh,H);
PLow = L < Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar) >= L;
PLowPrice = ValueWhen(PLow,L); 

startval_L = ValueWhen(PLow,L,1); 
endval_L = ValueWhen(PLow,L,0); 
startbar_L = ValueWhen(PLow,xx,1); 
endbar_L = ValueWhen(PLow,xx,0); 
aa_L = (endval_L-startval_L)/(endbar_L-startbar_L);
bb_L = startval_L;
trendline_L = aa_L * (xx - startbar_L) + bb_L;
dtrendline_L = trendline_L - Ref(trendline_L,-1);

startval_L_extend = ValueWhen(PLow,L,2); 
endval_L_extend = ValueWhen(PLow,L,1); 
startbar_L_extend = ValueWhen(PLow,xx,2); 
endbar_L_extend = ValueWhen(PLow,xx,1); 
aa_L_extend = (endval_L_extend-startval_L_extend)/(endbar_L_extend-startbar_L_extend);
bb_L_extend = startval_L;
trendline_L_extend = aa_L_extend * (xx - startbar_L) + endval_L_extend; 

dtrendline_L_extend = trendline_L_extend - Ref(trendline_L_extend,-1);
dtrendline_L_extend = IIf(PLow,Ref(dtrendline_L,-1),dtrendline_L_extend);

startval_L_extend2 = ValueWhen(PLow,L,3); 
endval_L_extend2 = ValueWhen(PLow,L,2); 
startbar_L_extend2 = ValueWhen(PLow,xx,3); 
endbar_L_extend2 = ValueWhen(PLow,xx,2); 
aa_L_extend2 = (endval_L_extend2-startval_L_extend2)/(endbar_L_extend2-startbar_L_extend2);
bb_L_extend2 = endval_L_extend2;
trendline_L_extend2 = aa_L_extend2 * (xx - endbar_L_extend2) + endval_L_extend2; 

dtrendline_L_extend2 = trendline_L_extend2 - Ref(trendline_L_extend2,-1);
dtrendline_L_extend2 = IIf(PLow,Ref(dtrendline_L_extend,-1),dtrendline_L_extend2);

startval_H = ValueWhen(PHigh,H,1); 
endval_H = ValueWhen(PHigh,H,0); 
startbar_H = ValueWhen(PHigh,xx,1); 
endbar_H = ValueWhen(PHigh,xx,0); 
aa_H = (endval_H-startval_H)/(endbar_H-startbar_H);
bb_H = startval_H;
trendline_H = aa_H * (xx - startbar_H) + bb_H; 
dtrendline_H = trendline_H - Ref(trendline_H,-1);

startval_H_extend = ValueWhen(PHigh,H,2); 
endval_H_extend = ValueWhen(PHigh,H,1); 
startbar_H_extend = ValueWhen(PHigh,xx,2); 
endbar_H_extend = ValueWhen(PHigh,xx,1); 
aa_H_extend = (endval_H_extend-startval_H_extend)/(endbar_H_extend-startbar_H_extend);
bb_H_extend = startval_H;
trendline_H_extend = aa_H_extend * (xx - startbar_H) + endval_H_extend; 
dtrendline_H_extend = trendline_H_extend - Ref(trendline_H_extend,-1);
dtrendline_H_extend = IIf(PHigh,Ref(dtrendline_H,-1),dtrendline_H_extend);

startval_H_extend2 = ValueWhen(PHigh,H,3); 
endval_H_extend2 = ValueWhen(PHigh,H,2); 
startbar_H_extend2 = ValueWhen(PHigh,xx,3); 
endbar_H_extend2 = ValueWhen(PHigh,xx,2); 
aa_H_extend2 = (endval_H_extend2-startval_H_extend2)/(endbar_H_extend2-startbar_H_extend2);
bb_H_extend2 = endval_H_extend2;
trendline_H_extend2 = aa_H_extend2 * (xx - endbar_H_extend2) + endval_H_extend2; 

dtrendline_H_extend2 = trendline_H_extend2 - Ref(trendline_H_extend2,-1);
dtrendline_H_extend2 = IIf(PHigh,Ref(dtrendline_H_extend,-1),dtrendline_H_extend2);

tld = ParamToggle("All trendlines","show|hide",1);
if (tld)
{
 
 trendline_L = IIf(dtrendline_L > 0,trendline_L,Null);
 trendline_L_extend = IIf(dtrendline_L_extend > 0,trendline_L_extend,Null);
 trendline_L_extend2 = IIf(dtrendline_L_extend2 > 0,trendline_L_extend2,Null);
 trendline_H = IIf(dtrendline_H < 0,trendline_H,Null);
 trendline_H_extend = IIf(dtrendline_H_extend < 0,trendline_H_extend,Null);
 trendline_H_extend2 = IIf(dtrendline_H_extend2 < 0,trendline_H_extend2,Null);
}

trendline_L_extend2 = IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null);
trendline_H_extend2 = IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null);

GraphXSpace = 5;
SetChartOptions(0, chartShowDates);

if (chartFlag)
{
 PlotOHLC(O, H, L, C, "Data", colorWhite, styleBar | styleThick); 
} 
else
{
 HaClose = (O+H+L+C)/4; 
 HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );  
 HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
 HaLow = Min( L, Min( HaClose, HaOpen ) ); 
 PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "Modified " + Name(), colorWhite, styleBar); 
}

PlotShapes(shapeUpArrow*PLow,colorYellow,0,L,-20);
PlotShapes(shapeDownArrow*PHigh,colorWhite,0,H,-20);


Plot(trendline_L, "", colorLime,styleLine|styleThick);

Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend,Null), "", colorLightGrey, styleLine | styleThick);
Plot(IIf(BarsSince(Plow) > nbar,trendline_L_extend,Null), "",colorBrightGreen,  styleLine | styleThick);

Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null), "",colorDarkGreen, styleLine| styleThick);

Plot(trendline_H, "", colorRed,styleLine|styleThick);

Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend,Null), "", colorLightGrey, styleLine | styleThick);
Plot(IIf(BarsSince(PHigh) > nbar,trendline_H_extend,Null), "",colorOrange, styleLine | styleThick);

Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null), "",colorOrange, styleLine | styleThick);


signalFlag = ParamToggle("Possible Signals","show|hide",1);

if (signalFlag)
{

Buy = (!IsEmpty(trendline_H_extend) AND Cross(C,trendline_H_extend) AND BarsSince(PHigh) > nbar) OR 
(!IsEmpty(trendline_H_extend2) AND Cross(C,trendline_H_extend2) AND !PHigh) OR
(PHigh AND C > trendline_H_extend2 AND Ref(C,-1) < Ref(trendline_H_extend,-1) AND !IsEmpty(trendline_H_extend) AND !IsEmpty(trendline_H_extend2) );
BuyPrice = C;
Short = (!IsEmpty(trendline_L_extend) AND Cross(trendline_L_extend,C)  AND BarsSince(PLow) > nbar)  OR 
(!IsEmpty(trendline_L_extend2) AND Cross(trendline_L_extend2,C) AND !PLow) OR
(PLow AND C < trendline_L_extend2 AND Ref(C,-1) > Ref(trendline_L_extend,-1) AND !IsEmpty(trendline_L_extend) AND !IsEmpty(trendline_L_extend2)); 
ShortPrice = C;
Sell = 0;
Cover = 0;


}

_SECTION_END();

_SECTION_BEGIN(" EXPLORE");

AL=(!IsEmpty(trendline_H_extend) AND Cross(C,trendline_H_extend) AND BarsSince(PHigh) > nbar) OR 
(!IsEmpty(trendline_H_extend2) AND Cross(C,trendline_H_extend2) AND !PHigh) OR
(PHigh AND C > trendline_H_extend2 AND Ref(C,-1) < Ref(trendline_H_extend,-1) AND !IsEmpty(trendline_H_extend) AND !IsEmpty(trendline_H_extend2) );
BuyPrice = C;

SAT=(!IsEmpty(trendline_L_extend) AND Cross(trendline_L_extend,C)  AND BarsSince(PLow) > nbar)  OR 
(!IsEmpty(trendline_L_extend2) AND Cross(trendline_L_extend2,C) AND !PLow) OR
(PLow AND C < trendline_L_extend2 AND Ref(C,-1) > Ref(trendline_L_extend,-1) AND !IsEmpty(trendline_L_extend) AND !IsEmpty(trendline_L_extend2)); 
ShortPrice = C;

AL_status=WriteIf(AL,"Alış Yap"," ");

SAT_status=WriteIf(SAT,"Satış Yap"," ");

AL_col=IIf(AL, colorDarkGreen , colorWhite);

SAT_col=IIf(SAT, colorRed,colorWhite);


Filter= AL OR SAT;

AddColumn(C, "KAPANIŞ", 1.2, IIf(C > Ref(C,-1),colorBlue, colorRed));
AddTextColumn(AL_status, "ALIŞ", 1.2, colorWhite, Al_col);
AddTextColumn(SAT_status, "SATIŞ", 1.2, colorWhite, SAT_col);
}

_SECTION_END();

_SECTION_BEGIN("Parabolic Sar");

acc=Param("Acceleration factor",0.02,0.01,0.05,0.01);
af_start=Param("Starting AF value",0.02,0.01,0.05,0.01);
af_max=Param("Maximum AF value",0.2,0.1,0.3,0.01);
Ct=Param("Crossover threshold in %",1,0,3,0.5);

PS = ParamToggle(" Parabolic Sar Göstermek ","hide|show",0);

Ct1=Ct/100;

IAF = acc; 
MaxAF = af_max;
psar = Close; 
long = 1;
af = af_start;
ep = Low[ 0 ];
hp = High [ 0 ];
lp = Low [ 0 ];

for( i = 2; i < BarCount; i++ )
{
 if ( long )
 {
  psar [ i ] = psar [ i-1 ] + af * ( hp - psar [ i-1 ] );
 }
 else
 {
  psar [ i ] = psar [ i-1 ] + af * ( lp - psar [ i-1 ] );
 }

 reverse =  0;

 if ( long )
 {
  if ( Low [ i ] < psar [ i ] * (1-Ct1) )
  {
   long = 0; reverse = 1;
   psar [ i ] =  hp;
   lp = Low [ i ];
   af = af_start;
  }
 }
 else
 {
  if ( High [ i ] > psar [ i ] * (1+Ct1) )
  {
   long = 1; reverse = 1; 
   psar [ i ] =  lp;
   hp = High [ i ];
   af = af_start;
  }
 }

 if ( reverse == 0 )
 {
  if ( long )
  {
   if ( High [ i ] > hp ) 
   {
    hp = High [ i ]; 
    af = af + IAF; 
    if( af > MaxAF ) af = MaxAF; 
   }
             
   if( Low[ i - 1 ] < psar[ i ] ) psar[ i ] = Low[ i - 1 ];
   if( Low[ i - 2 ] < psar[ i ] ) psar[ i ] = Low[ i - 2 ];
  }
       else
  {
   if ( Low [ i ] < lp )  
   { 
    lp = Low [ i ]; 
    af = af + IAF; 
    if( af > MaxAF ) af = MaxAF; 
   } 
    
   if( High[ i - 1 ] > psar[ i ] ) psar[ i ] = High[ i - 1 ];
   if( High[ i - 2 ] > psar[ i ] ) psar[ i ] = High[ i - 2 ];

  }
 }
}

if(PS)
{
Plot( psar, _DEFAULT_NAME(), ParamColor( "Color", colorYellow), styleDots | styleNoLine | styleThick ); 
PlotOHLC(O, H, L, C, "Data", colorWhite, styleBar | styleThick); 
}
_SECTION_END();

POLYNOMIAL for Amibroker (AFL)









_SECTION_BEGIN("nth ( 1 - 8)");


// *********************************************************
// *
// * VBS Function for Gaussian Elimination
// *
// *     Called by PolyFit ( AFL )
// *
// *********************************************************

EnableScript("VBScript");

<%
function Gaussian_Elimination (GE_Order, GE_N, GE_SumXn, GE_SumYXn)
    Dim b(10, 10)
    Dim w(10)
    Dim Coeff(10)

    for i = 1 To 10
        Coeff(i) = 0
    next

    n = GE_Order + 1

    for i = 1 to n
        for j = 1 to  n
            if i = 1 AND j = 1 then
                b(i, j) = cDBL(GE_N)
            else
                b(i, j) = cDbl(GE_SumXn(i + j - 2))
            end if
        next     
        w(i) = cDbl(GE_SumYXn(i))
    next

    n1 = n - 1
    for i = 1 to n1
        big = cDbl(abs(b(i, i)))
        q = i
        i1 = i + 1

        for j = i1 to n
            ab = cDbl(abs(b(j, i)))
            if (ab >= big) then
                big = ab
                q = j
            end if
        next

        if (big <> 0.0) then
            if (q <> i) then
                for j = 1 to n
                    Temp = cDbl(b(q, j))
                    b(q, j) = b(i, j)
                    b(i, j) = Temp
                next
                Temp = w(i)
                w(i) = w(q)
                w(q) = Temp
            end if
        end if

        for j = i1 to n
            t = cDbl(b(j, i) / b(i, i))
            for k = i1 to n
                b(j, k) = b(j, k) - t * b(i, k)
            next        
            w(j) = w(j) - t * w(i)
        next     
    next

    if (b(n, n) <> 0.0) then

        Coeff(n) = w(n) / b(n, n)
        i = n - 1

        while i > 0
            SumY = cDbl(0)
            i1 = i + 1
            for j = i1 to n
                SumY = SumY + b(i, j) * Coeff(j)
            next
            Coeff(i) = (w(i) - SumY) / b(i, i)
            i = i - 1
        wend  

        Gaussian_Elimination = Coeff

    end if
end function
%>

// *********************************************************
// *
// * AFL Function for nth Order Polynomial Fit
// *     Calls Gaussian_Elimination ( VBS )
// *
// *     Y      = The array to Fit
// *     BegBar = Beg Bar in range to fit
// *     EndBar = End Bar in range to fit
// *     Order  = 1 - 8 = Order of Poly Fit (Integer)
// *     ExtraB = Number of Bars to Extrapolate (Backward)
// *     ExtraF = Number of Bars to Extrapolate (Forward)
// *
// *********************************************************

function PolyFit(GE_Y, GE_BegBar, GE_EndBar, GE_Order, GE_ExtraB, GE_ExtraF)
{
    BI        = BarIndex();

    GE_N      = GE_EndBar - GE_BegBar + 1;
    GE_XBegin = -(GE_N - 1) / 2;
    GE_X      = IIf(BI < GE_BegBar, 0, IIf(BI > GE_EndBar, 0, (GE_XBegin + BI - GE_BegBar)));

    GE_X_Max  = LastValue(Highest(GE_X));

    GE_X      = GE_X / GE_X_Max;

    X1 = GE_X;
    AddColumn(X1, "X1", 1.9);

    GE_Y      = IIf(BI < GE_BegBar, 0, IIf(BI > GE_EndBar, 0, GE_Y));

    GE_SumXn  = Cum(0);
                                 GE_SumXn[1]   = LastValue(Cum(GE_X));
    GE_X2     = GE_X * GE_X;     GE_SumXn[2]   = LastValue(Cum(GE_X2));
    GE_X3     = GE_X * GE_X2;    GE_SumXn[3]   = LastValue(Cum(GE_X3));
    GE_X4     = GE_X * GE_X3;    GE_SumXn[4]   = LastValue(Cum(GE_X4));
    GE_X5     = GE_X * GE_X4;    GE_SumXn[5]   = LastValue(Cum(GE_X5));
    GE_X6     = GE_X * GE_X5;    GE_SumXn[6]   = LastValue(Cum(GE_X6));
    GE_X7     = GE_X * GE_X6;    GE_SumXn[7]   = LastValue(Cum(GE_X7));
    GE_X8     = GE_X * GE_X7;    GE_SumXn[8]   = LastValue(Cum(GE_X8));
    GE_X9     = GE_X * GE_X8;    GE_SumXn[9]   = LastValue(Cum(GE_X9));
    GE_X10    = GE_X * GE_X9;    GE_SumXn[10]  = LastValue(Cum(GE_X10));
    GE_X11    = GE_X * GE_X10;   GE_SumXn[11]  = LastValue(Cum(GE_X11));
    GE_X12    = GE_X * GE_X11;   GE_SumXn[12]  = LastValue(Cum(GE_X12));
    GE_X13    = GE_X * GE_X12;   GE_SumXn[13]  = LastValue(Cum(GE_X13));
    GE_X14    = GE_X * GE_X13;   GE_SumXn[14]  = LastValue(Cum(GE_X14));
    GE_X15    = GE_X * GE_X14;   GE_SumXn[15]  = LastValue(Cum(GE_X15));
    GE_X16    = GE_X * GE_X15;   GE_SumXn[16]  = LastValue(Cum(GE_X16));

    GE_SumYXn = Cum(0);
                                 GE_SumYXn[1]  = LastValue(Cum(GE_Y));
    GE_YX     = GE_Y    * GE_X;  GE_SumYXn[2]  = LastValue(Cum(GE_YX));
    GE_YX2    = GE_YX   * GE_X;  GE_SumYXn[3]  = LastValue(Cum(GE_YX2));
    GE_YX3    = GE_YX2  * GE_X;  GE_SumYXn[4]  = LastValue(Cum(GE_YX3));
    GE_YX4    = GE_YX3  * GE_X;  GE_SumYXn[5]  = LastValue(Cum(GE_YX4));
    GE_YX5    = GE_YX4  * GE_X;  GE_SumYXn[6]  = LastValue(Cum(GE_YX5));
    GE_YX6    = GE_YX5  * GE_X;  GE_SumYXn[7]  = LastValue(Cum(GE_YX6));
    GE_YX7    = GE_YX6  * GE_X;  GE_SumYXn[8]  = LastValue(Cum(GE_YX7));
    GE_YX8    = GE_YX7  * GE_X;  GE_SumYXn[9]  = LastValue(Cum(GE_YX8));

    GE_Coeff  = Cum(0);

    GE_VBS    = GetScriptObject();
    GE_Coeff  = GE_VBS.Gaussian_Elimination(GE_Order, GE_N, GE_SumXn, GE_SumYXn);

    for (i = 1; i <= GE_Order + 1; i++)
        printf(NumToStr(i, 1.0) + " = " + NumToStr(GE_Coeff[i], 1.9) + "\n");

    GE_X   = IIf(BI < GE_BegBar - GE_ExtraB - GE_ExtraF, 0, IIf(BI > GE_EndBar, 0, (GE_XBegin + BI - GE_BegBar + GE_ExtraF) / GE_X_Max));

    GE_X2  = GE_X   * GE_X; GE_X3  = GE_X2  * GE_X; GE_X4  = GE_X3  * GE_X; GE_X5  = GE_X4  * GE_X; GE_X6  = GE_X5  * GE_X;
    GE_X7  = GE_X6  * GE_X; GE_X8  = GE_X7  * GE_X; GE_X9  = GE_X8  * GE_X; GE_X10 = GE_X9  * GE_X; GE_X11 = GE_X10 * GE_X;
    GE_X12 = GE_X11 * GE_X; GE_X13 = GE_X12 * GE_X; GE_X14 = GE_X13 * GE_X; GE_X15 = GE_X14 * GE_X; GE_X16 = GE_X15 * GE_X;

    GE_Yn = IIf(BI < GE_BegBar - GE_ExtraB - GE_ExtraF, -1e10, IIf(BI > GE_EndBar, -1e10,
            GE_Coeff[1]  +
            GE_Coeff[2]  * GE_X   + GE_Coeff[3]  * GE_X2  + GE_Coeff[4]  * GE_X3  + GE_Coeff[5]  * GE_X4  + GE_Coeff[6]  * GE_X5  +
            GE_Coeff[7]  * GE_X6  + GE_Coeff[8]  * GE_X7  + GE_Coeff[9]  * GE_X8));

    return GE_Yn;
}

// *********************************************************
// *
// * Demo AFL to use PolyFit
// *
// *********************************************************

Filter = 1;

BI        = BarIndex();
PF_BegBar = BeginValue(BI);
PF_EndBar = EndValue(BI);
PF_Y      = (H + L) / 2;
PF_Order  = Param("nth Order",             3, 0,  8, 1);
PF_ExtraB = Param("Extrapolate Backwards", 0, 0, 50, 1);
PF_ExtraF = Param("Extrapolate Forwards",  0, 0, 50, 1);

Yn = PolyFit(PF_Y, PF_BegBar, PF_EndBar, PF_Order, PF_ExtraB, PF_ExtraF);

GraphXSpace = 10;

Plot(Yn, "nth Order Polynomial Fit - " + NumToStr(PF_Order, 1.0), IIf(BI > PF_EndBar - PF_ExtraF, colorWhite, IIf(BI < PF_BegBar - PF_ExtraF, colorWhite, colorBrightGreen)), styleThick, Null, Null, PF_ExtraF);
PlotOHLC(O, H, L, C, "Close", colorLightGrey, styleCandle);

_SECTION_END();