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

Re: [amibroker] Re: Using Functions to Process Arrays



PureBytes Links

Trading Reference Links

Hi Pete - see my comments inserted below...

Steve

----- Original Message ----- 
From: "Pete" <dryheat3@xxxxxxx>
To: <amibroker@xxxxxxxxxxxxxxx>
Sent: Saturday, August 09, 2008 8:49 PM
Subject: [amibroker] Re: Using Functions to Process Arrays


> Thanks for the ideas. Let me explain what I am trying to grasp. I'll
> show the sample given in the AB help file once again.
> Ok, so in the function below there is a loop but before the loop I see
> the two lines:
> <<result[ 0 ] = input[ 0 ];>>
> <<result[ 1 ] = input[ 1 ];>>
>
> Now, in my mind this is creating an array. In VBA code you create
> arrays by declaring a variable and assigning each element of the array
> by using (0), (1), (2)... and so on. You then use this same method to
> reference the individual elements in the array. So the way I read
> these two lines is like this.
> Assign Variable 'result' Element 1 = F-Argument 'input' Element 1
> Assign Variable 'result' Element 2 = F-Argument 'input' Element 2
>
> So far so good? This is where I'm confused. if you have input[0] and
> input[1] available at the time of execution, then the argument 'input'
> must be an array to begin with. Tell me if I'm wrong.

You are right! This sort of stuff sometimes confuses the experienced 
programmers more than the newbies because they have to unlearn the way they 
have been programming for years. AFL is an array-based language, everything 
is automatically an array, and if it isn't then it is automatically 
converted to one when necessary. Once you get the hang of it, it really 
simplifies things! It starts with your data, which are arrays. So, if you 
then just write

MyClose = Close;  //  now you have created a new array, MyClose.

I think about the only times you need to refer to individual elements are if 
you want to set/get the value of that specific element, or if you are using 
recursive methods ( since regular AFL operates on the entire array at once, 
it cannot refer to previous values of itself because all elements are 
created at the same time, hence the need for looping in that case )

>
> So, next is the loop:
> <<for( i = 2; i < BarCount; i++ )>>
>
> So I understand the 'i' is used to iterate through each element of the
> array. At this point I have to assume 'input' is an array passed to
> the function and the loop uses the first two elements of this array to
> calculate and return a value. Here is where I am lost. Considering the
> most likely candidate for 'input' would be the array of the closing
> price of the stock, how can this thing work if it only uses the values
> for the first two bars in the array [0] and [1]?


In this case, the formula is recursive ( the value of each element depends 
on the value of the preceding 2 elements ). For this, looping is needed as 
explained above.  So the first 2 lines are just to initialize the first 2 
elements, as those values are used inside the loop. After that, the looping 
code will create the required values for each new element as it walks the 
array, and those values will be used for future elements...anyway, its 
Saturday night and I need to get my exercises in - 12 oz curls!   8 - ). 
Will be back later!



>
>
>> function IIR2( input, f0, f1, f2 )
>> {
>>    result[ 0 ] = input[ 0 ];
>>    result[ 1 ] = input[ 1 ];
>>
>>    for( i = 2; i < BarCount; i++ )
>>    {
>>       result[ i ] = f0 * input[ i ] +
>>                     f1 * result[ i - 1 ] +
>>                     f2 * result[ i - 2 ];
>>    }
>>
>>   return result;
>> }
>> //end code ***************************************
>
>
>
>
> --- In amibroker@xxxxxxxxxxxxxxx, "Steve Dugas" <sjdugas@xxx> wrote:
>>
>> Hi again - just had another look at your question, I notice that you
> just
>> refer to "processing arrays". You picked an example that uses
> looping but
>> you can wrap any AFL in a function, you don't need to use looping
> unless you
>> really need it...Thnk of the function as sort of a container - it will
>> basically hold any code that you could just as easily put inline,
> not much
>> reason to use one I don't think unless you want to call the code from
>> several places or it is part of some larger organizational scheme...
>>
>> Steve
>>
>> ----- Original Message ----- 
>> From: "Steve Dugas" <sjdugas@xxx>
>> To: <amibroker@xxxxxxxxxxxxxxx>
>> Sent: Saturday, August 09, 2008 7:18 PM
>> Subject: Re: [amibroker] Using Functions to Process Arrays
>>
>>
>> > Hi - Don't know of any tutorial but not sure you really need one.
> Do you
>> > understand basic looping already? You could start with any inline
> looping
>> > code. Once you have that working and understand it, you can just
> wrap it
>> > in
>> > a function and pass it the arrays it needs via the function args.
> Also,
>> > make
>> > sure the function is defined before it is called. I believe there
> are a
>> > couple of of looping tutorials in the files section, I wrote a
> very brief
>> > one and I think it was GP that wrote a better one. If you have
> something
>> > you
>> > can't get to work, feel free to post it...
>> >
>> > Steve
>> >
>> > ----- Original Message ----- 
>> > From: "Pete" <dryheat3@xxx>
>> > To: <amibroker@xxxxxxxxxxxxxxx>
>> > Sent: Saturday, August 09, 2008 6:37 PM
>> > Subject: [amibroker] Using Functions to Process Arrays
>> >
>> >
>> >>I have a bit of experience with building functions in VBA so I get the
>> >> basic concept. But the projects I've built in the past did not rely
>> >> heavily on processing arrays within functions. What I would really
>> >> like is to get my brain around this so I understand exactly what's
>> >> taking place and, hopefully, to better apply this technique to my AFL
>> >> coding.
>> >> From the AB help files:
>> >> I simple function for a second order smoother:
>> >> //begin code ************************************
>> >> function IIR2( input, f0, f1, f2 )
>> >> {
>> >>    result[ 0 ] = input[ 0 ];
>> >>    result[ 1 ] = input[ 1 ];
>> >>
>> >>    for( i = 2; i < BarCount; i++ )
>> >>    {
>> >>       result[ i ] = f0 * input[ i ] +
>> >>                     f1 * result[ i - 1 ] +
>> >>                     f2 * result[ i - 2 ];
>> >>    }
>> >>
>> >>   return result;
>> >> }
>> >> //end code ***************************************
>> >>
>> >> Do any of you know where I can get a quick and easy tutorial on
>> >> processing arrays in functions so I can better understand how this
>> >> looping works?
>> >>
>> >>  Pete  :-)
>> >>
>> >>
>> >> ------------------------------------
>> >>
>> >> 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
>> >>
>> >>
>> >>
>> >>
>> >
>> >
>> >
>> > ------------------------------------
>> >
>> > 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
>> >
>> >
>> >
>> >
>>
>
>
>
> ------------------------------------
>
> 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
>
>
>
> 



------------------------------------

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/