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

[amibroker] Re: Tips for reducing CPU load



PureBytes Links

Trading Reference Links



Bisto -

Yes, replace your entire previous example program with the code at the end of this post.  First, let's restate your problem to be solved -

"For each bar in an array, find the value of RSI over a lookback period that represents a given percentile rank of the min/max range over the same lookback period."

Now from the Amibroker Help file for the Percentile() function, note the syntax and return value -

SYNTAX Percentile( array, period, rank )
RETURNSARRAY

I've underlined the words in the problem statement above that correspond to the parameters.  And, note that the Percentile() function returns an array - so, it does the operation for each bar.  In other words, it is doing everything for you, including the bar loop.  Obviously, there is a lot of calculations and sorting being done in side the function.  You will see slightly different values from your original program because the Percentile() function may have to interpolate the value of the target rank.  But that is why I said it is "close enough".

Anyway, here's a complete AFL.  Replace your ENTIRE original example AFL with -

periods = Param( "Periods", 15, 1, 200, 1 );
Oscill = RSI( periods );
NMonths = 6;
Lback = NMonths * 22;
spcnt = 5;
bpcnt = 100 - spcnt;
//  The use of Ceil() and Floor() are to make the results closer to the original AFL
//  and are OPTIONAL
spcntval = ceil( Percentile( Oscill, lback, spcnt ) );
bpcntval = floor( Percentile( Oscill, lback, bpcnt ) );
//  Assign to original names to indicate comparison target in the original AFL
IperSArray = spcntval;
IperBArray = bpcntval;
Filter = 1;
AddColumn( RSI( periods ), "RSI", 8.3 );
AddColumn( IperSArray, "IperSArray", 5.1 );
AddColumn( IperBArray, "IperBArray", 5.1 );


- In amibroker@xxxxxxxxxxxxxxx, "Bisto" <bistoman73@xxx> wrote:
>
>
> Wow Bruce,
>
> I really missed Percentile() indeed! It removes the need to use the while loop that forced me to not use array operators. I was sure that there was a better way to code my indicator but I was not suspecting a _so better_ way thanks to a built in function.
>
> If I well understood you you was talking that there was also the chance to remove the outer "bar" loop still using the while loop?
> How is it possible? This just to learning pourpose
>
> My ugly coded indicator took ONLY 5.4s to be calculated, not 27s (5.4 x5 optimization steps) :-)
>
> Thanks Bruce, TJ, Howard and all the people who are always so available to teach to motivated newbie
>
>
> --- In amibroker@xxxxxxxxxxxxxxx, "Bruce" brucer@ wrote:
> >
> > Bisto -
> >
> > I was catching up on reading the board and got tickled by your exchange
> > with Tomasz. In talking about saving results in static variables, he
> > said - " IMHO, it is not worth the effort, unless you are doing
> > something _extremely_ complex.". But, then you replied that you
> > indicator took 27 seconds !! So, certainly static save made a
> > difference !
> >
> > I think that you recognized that there might be a better way to code
> > what you were after. And, I was about to show how to eliminate the
> > outer "bar" loop with the same algorithm, but I took another look at
> > what you are trying to accomplish. You really need to take a look
> > instead at the Percentile() function that is built into Amibroker. It
> > is not talked about much and is easy to miss.
> >
> > Consider this. What you seem to be basically doing is calculating a
> > percentage of lookback days as -
> >
> > Limit = 5;
> > DayLimit = NMonths * 22 * Limit / 100;
> >
> > And, then I think that you find the integer RSI value for which that
> > number of days is less than for the sell and greater than for the buy
> > daylimit.
> >
> > The Percentile() function can be used to do this. Replace the entire
> > For() loop and nested code with just the code below -
> >
> > Lback = NMonths * 22;
> > spcnt = 5;
> > bpcnt = 100 - spcnt;
> > // The use of Ceil() and Floor() to yield integer levels is really a
> > business decision
> > // about trading a little earlier OR little later - AND should be set
> > appropriately
> > spcntval = ceil( Percentile( Oscill, lback, spcnt ) );
> > bpcntval = floor( Percentile( Oscill, lback, bpcnt ) );
> > IperSArray = spcntval;
> > IperBArray = bpcntval;
> >
> > It will give slightly different results due to interpolation of values,
> > but should be equal your RSI values +/- 2. Like horseshoes and hand
> > grenades, close enough should be fine and it is fast - the speed is
> > about 100x.
> >
> > I will detail a coding pattern at another time that shows how to
> > eliminate many outer For() loops. I want to use a different, more
> > straightforward example.
> >
> > -- BruceR
> >
> >
> >
> >
> >
> >
> >
> > --- In amibroker@xxxxxxxxxxxxxxx, "Bisto" <bistoman73@> wrote:
> > >
> > > thanks TJ,
> > >
> > > I just finished a quick and "accademic" test of the static variable
> > tip... it solved!
> > >
> > > I wrote a dummy TS with my very time consuming indicator and did an
> > optimization on 4 years EOD data of only one symbol. The optimization
> > had only 5 steps
> > >
> > > with Static variable --> lightning fast
> > >
> > > without Static variable --> 27s (!!) --> not useful in real conditions
> > >
> > > I don't believe that my indicator is extremely complex, it's more
> > likely extremely bad coded... nevertheless I did my best. but let's
> > speak later about it
> > >
> > > let's consider for a moment that it's well coded, or that it is a more
> > complex array well coded by a professionist, I wonder that declaring as
> > Invariant an array in AFL, to avoid to be recalculated n times during
> > optimization, would give an other edge to AB against other sw. something
> > like:
> > >
> > > Invariant( "MyComplexArray" );
> > >
> > > or better something like #calculate_once<invariant.afl> (similar to
> > #include_once <Common.afl>) to refer to some code to be executed once
> > at the beginning of the process
> > >
> > > maybe I didn't explain well but I am sure that you get what I mean.
> > >
> > > -------------
> > >
> > > my time consuming indicator finds *for every bar* which was the value
> > of RSI that was exceeded for a limit % number of times in the last N
> > months. it's useful to have an adaptative value of iperbought and
> > ipersold limit of RSI. never mind about the need to adapt at every bar
> > or that it's useful in general. Up to now this is only prepatory for an
> > other my project about pivot points.
> > >
> > > I didn't find a way to use array operators to do it but I was forced
> > to use nested loop... it looks ugly even to my eyes... any tip?
> > >
> > >
> > > Limit = 5;
> > >
> > > NMonths = 6;
> > >
> > > periods = Param( "Periods", 15, 1, 200, 1 );
> > >
> > > IperSArray = IperBArray = Null;
> > >
> > > DayLimit = NMonths * 22 * Limit / 100;
> > >
> > > Oscill = RSI( periods );
> > >
> > > for ( i = NMonths * 22 ; i < BarCount; i++ )
> > >
> > > {
> > >
> > > IperS = 1;
> > >
> > > IperB = 99;
> > >
> > > DayIperS= DayIperB= 0;
> > >
> > > while ( DayIperS< DayLimit )
> > >
> > > {
> > >
> > > DayIperS= 0;
> > >
> > > for ( j = 0; j < NMonths * 22; j++ )
> > >
> > > if ( Oscill [i-j] < IperS )
> > >
> > > DayIperS++ ;
> > >
> > > IperS ++ ;
> > >
> > > }
> > >
> > > while ( DayIperB< DayLimit )
> > >
> > > {
> > >
> > > DayIperB= 0;
> > >
> > > for ( j = 0; j < NMonths * 22; j++ )
> > >
> > > if ( Oscill [i-j] > IperB )
> > >
> > > DayIperB++ ;
> > >
> > > IperB -- ;
> > >
> > > }
> > >
> > > IperSArray[i] = IperS;
> > >
> > > IperBArray[i] = IperB;
> > >
> > > }
> > >
> > > StaticVarSet("IperSArray",IperSArray);
> > >
> > > StaticVarSet("IperBArray",IperBArray);
> > >
> > >
> > > --- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" groups@ wrote:
> > > >
> > > > IMHO, it is not worth the effort, unless you are doing something
> > _extremely_ complex.
> > > > AFL execution is just a fraction of total time, you would end up
> > spending lots of time with very
> > > > little effect. Remember that your time is way more precious that CPU
> > time.
> > > >
> > > > Best regards,
> > > > Tomasz Janeczko
> > > > amibroker.com
> > > > ----- Original Message -----
> > > > From: "Bisto" bistoman73@
> > > > To: amibroker@xxxxxxxxxxxxxxx
> > > > Sent: Tuesday, October 06, 2009 4:03 PM
> > > > Subject: [amibroker] Re: Tips for reducing CPU load
> > > >
> > > >
> > > > > Thanks TJ,
> > > > >
> > > > > your posts are always very instructive, it seems that you know
> > well how AB works :-)
> > > > >
> > > > > good to know that using static variables is the best way, I try to
> > summarize:
> > > > >
> > > > > 1) to create static variables using a single pass of an AFL on a
> > symbol/watchlist, like an Explore with CreateStaticVariables.AFL
> > > > >
> > > > > 2) to optimize TradingSystem.AFL on the same symbol/watchlist who
> > points to the static variables created in point 1 who are
> > > > > "alive" till overwritten or AB closed
> > > > >
> > > > > of course this is not the case of MA( C,15) but something a lot
> > more complex
> > > > >
> > > > >
> > > > >
> > > > > --- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" <groups@>
> > wrote:
> > > > >>
> > > > >> Hello,
> > > > >>
> > > > >> They are indeed recalculated when going through several
> > backtests.
> > > > >> You could store such values in static variables and use them
> > later in subsequent runs,
> > > > >> but it is in most cases _wasted_ effort, because array functions
> > like MA( C, 15 ) are EQUALLY fast
> > > > >> as STATIC variables. This is so because static variables requires
> > one pass any way (date/time synchronization)
> > > > >> the same as MA. MA can actually be slightly faster than static
> > array variable because of locality of reference.
> > > > >>
> > > > >> Situation is different with normal variables (as I pointed out in
> > my previous post), because normal variables
> > > > >> do not require synchronization (they are already in perfect sync)
> > and do not require extra memory
> > > > >> nor ANY computation as they are simple POINTERS to values that
> > were returned by function.
> > > > >> Storing/reading a pointer to array is a matter of single CPU
> > instruction, i.e. is blazing fast (one CPU clock cycle).
> > > > >>
> > > > >> Regarding AddToComposite/Foreign - they are way slower than
> > Static variables, so it makes no sense to use them for that purpose.
> > > > >>
> > > > >> Best regards,
> > > > >> Tomasz Janeczko
> > > > >> amibroker.com
> > > > >> ----- Original Message -----
> > > > >> From: "Bisto" <bistoman73@>
> > > > >> To: amibroker@xxxxxxxxxxxxxxx
> > > > >> Sent: Tuesday, October 06, 2009 1:52 PM
> > > > >> Subject: [amibroker] Re: Tips for reducing CPU load
> > > > >>
> > > > >>
> > > > >> >I was thinking about this issue yesterday and I have a question,
> > please correct me if I am wrong:
> > > > >> >
> > > > >> > During an optimization process there are so many repetitions of
> > a function with invariant parameter equal to the total number
> > > > >> > of
> > > > >> > the steps of the optimization.
> > > > >> >
> > > > >> > In this case MA(C,15) will be calculated for every set of
> > optimization parameter even if it is invariant, OK?
> > > > >> >
> > > > >> > If we have a very time consuming invariant function in the TS
> > it will be calculated a lot of times even if it is not needed.
> > > > >> > This
> > > > >> > is true if I have well interstood how AB works, please correct
> > me
> > > > >> >
> > > > >> > my idea is to store this very time consuming invariant function
> > in a "fake" symbol (using Addtocomposite) and access to its
> > > > >> > values
> > > > >> > by Setforeing.
> > > > >> >
> > > > >> > Is it a good idea?
> > > > >> >
> > > > >> > Bisto
> > > > >> >
> > > > >> > --- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" <groups@>
> > wrote:
> > > > >> >>
> > > > >> >> Hello,
> > > > >> >>
> > > > >> >> For example, take a look at this simple code:
> > > > >> >>
> > > > >> >> Buy = Cross( MA(C, 5 ), MA( C, 15 ) );
> > > > >> >> Sell = Cross( MA( C, 15 ), MA( C, 5 ) );
> > > > >> >>
> > > > >> >> There are 2 repetitions of same function call with invariant
> > parameters:
> > > > >> >> MA(C, 5 ) - is called twice
> > > > >> >> MA( C, 15 ) - is called twice
> > > > >> >>
> > > > >> >> This can be written in more efficient manner using variables:
> > > > >> >>
> > > > >> >> shortma = MA(C, 5 );
> > > > >> >> longma = MA( C, 15 );
> > > > >> >> Buy = Cross( shortma, longma );
> > > > >> >> Sell = Cross( longma, shortma );
> > > > >> >>
> > > > >> >> That is what I meant.
> > > > >> >>
> > > > >> >> Best regards,
> > > > >> >> Tomasz Janeczko
> > > > >> >> amibroker.com
> > > > >> >> ----- Original Message -----
> > > > >> >> From: "Rob" <sidhartha70@>
> > > > >> >> To: amibroker@xxxxxxxxxxxxxxx
> > > > >> >> Sent: Tuesday, October 06, 2009 10:21 AM
> > > > >> >> Subject: [amibroker] Re: Tips for reducing CPU load
> > > > >> >>
> > > > >> >>
> > > > >> >> > TJ,
> > > > >> >> >
> > > > >> >> > Sorry my programming terminology is not as good as yours...
> > > > >> >> > What do you mean by 'replacing repeated function calls with
> > invariant parameters'...??
> > > > >> >> >
> > > > >> >> > --- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko"
> > <groups@> wrote:
> > > > >> >> >>
> > > > >> >> >> First and foremost: use AFL Editor, Tools->Code Check And
> > Profile
> > > > >> >> >> to reduce complexity of your code, by removing repeated
> > function calls (with invariant parameters),
> > > > >> >> >> using array operators wherever possible instead of loops,
> > > > >> >> >> re-thinking code to get simpler formulation,
> > > > >> >> >> avoiding any JScript/_vbscript_ / COM objects in charts, etc.
> > > > >> >> >>
> > > > >> >> >> Best regards,
> > > > >> >> >> Tomasz Janeczko
> > > > >> >> >> amibroker.com
> > > > >> >> >> ----- Original Message -----
> > > > >> >> >> From: "Ara Kaloustian" <ara1@>
> > > > >> >> >> To: amibroker@xxxxxxxxxxxxxxx
> > > > >> >> >> Sent: Tuesday, October 06, 2009 12:23 AM
> > > > >> >> >> Subject: Re: [amibroker] Tips for reducing CPU load
> > > > >> >> >>
> > > > >> >> >>
> > > > >> >> >> >- review your memory settings in preferences... make sure
> > you are not
> > > > >> >> >> > allocating memory that you are not using
> > > > >> >> >> >
> > > > >> >> >> > - reduce number of symbols you monitor if you don't need
> > them all
> > > > >> >> >> >
> > > > >> >> >> >
> > > > >> >> >> > ----- Original Message -----
> > > > >> >> >> > From: "Rob" <sidhartha70@>
> > > > >> >> >> > To: amibroker@xxxxxxxxxxxxxxx
> > > > >> >> >> > Sent: Monday, October 05, 2009 2:33 PM
> > > > >> >> >> > Subject: [amibroker] Tips for reducing CPU load
> > > > >> >> >> >
> > > > >> >> >> >
> > > > >> >> >> >> Hi All,
> > > > >> >> >> >>
> > > > >> >> >> >> I seem to be maxing out the core I'm using on AB...
> > starting to cause
> > > > >> >> >> >> problems operationally. I'm mainly doing charting and
> > real time AFL.
> > > > >> >> >> >>
> > > > >> >> >> >> Any tips for reducing CPU load so I can perhaps give
> > myself a little more
> > > > >> >> >> >> breathing space....??
> > > > >> >> >> >>
> > > > >> >> >> >> TIA
> > > > >> >> >> >>
> > > > >> >> >> >>
> > > > >> >> >> >>
> > > > >> >> >> >> ------------------------------------
> > > > >> >> >> >>
> > > > >> >> >> >> **** IMPORTANT PLEASE READ ****
> > > > >> >> >> >> This group is for the discussion between users only.
> > > > >> >> >> >> This is *NOT* technical support channel.
> > > > >> >> >> >>
> > > > >> >> >> >> TO GET TECHNICAL SUPPORT send an e-mail directly to
> > > > >> >> >> >> SUPPORT {at} amibroker.com
> > > > >> >> >> >>
> > > > >> >> >> >> TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
> > > > >> >> >> >> http://www.amibroker.com/feedback/
> > > > >> >> >> >> (submissions sent via other channels won't be
> > considered)
> > > > >> >> >> >>
> > > > >> >> >> >> For NEW RELEASE ANNOUNCEMENTS and other news always
> > check DEVLOG:
> > > > >> >> >> >> http://www.amibroker.com/devlog/
> > > > >> >> >> >>
> > > > >> >> >> >> Yahoo! Groups Links
> > > > >> >> >> >>
> > > > >> >> >> >>
> > > > >> >> >> >>
> > > > >> >> >> >>
> > > > >> >> >> >
> > > > >> >> >> >
> > > > >> >> >> >
> > > > >> >> >> >
> > > > >> >> >> > ------------------------------------
> > > > >> >> >> >
> > > > >> >> >> > **** IMPORTANT PLEASE READ ****
> > > > >> >> >> > This group is for the discussion between users only.
> > > > >> >> >> > This is *NOT* technical support channel.
> > > > >> >> >> >
> > > > >> >> >> > TO GET TECHNICAL SUPPORT send an e-mail directly to
> > > > >> >> >> > SUPPORT {at} amibroker.com
> > > > >> >> >> >
> > > > >> >> >> > TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
> > > > >> >> >> > http://www.amibroker.com/feedback/
> > > > >> >> >> > (submissions sent via other channels won't be considered)
> > > > >> >> >> >
> > > > >> >> >> > For NEW RELEASE ANNOUNCEMENTS and other news always check
> > DEVLOG:
> > > > >> >> >> > http://www.amibroker.com/devlog/
> > > > >> >> >> >
> > > > >> >> >> > Yahoo! Groups Links
> > > > >> >> >> >
> > > > >> >> >> >
> > > > >> >> >> >
> > > > >> >> >>
> > > > >> >> >
> > > > >> >> >
> > > > >> >> >
> > > > >> >> >
> > > > >> >> > ------------------------------------
> > > > >> >> >
> > > > >> >> > **** IMPORTANT PLEASE READ ****
> > > > >> >> > This group is for the discussion between users only.
> > > > >> >> > This is *NOT* technical support channel.
> > > > >> >> >
> > > > >> >> > TO GET TECHNICAL SUPPORT send an e-mail directly to
> > > > >> >> > SUPPORT {at} amibroker.com
> > > > >> >> >
> > > > >> >> > TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
> > > > >> >> > http://www.amibroker.com/feedback/
> > > > >> >> > (submissions sent via other channels won't be considered)
> > > > >> >> >
> > > > >> >> > For NEW RELEASE ANNOUNCEMENTS and other news always check
> > DEVLOG:
> > > > >> >> > http://www.amibroker.com/devlog/
> > > > >> >> >
> > > > >> >> > Yahoo! Groups Links
> > > > >> >> >
> > > > >> >> >
> > > > >> >> >
> > > > >> >>
> > > > >> >
> > > > >> >
> > > > >> >
> > > > >> >
> > > > >> > ------------------------------------
> > > > >> >
> > > > >> > **** IMPORTANT PLEASE READ ****
> > > > >> > This group is for the discussion between users only.
> > > > >> > This is *NOT* technical support channel.
> > > > >> >
> > > > >> > TO GET TECHNICAL SUPPORT send an e-mail directly to
> > > > >> > SUPPORT {at} amibroker.com
> > > > >> >
> > > > >> > TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
> > > > >> > http://www.amibroker.com/feedback/
> > > > >> > (submissions sent via other channels won't be considered)
> > > > >> >
> > > > >> > For NEW RELEASE ANNOUNCEMENTS and other news always check
> > DEVLOG:
> > > > >> > http://www.amibroker.com/devlog/
> > > > >> >
> > > > >> > Yahoo! Groups Links
> > > > >> >
> > > > >> >
> > > > >> >
> > > > >>
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > ------------------------------------
> > > > >
> > > > > **** IMPORTANT PLEASE READ ****
> > > > > This group is for the discussion between users only.
> > > > > This is *NOT* technical support channel.
> > > > >
> > > > > TO GET TECHNICAL SUPPORT send an e-mail directly to
> > > > > SUPPORT {at} amibroker.com
> > > > >
> > > > > TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
> > > > > http://www.amibroker.com/feedback/
> > > > > (submissions sent via other channels won't be considered)
> > > > >
> > > > > For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
> > > > > http://www.amibroker.com/devlog/
> > > > >
> > > > > Yahoo! Groups Links
> > > > >
> > > > >
> > > > >
> > > >
> > >
> >
>


__._,_.___


**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

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





Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___