Auto Channel Trading System with Plotting Volume
// asfatul.alam@yahoo.com
_SECTION_BEGIN("Chart Display");
GraphXSpace = 10;
SetBarsRequired( 1,1);
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(colorLightGrey);
SetBarFillColor( IIf( C>O, colorWhite, colorBlack ) );
Plot( C, "Close", ParamColor("Candle Color", colorBlack), styleCandle|styleNoTitle);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
_SECTION_END();
_SECTION_BEGIN("Volume");
Plot( Volume, _DEFAULT_NAME(), ParamColor("Color", colorGrey50), ParamStyle( "Style", styleHistogram | styleOwnScale, maskHistogram), 2 );
_SECTION_END();
_SECTION_BEGIN("Trading System");
x=Param("Short EMA",14,1,200,1);
y=Param("Long EMA", 21,1,200,1);
EMA1= EMA(C,x);
EMA2=EMA(C,y);
goldencross= Cross(EMA1, EMA2)AND C>EMA2 ;
deadcross= Cross(EMA2, EMA1);
_SECTION_END();
BuyCondition = goldencross ;
SellCondition = deadcross;
Buy = BuyCondition;
Short = SellCondition;
_SECTION_END();
_SECTION_BEGIN("Exploration");
PlotShapes(IIf(Short,shapeSmallUpTriangle,shapeNone),colorOrange,0,L,-20);
PlotShapes(IIf(Buy,shapeSmallUpTriangle,shapeNone),colorBlue,0,L,-25);
SetOption("NoDefaultColumns",True);
bkColour = IIf(BuyCondition ,colorGreen,colorRed);
txtColour = colorWhite;
AddTextColumn(Name() ," Ticker ",1.2,txtColour,bkColour,75);
AddTextColumn(FullName() ," Full Name ",1.2,txtColour,bkColour,100);
AddColumn(DateTime()," Date / Time ",formatDateTime,txtColour,bkColour,125);
AddColumn(C," Close ",1.2,txtColour,bkColour,75);
AddColumn(ROC( C,1)," % Change ",1.2,txtColour,bkColour,50);
AddColumn(V," Volume ",1.0,txtColour,bkColour,100);
Filter = Buy OR Short;
AddColumn( IIf( BuyCondition, 66, 83 ), "Buy/Sell?", formatChar,txtColour,bkColour,40);
Var = WriteIf(goldencross,"GoldenCross",WriteIf(deadcross,"DeadCross",""));
AddTextColumn( Var , "Remark", 1.2 , colorBlack, IIf( goldencross, colorLime,IIf(deadcross,colorOrange,colorWhite)),60);
_SECTION_END();
_SECTION_BEGIN("EMA3");
P = ParamField("Price field",-1);
Periods = Param("Periods", 50, 1, 300, 0, 0 );
Plot( EMA( P, Periods ), "", ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();
_SECTION_BEGIN("EMA4");
P = ParamField("Price field",-1);
Periods = Param("Periods", 20, 1, 300, 0,0 );
Plot( EMA( P, Periods ), "", ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();
_SECTION_BEGIN("parameters");
sdfact=Param("Standard Deviation Factor",2,0.5,5,0.1);
offset=Param("Offset",2,1,50,1);
tc=ParamList("Channel Display",List="No Channel|Channel|ChannelRT|Both Channels",1);
_SECTION_END();
function getUptrend(cp)
{
return IIf(cp>MA(cp,100),1,0);
}
function getDowntrend(cp)
{
return IIf(cp<=MA(cp,100),-1,0);
}
cp=(H+L)/2;
TrendUp=getUptrend(cp);
TrendDown=getDowntrend(cp);
totalTrend=IIf(TrendUp,TrendUp,TrendDown);
dtotalTrend=totalTrend-Ref(totalTrend,-1);
vtotalTrend=ValueWhen(dtotalTrend,dtotalTrend);
cbull=vtotalTrend>0 AND Ref(vtotalTrend,-1)<0;cbull=Ref(cbull,1);
cbear=vtotalTrend<0 AND Ref(vtotalTrend,-1)>0;cbear=Ref(cbear,1);
cbull=vtotalTrend>0 AND Ref(vtotalTrend,-1)<0;cbull=Ref(cbull,1);cbull[BarCount-1]=1;
cbear=vtotalTrend<0 AND Ref(vtotalTrend,-1)>0;cbear=Ref(cbear,1);cbear[BarCount-1]=1;
nwbull=Ref(Flip(cbull,cbear),-1);nwbear=Ref(Flip(cbear,cbull),-1);
xxbull=BarsSince(cbull);xxbull[BarCount-1]=xxbull[BarCount-2]+1;
aaabull=LinRegIntercept(cp,xxbull);bbbbull=LinRegSlope(cp,xxbull);
daabull=Ref(ValueWhen(cbear,aaabull,0),-1);dbbbull=Ref(ValueWhen(cbear,bbbbull,0),-1);
yybull=daabull+dbbbull*(xxbull-1);yybull=IIf(xxbull>Max(offset,1),yybull,Null);
wdbull=sdfact*StdErr(cp,xxbull);wdbull=Ref(ValueWhen(cbear,wdbull,0),-1);
xxbear=BarsSince(cbear);xxbear[BarCount-1]=xxbear[BarCount-2]+1;
aaabear=LinRegIntercept(cp,xxbear);bbbbear=LinRegSlope(cp,xxbear);
daabear=Ref(ValueWhen(cbull,aaabear,0),-1);dbbbear=Ref(ValueWhen(cbull,bbbbear,0),-1);
yybear=daabear+dbbbear*(xxbear-1);yybear=IIf(xxbear>Max(offset,1),yybear,Null);
wdbear=sdfact*StdErr(cp,xxbear);wdbear=Ref(ValueWhen(cbull,wdbear,0),-1);
llbull=LinearReg(cp,xxbull);llbull=IIf(xxbull>Max(Offset,1),llbull,Null);
llbullp=LinearReg(cp,xxbull)+sdfact*StdErr(cp,xxbull);llbullp=IIf(xxbull>Max(Offset,2),llbullp,Null);
llbullm=LinearReg(cp,xxbull)-sdfact*StdErr(cp,xxbull);llbullm=IIf(xxbull>Max(Offset,2),llbullm,Null);
llbear=LinearReg(cp,xxbear);llbear=IIf(xxbear>Max(Offset,1),llbear,Null);
llbearp=LinearReg(cp,xxbear)+sdfact*StdErr(cp,xxbear);llbearp=IIf(xxbear>Max(Offset,2),llbearp,Null);
llbearm=LinearReg(cp,xxbear)-sdfact*StdErr(cp,xxbear);llbearm=IIf(xxbear>Max(Offset,2),llbearm,Null);
_SECTION_BEGIN("Chart Colors");
SetChartBkColor(ParamColor("Background Color",colorBlack));
SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color",colorGreen),IIf(C<=O,ParamColor("Candle Down Color",colorRed),colorLightGrey)));
Plot(C,"Price",IIf(C>O,ParamColor("Wick UP Color",colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color",colorDarkRed),colorLightGrey)),64,0,0,0,0);
switch(tc)
{
case("No Channel"):
break;
case("Channel"):
Plot(IIf(nwbull,yybull,Null),"",bbb=ColorRGB(0,255,0),styleDashed);
Plot(IIf(nwbull,yybull+wdbull,Null),"",bbb,styleThick);
Plot(IIf(nwbull,yybull-wdbull,Null),"",bbb,styleThick);
Plot(IIf(nwbear,yybear,Null),"",sss=ColorRGB(255,0,0),styleDashed);
Plot(IIf(nwbear,yybear+wdbear,Null),"",sss,styleThick);
Plot(IIf(nwbear,yybear-wdbear,Null),"",sss,styleThick);
PlotOHLC(IIf(nwbull,yybull+wdbull,Null),IIf(nwbull,yybull+wdbull,Null),IIf(nwbull,yybull-wdbull,Null),
IIf(nwbull,yybull-wdbull,Null),"",ColorRGB(10,15,10),styleCloud|styleNoLabel,0,0,0,-1);
PlotOHLC(IIf(nwbear,yybear+wdbear,Null),IIf(nwbear,yybear+wdbear,Null),IIf(nwbear,yybear-wdbear,Null),
IIf(nwbear,yybear-wdbear,Null),"",ColorRGB(15,10,10),styleCloud|styleNoLabel,0,0,0,-1);
break;
case("ChannelRT"):
Plot(IIf(nwbull,llbull,Null),"",colorGreen,styleDashed,0,1,0,1);
Plot(IIf(nwbull,llbullp,Null),"",colorGreen,styleDashed,0,1,0,1);
Plot(IIf(nwbull,llbullm,Null),"",colorGreen,styleDashed,0,1,0,1);
Plot(IIf(nwbear,llbear,Null),"",colorRed,styleDashed,0,1,0,1);
Plot(IIf(nwbear,llbearp,Null),"",colorRed,styleDashed,0,1,0,1);
Plot(IIf(nwbear,llbearm,Null),"",colorRed,styleDashed,0,1,0,1);
PlotOHLC(IIf(nwbull,llbullp,Null),IIf(nwbull,llbullp,Null),IIf(nwbull,llbullm,Null),
IIf(nwbull,llbullm,Null),"",ColorRGB(10,15,10),styleCloud|styleNoLabel,0,0,0,-1);
PlotOHLC(IIf(nwbear,llbearp,Null),IIf(nwbear,llbearp,Null),IIf(nwbear,llbearm,Null),
IIf(nwbear,llbearm,Null),"",ColorRGB(15,10,10),styleCloud|styleNoLabel,0,0,0,-1);
break;
case("Both Channels"):
Plot(IIf(nwbull,yybull,Null),"",bbb=ColorRGB(0,255,0),styleLine);
Plot(IIf(nwbull,yybull+wdbull,Null),"",bbb,styleThick);
Plot(IIf(nwbull,yybull-wdbull,Null),"",bbb,styleThick);
Plot(IIf(nwbear,yybear,Null),"",sss=ColorRGB(255,0,0),styleLine);
Plot(IIf(nwbear,yybear+wdbear,Null),"",sss,styleThick);
Plot(IIf(nwbear,yybear-wdbear,Null),"",sss,styleThick);
Plot(IIf(nwbull,llbull,Null),"",colorGreen,styleDashed,0,1,0,1);
Plot(IIf(nwbull,llbullp,Null),"",colorGreen,styleDashed,0,1,0,1);
Plot(IIf(nwbull,llbullm,Null),"",colorGreen,styleDashed,0,1,0,1);
Plot(IIf(nwbear,llbear,Null),"",colorRed,styleDashed,0,1,0,1);
Plot(IIf(nwbear,llbearp,Null),"",colorRed,styleDashed,0,1,0,1);
Plot(IIf(nwbear,llbearm,Null),"",colorRed,styleDashed,0,1,0,1);
break;
}
_SECTION_END();
No comments:
Post a Comment
Thanks