// Downloaded From https://www.WiseStockTrader.com
// AFL code E.M.Pottasch, Aug 2013
x=BarIndex();Lx=LastValue(x);
fvb=Status("firstvisiblebarindex");lvb=Min(Lx,Status("lastvisiblebarindex"));
DisplayLineAnnotation=ParamToggle("Display Line Annotation","Off|On",1);
DisplaySignalAnnotation=ParamToggle("Display Signal Annotation","Off|On",1);
SetTradeDelays(0,0,0,0);
SetOption("CommissionMode",3);
SetOption("CommissionAmount",2.01);
SetOption("FuturesMode",True);
SetOption("AllowSameBarExit",True);
SetOption("InitialEquity",150000);
NumContracts=1;
//PositionSize=NumContracts*MarginDeposit;
beginCalcTime=ParamTime("Begin Calc Time","09:30:00");
endCalcTime=ParamTime("End Calc Time","16:00:00");
beginTradeTime=ParamTime("Begin Trade Time","09:30:00");
endTradeTime=ParamTime("End Trade Time","15:55:00");
timeWindowTrade=TimeNum()>=beginTradeTime AND TimeNum()<=endTradeTime;
timeWindowCalc=TimeNum()>=beginCalcTime AND TimeNum()<=endCalcTime;
firstBarOfDay=TimeNum()>=beginCalcTime ;firstBarOfDay=(firstBarOfDay-Ref(firstBarOfDay,-1))==1;
lastBarOfDay=TimeNum()>=endCalcTime;lastBarOfDay=(lastBarOfDay-Ref(lastBarOfDay,-1))==1;
firstTradeBarOfDay=TimeNum()>=beginTradeTime;firstTradeBarOfDay=(firstTradeBarOfDay-Ref(firstTradeBarOfDay,-1))==1;
lastTradeBarOfDay=TimeNum()>=endTradeTime;lastTradeBarOfDay=(lastTradeBarOfDay-Ref(lastTradeBarOfDay,-1))==1;
ttt=firstBarOfDay OR lastBarOfDay;
inTrade=Flip(firstBarOfDay,LastBarOfDay);
procedure entryExit_proc(Buy1,Short1,stopLong,stopShort,BY,BT1,BT2,BT3,SH,ST1,ST2,ST3,timearr,endTradeTime,TickSize)
{
global BuyAdjusted;
global BuyPriceAdjusted;
global BuyAdjustedScale1;
global BuyAdjustedScale2;
global BuyPriceAdjustedScale1;
global BuyPriceAdjustedScale2;
global ShortAdjusted;
global ShortPriceAdjusted;
global ShortAdjustedScale1;
global ShortAdjustedScale2;
global ShortPriceAdjustedScale1;
global ShortPriceAdjustedScale2;
global SellAdjusted;
global SellPriceAdjusted;
global CoverAdjusted;
global CoverPriceAdjusted;
global NContractArray;
BuyAdjusted=0;
BuyPriceAdjusted=0;
BuyAdjustedScale1=0;
BuyAdjustedScale2=0;
BuyAdjustedScale3=0;
BuyPriceAdjustedScale1=0;
BuyPriceAdjustedScale2=0;
BuyPriceAdjustedScale3=0;
ShortAdjusted=0;
ShortPriceAdjusted=0;
ShortAdjustedScale1=0;
ShortAdjustedScale2=0;
ShortAdjustedScale3=0;
ShortPriceAdjustedScale1=0;
ShortPriceAdjustedScale2=0;
ShortPriceAdjustedScale3=0;
SellAdjusted=0;
SellPriceAdjusted=0;
CoverAdjusted =0;
CoverPriceAdjusted=0;
NContractArray=0;
delay=0;
slip=TickSize*0;
for(i=1;i<BarCount;i++)
{
exitType=0;
if(Buy1[i])
{
BuyAdjusted[i]=1;
BuyPriceAdjusted[i]=O[i];
NContractArray[i]=3;
for(j=i+delay;j<BarCount;j++)
{
if(j>i) NContractArray[j]=NContractArray[j-1];
if (timearr[j-1]>=endTradeTime)
{
SellAdjusted[j]=1;
SellPriceAdjusted[j]=O[j]-slip;
NContractArray[j]=0;
i=j;
break;
}
else if(C[j-1]<stopLong[j-1])
{
SellAdjusted[j]=1;
SellPriceAdjusted[j]=O[j]-slip;
NContractArray[j]=0;
i=j;
break;
}
else if(H[j]>BT3[j] AND exitType==0)//1
{
exitType=2;
BuyAdjustedScale1[j]=sigScaleOut;
BuyPriceAdjustedScale1[j]=Max(O[j],BT1[j])-slip;
NContractArray[j]=NContractArray[j-1]-1;
BuyAdjustedScale2[j]=sigScaleOut;
BuyPriceAdjustedScale2[j]=Max(O[j],BT2[j])-slip;
NContractArray[j]=NContractArray[j-1]-1;
SellAdjusted[j]=1;
SellPriceAdjusted[j]=Max(O[j],BT3[j])-slip;
NContractArray[j]=0;
i=j;
break;
}
else if(H[j]>BT3[j] AND exitType==1)//2
{
exitType=2;
BuyAdjustedScale2[j]=sigScaleOut;
BuyPriceAdjustedScale2[j]=Max(O[j],BT2[j])-slip;
NContractArray[j]=NContractArray[j-1]-1;
SellAdjusted[j]=1;
SellPriceAdjusted[j]=Max(O[j],BT3[j])-slip;
NContractArray[j]=0;
i=j;
break;
}
else if(H[j]>BT2[j] AND H[j]<=BT3[j] AND exitType==0)//3
{
exitType=2;
BuyAdjustedScale1[j]=sigScaleOut;
BuyPriceAdjustedScale1[j]=Max(O[j],BT1[j])-slip;
NContractArray[j]=NContractArray[j-1]-1;
BuyAdjustedScale2[j]=sigScaleOut;
BuyPriceAdjustedScale2[j]=Max(O[j],BT2[j])-slip;
NContractArray[j]=NContractArray[j-1]-1;
}
else if(H[j]>BT1[j] AND H[j]<=BT2[j] AND exitType==0)//4
{
exitType=1;
BuyAdjustedScale1[j]=sigScaleOut;
BuyPriceAdjustedScale1[j]=Max(O[j],BT1[j])-slip;
NContractArray[j]=NContractArray[j-1]-1;
}
else if(H[j]>BT2[j] AND H[j]<=BT3[j] AND exitType==1)//5
{
exitType=2;
BuyAdjustedScale2[j]=sigScaleOut;
BuyPriceAdjustedScale2[j]=Max(O[j],BT2[j])-slip;
NContractArray[j]=NContractArray[j-1]-1;
}
else if(H[j]>BT3[j] AND exitType==2)//6
{
SellAdjusted[j]=1;
SellPriceAdjusted[j]=Max(O[j],BT3[j])-slip;
NContractArray[j]=0;
i=j;
break;
}
else if(j==BarCount-1)
{
i=BarCount;
break;
}
}
}
else if(Short1[i])
{
ShortAdjusted[i]=1;
ShortPriceAdjusted[i]=O[i];
NContractArray[i]=-3;
for(j=i+delay;j<BarCount;j++)
{
if(j>i) NContractArray[j]=NContractArray[j-1];
if(timearr[j-1]>=endTradeTime)
{
CoverAdjusted[j]=1;
CoverPriceAdjusted[j]=O[j]+slip;
NContractArray[j]=0;
i=j;
break;
}
else if(C[j-1]>stopShort[j-1])
{
CoverAdjusted[j]=1;
CoverPriceAdjusted[j]=O[j]+slip;
NContractArray[j]=0;
i=j;
break;
}
else if(L[j]<ST3[j] AND exitType==0)//1
{
ShortAdjustedScale1[j]=sigScaleOut;
ShortPriceAdjustedScale1[j]=Min(O[j],ST1[j])+slip;
NContractArray[j]=NContractArray[j-1]+1;
ShortAdjustedScale2[j]=sigScaleOut;
ShortPriceAdjustedScale2[j]=Min(O[j],ST2[j])+slip;
NContractArray[j]=NContractArray[j-1]+1;
CoverAdjusted[j]=1;
CoverPriceAdjusted[j]=Min(O[j],ST3[j])+slip;
NContractArray[j]=0;
i=j;
break;
}
else if(L[j]<ST3[j] AND exitType==1)//2
{
ShortAdjustedScale2[j]=sigScaleOut;
ShortPriceAdjustedScale2[j]=Min(O[j],ST2[j])+slip;
NContractArray[j]=NContractArray[j-1]+1;
CoverAdjusted[j]=1;
CoverPriceAdjusted[j]=Min(O[j],ST3[j])+slip;
NContractArray[j]=0;
i=j;
break;
}
else if(L[j]<ST2[j] AND L[j]>=ST3[j] AND exitType==0)//3
{
exitType=2;
ShortAdjustedScale1[j]=sigScaleOut;
ShortPriceAdjustedScale1[j]=Min(O[j],ST1[j])+slip;
NContractArray[j]=NContractArray[j-1]+1;
ShortAdjustedScale2[j]=sigScaleOut;
ShortPriceAdjustedScale2[j]=Min(O[j],ST2[j])+slip;
NContractArray[j]=NContractArray[j-1]+1;
}
else if(L[j]<ST1[j] AND L[j]>=ST2[j] AND exitType==0)//4
{
exitType=1;
ShortAdjustedScale1[j]=sigScaleOut;
ShortPriceAdjustedScale1[j]=Min(O[j],ST1[j])+slip;
NContractArray[j]=NContractArray[j-1]+1;
}
else if(L[j]<ST2[j] AND L[j]>=ST3[j] AND exitType==1)//5
{
exitType=2;
ShortAdjustedScale2[j]=sigScaleOut;
ShortPriceAdjustedScale2[j]=Min(O[j],ST2[j])+slip;
NContractArray[j]=NContractArray[j-1]+1;
}
else if(L[j]<ST3[j] AND exitType==2)//6
{
CoverAdjusted[j]=1;
CoverPriceAdjusted[j]=Min(O[j],ST3[j])+slip;
NContractArray[j]=0;
i=j;
break;
}
else if(j==BarCount-1)
{
i=BarCount;
break;
}
}
}
}
}
myHigh=ValueWhen(timeWindowCalc,HighestSince(firstBarOfDay,High));
myLow=ValueWhen(timeWindowCalc,LowestSince(firstBarOfDay,Low));
myClose=ValueWhen(lastBarOfDay,C);
DH=TimeFrameCompress(myHigh,inDaily,compressLast);
DL=TimeFrameCompress(myLow,inDaily,compressLast);
DC=TimeFrameCompress(myClose,inDaily,compressLast);
DH=Ref(DH,-1);DL=Ref(DL,-1);DC=Ref(DC,-1);
YHigh=TimeFrameExpand(DH,inDaily,expandFirst);
YLow=TimeFrameExpand(DL,inDaily,expandFirst);
YClose=TimeFrameExpand(DC,inDaily,expandFirst);
fact=1.1;
R3=YClose+((YHigh-Ylow)*fact)/4;
R4=YClose+((YHigh-Ylow)*fact)/2;
R5=((YHigh-Ylow)*0.82)+YClose;
R6=((YHigh-Ylow)*1.09)+YClose;
R7=((YHigh-Ylow)*1.35)+YClose;
R8=((YHigh-Ylow)*1.62)+YClose;
S3=YClose-((YHigh-Ylow)*fact)/4;
S4=YClose-((YHigh-Ylow)*fact)/2;
S5=YClose-((YHigh-Ylow)*0.82);
S6=YClose-((YHigh-Ylow)*1.09);
S7=YClose-((YHigh-Ylow)*1.35);
S8=YClose-((YHigh-Ylow)*1.62);
BY=R4;BT1=R6;BT2=R7;BT3=R8;
SH=S4;ST1=S6;ST2=S7;ST3=S8;
Buy1=Cross(C,BY) AND C<BT1;Buy1=Ref(Buy1,-1) AND timeWindowTrade;
Short1=Cross(SH,C) AND C>ST1;Short1=Ref(Short1,-1) AND timeWindowTrade;
stopLong=(R3+R4)/2;stopShort=(S3+S4)/2;
entryExit_proc(Buy1,Short1,stopLong,stopShort,BY,BT1,BT2,BT3,SH,ST1,ST2,ST3,TimeNum(),endTradeTime,TickSize);
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(ColorRGB(0,0,0));
SetBarFillColor(IIf(C>O,ColorRGB(0,75,0),IIf(C<=O,ColorRGB(75,0,0),colorLightGrey)));
Plot(C,"\nPrice",IIf(C>O,ColorRGB(0,255,0),IIf(C<=O,ColorRGB(255,0,0),colorLightGrey)),64,0,0,0,0);
if(Interval()<inDaily)
{
PlotShapes(IIf(BuyAdjusted,shapeUpArrow,shapeNone),colorGreen,0,L,-15);
PlotShapes(IIf(BuyAdjusted,shapeSmallCircle,shapeNone),ColorRGB(255,20,147),0,BuyPriceAdjusted,0);
PlotShapes(IIf(BuyAdjusted,shapeHollowSmallCircle,shapeNone),ColorRGB(255,255,255),0,BuyPriceAdjusted,0);
PlotShapes(IIf(SellAdjusted,shapeDownArrow,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(SellAdjusted,shapeSmallCircle,shapeNone),ColorRGB(255,20,147),0,SellPriceAdjusted,0);
PlotShapes(IIf(SellAdjusted,shapeHollowSmallCircle,shapeNone),ColorRGB(255,255,255),0,SellPriceAdjusted,0);
PlotShapes(IIf(ShortAdjusted,shapeSmallDownTriangle,shapeNone),colorRed,0,H,IIf(ShortAdjusted AND SellAdjusted,-30,-15));
PlotShapes(IIf(ShortAdjusted,shapeSmallCircle,shapeNone),ColorRGB(255,20,147),0,ShortPriceAdjusted,0);
PlotShapes(IIf(ShortAdjusted,shapeHollowSmallCircle,shapeNone),ColorRGB(255,255,255),0,ShortPriceAdjusted,0);
PlotShapes(IIf(CoverAdjusted,shapeSmallUpTriangle,shapeNone),colorGreen,0,L,IIf(CoverAdjusted AND BuyAdjusted,-30,-15));
PlotShapes(IIf(CoverAdjusted,shapeSmallCircle,shapeNone),ColorRGB(255,20,147),0,CoverPriceAdjusted,0);
PlotShapes(IIf(CoverAdjusted,shapeHollowSmallCircle,shapeNone),ColorRGB(255,255,255),0,CoverPriceAdjusted,0);
PlotShapes(IIf(BuyAdjustedScale1,shapeSmallDownTriangle,shapeNone),colorOrange,0,H,-15);
PlotShapes(IIf(BuyAdjustedScale1,shapeSmallCircle,shapeNone),ColorRGB(255,20,147),0,BuyPriceAdjustedScale1,0);
PlotShapes(IIf(BuyAdjustedScale1,shapeHollowSmallCircle,shapeNone),ColorRGB(255,255,255),0,BuyPriceAdjustedScale1,0);
PlotShapes(IIf(BuyAdjustedScale2,shapeSmallDownTriangle,shapeNone),colorOrange,0,H,-15);
PlotShapes(IIf(BuyAdjustedScale2,shapeSmallCircle,shapeNone),ColorRGB(255,20,147),0,BuyPriceAdjustedScale2,0);
PlotShapes(IIf(BuyAdjustedScale2,shapeHollowSmallCircle,shapeNone),ColorRGB(255,255,255),0,BuyPriceAdjustedScale2,0);
PlotShapes(IIf(ShortAdjustedScale1,shapeSmallUpTriangle,shapeNone),colorBlue,0,L,-15);
PlotShapes(IIf(ShortAdjustedScale1,shapeSmallCircle,shapeNone),ColorRGB(255,20,147),0,ShortPriceAdjustedScale1,0);
PlotShapes(IIf(ShortAdjustedScale1,shapeHollowSmallCircle,shapeNone),ColorRGB(255,255,255),0,ShortPriceAdjustedScale1,0);
PlotShapes(IIf(ShortAdjustedScale2,shapeSmallUpTriangle,shapeNone),colorBlue,0,L,-15);
PlotShapes(IIf(ShortAdjustedScale2,shapeSmallCircle,shapeNone),ColorRGB(255,20,147),0,ShortPriceAdjustedScale2,0);
PlotShapes(IIf(ShortAdjustedScale2,shapeHollowSmallCircle,shapeNone),ColorRGB(255,255,255),0,ShortPriceAdjustedScale2,0);
Plot(IIf(timeWindowTrade,BY,Null),"BY",colorLime,styleLine|styleNoRescale|styleNoTitle);
Plot(IIf(timeWindowTrade,BT1,Null),"BT1",colorGreen,styleLine|styleNoRescale|styleNoTitle);
Plot(IIf(timeWindowTrade,BT2,Null),"BT2",colorGreen,styleLine|styleNoRescale|styleNoTitle);
Plot(IIf(timeWindowTrade,BT3,Null),"BT3",colorGreen,styleLine|styleNoRescale|styleNoTitle);
Plot(IIf(timeWindowTrade,SH,Null),"SH",colorRed,styleLine|styleNoRescale|styleNoTitle);
Plot(IIf(timeWindowTrade,ST1,Null),"ST1",colorBrown,styleLine|styleNoRescale|styleNoTitle);
Plot(IIf(timeWindowTrade,ST2,Null),"ST2",colorBrown,styleLine|styleNoRescale|styleNoTitle);
Plot(IIf(timeWindowTrade,ST3,Null),"ST3",colorBrown,styleLine|styleNoRescale|styleNoTitle);
Plot(IIf(timeWindowTrade,stopLong,Null),"",colorYellow,styleDashed);
Plot(IIf(timeWindowTrade,stopShort,Null),"",colorYellow,styleDashed);
Plot(firstTradeBarOfDay,"",ColorRGB(0,0,255),styleHistogram|styleThick|styleOwnScale|styleNoLabel,0,1,0,0);
Plot(lastTradeBarOfDay,"",ColorRGB(200,200,200),styleHistogram|styleThick|styleOwnScale|styleNoLabel,0,1,0,0);
for(i=fvb;i<=lvb;i++)
{
if(displaySignalAnnotation)
{
if(BuyAdjusted[i]==1) PlotText("Buy",i+2,L[i],colorWhite,colorGreen);
if(BuyAdjustedScale1[i]==sigScaleOut) PlotText("Scale Out",i+2,H[i],colorBlack,colorYellow);
if(BuyAdjustedScale2[i]==sigScaleOut) PlotText("Scale Out",i+2,H[i],colorBlack,colorYellow);
if(SellAdjusted[i]==1) PlotText("Sell",i+2,H[i],colorWhite,colorRed);
if(ShortAdjusted[i]==1) PlotText("Short",i+2,H[i],colorWhite,colorRed);
if(ShortAdjustedScale1[i]==sigScaleOut) PlotText("Scale Out",i+2,L[i],colorBlack,colorYellow);
if(ShortAdjustedScale2[i]==sigScaleOut) PlotText("Scale Out",i+2,L[i],colorBlack,colorYellow);
if(CoverAdjusted[i]==1) PlotText("Cover",i+2,L[i],colorWhite,colorGreen);
}
if(displayLineAnnotation)
{
if(lastTradeBarOfDay[i])
{
PlotText("ENTRY LONG",i+3,BY[i],colorWhite);
PlotText("TARGET 1",i+3,BT1[i],colorWhite);
PlotText("TARGET 2",i+3,BT2[i],colorWhite);
PlotText("TARGET 3",i+3,BT3[i],colorWhite);
PlotText("ENTRY SHORT",i+3,SH[i],colorWhite);
PlotText("TARGET 1",i+3,ST1[i],colorWhite);
PlotText("TARGET 2",i+3,ST2[i],colorWhite);
PlotText("TARGET 3",i+3,ST3[i],colorWhite);
PlotText("LONG STOP",i+3,stoplong[i],colorRed);
PlotText("SHORT STOP",i+3,stopshort[i],colorRed);
}
}
}
}