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

Re: [amibroker] Re: Running Median Calculation Starting at Bar 0



PureBytes Links

Trading Reference Links

>The correct Median value of an even set of 
> numbers is the average of the two middle values. 

I disagree. There are actually two conventions.
In case of even number of inputs, there are TWO equally good median values (i.e. median is not unique).
In one convention the median value actually must EXIST in the input data set. 
That is convention AmiBroker uses.
In second convention you are generating artificial value that does not
exist in the input set by taking mean of two middle values.

Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message ----- 
From: "Mike" <sfclimbers@xxxxxxxxx>
To: <amibroker@xxxxxxxxxxxxxxx>
Sent: Wednesday, November 05, 2008 11:56 AM
Subject: [amibroker] Re: Running Median Calculation Starting at Bar 0


> This won't be the fastest implementation, but I believe that it will 
> work. Assuming a period of 20...
> 
> 1. Let P = Period - 1 (e.g. 20 - 1 = 19)
> 2. Make P calls to the AFL Median function using 1..P (e.g. 1..19) as 
> the period scaler.
> 3. Store the P-1th value for each result in a dynamic variable of the 
> same name. P-1 will be the first zero indexed, non null value for 
> each array. You should end up with:
> 
> M0 = Median(Close, 1)[0];
> M1 = Median(Close, 2)[1];
> ...
> M18 = Median(Close, 19)[18];
> 
> 4. Write a loop to set the first P values to the dynamic variables 
> and calculate a regular EMA for the remainder. Your chart should 
> converge quickly after the first Period bars.
> 
> e.g.
> 
> Period = Param("Period", 20);
> 
> // Calculate dynamic median for initial Period-1 bars, stored as
> // dynamic variables M0, M1, ..., MP where P = Period - 1.
> 
> P = Period - 1;
> 
> for (i = 0; i < P; i++) {
>  M = Median(Close, i + 1);
>  VarSet("M" + i, M[i]);
> }
> 
> // Calculate EMA substituting our dynamic variables for the first
> // Period - 1 values.
> 
> Exponent = 2/(Period + 1);
> 
> for (i = 0; i < BarCount; i++) {
>  if (i < P) {
>    MyEMA[i] = VarGet("M" + i);
>  } else {
>    MyEMA[i] = (Close[i] * Exponent) + (MyEMA[i - 1] * (1 - 
> Exponent));
>  }
> }
> 
> Plot(EMA(Close, 20), "EMA", colorGreen);
> Plot(MyEMA, "MyEMA", colorBlue);
> Plot(Close, "Close", colorRed); 
> 
> Note that there appears to be a bug in AmiBroker (else in my code!) 
> such that Median is not being correctly calculated when there are an 
> even number of values. The correct Median value of an even set of 
> numbers is the average of the two middle values. However, AmiBroker 
> appears to be returning the first of the two middle values, ignoring 
> the second of the two middle values.
> 
> Alternatively, you could calculate the Median yourself using an inner 
> loop to fill a temporary array which you would then pass to a sorting 
> function and your own Median implementation. This would have the 
> advantage of operating only on a maximum of Period values (instead of 
> all visible values), but would not be in binary code as the built in 
> Median is.
> 
> Mike
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "searnp" <searnp@xxx> wrote:
>>
>> Bump
>> 
>> --- In amibroker@xxxxxxxxxxxxxxx, "searnp" <searnp@> wrote:
>> >
>> > Hi Howard, thanks for your reply.
>> > 
>> > I am looking at calculating an exponential moving average, but 
> would
>> > like to have initial values as well (instead of null values).
>> > 
>> > For example:
>> > If I do an EMA(Close, 20), I will get a chart of the 20-day EMA of
>> > close with null values for the first 20 bars.
>> > 
>> > So what I am trying to do is create my own EMA function via 
> looping
>> > (similar to pg.114 of the Amibroker User Guide), where the running
>> > median would be calculated for (i + 1) < Period. I can do it with 
> an
>> > initial simple running average calculation (since I can 
> incorporate
>> > that in the loop for (i + 1) < Period ), but would prefer to use 
> the
>> > median in case there are initial spikes.
>> > 
>> > Another question loosely related to this:
>> > When I incorporate a manual bar count via looping (see code 
> below) and
>> > then click on a bar on the chart, the value changes when I scroll 
> left
>> > or right or zoom in or out. Why is this?
>> > 
>> > CT[0] = 1;
>> > for( i = 1; i < BarCount; i++ )
>> > {
>> > CT[i] = 1 + CT[i-1];
>> > }
>> > 
>> > Plot( CT, _DEFAULT_NAME(), ParamColor( "Color", colorCycle ),
>> > ParamStyle("Style") );
>> > 
>> > 
>> > Thanks.
>> > 
>> > --- In amibroker@xxxxxxxxxxxxxxx, "Howard B" <howardbandy@> wrote:
>> > >
>> > > Hi Searnp --
>> > > 
>> > > The User's Guide states:
>> > > AFL Function Reference - MEDIAN  *MEDIAN
>> > > *- calculate median (middle element)
>> > > 
>> > > *Statistical functions
>> > > *(AFL 2.5)
>> > > 
>> > >   *SYNTAX* *Median( array, period ) * *RETURNS* ARRAY  
> *FUNCTION*
>> > The Median
>> > > function - finds median (middle element) value of the *array* 
> over
>> > > *period*elements.
>> > > *EXAMPLE* // list only symbols which volume is greater than
>> > > // median Volume from past 50 days
>> > > *Filter* = *Volume* > Median( *Volume*, 50 );
>> > > AddColumn( *V*, "Volume" );
>> > > Period must be a scalar -- a constant.  When you run this code:
>> > > 
>> > > RM = Median(Close, BarIndex() + 1);
>> > > 
>> > > The entire Close array is passed to the Median function at one 
> time.
>> >  The
>> > > value of BarIndex changes with each element of the Close array. 
>> > Looked at
>> > > simplistically, the afl processor does not know which value of
>> > BarIndex to
>> > > use, so it coughs.
>> > > 
>> > > You will have to write your own function using looping code.
>> > > 
>> > > Before you start, are you certain that you want the median 
> value of an
>> > > expanding list of data values?  Today's value will depend on how
>> > much data
>> > > was loaded, which is generally not a good programming or systems
>> > idea.  Will
>> > > Median(C,50), or some variation, give you what you need?
>> > > 
>> > > Thanks,
>> > > Howard
>> > > 
>> > > 
>> > > 
>> > > On Mon, Nov 3, 2008 at 11:26 AM, searnp <searnp@> wrote:
>> > > 
>> > > >   I am trying to calculate a running median that starts at 
> bar 0.
>> > > >
>> > > > Code:
>> > > >
>> > > > RM = Median(Close, BarIndex() + 1);
>> > > >
>> > > >
>> > > > Then I get the following error message:
>> > > >
>> > > > Error 5. Argument #2 has incorrect type (the function expects
>> > different
>> > > > argument type here)
>> > > >
>> > > >
>> > > > I am using "Barindex() + 1" to get a running count at each 
> bar to
>> > be used
>> > > > in the median's lookback period. Is there a way to implement 
> this?
>> > > >
>> > > > Thanks.
>> > > >  
>> > > >
>> > >
>> >
>>
> 
> 
> 
> ------------------------------------
> 
> **** IMPORTANT ****
> This group is for the discussion between users only.
> This is *NOT* technical support channel.
> 
> *********************
> TO GET TECHNICAL 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
> 
> 
> 

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

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

*********************
TO GET TECHNICAL 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/