Hello!
I need some help.
I have the code of the TB-F indicator. It is the TradeStation code. Could someone recode it to AFL?
Here it is:
Input: Vol_D(0), //Input Volume ("D")
TBF_Price(L), //Bar Price to use (L or H or (O+H+L)/3)
MyVolume, //Bar Volume information to use;
StartingDate(1120101), //Start Date in TradeStation date format: YYYMMDD, where YYY is years since 1900. Example date is January 1, 2012 StartingTime(1530), //Start Time in military time with nopunctuation; ample time is 3:30 pm
StartColor(Yellow), //Color of TBF curve will start with StartColor and end with EndColor . . .
EndColor(Red); //. . . and changes according to % D completion
Vars: running(false), //whether or not TBF calculation has started and not ended
pv(0), //cumulative price ā volume
vol(0), //cumulative volume
_D(0), //variable that holds input volume, D
pvInt(0), //interpolated pv
j(0), //loop iterator
e(0), //"e" variable
eT(0), //temporary copy of "e" used for iteration
tbf(0), //current calculated price of TBF curve
pct_D(0); //percent completion of TBF curve
//begin at user specified date and time
if (date = StartingDate and time = StartingTime) or running then begin
running = true;
pv = pv + TBF_Price ā MyVolume; //add current barās price ā volume to cumulative total
vol = vol + (MyVolume); //running total of volume
//begin calculation of TBF price
if Vol_D <> 0 then begin
_D = Vol_D; //store copy of input volume
e = vol ā (1 - vol / _D); //calculate "e"
//if "e" greater than zero, continue to
//calculate TBF price
//otherwise, TBF is completed
if e > 0 then begin
eT = e; //temporary copy of "e"
j = -1; //used for iteration
//iterate backwards until the cumulative
//displaced volume is greater than or
//equal to "e"
while eT > 0 begin
j = j + 1;
eT = eT - MyVolume[j];
end;
//If displaced volume is greater than "e"
//(nearly always),
//an interpolated pv amount is calculated
//for "j" bars ago using only that part of
//"j" barās volume needed to make cumulative
//displaced volume equal to "e".
//Note that at this point, "eT" is negative
//and contains the partial volume
//of "j" bars ago that should be excluded.
if eT < 0 then pvInt = TBF_Price[j] ā
(MyVolume[j] + eT) else pvInt = 0;
tbf = (pv - pv[j] + pvInt) / e; //calculate
//TBF curve
//price for
//this bar
pct_D = vol / _D ā 100; //calculate
//percent TBF
//completion
plot1(tbf, "TBF");
//Set Plot Color based on gradient between two
//Input colors
SetPlotColor(1, GradientColor(pct_D, 0, 100,
//StartColor, EndColor));
end
else running = false; //TBF curve is completed; do
//not run anymore
end;
end;
I have some AFL code. But it is working wrong. see the picture: http://imglink.ru/pictures/01-11-17/be908b33338e010cddce9917e369aa8a.jpg
And this is the right (blue or orange lines): http://imglink.ru/pictures/01-11-17/9945a772c9ef7d78559a83aee40557df.jpg
And here is the afl code (that is working wrong):
SetBarsRequired(sbrAll);
sd = ParamDate("Input Date","2014-10-06",0);
st=ParamTime("input time","10:55",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,500,900000000000000000,500);
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[i]>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",ParamColor( "Color", colorCycle ), ParamStyle("Style") );
Could someone help me?
Thanks!