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

[amibroker] Re: using global variables in multi-time frame



PureBytes Links

Trading Reference Links

Graham,

For your, and anyone else's, interest I found the solution. I've
simplified the code with a simple MA to illustrate. Example 2 is what
works. The problem lay in where I defined the "Price" variable. See code
for comments.

I had been under the mistaken assumption that when TimeFrameSet command
is used then it compresses ALL global variables, but obviously this is
not the case. In fact I hand't thought the process through properly and
it makes perfect sense now...

Also, the Y=0;  initialisation does the same job as the "global"
statement...it sets it as a global variable as it is defined outside the
procedure and before. Therefore no need to use "global" inside the
procedure.

Anyway, thanks for listening and taking the time to try to help.
Andy


EXAMPLE 1 (this doesn't work)

X = ParamToggle("Price Field", "Mid Price | Close", 0);
Price = IIf(X==0,(H+L)/2,Close);     //although this is a global
variable, it is defined early and not
                                                                     
subsequently re-defined following the TimeFrameSet command
Y = 0;

procedure Test()
  {
     Y = MA(Price,20);
  }

Test();
Plot( Y,"",colorRed );

TimeFrameSet(inWeekly);
Test();
TimeFrameRestore();
Plot( TimeFrameExpand(Y,inWeekly,expandLast),"",colorBlue);


EXAMPLE 2 (this is correct)

X = ParamToggle("Price Field", "Mid Price | Close", 0);
Y = 0;

procedure Test()
  {
     Price = IIf(X==0,(H+L)/2,Close);   //the price array is now a local
variable, but it's properly compressed
                                             by the TimeFrameSet command
when the procedure is called the second time around
     Y = MA(Price,20);
  }

Test();
Plot( Y,"",colorRed );

TimeFrameSet(inWeekly);
Test();
TimeFrameRestore();
Plot( TimeFrameExpand(Y,inWeekly,expandLast),"",colorBlue);


--- In amibroker@xxxxxxxxxxxxxxx, Graham <kavemanperth@xxx> wrote:
>
> You must use the global inside the procedure otherwise the values are
not
> passed to the general AFL
> Remember a procedure does not pass any values back to the aFL, it only
does
> something, like plot or explore addcolumn
> A Function will pass a single variable array to the general AFL, wihch
is
> defined in the return statement.
>
> --
> Cheers
> Graham
> AB-Write >< Professional AFL Writing Service
> Yes, I write AFL code to your requirements
> http://www.aflwriting.com
>
>
> On 06/12/06, andy_davidson_uk1 AndyDavidson@xxx wrote:
> >
> > Graham,
> >
> > Thanks for the reply. However, I've used both approaches and there
> > seems to be no difference. I still get the wrong result from the
> > "inWeekly" procedure call.
> >
> > So I tested the general approach with some much simpler code (a
couple
> > of MAs) and all seems to work OK. The logical conclusion therefore
is
> > that the error has something to do with what I'm doing in the
"process
> > some stuff" stage. Quite possible since, as I said before, it's
quite
> > lengthy code. But quite what that problem is has got me stumped...
> >
> > I'm going to have to re-build the code block-by-block to try and
find
> > out where the problem is. What fun.
> >
> > Question on your "Method 2" though. Is there any need for the
'global'
> > definitions inside the procedure when the variables have already
been
> > defined as global (i.e. they were named and initialised outside and
> > before the procedure)? It makes no difference to my observations
with
> > my code either way, but just curious if there is a best-practice
> > reason at play there?
> >
> > Regards,
> > Andy
> >
> >
> > --- In amibroker@xxxxxxxxxxxxxxx, Graham kavemanperth@ wrote:
> > >
> > > 2 ways you would need to do this as procedures do not provide any
real
> > > output, only an action
> > >
> > > METHOD 1
> > >
> > > X =Y = 0; //initialise global variables on first pass through code
> > >
> > > procedure Test(TimeFrame)
> > > {
> > >    TimeFrameSet( Timeframe );
> > >        ...now process some stuff and allocate values to the global
> > > variables ...
> > >    X = xx;
> > >    Y = yy;
> > >    TimeFrameRestore();
> > > Plot(TimeFrameExpand(X,Timeframe ,expandLast), "", colorblue);
> > > Plot(TimeFrameExpand(Y,Timeframe expandLast), "", colorblue);
> > >
> > > }
> > >
> > > Test(indaily);
> > > Test(inweekly);
> > >
> > >
> > >
> > >
> > > METHOD 2
> > >
> > >
> > > X =Y = 0; //initialise global variables on first pass through code
> > >
> > > procedure Test(TimeFrame)
> > > {
> > > Global x;
> > > global y;
> > >    TimeFrameSet( Timeframe );
> > >        ...now process some stuff and allocate values to the global
> > > variables ...
> > >    X = xx;
> > >    Y = yy;
> > >    TimeFrameRestore();
> > > }
> > >
> > > //now call on the procedure in daily time frame and plot
> > > Test(inDaily);
> > > Plot(TimeFrameExpand(X,inDaily,expandLast), "", colorblue);
> > > Plot(TimeFrameExpand(Y,inDaily,expandLast), "", colorblue);
> > >
> > > //now call on the procedure in weekly time frame and plot
> > > Test(inWeekly);
> > > Plot(TimeFrameExpand(X,inWeekly,expandLast),"",, "", colorred);
> > > Plot(TimeFrameExpand(Y,inWeekly,expandLast),"",, "", colorred);
> > >
> > >
> > >
> > > --
> > > Cheers
> > > Graham
> > > AB-Write >< Professional AFL Writing Service
> > > Yes, I write AFL code to your requirements
> > > http://www.aflwriting.com
> > >
> > >
> > > On 05/12/06, andy_davidson_uk1 AndyDavidson@ wrote:
> > > >
> > > > I'm using a procedure to output several global variables. This
works
> > > > fine normally (i.e. all variables are returned OK), but when
when
> > I try
> > > > to use it in conjunction with multi-time frame analysis I run
across
> > > > problems.
> > > >
> > > > A simplified version of the code is shown below. I would copy it
> > all but
> > > > there's a lot of lines and I'm sure the "process some stuff" bit
> > is all
> > > > working fine anyhow. The problem is that (on an EOD chart) the
daily
> > > > plots are fine, but the expanded weekly TF plots are not.
Variables X
> > > > and Y are unchanged by the second procedure call...i.e. they are
the
> > > > same *values* as discovered in the daily time frame - only
> > expanded to a
> > > > weekly plot. If I switch the chart to a weekly plot I get
> > > > differing values for the X and Y variables, so there is no doubt
that
> > > > this is plotting incorrectly.
> > > >
> > > > In the full code there are a lot more than just two variables
returned
> > > > by the procedure, so I need to get this sorted to avoid getting
> > > > unecessarily messy.
> > > >
> > > > The basic logic looks fine to me. Grateful if someone could
point out
> > > > what I'm missing,
> > > > TIA
> > > > Andy
> > > >
> > > > //-----------------------------
> > > > X =Y = 0; //initialise global variables on first pass through
code
> > > >
> > > > procedure Test(TimeFrame)
> > > > {
> > > >     TimeFrameSet( Timeframe );
> > > >         ...now process some stuff and allocate values to the
global
> > > > variables ...
> > > >     X = xx;
> > > >     Y = yy;
> > > >     TimeFrameRestore();
> > > > }
> > > >
> > > > //now call on the procedure in daily time frame and plot
> > > > Test(inDaily);
> > > > Plot(TimeFrameExpand(X,inDaily,expandLast), "", colorblue);
> > > > Plot(TimeFrameExpand(Y,inDaily,expandLast), "", colorblue);
> > > >
> > > > //now call on the procedure in weekly time frame and plot
> > > > Test(inWeekly);
> > > > Plot(TimeFrameExpand(X,inWeekly,expandLast),"",, "", colorred);
> > > > Plot(TimeFrameExpand(Y,inWeekly,expandLast),"",, "", colorred);
> > > >
> > > > //------------------------------------
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > 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
> >
> >
> >
> >
>



Content-Description: "AVG certification"
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.15.9/573 - Release Date: 12/5/2006 4:07 PM