AFL coding help needed ! Thanks !

#1
AFL coding checking needed ! Experts, thanks !

Hi, experts,

I am interested in the topfinder indicator by Dr. Paul Levine.

I found a MQ4 program with a programming of the topfinder.

I coded with Amibroker (as shown below the MQ4 program) but the result is not working. I think I was stuck by the complicated array and internal loops of amibroker programming, please help to check what is/are wrong in my programming.


Big thanks in advance.

AK

//+------------------------------------------------------------------+
//| MIDAS_v1.mq4 |
//| Akif TOKUZ |
//| Implementation is based on article "Introducing the MIDAS Method |
//| of Technical Analysis" by Dr. Paul Levine. |
//| 06.08.2009 |
//+------------------------------------------------------------------+
#property copyright "Akif TOKUZ"
#property link "[email protected]"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Magenta
#property indicator_color2 Magenta
#property indicator_color3 FireBrick
#property indicator_color4 Yellow
#property indicator_width1 2
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 2
#property indicator_style2 2

//---- input parameters
extern datetime StartDate=D'2009.08.06 00:00';
extern bool ShowVWAP=true;
extern bool ShowTopFinder=false;
extern double TopFinderVolumeNumBars=30;
extern bool ShowShiftedVWAP=false;
extern double shift=20;
extern bool ShowAveragePrice=false;

//---- indicator buffers
double VWAP[];
double TopFinder[];
double VWAPShift[];
double AvgPrice[];
double CumVol[];//not displayed
double CumPVol[];//not displayed

//---- global variables for holding up values at fixed "start day"
double CumPVol_J=-1;
double CumVol_J=-1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(6);

SetIndexBuffer(0,VWAP);
SetIndexBuffer(1,VWAPShift);
SetIndexBuffer(2,TopFinder);
SetIndexBuffer(3,AvgPrice);
SetIndexBuffer(4,CumVol);
SetIndexBuffer(5,CumPVol);

if (ShowVWAP==true) SetIndexStyle(0,DRAW_LINE);
else SetIndexStyle(0,DRAW_NONE);

if (ShowShiftedVWAP==true) SetIndexStyle(1,DRAW_LINE);
else SetIndexStyle(1,DRAW_NONE);

if (ShowTopFinder==true) SetIndexStyle(2,DRAW_LINE);
else SetIndexStyle(2,DRAW_NONE);

if (ShowAveragePrice==true) SetIndexStyle(3,DRAW_LINE);
else SetIndexStyle(3,DRAW_NONE);


string short_name="MIDAS";
IndicatorShortName(short_name);

SetIndexLabel(0,"VWAP");
SetIndexLabel(1,"AvgPrice");
SetIndexLabel(2,"TopFinder");
SetIndexLabel(3,"VWAPShift");
SetIndexLabel(4,"CumVol");
SetIndexLabel(5,"CumPVol");


//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i;
int counted_bars=IndicatorCounted();
double d;
double e;
double result;
double x;
double D;

//---- check for possible errors
if(counted_bars<0) return(-1);

for (i=Bars-counted_bars-1; i>=0; i--)
{
AvgPrice=(High+Low)/2;
CumVol=CumVol[i+1]+Volume;
CumPVol=CumPVol[i+1]+Volume*AvgPrice;

if (StartDate <= Time)
{
if ((CumPVol_J==-1)&&(CumVol_J==-1)) // save starting day values
{
CumPVol_J = CumPVol;
CumVol_J = CumVol;
VWAP=AvgPrice; // at the start day we need to put initial values
VWAPShift=VWAP+VWAP*shift/10000; // which are calculated differently
TopFinder=AvgPrice; // (check Dr. Levine's lectures)
}else
{
VWAP=( CumPVol - CumPVol_J ) / ( CumVol - CumVol_J );
VWAPShift=VWAP+VWAP*shift/10000;

d=( CumVol - CumVol_J ); // calculate TopFinder
D=TopFinderVolumeNumBars*(CumVol/Bars); // multiply TopFinderVolumeNumBars input with average volume per bar
e=d*(1-d/D); // when d/D gets close to 1, e will be zero.

x=CumVol-e;
if (d/D<0.9999) // protection from division by zero
{
for (int j=0;j<Bars-counted_bars-1;j++)
{
if (CumVol[j]<x) break;
}

if (j!=0) // Zero bar check
{
// interpolation required to find the exact value (again check Dr. Levine's lectures)
result= CumPVol[j]*(x - CumVol[j-1])/(CumVol[j] - CumVol[j-1]) +
CumPVol[j-1]*(x - CumVol[j])/(CumVol[j-1] - CumVol[j]);
TopFinder=( CumPVol - result ) / e;
}
}
}
}
}

//---- done
return(0);
}
//+------------------------------------------------------------------+


VolNumBars = Param("VolNumBars",50,5,1500,1);

dn = DateTime();
sd = SelectedValue( dn );

start = dn == sd;

Cv[BarCount-1] = 0;
Cpv[BarCount-1] = 0;
tfinder[0] = LastValue((H+L)/2);
startpv = -1;
startcv = -1;
for (i = BarCount -2 ; i >=0; i--)
{
mp = L;
cv = cv[i+1] + V;
cpv = cpv[i+1] + V * mp;
vwap = mp;

if (dn >= sd)
{
if (dn == sd)
{
startpv = Cpv;
startcv = Cv;
vwap = mp;
tfinder = mp;
}
else
{
vwap = (cpv - startpv)/(cv - startcv);
d = (cv - startpv);
DD = VolNumBars * cv /BarCount;
e = d * (1 - d/DD);
x = cv - e;

if (d/DD < 0.9999)
{
for (j = 0; j <BarCount-1; j++)
{
if (Cv[j] < x) break;
}
if (j !=0)
{
//interpolation
result= PV[j] * ( x - CV[j-1])/(CV[j] - CV[j-1]) + PV[j-1] * (x - CV[j]) /(CV[j-1] - CV[j]);
tfinder[j] = (PV[j] - result)/e;
}
}

}
}
}


Plot( tfinder, "TopFinder", colorRed , styleLine | styleThick | styleNoRescale );
 
Last edited:

zultan

Well-Known Member
#2
if possible to see Image with MQ4
thank you



this is somthing els of Andrew Coles draws upon previous market analysis work done by Paul Levine and George Reyna.


AMIBROKER: ANCHORED VWAP CHANNEL
Implementing the anchored Midas percentage channel indicator presented in Andrew Coles’ article in this issue is easy in AmiBroker Formula Language.
A ready-to-use formula is presented in the Listing 1. To use it, enter the formula in the Afl Editor, then press the “Insert indicator” button. To select an anchor point, simply click on the chart in desired place. AmiBroker will plot the channel automatically. You may also adjust the channel width by clicking on the chart with the right mouse button and choosing


if you are color blind you will not see the listed below formula:)



Uploaded with ImageShack.us



_SECTION_BEGIN("ANCHORED VWAP CHANNEL");

dn = DateTime();
sd = SelectedValue( dn );

start = dn == sd;

mp = (H+L)/2;

PV = mp * V;
CV = Cum( V );
VSS = CV - ValueWhen( start, CV );

denom = IIf( VSS == 0, 1, VSS );
num = Cum( PV ) - ValueWhen( start, Cum( PV ) );

M = IIf( BarsSince( start ), num/denom, mp );

Q1 = Param("Percentage Upper", 1, 0, 10, 0.01 );
Q2 = Param("Percentage Lower", 1, 0, 10, 0.01 );

Plot( C, Date() + " Close", colorBlack, styleBar );
Plot( M, "M" + _PARAM_VALUES(), colorBlue );
Plot( M * ( 1 + Q1 * 0.01 ), "Upper", colorGreen );
Plot( M * ( 1 - Q2 * 0.01 ), "Lower", colorRed );
_SECTION_END();
 
Last edited:
#3
Hi, Zultan,

Thanks for your help to look into my case. Appreciated.

Midas is very good to determine support and resistance. Topfinder/Bottomfinder is more complex but it is useful to determine the top and the bottom based on the principle of Midas but it needs interpolation and curve fitting a suitable path for the trend to end.

It is more advanced. I found a site with the details of topfinder.

http://www.baylight.com/aaii/VolAnalysis/Part3/sld004.htm

I consider it is a very good indicator to help us to arrive a top after explosive run.

If you have time, please study it.....

Anyway, big thanks for your attention.


Other experts, please help me to point out my mistakes in my Amibroker AFL attached. I was stuck by array structure of Amibroker variables because my experience with programming in Visual Basic. In VB, I can always use a variable to store up a single value so that I can store up the value of CV/CPV at the start date, e and d in single value variables and will not confused by array structure variable (with multiple values based on the index) in all variables of Amibroker. The situation is worsen because you can have array variable operations (manipulate all values of array variables in one operation) and internal looping to manipulate individual values of an array variable.

I cannot break through these hurdles after almost a year of work.
If any expert can give me some points/tip/hints how to get over this, please help out to provide some guidelines. I am much appreciated your help in advance.

Best regards,

AK
 
#4
Hi, Zultan,

Thanks for your help to look into my case. Appreciated.

Midas is very good to determine support and resistance. Topfinder/Bottomfinder is more complex but it is useful to determine the top and the bottom based on the principle of Midas but it needs interpolation and curve fitting a suitable path for the trend to end.

It is more advanced. I found a site with the details of topfinder.

http://www.baylight.com/aaii/VolAnalysis/Part3/sld004.htm

I consider it is a very good indicator to help us to arrive a top after explosive run.

If you have time, please study it.....

Anyway, big thanks for your attention.


Other experts, please help me to point out my mistakes in my Amibroker AFL attached. I was stuck by array structure of Amibroker variables because my experience with programming in Visual Basic. In VB, I can always use a variable to store up a single value so that I can store up the value of CV/CPV at the start date, e and d in single value variables and will not confused by array structure variable (with multiple values based on the index) in all variables of Amibroker. The situation is worsen because you can have array variable operations (manipulate all values of array variables in one operation) and internal looping to manipulate individual values of an array variable.

I cannot break through these hurdles after almost a year of work.
If any expert can give me some points/tip/hints how to get over this, please help out to provide some guidelines. I am much appreciated your help in advance.

Best regards,

AK
 
#5
Hi, Zultan,

Thanks for your help to look into my case. Appreciated.

Midas is very good to determine support and resistance. Topfinder/Bottomfinder is more complex but it is useful to determine the top and the bottom based on the principle of Midas but it needs interpolation and curve fitting a suitable path for the trend to end.

It is more advanced. I found a site with the details of topfinder.

http://www.baylight.com/aaii/VolAnalysis/Part3/sld004.htm

I consider it is a very good indicator to help us to arrive a top after explosive run.

If you have time, please study it.....

Anyway, big thanks for your attention.


Other experts, please help me to point out my mistakes in my Amibroker AFL attached. I was stuck by array structure of Amibroker variables because my experience with programming in Visual Basic. In VB, I can always use a variable to store up a single value so that I can store up the value of CV/CPV at the start date, e and d in single value variables and will not confused by array structure variable (with multiple values based on the index) in all variables of Amibroker. The situation is worsen because you can have array variable operations (manipulate all values of array variables in one operation) and internal looping to manipulate individual values of an array variable.

I cannot break through these hurdles after almost a year of work.
If any expert can give me some points/tip/hints how to get over this, please help out to provide some guidelines. I am much appreciated your help in advance.

Best regards,

AK
 
#6
Re: AFL coding checking needed ! Experts, thanks !

.
.
.


for (i=Bars-counted_bars-1; i>=0; i--)
{
AvgPrice=(High+Low)/2;
CumVol=CumVol[i+1]+Volume;
CumPVol=CumPVol[i+1]+Volume*AvgPrice;

.
.
.
.


Array of array is not supported in ami. High, Low, Volume etc are arrays in themselves. And another array of them is not supported. If you know that i is a fixed value, you can write code using fixed number of variables. Or it is going to be ultra complex code that is going to run super slow.

thank you
nagarjuna
 
#7
Dear Nagarjuna,

The code you quoted is the MT4 code that I am trying to follow to recode topfinder into Amibroker. I did not use array of array in AFL below.

My code in AFL is as follows:

//+------------------------------------------------------------------+


VolNumBars = Param("VolNumBars",50,5,1500,1);

dn = DateTime();
sd = SelectedValue( dn );

start = dn == sd;

Cv[BarCount-1] = 0;
Cpv[BarCount-1] = 0;
tfinder[0] = LastValue((H+L)/2);
startpv = -1;
startcv = -1;
for (i = BarCount -2 ; i >=0; i--)
{
mp = L;
cv = cv[i+1] + V;
cpv = cpv[i+1] + V * mp;
vwap = mp;

if (dn >= sd)
{
if (dn == sd)
{
startpv = Cpv;
startcv = Cv;
vwap = mp;
tfinder = mp;
}
else
{
vwap = (cpv - startpv)/(cv - startcv);
d = (cv - startpv);
DD = VolNumBars * cv /BarCount;
e = d * (1 - d/DD);
x = cv - e;

if (d/DD < 0.9999)
{
for (j = 0; j <BarCount-1; j++)
{
if (Cv[j] < x) break;
}
if (j !=0)
{
//interpolation
result= PV[j] * ( x - CV[j-1])/(CV[j] - CV[j-1]) + PV[j-1] * (x - CV[j]) /(CV[j-1] - CV[j]);
tfinder[j] = (PV[j] - result)/e;
}
}

}
}
}


Plot( tfinder, "TopFinder", colorRed , styleLine | styleThick | styleNoRescale );

I did not find any error in my code but it did not give a good line for topfinder...

By the way, do you know how to list out the values of an array ?
If I used some thing like
for (i = 0; i <BarCount-1; i++)
{
"TFinder = " + tfinder;
}

I do not get anything print out and so I cannot do any further debug ?

Best regards,

AK
 
#8
Dear Zultan,

I managed to find a chart for Midas (Purple) and TFinder(Red).

You can see the topfinder can predict the top quite well and therefore I am interested to try to code it in Amibroker.

Any help from experts are appreciated !





Thanks...

AK
 
#9
MIDAS TOP/BOTTOM FINDER

//

SetBarsRequired(sbrAll);

sd = ParamDate("Input Date","2012-02-14",0);
st=ParamTime("input time","1:44",0);
dn = DateNum();
tn=TimeNum();

start = dn == sd AND tn==st;



mp = ParamField("Price field",-1);

PV1= Cum(mp * V);
CV1= Cum( V );

D=Param("D",6120000000,50000,9000000000000);

PV=PV1-ValueWhen(sd==dn AND tn==st,PV1,1);
CV=CV1-ValueWhen(sd==dn AND tn==st,CV1,1);

E=CV*(1-CV/D);

ET=CV-E;


startloop=EndValue(ValueWhen(sd==dn AND tn==st,BarIndex(),1));
endloop=EndValue(IIf(CV>D,ValueWhen(CV<D,BarIndex( ),1),BarCount));

barint=0;

TBF=mp;

for (j=startloop+1;j<endloop;j++)
{
k=0;
i=startloop+1;
while (ET[j]>0 AND k==0)
{

if (CV>ET[j])
{
k=1;

}
i=i+1;
}

if (k==1)
{
barint[j]=i-1;
}

Volint[j]=CV[barint[j]]-ET[j];
Mpint[j]=Low[barint[j]]*Volint[j];

CVint[j]=CV[j]-CV[barint[j]]+Volint[j];
PVint[j]=PV[j]-PV[barint[j]]+Mpint[j];
TBF[0]=Low[0];
TBF[j]=PVint[j]/CVInt[j];




}

TBF1=IIf(BarsSince(start),TBF,Null);

PCT=100*CV/D;



tbf2=IIf(BarIndex()<endloop,TBF1,Null);









Plot(TBF2,"PCT="+WriteVal(PCT,1.0)+"TBF",ParamColo r( "Color", colorCycle ), ParamStyle("Style") );


//

(Coded in AMIBROKER by me)

WORD OF CAUTION----- this is a very sophisticated tool pinpointing the end of a trending move and usually used either to book profits or to add positions------ do not use it without going through LEVINE's basic lessons on MIDAS ( freely available on web) and/or David Hawkins and Andrew Coles work
 
#10
Wildeazoscar,

Did you run your AFL coding for levine's TBF? The reason I ask is that having just bought AB, I copied your program & received the dreaded "Syntax error" message.

ATB,

Jo
 

Similar threads