[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Procedures and Functions



PureBytes Links

Trading Reference Links

You have a variable scoping problem, where all those variables you
initialise inside GetData are local and cannot be accessed from
outside. You can declare them all global to get around that, but
unless you're going to call GetData more than once, you may as well
just inline the function (ie. put the code directly into your main
code and not have a function at all).

To make the variables global, you can either declare them global
inside the function, or just list them all before the function:

BuyDate1 = "";
BuyPrice1 = Null;
P1 = Null;
D1 = Null;
Pct1 = Null;

function GetData(VarA)
{
etc.

For the variable Sym1 which is returned from the function, in your
main code where you store it in YY, you should subsequently use YY
instead of Sym1:

_TRACE(YY);
PrintLine(YY,etc.);

Regards,
GP


--- In amibroker@xxxxxxxxxxxxxxx, "Ken Close" <ken45140@xxx> wrote:
>
> 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
>




Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/