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

Re: [amibroker] Problem with using "function" in AFL



PureBytes Links

Trading Reference Links

A function provides a value to a variable within the code, thus the
need for the return statement. Without this return statement the
function does nothing at all
A procedure does an action, like Plot something, but does not provide
any data to the rest of the code.
It is possible to provide variable values to the code with a
procedure, or more than one variable value with a function, if you
define variables within the function or procedure as Global variables.

Your original code
function rule1(aver,width,percent)
{
       Bbot = BBandBot(Close,aver,width);
       Buy_cond1 = Close < BBot /percent;
       BuyPrice = Close;
}

if (p11 = 1) rule1(10,1.5,1.005);

Buy = Buy_cond1;

has some main problems.
1 you have no return in the function, yet you try to use this in your
buy statement
2 you ahve made the use of the function in an if conditional
statement, but not given it any value if that condition were false and
so there is no values for the buy to use, it will return an error
3 you use many variables within the function that will not be read
outside the function, like the buyprice. See my comments about the
global variables
4 your IF condition is wrong as you are defining P11 to be 1
5 you do not use the return variable in the main code, this is just
for defining what values are to be passed back to the code


to correct these
function rule1(aver,width,percent)
{
       Bbot = BBandBot(Close,aver,width);
       Buy_cond1 = Close < BBot /percent;
return buy_cond1;
}

Buy = iif(p11==1, rule1(10,1.5,1.005) , 0 ) ;
BuyPrice = Close;

typically you could have done this without the function, but I also
assume this is just a trial you are doing to learn how to use them

Bbot = BBandBot(Close,aver,width);
Buy = Close < BBot /percent;
BuyPrice = Close;

-- 
Cheers
Graham
AB-Write >< Professional AFL Writing Service
Yes, I write AFL code to your requirements
http://e-wire.net.au/~eb_kavan/ab_write.htm



On 28/05/06, Terry <MagicTH@xxxxxxxxxxx> wrote:
> I'm can't explain why it works one way and not the other.
>
> This code give a compile error at this line:
>
> Buy = Buy_Cond1;
>
> The reason is Buy_Cond1 is undefined because it is inside a function and
> is local to that function.
>
> Add this line as the last line of your function:
>     return Buy_Cond1:
>
> Then change your function call to:
>
>     Buy = rule1(10,1.5,1.005);
> --
> Terry
> -----Original Message-----
> From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On
> Behalf Of soundscribe_studios
> Sent: Saturday, May 27, 2006 15:24
> To: amibroker@xxxxxxxxxxxxxxx
> Subject: [amibroker] Problem with using "function" in AFL
>
> I need to call a function with parameters, but for some reason I can't
> get it to work when using the variables passed into the function. In
> the code below, if I explicitly set the variable values, the function
> works fine -- executing about 80 trades. If I have them commented out
> (and the passed in values are used), the system doesn't execute any
> trades. I make no other changes.
>
> I've plotted the values when passed in and they appear to be correct.
> What am I missing here? This is AB 4.80.1 although it behaves the same
> on older versions as well.
>
> What's also odd is if I plot the "Buy" array, it looks exactly the
> same whether or not the variables are explicitly set -- just no trades
> get executed when the passed in values are used. At a loss here...
>
> /////////////////////////////////
> ... some code to read from a file ...
>
> function rule1(aver,width,percent)
>         {
>
> //aver =10;
> //width=1.5;
> //percent=1.005;
>
>         Bbot = BBandBot(Close,aver,width);
>         Buy_cond1 = Close < BBot /percent;
>         BuyPrice = Close;
> }
>
> if (p11 = 1)
> {
> //rule1(p12,p13,p14);
> rule1(10,1.5,1.005);
> }
>
>
> .... some other code -- nothing touching Buy_cond1 or Buy ....
>
>
> entrybar = -1;
>
> //Buy = Buy_cond1 AND Buy_cond2 AND Buy_cond3;
> Buy = Buy_cond1;
>
> // exit code
>
> for( i = 0; i < BarCount; i++ )
>         {
>         if( entrybar == -1 AND Buy[ i ] )
>                 {
>                 entrybar = i;
>                 }
>
>         if( entrybar != -1 AND i > entrybar AND Close[ i ] > Close[
> entrybar
> ]+0.02 OR i-entrybar>=20 )
>                 {
>                 Sell[ i ] = 1;
>                 SellPrice[ i ] = Close[ i ];
>                 entrybar = -1;
>                 }
>         else
>                 {
>                 Sell[ i ] = 0;
>                 }
>         }
>
>
>
>
>
>
>
> 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 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 other support material please check also:
> http://www.amibroker.com/support.html
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>


------------------------ Yahoo! Groups Sponsor --------------------~--> 
You can search right from your browser? It's easy and it's free.  See how.
http://us.click.yahoo.com/_7bhrC/NGxNAA/yQLSAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

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 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/

<*> 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/