_SECTION_BEGIN("Background_Setting");
SetChartBkGradientFill( ParamColor("BgTop", colorBlack),
ParamColor("BgBottom", colorDarkGrey),ParamColor("TitleBack",colorGrey40) );
SetChartBkColor(ParamColor("Outer Panel",colorPaleBlue));
_SECTION_END();
Plot( Close, "", colorBlack, styleCandle, styleThick );
_SECTION_BEGIN("SingleMA");
SetChartOptions(0, chartShowDates | chartWrapTitle);
Type = ParamList("Average Type", "Wilders,SMA,EMA");
P = Param("Averaging Period", 20, 3, 100,1);
Q = Param("%Change", 1, 0.1, 10, 0.1);
BP = Param("BB Period", 20, 3, 100);
BW = Param("BB Width", 2, 0.5, 10, 0.5);
BBOption = ParamToggle("Plot BB", "No I Yes");
Report = ParamList("Trigs or Update or Tgt-SL?", "Triggers|Update|Tgt-SL");
if(Type == "Wilders") A = Wilders(C, P);
if(Type == "SMA") A = MA(C, P);
if(Type == "EMA") A = EMA(C, P);
SL = Max(LLV(L, 5), Trough(L, Q, 1));
Tgt = 2 * H - SL;
MeanPrice = Prec((O + C) / 2, 2);
Part = 100 * (H - A) / (H - L);
BBTop = BBandTop(C, BP, BW);
BBBot = BBandBot(C, BP, BW);
Buy = (Prec(C, 2) > Prec(A, 2)) AND Part > 70;
Sell = H < A;
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
Bought = Flip(Buy, Sell);
Sold = Flip(Sell, Buy);
NextTgt = ValueWhen(Buy, Tgt, 1);
for(i = 1; i < BarCount; i++)
{
"if(Bought AND NOT Buy)";
{
SL = Max(SL, SL[i - 1]);
if(C[i - 1] >= 0.9999 * NextTgt[i - 1]) NextTgt = Tgt[i - 1];
NextTgt = Max(NextTgt, NextTgt[i - 1]);
}
}
BuyDate = ValueWhen(Buy, Ref(DateTime(), 1), 1);
BuyPrice = ValueWhen(Buy, Ref(MeanPrice, 1), 1);
SellPrice = ValueWhen(Sell, Ref(MeanPrice, 1), 1);
TgtReached = IIf(Bought AND NOT Buy AND C >= 0.9999 * NextTgt, True, False);
SLHit = IIf(Bought AND NOT Buy AND C < SL, True, False);
SLHit = ExRem(SLHit, Buy);
//
if(TgtReached[BarCount - 1]) NextTgt[BarCount - 1] = 2 * H[BarCount -1] - SL[BarCount - 1];
//
if(Status("action") == actionIndicator)
{
Ttl = EncodeColor(colorTurquoise) + "Single MA system," + "\n"
+ WriteIf(Buy, EncodeColor(colorGreen) + "Buy Triggered Today, Buy this stock Tomorrow.","")
+ WriteIf(Sell, EncodeColor(colorRed) + "Sell Triggered Today, Sell This stock Tomorrow.", "")
+ EncodeColor(colorTan) + WriteIf(Bought AND NOT Buy, "Bought @ " + BuyPrice + ". "
+ "Target Price = " + NextTgt + ", Stop Loss = " + SL + ".\n"
+ WriteIf(TgtReached, "Target Reached. Next Target = " + Ref(NextTgt, 1) + ".\n", "")
+ EncodeColor(colorGold) + "Profit / Loss so far = " + Prec(100 * (C - BuyPrice) / BuyPrice, 2) + "%", "")
+ WriteIf(Sold AND NOT Sell, "Sold @ " + SellPrice + "\nProfit / Loss in Previous Trade = " + Prec(100 * (SellPrice - BuyPrice) / BuyPrice, 2) + "%", "");
_N(Title = StrFormat("{{NAME}} ({{INTERVAL}}), {{DATE}} ; {{OHLCX}}, V=%1.0f\n {{VALUES}}\n\n", V) + Ttl);
ChartStyle = ParamStyle("Chart Type", styleBar, maskAll);
//PlotOHLC(O, H, L, C, "", colorLightGrey, ChartStyle);
Plot(A, Type + "(" + P +")", colorRed, styleLine | styleThick);
Plot(IIf(Bought, NextTgt, Null), "Target", colorBlueGrey, styleLine);
Plot(SL, "Trail SL", colorTeal, styleLine);
//PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-20);
//PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-30);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorBlack, 0,L, Offset=-25);
//PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=20);
//PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=30);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorBlack, 0,H, Offset=-25);
if(BBOption) Plot(BBtop, "BB-Top", colorPink, styleLine);
if(BBOption) Plot(BBBot, "BB-Bot", colorPink, styleLine);
}
if((Status("action") == actionExplore) AND Report == "Triggers")
{
Filter = Buy OR Sell;
SetOption("NoDefaultColumns", True);
AddTextColumn(Name(), "Symbol", 77, colorDefault, colorDefault, 120);
AddColumn(DateTime(), "Trigger Date", formatDateTime);
AddColumn(IIf(Buy, 66, 83), "Signal", formatChar, colorYellow, IIf(Buy, colorGreen, colorRed));
AddColumn(C, "C. M. P.", 6.2);
AddColumn(IIf(Buy OR Bought, NextTgt, Null), "Target", 6.2);
AddColumn(IIf(Buy OR Bought, SL, Null), "StopLoss", 6.2);
}
if((Status("action") == actionExplore) AND Report == "Update")
{
Filter = True;
SetOption("NoDefaultColumns", True);
AddColumn(DateTime(), "Updated On", formatDateTime, colorDefault, colorDefault, 96);
AddTextColumn(Name(), "Symbol", 77, colorDefault, colorDefault, 120);
AddColumn(BuyDate, "Buy Date", formatDateTime, colorDefault, colorDefault, 96);
AddColumn(BuyPrice, "Buy Price", 6.2);
AddColumn(NextTgt, "Target", 6.2);
AddColumn(SL, "StopLoss", 6.2);
AddColumn(C, "CMP", 6.2, colorDefault, colorDefault, 96);
}
if((Status("action") == actionExplore) AND Report == "Tgt-SL")
{
Filter = TgtReached OR SLHit;
SetOption("NoDefaultColumns", True);
AddColumn(DateTime(), "Updated On", formatDateTime, colorDefault, colorDefault, 96);
AddTextColumn(Name(), "Symbol", 77, colorDefault, colorDefault, 120);
AddColumn(BuyDate, "Buy Date", formatDateTime, colorDefault, colorDefault, 96);
AddColumn(BuyPrice, "Buy Price", 6.2);
AddColumn(NextTgt, "Target", 6.2);
AddColumn(SL, "StopLoss", 6.2);
AddColumn(C, "CMP", 6.2, colorDefault, colorDefault, 96);
AddColumn(IIf(TgtReached, 89, 32), "Tgt Hit?", formatChar, colorYellow, IIf(TgtReached, colorGreen, colorDefault));
AddColumn(IIf(TgtReached, 2 * H - SL, Null), "Next Tgt", 1.2);
AddColumn(IIf(SLHit, 89, 32), "SL-Hit", formatChar, colorYellow, IIf(SLHit, colorRed, colorDefault));
}
dist = 2.0*ATR(20);
for( i = 0; i < BarCount; i++ )
{
//if( Buy[i] ) PlotText( "Buy\n@" + C[ i ]+"\n sl :"+ValueWhen( supbase==Base, LLV(L,HHVperiod) ) , i, L[ i ]-dist[i], colorBlack,colorGreen );
//if( Short[i] ) PlotText( "Sell\n@" + C[ i ]+"\n sl :"+ ValueWhen( ResBase==Base, HHV(H,HHVperiod) ), i, H[ i ]+dist[i], colorBlack, colorRed );
if( Buy[i] ) PlotText( "Buy\n@" + C[ i ] , i, L[ i ]-dist[i], colorBlack,colorWhite );
if( Sell[i] ) PlotText( "Sell\n@" + C[ i ], i, H[ i ]+dist[i], colorBlack, colorWhite );
}
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);
Buyplus1=(Bprice+150);
Buyplus2=(Bprice+200);
Buyplus3=(Bprice+250);
//Buyminus=(Bprice-40);
Plot(Buyplus1,"R1",colorBlue,styleDots|styleThick|styleNoTitle);
Plot(Buyplus2,"R1",colorBlue,styleDots|styleThick|styleNoTitle);
Plot(Buyplus3,"R1",colorBlue,styleDots|styleThick|styleNoTitle);
//Plot(Buyminus,"R2",colorBlue,styleDots|styleThick|styleNoTitle);
//Sellplus=(Sprice+40);
Sellminus1=(Sprice-150);
Sellminus2=(Sprice-200);
Sellminus3=(Sprice-250);
//Plot(Sellplus,"R3",colorBlack,styleDots|styleThick|styleNoTitle);
Plot(Sellminus1,"R4",colorBlack,styleDots|styleThick|styleNoTitle);
Plot(Sellminus2,"R4",colorBlack,styleDots|styleThick|styleNoTitle);
Plot(Sellminus3,"R4",colorBlack,styleDots|styleThick|styleNoTitle);
GfxSetBkMode(1);
GfxSelectPen( colorBlack, 1, 0) ;
GfxSelectSolidBrush( colorPink );
GfxRoundRect( 2, 15, 95, 35, 9, 9 ) ;
GfxSelectFont("romans", 9, 700 );
GfxTextOut(" A S N V", 5, 18);
GfxSetBkMode(1);
GfxSelectPen( colorBlack, 1, 0) ;
GfxSelectSolidBrush( colorPink );
GfxRoundRect( 100, 15, 200,42, 9, 9 ) ;
GfxSelectFont("romans", 18, 700 );
GfxTextOut( ""+C, 110, 13);
GfxSetBkMode(1);
GfxSelectPen( colorBlack, 1, 0) ;
GfxSelectSolidBrush( colorPink );
GfxRoundRect( 205, 15, 330, 35, 9, 9 ) ;
GfxSelectFont("romans", 9, 700 );
GfxTextOut("" +Date() , 210, 18);
//////////////////////////////////////////////////
// Calculate the Last Five Trades Profit/Losses //
//////////////////////////////////////////////////
tradesback = 5;
Signum = Cum( Buy ) + Cum( Sell );
Signumstart1 = LastValue( SigNum ) - ( tradesback - 1 );
Signumstart2 = LastValue( SigNum ) - ( tradesback - 2 );
Signumstart3 = LastValue( SigNum ) - ( tradesback - 3 );
Signumstart4 = LastValue( SigNum ) - ( tradesback - 4 );
Signumstart5 = LastValue( SigNum ) - ( tradesback - 5 );
bi = BarIndex();
bistart = ValueWhen( signum == signumstart1, bi );
bicond = bi >= bistart AND bi <= LastValue( bi );
SellPL = IIf( Sell AND bicond, C-BuyPrice, 0 );
CovPL = IIf( Sell AND bicond, ShortPrice - C,0 );
cumPL = SellPL + CovPL;
//Plot (SellPL,"Sell",colorGreen,styleHistogram,maskhistogram);
///Plot (CovPL,"Cover", colorRed,styleHistogram,maskhistogram);
lsince = LowestSince(Sell OR Sell, cumPL, 0);
hsince = HighestSince(Sell OR Sell, CumPL, 0);
vs= IIf(lsince==0,hsince,lsince);
PL1 = ValueWhen( signum == signumstart1 , vs,1 );
PL2 = ValueWhen( signum == signumstart2 , vs,1 );
PL3 = ValueWhen( signum == signumstart3 , vs,1 );
PL4 = ValueWhen( signum == signumstart4 , vs,1 );
PL5 = ValueWhen( signum == signumstart5, vs ,1 );
//////////////////////////////////////////////////
// Plot the Last Five Trades Profit/Losses //
//////////////////////////////////////////////////
Title = EncodeColor(colorWhite)+ "Backtest Results from www.marketcalls.in" + "
- " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) +
EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+" "+"Hi-"+H+"
"+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+ EncodeColor(colorYellow)+ "\n\n\nLast 5
Trade Results\n" +
"\nTrade1= " + PL1
+"\n"+ "Trade2= " + CovPL
+"\n"+ "Trade3= " + PL3
+"\n"+ "Trade4= " + PL4
+"\n"+ "Trade5= " + PL5;