// thanks to happy ji and pratap ji for the valuable contribution
//test happy and pivots.afl
_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", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("Visual Pivots");
ro = ParamField("Open", 0) ;
rh = ParamField("High", 1) ;
rl = ParamField("Low", 2) ;
rc = ParamField("Close", 3) ;
P = (rH+rL+rC)/3 ;
p1 = Param("Fast", 1, 1, 100, 1) ;
p2 = Param("Slow", 3, 1, 100, 1) ;
usebpvts = ParamToggle("Use Body Pvts", "No|Yes", 0) ;
isemapvts = ParamToggle("Pvt Type", "H-L|EMA", 1) ;
ismph = H-H ;
ismpl = L-L ;
bph = Max(rO,rC);
bpl = Min(rO,rC);
if (isemapvts)
{
EMA1 = EMA(P, p1) ;
EMA2 = EMA(P, p2) ;
upcross = Cross(EMA1, EMA2) ;
downcross = Cross(EMA2, EMA1) ;
//bph = Max(O,C);
//bpl = Min(O,C);
bi = BarIndex() ;
mphbar = HighestSinceBars(upcross, rH) ;
mplbar = LowestSinceBars(downcross, rL) ;
mphbi = ValueWhen(downcross, bi, 0) - ValueWhen(downcross, mphbar, 0) ;
ismph = mphbi == bi ;
mplbi = ValueWhen(upCross, bi, 0) - ValueWhen(upCross, mplbar, 0) ;
ismpl = mplbi == bi ;
ismph = IIf(downCross AND mphbar == 0, True, ismph) ;
ismpl = IIf(upcross AND mplbar == 0, True, ismpl) ;
bmphbar = HighestSinceBars(upcross, bph) ;
bmplbar = LowestSinceBars(downcross, bpl) ;
bmphbi = ValueWhen(downcross, bi, 0) - ValueWhen(downcross, bmphbar, 0) ;
isbmph = bmphbi == bi ;
bmplbi = ValueWhen(upCross, bi, 0) - ValueWhen(upCross, bmplbar, 0) ;
isbmpl = bmplbi == bi ;
isbmph = IIf(downCross AND bmphbar == 0, True, isbmph) ;
isbmpl = IIf(upcross AND bmplbar == 0, True, isbmpl) ;
}
else
{
isbmph = ismph = rH > Ref(rH, -1) AND rH > Ref(rH, 1) ;
isbmpl = ismpl = rL < Ref(rL, -1) AND rL < Ref(rL, 1) ;
}
showminor = ParamToggle("Show Minor Pvts", "No|Yes", 1) ;
issquare = ParamToggle("Pvt style", "Circle|Square", 0) ;
Clrmph = ParamColor("MPH Color", colorGreen) ;
Clrmpl = ParamColor("MPL Color", colorBrown) ;
pvtmarker = IIf(issquare, shapeSmallSquare, shapeSmallCircle) ;
if (showminor)
{
if (usebpvts)
{
PlotShapes(isbmph*pvtmarker , Clrmph , 0, rH, 12) ;
PlotShapes(isbmpl*pvtmarker , Clrmpl, 0, rL, -12) ;
}
else
{
PlotShapes(ismph*pvtmarker , Clrmph , 0, rH, 12) ;
PlotShapes(ismpl*pvtmarker , Clrmpl, 0, rL, -12) ;
}
}
showminorlvls = ParamToggle("Show Minor Levels", "No|Yes", 0) ;
mph = ValueWhen(ismph, rH) ;
mpl = ValueWhen(ismpl, rL) ;
if (showminorlvls)
{
x = LineArray(0, SelectedValue(mph), (BarCount-1), SelectedValue(mph));
Plot(x, "", colorGold, styleDashed);
x = LineArray(0, SelectedValue(mpl), (BarCount-1), SelectedValue(mpl));
Plot(x, "", colorGold, styleDashed);
}
showminortrend = ParamToggle("Show Minor Trend", "No|Yes", 0) ;
if (showminortrend)
{
// initialize minor levels
mph = ValueWhen(ismph, rH) ;
mpl = ValueWhen(ismpl, rL) ;
mbph = ValueWhen(isbmph, bph) ;
mbpl = ValueWhen(isbmpl, bpl) ;
// minor trend
mut = rH > mph OR (rC > mbph) OR (ismph AND rH > Ref(mph, -1));
mdt = rL < mpl OR (rC < mbpl) OR (ismpl AND rL < Ref(mpl, -1));
mdt = Flip(mdt, mut) ;
mut = Flip(mut, mdt) ;
//mutst = ExRem(mut, mdt) ;
//mdtst = ExRem(mdt, mut) ;
Clr = IIf(mut, colorBlue, colorRed) ;
PlotOHLC(rO,rH,rL,rC, "", Clr, styleCandle|styleNoLabel);
Plot(4, "", Clr, styleArea|styleOwnScale, 0, 100, 0, -1) ;
}
tffactor = Param("TF Factor", 3, 1, 100, 1) ;
showhigher = ParamToggle("Show Higher", "No|Yes", 1) ;
//if (showhigher)
//{
tfs = tffactor *Interval() ;
TimeFrameSet(tfs) ;
if (isemapvts)
{
Ptf = (H+L+C)/3 ;
EMA1tf = EMA(Ptf, p1) ;
EMA2tf = EMA(Ptf, p2) ;
upcrosstf = Cross(EMA1tf, EMA2tf) ;
downcrosstf = Cross(EMA2tf, EMA1tf) ;
bphtf = Max(O,C) ;
bpltf = Min(O,C) ;
if (usebpvts)
{
Refhtf = bphtf ;
Refltf = bpltf ;
}
else
{
Refhtf = H ;
Refltf = L ;
}
mphbartf = HighestSinceBars(upcrosstf, Refhtf) ;
mplbartf = LowestSinceBars(downcrosstf, Refltf) ;
bitf = BarIndex() ;
mphbitf = ValueWhen(downcrosstf, bitf, 0) - ValueWhen(downcrosstf, mphbartf, 0) ;
ismphtf = mphbitf == bitf ;
mplbitf = ValueWhen(upCrosstf, bitf, 0) - ValueWhen(upCrosstf, mplbartf, 0) ;
ismpltf = mplbitf == bitf ;
ismphtf = IIf(downCrosstf AND mphbartf == 0, True, ismphtf) ;
ismpltf = IIf(upcrosstf AND mplbartf == 0, True, ismpltf) ;
}
else
{
ismphtf = H > Ref(H, -1) AND H > Ref(H, 1) ;
ismpltf = L < Ref(L, -1) AND L < Ref(L, 1) ;
}
if (showminortrend)
{
// initialize minor levels
mphtf = ValueWhen(ismphtf, H) ;
mpltf = ValueWhen(ismpltf, L) ;
mbphtf = ValueWhen(ismphtf, bphtf) ;
mbpltf = ValueWhen(ismpltf, bpltf) ;
// minor trend
muttf = H > mphtf OR (C > mbphtf) OR (ismphtf AND H > Ref(mphtf, -1));
mdttf = L < mpltf OR (C < mbpltf) OR (ismpltf AND L < Ref(mpltf, -1));
mdttf = Flip(mdttf, muttf) ;
muttf = Flip(muttf, mdttf) ;
//mutst = ExRem(mut, mdt) ;
//mdtst = ExRem(mdt, mut) ;
Clrtf = IIf(muttf, colorBlue, colorRed) ;
// PlotOHLC(rO,rH,rL,rC, "", Clr, styleCandle|styleNoLabel);
Clrtfe = TimeFrameExpand(Clrtf, tfs, expandLast) ;
Plot(2, "htf", Clrtfe, styleArea|styleOwnScale, 0, 100) ;
}
ismphtfe = TimeFrameExpand(ismphtf, tfs, expandFirst) ;
ismpltfe = TimeFrameExpand(ismpltf, tfs, expandFirst) ;
He=TimeFrameExpand(H, tfs, expandFirst) ;
Le=TimeFrameExpand(L, tfs, expandFirst) ;
TimeFrameRestore() ;
ishtfmph = IIf(ismph == ismphtfe, 1, 0) ;
ishtfmpl = IIf(ismpl == ismpltfe, 1, 0) ;
PlotShapes(ismphtfe *shapeSmallSquare, Clrmph , 0, He, 12) ;
PlotShapes(ismpltfe *shapeSmallSquare, Clrmpl, 0, Le, -12) ;
showhtflvls = ParamToggle("Show HTF levels", "No|Yes", 1) ;
Clrhtflvl = colorPaleGreen ;
//mphhtf = ValueWhen(ismphtfe , He, 1) ;
//mplhtf = ValueWhen(ismpltfe , Le, 1) ;
//Plot(2, "", IIf(ismph , colorBlue, IIf(ismpl, colorRed, colorGrey40)), styleArea|styleOwnScale, 0, 100) ;
//Plot(4, "", IIf(ishtfmph , colorBlue, IIf(ishtfmpl , colorRed, colorGrey40)), styleArea|styleOwnScale, 0, 100) ;
if (showhtflvls )
{
mphhtf = ValueWhen(ismphtfe , He, 1) ;
mplhtf = ValueWhen(ismpltfe , Le, 1) ;
x = LineArray(0, SelectedValue(mphhtf), (BarCount-1), SelectedValue(mphhtf));
Plot(x, "", Clrhtflvl ,styleLine| styleDots);
x = LineArray(0, SelectedValue(mplhtf), (BarCount-1), SelectedValue(mplhtf));
Plot(x, "", Clrhtflvl ,styleLine| styleDots);
}
//Plot(2, "", IIf(ismphtfe , colorRed, IIf(ismpltfe , colorBlue, colorGrey40)), styleArea|styleOwnScale, 0, 100) ;
//Plot(4, "", IIf(ismph, colorRed, IIf(ismpl , colorBlue, colorGrey40)), styleArea|styleOwnScale, 0, 100) ;
_SECTION_END();
// Using Ratio 0-10 will make optimize loops impossiblly huge :)
// Range 0 to 1 makes it the type of - OFF / ON For every MA
// i.e 0 = Exclude & 1 = Include that type of MA
_SECTION_BEGIN("SUPER_MA");
R1 = Optimize("EMA RATIO",Param("EMA RATIO",1,0,10,1),0,1,1);
R2 = Optimize("DEMA RATIO",Param("DEMA RATIO",1,0,10,1),0,1,1);
R3 = Optimize("TEMA RATIO",Param("TEMA RATIO",1,0,10,1),0,1,1);
R4 = Optimize("WMA RATIO",Param("WMA RATIO",1,0,10,1),0,1,1);
R5 = Optimize("HMA RATIO",Param("HMA RATIO",1,0,10,1),0,1,1);
R6 = Optimize("MA RATIO",Param("MA RATIO",1,0,10,1),0,1,1);
R7 = Optimize("Wilder RATIO",Param("Wilder RATIO",1,0,10,1),0,1,1);
R8 = Optimize("LinReg RATIO",Param("LinReg RATIO",1,0,10,1),0,1,1);
s = R1+R2+R3+R4+R5+R6+R7+R8;
function SuperMA(P, n)
{
m1 = EMA(P,n); m2 = DEMA(P,n); m3 = TEMA(P,n);
m4 = WMA(P,n); m5 = HMA(P,n); m6 = MA(P,n);
m7 = Wilders(P,n); m8 = LinearReg(P,n);
m = (R1*m1+R2*m2+R3*m3+R4*m4+R5*m5+R6*m6+R7*m7+R8*m8)/s;
return m;
}
Len = Optimize("PERIOD",Param("PERIOD",60,2,99,1),25,90,5);
SMA_C = SuperMA(C,Len); SMA_O = SuperMA(O,Len);
SMA_H = SuperMA(H,Len); SMA_L = SuperMA(L,Len);
Col = IIf(SMA_C > SMA_O,colorBlue,colorRed);
Plot(SMA_H,"SMA_H",Col,styleThick);
Plot(SMA_L,"SMA_L",Col,styleThick);
_SECTION_END();
_SECTION_BEGIN("SYSTEM");
SetPositionSize(75,4);
Buy1 = SMA_C > SMA_O AND C > SMA_H; Short1 = SMA_C < SMA_O AND C < SMA_L;
Sell1 = SMA_C < SMA_O AND C < SMA_L; Cover1 = SMA_C > SMA_O AND C > SMA_H;
Sellpivot = ValueWhen(Buy1,Ref(mplhtf,-1)); Coverpivot = ValueWhen(short1,Ref(mphhtf,-1));
Buypivot = ValueWhen(Buy1,Ref(mphhtf,-1)); shortpivot = ValueWhen(short1,Ref(mplhtf,-1));
Buy = Buy1 AND Cross(C,Buypivot);
Short = Short1 AND Cross(Shortpivot,C);
Sell= Sell1 AND Cross(Sellpivot,C);
Cover = Cover1 AND Cross(C,Coverpivot);
Buy = ExRem(Buy,Sell); Short = ExRem(Short,Cover);
Sell = ExRem(Sell,Buy); Cover = ExRem(Cover,Short);
PlotShapes(Buy+2*Sell,colorWhite,0,IIf(Buy,L,H),-30);
_SECTION_END();