Need Help with this AFL

Ajax

Well-Known Member
#13
HI ASHUTOSH & DEEDEED

Following is the AFL for ploting Pivot points on the chart. However, I need the value and no. of bars from last close of last 2 pivot highs and last 2 pivot lows. Can u help me please .

I am attaching a chart indicating requirements :::::


_SECTION_BEGIN("Pivots");

// -- what will be our lookback range for the hh and ll?
farback= Param("How Far back to go",100,50,5000,10);
nBars = Param("Number of bars", 12, 5, 40);


// -- Plot basic candle chart

PlotOHLC(Open, High, Low, Close,

"BIdx = " + BarIndex() +

"\n" + "O = " + O + ", H = "+ H + ", L = " + L

+ ", C ",

colorBlack, styleCandle);

GraphXSpace=7;

// -- Create 0-initialized arrays the size of barcount

aHPivs = H - H;

aLPivs = L - L;

// -- More for future use, not necessary for basic plotting

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;

// -- looking back from the current bar, how many bars

// back were the hhv and llv values of the previous

// n bars, etc.?

aHHVBars = HHVBars(H, nBars);

aLLVBars = LLVBars(L, nBars);

aHHV = HHV(H, nBars);

aLLV = LLV(L, nBars);

// -- Would like to set this up so pivots are calculated back from

// last visible bar to make it easy to "go back" and see the pivots

// this code would find. However, the first instance of

// _Trace output will show a value of 0

aVisBars = Status("barvisible");

nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));

_TRACE("Last visible bar: " + nLastVisBar);

// -- Initialize value of curTrend

curBar = (BarCount-1);

curTrend = "";

if (aLLVBars[curBar] <

aHHVBars[curBar]) {

curTrend = "D";

}

else {

curTrend = "U";

}

// -- Loop through bars. Search for

// entirely array-based approach

// in future version

for (i=0; i<farback; i++) {

curBar = (BarCount - 1) - i;

// -- Have we identified a pivot? If trend is down...

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

// ... and had been up, this is a trend change

if (curTrend == "U") {

curTrend = "D";

// -- Capture pivot information

curPivBarIdx = curBar - aLLVBars[curBar];

aLPivs[curPivBarIdx] = 1;

aLPivLows[nLPivs] = L[curPivBarIdx];

aLPivIdxs[nLPivs] = curPivBarIdx;

nLPivs++;

}

// -- or current trend is up

} else {

if (curTrend == "D") {

curTrend = "U";

curPivBarIdx = curBar - aHHVBars[curBar];

aHPivs[curPivBarIdx] = 1;

aHPivHighs[nHPivs] = H[curPivBarIdx];

aHPivIdxs[nHPivs] = curPivBarIdx;

nHPivs++;

}

// -- If curTrend is up...else...

}

// -- loop through bars

}

// -- 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, shapeDownArrow, shapeNone), colorRed, 0,

High, Offset=-15);

PlotShapes(

IIf(aLPivs==1, shapeUpArrow , shapeNone), colorGreen, 0,

Low, Offset=-15);

//////////////////////////

_SECTION_END();

I think u can get these values & no of bars from latest close (Pivot highs & lows ) from text marked red & green. But how, I dont know.
Can u help me please.

see attached chart.

regards

Ajax
 
Last edited:
#14
Hi Ajax,
try this.
Code:
GraphXSpace = 20;
dist = 1.1*ATR(20);
for(m=0;m<=BarCount-1;m++){
	if(aHPivs[m] == 1)PlotText("" + (BarCount - m - 1), m, H[m]+dist[m],colorBlack);
	if(aLPivs[m] == 1)PlotText("" + (BarCount - m - 1), m, L[m]-dist[m], colorBlack);
}
for including close, try this:
Code:
GraphXSpace = 20;
dist = 1.1*ATR(20);
for(m=0;m<=BarCount-1;m++){
	if(aHPivs[m] == 1)PlotText("" + (BarCount - m - 1) + "C" + C[m], m, H[m]+dist[m],colorBlack);
	if(aLPivs[m] == 1)PlotText("" + (BarCount - m - 1) + "C" + C[m], m, L[m]-dist[m], colorBlack);
}
 
Last edited:

Ajax

Well-Known Member
#15
Hi Aushotosh

Thanx a ton for the formula. It really shows nicely on the charts the desired values...
Now a step further...
Need to build a Automatic analysis query

Need values of last 2 Pivot tops & last 2 Pivot Lows

T1 = Last Pivot high
T2 = Previous Pivot high (before T1)

B1 = Last Pivot low
B2 = Previous Pivot low (before B1)

Buy = Close > T1 and T1 > T2 and B1 > B2

Sell = Close < B1

I think from code u gave us, its possible to extract these values
Please help

Regards

PS: I will post this as AFL. Also see attached chart
 
Last edited:

Ajax

Well-Known Member
#16
Hi Aushotosh

Thanx a ton for the formula. It really shows nicely on the charts the desired values...
Now a step further...
Need to build a Automatic analysis query

Need values of last 2 Pivot tops & last 2 Pivot Lows

T1 = Last Pivot high
T2 = Previous Pivot high (before T1)

B1 = Last Pivot low
B2 = Previous Pivot low (before B1)

Buy = Close > T1 and T1 > T2 and B1 > B2

Sell = Close < B1

I think from code u gave us, its possible to extract these values
Please help

Regards

PS: I will post this as AFL. Also see attached chart
Hi auhotosh

I am adding the Analysis AFL . Please help...

////////////////////// >>>>>>>

// -- what will be our lookback range for the hh and ll?
farback= 70 ; //Param("How Far back to go",100,50,5000,10);
nBars = Param("Number of bars", 12, 5, 40);


// -- Plot basic candle chart

PlotOHLC(Open, High, Low, Close,

"BIdx = " + BarIndex() +

"\n" + "O = " + O + ", H = "+ H + ", L = " + L

+ ", C ",

colorBlack, styleCandle);

GraphXSpace=7;

// -- Create 0-initialized arrays the size of barcount

aHPivs = H - H;

aLPivs = L - L;

// -- More for future use, not necessary for basic plotting

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;

// -- looking back from the current bar, how many bars

// back were the hhv and llv values of the previous

// n bars, etc.?

aHHVBars = HHVBars(H, nBars);

aLLVBars = LLVBars(L, nBars);

aHHV = HHV(H, nBars);

aLLV = LLV(L, nBars);

// -- Would like to set this up so pivots are calculated back from

// last visible bar to make it easy to "go back" and see the pivots

// this code would find. However, the first instance of

// _Trace output will show a value of 0

aVisBars = Status("barvisible");

nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));

_TRACE("Last visible bar: " + nLastVisBar);

// -- Initialize value of curTrend


curBar = (BarCount-1);

curTrend = "";

if (aLLVBars[curBar] <

aHHVBars[curBar]) {

curTrend = "D";

}

else {

curTrend = "U";

}

// -- Loop through bars. Search for

// entirely array-based approach

// in future version

for (i=0; i<farback; i++) {

curBar = (BarCount - 1) - i;

// -- Have we identified a pivot? If trend is down...

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

// ... and had been up, this is a trend change

if (curTrend == "U") {

curTrend = "D";

// -- Capture pivot information

curPivBarIdx = curBar - aLLVBars[curBar];

aLPivs[curPivBarIdx] = 1;

aLPivLows[nLPivs] = L[curPivBarIdx];

aLPivIdxs[nLPivs] = curPivBarIdx;

nLPivs++;

}

// -- or current trend is up

} else {

if (curTrend == "D") {

curTrend = "U";

curPivBarIdx = curBar - aHHVBars[curBar];

aHPivs[curPivBarIdx] = 1;

aHPivHighs[nHPivs] = H[curPivBarIdx];

aHPivIdxs[nHPivs] = curPivBarIdx;

nHPivs++;

}

// -- If curTrend is up...else...

}

// -- loop through bars

}

// -- 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, shapeDownArrow, shapeNone), colorRed, 0,

High, Offset=-15);

PlotShapes(

IIf(aLPivs==1, shapeUpArrow , shapeNone), colorGreen, 0,

Low, Offset=-15);

//...........

GraphXSpace = 20;
dist = 1.1*ATR(20);
for(m=0;m<=BarCount-1;m++){
if(aHPivs[m] == 1)PlotText("" + (BarCount - m - 1), m, H[m]+dist[m],colorBlack);

if(aLPivs[m] == 1)PlotText("" + (BarCount - m - 1), m, L[m]-dist[m], colorBlack);
}

GraphXSpace = 20;
dist = 1.1*ATR(20);
for(m=0;m<=BarCount-1;m++){
if(aHPivs[m] == 1)PlotText("" + (BarCount - m - 1) + "H" + C[m], m, H[m]+dist[m],colorBlack);
if(aLPivs[m] == 1)PlotText("" + (BarCount - m - 1) + "L" + C[m], m, L[m]-dist[m], colorBlack);
}

////////////////

T1 = ? ; // Last Pivot High ;
T2 = ? ; // Previous Pivot High (before T1)

B1 = ? ; // Last Pivot Low
B2 = ? ; // Previous Pivot Low (before B1)

Buy = Close > T1 AND T1 > T2 AND B1 > B2 ;

Sell = Close < B1 ;
 
#17
Hi Ajax,
Remove the previous code and add this instead:
Code:
GraphXSpace = 20;
dist = 1.1*ATR(20);
T1 = 0; T2 = 0; B1 = 0; B2 = 0;
for(m=0;m<=BarCount-1;m++){
	if(aHPivs[m] == 1){
		PlotText("" + (BarCount - m - 1)+ "C" + C[m], m, H[m]+dist[m],colorBlack);
		if (T1 != 0) T2 = T1;
		T1 = H[m];
	}
	if(aLPivs[m] == 1){
		PlotText("" + (BarCount - m - 1)+ "C" + C[m], m, L[m]-dist[m], colorBlack);
		if(B1!=0) B2 = B1;
		B1 = L[m];
	}	
}

Buy = C > T1 AND T1 > T2 AND B1 > B2 ;
Sell = C < B1 ;
 

SGM

Active Member
#18
Hi Ajax,
Remove the previous code and add this instead:
Hello Ashutosh

Excellent Coding.

Hello Ajax,

Besides using HHV / LLV (as you have done) for the plotting of the pivot points we can also use zigzag function. If you have tried coding patterns like 3BR, you can use similar code for marking PPs, will post the code after mrkts.


Overall a very nice effort, keep plugging.

Regards
Sanjay

Code:
_SECTION_BEGIN("Plot-Pivots");
change = Param("% change",0.05,0.1,25,0.05);
ZigZArray = Zig(C, change);
j = k = PH[0] = PL[0] = 0;
for( i = 1; i < BarCount-1; i++ ) 
{ 
	if (ZigZArray[i] < ZigZArray[i-1] AND ZigZArray[i] < ZigZArray[i+1]) 
	{
		if (PL[k] > ZigZArray[i])
		{
		  	PlotText("LL",i, L[i]-5, colorLime);
		}
		else
		{
			PlotText("HL",i, L[i]-5, colorBlueGrey);
		}
		k = k + 1;
		PL[k] = ZigZArray[i];
	}
	if (ZigZArray[i] > ZigZArray[i-1] AND ZigZArray[i] > ZigZArray[i+1]) 
	{
		if (PH[j] < ZigZArray[i])
		{
		  	PlotText("HH", i, H[i]+5, colorBlueGrey);
		}
		else
		{
			PlotText("LH", i, H[i]+5, colorLime);
		}
		j = j + 1;
		PH[j] = ZigZArray[i];
	}
}
_SECTION_END();
 
Last edited:

Ajax

Well-Known Member
#19
Hi Ajax,
Remove the previous code and add this instead:
Code:
GraphXSpace = 20;
dist = 1.1*ATR(20);
T1 = 0; T2 = 0; B1 = 0; B2 = 0;
for(m=0;m<=BarCount-1;m++){
	if(aHPivs[m] == 1){
		PlotText("" + (BarCount - m - 1)+ "C" + C[m], m, H[m]+dist[m],colorBlack);
		if (T1 != 0) T2 = T1;
		T1 = H[m];
	}
	if(aLPivs[m] == 1){
		PlotText("" + (BarCount - m - 1)+ "C" + C[m], m, L[m]-dist[m], colorBlack);
		if(B1!=0) B2 = B1;
		B1 = L[m];
	}	
}

Buy = C > T1 AND T1 > T2 AND B1 > B2 ;
Sell = C < B1 ;
Hi Ashutosh

Tried to run the automatic analysis with Pivot code + above code.
Its giving following error:

Line 111 column 20..Error 10 Subscript out of range. You must not access array elements outside 0..(Barcount-1) range.

It seems program does not run beyond Line 111 because of this error.
I am totally ignorant about how to program. Ashutosh u have to bail us out.
Waiting for u to fix this problem
Regards
Ajax
 

Ajax

Well-Known Member
#20
Hello Ashutosh

Excellent Coding.

Hello Ajax,

Besides using HHV / LLV (as you have done) for the plotting of the pivot points we can also use zigzag function. If you have tried coding patterns like 3BR, you can use similar code for marking PPs, will post the code after mrkts.


Overall a very nice effort, keep plugging.

Regards
Sanjay
[/code]
Dear Sanjay

I know very little about the program. I picked this Pivot AFL from
AMibroker library.
It shows very nicely on charts, even better with Ashutosh/s code.
But I wish to run automatic analysis with this AFL with Buy & Sell
arguements as given earlier.. Hope u can help
Regards
Ajax
 

Similar threads