Sunday 17 May 2015

All on One for Amibroker (AFL)


All on One for Amibroker (AFL)






_SECTION_BEGIN("SuperTrend");
SetBarsRequired(100000,0);
GraphXSpace = 15;
SetChartOptions(0,chartShowArrows|chartShowDates);

Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle |
ParamStyle("Style") | GetPriceStyle() );

PositionSize = 100000;

Factor=Optimize("Factor",2,2,10,1);
Pd=Optimize("ATR Periods",11,1,20,1);
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;

for (i = 1; i <BarCount-1; i++) {
      TrendUp[i] = Null;
      TrendDown[i] = Null;
    
      trend[i]=1;
  
     
      if (Close[i]>Up[i-1]) {
         trend[i]=1;
         if (trend[i-1] == -1) changeOfTrend = 1;
        
      }
      else if (Close[i]<Dn[i-1]) {
         trend[i]=-1;
         if (trend[i-1] == 1) changeOfTrend = 1;
      }
      else if (trend[i-1]==1) {
         trend[i]=1;
         changeOfTrend = 0;      
      }
      else if (trend[i-1]==-1) {
         trend[i]=-1;
         changeOfTrend = 0;
      }

      if (trend[i]<0 && trend[i-1]>0) {
         flag=1;
      }
      else {
         flag=0;
      }
     
      if (trend[i]>0 && trend[i-1]<0) {
         flagh=1;
      }
      else {
         flagh=0;
      }
     
      if (trend[i]>0 && Dn[i]<Dn[i-1]){
         Dn[i]=Dn[i-1];
                }
     
      if (trend[i]<0 && Up[i]>Up[i-1])
        { Up[i]=Up[i-1];
                }
     
      if (flag==1)
       {  Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]);;
        }
      if (flagh==1)
        { Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]);;
         }
      if (trend[i]==1) {
         TrendUp[i]=Dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i-1] = TrendDown[i-1];
            changeOfTrend = 0;
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=Up[i];
         if (changeOfTrend == 1) {
            TrendDown[i-1] = TrendUp[i-1];
            changeOfTrend = 0;
         }
      }
   }

Plot(TrendUp,"Trend",colorGreen);
Plot(TrendDown,"Down",colorRed);

Buy = trend==1;
Sell=trend==-1;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=Sell;
Cover=Buy;

BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
ShortPrice=ValueWhen(Short,C);
CoverPrice=ValueWhen(Cover,C);

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);      
             
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
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);

_SECTION_END();

_SECTION_BEGIN("MA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") );
_SECTION_END();

_SECTION_BEGIN("Label");


GfxSetTextAlign( 6 );
FS=Param("Font Size",16);
GfxSelectFont("Arial", FS, 700, True );
GfxSetBkMode(0);
GfxSetTextColor( colorLightOrange );
Hor=Param("Horizonta Position",1000);
Ver=Param("Vertica Position",18);
GfxTextOut(""+C, Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
FS2=Param("Font Size2",11);
GfxSelectFont("Arial", FS2,11, 700, True );
GfxSetBkMode( colorGreen );
Hor1=Param("Horizontal Position",1000);
Ver1=Param("Vertical Position",1); 
GfxSetTextColor( colorLightOrange );
GfxTextOut(""+DD+"  ("+xx+"%)", Hor1 , Ver1+45 );

dec = (Param("Decimals",2,0,7,1)/10)+1;

Title =EncodeColor(255)+ Name() + " " + EncodeColor(30) + Date() +
" " + EncodeColor(5) + "{{INTERVAL}} " +
EncodeColor(255)+ " Open = "+ EncodeColor(30)+ WriteVal(O,dec) +
EncodeColor(255)+ " High = "+ EncodeColor(30)+ WriteVal(H,dec) +
EncodeColor(255)+ " Low = "+ EncodeColor(30)+ WriteVal(L,dec) +
EncodeColor(255)+ " Close = "+ EncodeColor(35)+ WriteVal(C,dec)+
EncodeColor(255)+ " Volume = "+ EncodeColor(30)+ WriteVal(V,1) ;

_SECTION_END();

_SECTION_BEGIN("Advance Trenlines with Candle Pivots");
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 (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]);
}

a1 = ahpivs == 1;
a2 = alpivs == 1;
PlotShapes(a1 *shapeSmallCircle, colorLightOrange, 0, H, Offset = 5);
PlotShapes(a2 *shapeSmallCircle, colorLightOrange, 0, L, Offset = -5);


Para = ParamToggle("Plot Parallel Lines","Off,On");
ColorS= ParamColor("Support",colorLightOrange);
ColorR= ParamColor("Resistance",colorLightOrange);
x = Cum(1);
s1 = L;
s11 = H;
pS = a2 == 1;


endt = LastValue(ValueWhen(ps,x,1));
startt = LastValue(ValueWhen(ps,x,2));
ends = LastValue(ValueWhen(ps,S1,1));
starts = LastValue(ValueWhen(ps,S1,2));
dtS = endt - startt;
aS = (endS - startS) / dtS;
bS = endS;
trendlineS = aS *(x - endt) + bS;
g3 = IIf(x > startt - 10, trendlineS, Null);
Plot(g3, "", colors );
pR = a1 == 1;
endt1 = LastValue(ValueWhen(pr,x,1));
startt1 = LastValue(ValueWhen(pr,x,2));
endr = LastValue(ValueWhen(pr,S11,1));
startr = LastValue(ValueWhen(pr,S11,2));
dtR = endt1 - startt1;
aR = (endR - startR) / dtR;
bR = endR;
trendlineR = aR *(x - endt1) + bR;
g4 = IIf(x > startT1 - 10, trendlineR, Null);
Plot(g4, "", colorr );

acd = log(endr/startr)/(endt1-startt1);
res1 = exp((x-endt1)*acd)*endr;
RRL = ROC(res1,1);
bcd = log(ends/starts)/(endt-startt);
sup1= exp((x-endt)*bcd)*ends;
RSL = ROC(sup1,1);

Low_Value = LastValue(Ref(LLV(L,endt1-startt1),-(x-endt1)));
x2 = LastValue(ValueWhen(L==Low_Value & x>startt1 & x<endt1,x));
PLL = IIf(LastValue(x)-x2>5,exp((x-x2)*acd)*Low_Value,Null);
Hi_Value = LastValue(Ref(HHV(H,endt-startt),-(x-endt)));
x3 = LastValue(ValueWhen(H==Hi_Value & x>startt & x<endt,x));
PHL = IIf(LastValue(x)-x3>5,exp((x-x3)*bcd)*Hi_Value,Null);
SLabs = sup1-Ref(sup1,-1); RLabs = res1-Ref(res1,-1);
ROC2SL = (SLabs/C)*100; ROC2RL = (RLabs/C)*100;
PLLd = abs((LastValue(C)/LastValue(PLL))-1)<0.01*100;
PHLd = abs((LastValue(C)/LastValue(PHL))-1)<0.01*100;
barvisible = Status("barvisible");
firstvisiblebar = barvisible & NOT
Ref(barvisible,-1);
HHvisible = LastValue(HighestSince(firstvisiblebar,High));
LLvisible = LastValue(LowestSince(firstvisiblebar,Low));
RaH = HHvisible *1.05; RaL = LLVisible *0.95;
AnZ= starts==0 OR ends==0 OR startr==0 OR endr==0;
PLplot = IIf(x-x2>=0 & abs(LastValue(L/PLL)-1) <abs(LastValue((100/1000)*ATR(14))) & PLL>RaL & PLL<RaH & NOT AnZ,PLL,IIf(x-x2>=0 & RaL==0 & PLLd & abs(LastValue(L/PLL)-1) <abs(LastValue((100/1000)*ATR(14)))& NOT AnZ,PLL,Null));
PHplot = IIf(x-x3>=0 & abs(LastValue(H/PHL)-1) <abs(LastValue((100/1000)*ATR(14))) & PHL>RaL & PHL<RaH & NOT AnZ,PHL,IIf(x-x3>=0 & RaL==0 & PHLd & abs(LastValue(H/PHL)-1) <abs(LastValue((100/1000)*ATR(14)))& NOT AnZ,PHL,Null));
Plot(IIf(Para,PLplot,Null)," ",colorr,styleDashed,maskDefault+styleNoRescale);
Plot(IIf(Para,PHplot,Null)," ",colors,styleDashed,maskDefault+styleNoRescale);
_SECTION_END();


_SECTION_BEGIN("Fibonacci");



Period = Param("Period", 5, 1, 50);
ShowExt = ParamToggle("Show Extension ?", "No|Yes");


Period = Period*(13*.618);

Hhb = LastValue(Ref(HHVBars(H, Period), -1))+1;
Llb = LastValue(Ref(LLVBars(L, Period), -1))+1;
Hv = LastValue(Ref(HHV(H, Period), -1));
Lv = LastValue(Ref(LLV(L, Period), -1));

Range = (Hv - Lv);
LText = "  0 %, 23.6 %, 38.2 %, 50  %, 61.8 %, 78.6 %, 100 %,127 % ,161.8 %,  200 %";
if(Hhb > Llb)
{
Levels[0] = Lv;
Levels[1]= (Range *.236)+Levels[0];
Levels[2]= (Range *.382)+Levels[0];
Levels[3]= (Range *.5)+Levels[0];
Levels[4]= (Range *.618)+Levels[0];
Levels[5]= (Range *.786)+Levels[0];
Levels[6]= Hv;
Levels[7]= (Range *.270)+Levels[6];
Levels[8]= (Range *.618)+Levels[6];
Levels[9]= (Range )+Levels[6];
x0 = BarCount - 1 - Llb;
x1 = BarCount - 1 - Hhb;
}
else
{
Levels[0]= Hv;
Levels[1]= Levels[0]- (Range *.236);
Levels[2]= Levels[0]- (Range *.382);
Levels[3]= Levels[0]- (Range *.5);
Levels[4]= Levels[0]- (Range *.618);
Levels[5]= Levels[0]- (Range *.786);
Levels[6]= Lv;
Levels[7]= Levels[6]- (Range *.270);
Levels[8]= Levels[6]- (Range *.618);
Levels[9]= Levels[6]- (Range );
x0 = BarCount - 1 - Hhb;
x1 = BarCount - 1 - Llb;
}



for(i=0; i<10; i++)
{
if(i!=6)
x = x0;
else
x = x1;
if(i<7 OR ShowExt)
{
Plot(LineArray(x, Levels[i], BarCount, Levels[i]), "", i+2, styleDashed);
PlotText(StrExtract(LText, i), BarCount, Levels[i], i+2);
}
}




_SECTION_END();

No comments:

Post a Comment

Thanks