----- Original Message -----
Sent: Monday, July 30, 2007 8:06 AM
Subject: [amibroker] Procedures and
Functions
I am trying to do
some repetitive string manipulations, and keep getting errors when trying to
use a function.
Can you help me
out with this simplified code to exemplify what I am trying to
do.
Lets say I have a
series of strings like this:
T1 = "DODFX
06/10/2007 50.78 ";
T2 = "JORNX
07/13/2007 11.84";
The contents are
obvious, and I would like to set up some code which cycles through these
strings and extracts the relevant data.
In Logic, Loop
through each string, extract Sym, Date, Purchase Price, calculate some stats,
Print a line of results in an Explore grid.
I tried code like
this:
function GetData(VarA)
{
Sym1 = StrMid(VarA,1,5);
BuyDate1 = StrMid(VarA,7,10);
BuyPrice1 = StrToNum(StrMid(VarA,19,5));
P1 = Foreign(Sym1,"C");
D1 = LastValue(P1) - BuyPrice1;
Pct1 = 100 * (P1 - BuyPrice1)/BuyPrice1;
return
Sym1;
}
then
this:
function
PrintLine(Sym1,BuyDate1,BuyPrice1,P1,D1)
{
AddTextColumn(Sym1,"Symbol",1.0);
AddTextColumn(BuyDate1,"BuyDate",1.0);
AddColumn(BuyPrice1,"BuyPrice",1.2);
AddColumn(P1,"CurPrice",1.2);
AddColumn(D1,"Gain",1.2);
AddColumn(Pct1,"PcntGain",1.2);
}
for( i = 1; i < 3; i++
)
{
if (i == 1) XX = T1;
if (i == 2) XX = T2;
YY = GetData(XX);
_TRACE(Sym1);
PrintLine(Sym1,BuyDate1,BuyPrice1,P1,D1);
}
However, I can not
get past the first step of recognizing the first variable, Sym1 (Error: Sym1
has not been initialized).
I thought once I
call the function, that the pass through the function establishes the
variables contained within the function. IOW, the first call to
GetData(VarA) establishes the Variable "Sym1". This does not seem to be
the case.
So what might I do
differently? At least to get the GetData function to operate
properly. I have several other questions but would appreciate getting at
least this far.
Thanks for any
help.
Ken