Tuesday, 16 September 2014

Auto Plot HH HL LL for Amibroker (AFL)





Auto Plot HH HL LL for Amibroker (AFL)



Q = Param( "% Change", 2, 0.1, 10, 0.1 );
Z = Zig( C , q ) ;
HH = ( ( Z < Ref( Z, -1 ) AND Ref( Z, -1 ) > Ref( Z, -2 ) ) AND (Peak( z, q, 1 ) > Peak( Z, q, 2 ) ) );
LH = ( ( Z < Ref( Z, -1 ) AND Ref( Z, -1 ) > Ref( Z, -2 ) ) AND (Peak( Z, q, 1 ) < Peak( Z, q, 2 ) ) );
HL = ( ( Z > Ref( Z, -1 ) AND Ref( Z, -1 ) < Ref( Z, -2 ) ) AND (Trough( Z, q, 1 ) > Trough( Z, q, 2 ) ) );
LL = ( ( Z > Ref( Z, -1 ) AND Ref( Z, -1 ) < Ref( Z, -2 ) ) AND (Trough( Z, q, 1 ) < Trough( Z, q, 2 ) ) );
GraphXSpace = 5;
dist = 0.5 * ATR( 20 );

for ( i = 0; i < BarCount; i++ )
{
    if ( HH[i] )
        PlotText( "HH", i, H[ i ] + dist[i], colorYellow );

    if ( LH[i] )
        PlotText( "LH", i, H[ i ] + dist[i], colorYellow );

    if ( HL[i] )
        PlotText( "HL", i, L[ i ] - dist[i], colorPink );

    if ( LL[i] )
        PlotText( "LL", i, L[ i ] - dist[i], colorPink );

}
_SECTION_BEGIN("Background_Setting");
SetChartBkGradientFill( ParamColor("BgTop", colorBlack),

ParamColor("BgBottom", colorBlack),ParamColor("titleblock",colorDarkTeal )); 
_SECTION_END();


_SECTION_BEGIN("The_Beast_2");
SetBarsRequired(10000,10000); 
Prd1=Param("ATR Period 1-20",4,1,20,1);
Prd2=Param("LookBack Period 1-20",7,1,20,1);
Green=HHV(LLV(L,Prd1)+ATR(Prd1),Prd2); 

RED=LLV(HHV(H,Prd1)-ATR(Prd1),Prd2); 


HaClose =EMA((O+H+L+C)/4,3); 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) ); 
Color = IIf(C>Green ,colorBrightGreen,IIf(C < RED,colorRed,colorWhite)); 

PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", color, styleCandle,styleThick );

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));


_SECTION_END();

_SECTION_BEGIN("Trailing_Stop_Short");
stoplossPercentage = Param("Stoploss Percentage", 5, 2, 10);
lookbackPeriod = Param("Lookback period", 10, 5, 30);
Plot(HHV(C,lookbackPeriod) - HHV(C,lookbackPeriod) * (stoplossPercentage / 100), "Trailing stoploss", ParamColor( "Color", colorCycle ),ParamStyle("Style",styleLine|Stylehidden ,maskAll));
_SECTION_END();

_SECTION_BEGIN("Trailing_Stop_Long");

mf = Param("ATR multiplier",3,1,3,0.25); 
ap=Param("ATR Period",10,5,30,1);
Lb=Param("Lookback Period",20,10,40,1);
ps=ParamToggle("Use last low pivot","Use,Dont",1);


t1=HHV(C,Lb);
t2=(mf*ATR(ap)); 
t3=Trough(C,2,1);
t4=t1-t2;
t5=Min(t4,t3);

if(ps)
{
t6 = t1-t2;
}
else
{
t6=t5;
}

initial=t6; 
stop[ 0 ] = Close[ 0 ]; 

for( i = 1 ; i < BarCount; i++) 
if( Close[ i ] > stop[ i - 1]) 
temp = t6[ i ]; 
if( temp > stop[ i - 1 ] ) stop[ i ] = temp; 
else stop[ i ] = stop[ i - 1 ]; 
else 
stop[ i ] = initial[ i ]; 


Plot(stop,"ATR Stop",ParamColor( "Color", colorSeaGreen ),ParamStyle("Style",styleLine|Stylehidden,maskAll));











_SECTION_BEGIN("Pivot_Finder");



farback=Param("How Far back to go",100,0,5000,10);
nBars = Param("Number of bars", 12, 5, 40);


GraphXSpace=7;



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++;

}



}



}

// -- Basic attempt to add a pivot this logic may have missed

// -- OK, now I want to look at last two pivots. If the most

// recent low pivot is after the last high, I could

// still have a high pivot that I didn't catch

// -- Start at last bar

curBar = (BarCount-1);

candIdx = 0;

candPrc = 0;

lastLPIdx = aLPivIdxs[0];

lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];

lastHPH = aHPivHighs[0];

if (lastLPIdx > lastHPIdx) {

// -- Bar and price info for candidate pivot

candIdx = curBar - aHHVBars[curBar];

candPrc = aHHV[curBar];

if (

lastHPH < candPrc AND

candIdx > lastLPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aHPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

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 {


// -- Bar and price info for candidate pivot

candIdx = curBar - aLLVBars[curBar];

candPrc = aLLV[curBar];

if (

lastLPL > candPrc AND

candIdx > lastHPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aLPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

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++;

}

}

// -- Dump inventory of high pivots for debugging



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]);

}



// -- OK, let's plot the pivots using arrows

PlotShapes(

IIf(aHPivs==1, shapeSmallSquare, shapeNone), colorCustom12, 0, H, 25);

PlotShapes(

IIf(aLPivs==1, shapeSmallSquare , shapeNone), colorOrange, 0, L, -20);

_SECTION_BEGIN("Graphics");
GrpPrm=Param("Graphic Space",1,0,10);
GraphXSpace=GrpPrm;
_SECTION_END();

_SECTION_BEGIN("Analysis");
M1=EMA(((2*EMA(C,29/2))-EMA(C,29)),2);
M2=EMA(((2*EMA(C,29/2))-EMA(C,29)),5);
Buy     = Cross(M1, M2) ;
Sell    = Cross(M2, M1) ;

Buy     = ExRem(Buy, Sell);
Sell    = ExRem(Sell, Buy);
mycolor = IIf(M1 > M2, colorLime, colorRed);
shape   = Buy * shapeUpArrow + Sell * shapeDownArrow ;


PlotShapes( shape, IIf( Buy, colorGreen, colorRed ),0, IIf( Buy, Low, High ) );
dist = 1.5*ATR(20);
for( i = 0; i < BarCount; i++ ) {
       if( Buy[i] ) PlotText( "Buy\n@" + Close[i], i, Low[i] - dist[i], colorGreen );
       if( Sell[i] ) PlotText( "sell\n@" + Close[i], i, Low[i] + dist[i], colorRed );
}

Plot( Close, "Close", mycolor, styleNoTitle );
_SECTION_END();

1 comment:

  1. error when scan or explore

    if (aLLVBars[curBar] < aHHVBars[curBar]) {

    Error message below
    Error 10. array Subscript out of range. You must not access array elements outside 0..(BarCount-1) range. you attempted.....(Ln:197, Col:39)

    ReplyDelete

Thanks