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

[amibroker] Re: median calculation



PureBytes Links

Trading Reference Links


DC,
If I understand your description, if x is a Peak value[ according to 
your Peak definition] then your median x is the average of the 2nd 
and 3rd highest x for the last n=20 days. 
I will try the descriptive way:
1. Define x
2. Find the highest x and make it zero
3. Find the next highest x. Save it and then make it zero.
4. Find the next highest x. Save it and 
5. Calculate the median as an average of the saved values.
This can be done with

y=Cum(1);Ly=LastValue(y); 
x=IIf(Ref(H,-1)==HHV(H,3),Ref(H,-1),0);//your Peak definition
n=20;//the lookback period
x=IIf(y>Ly-n,x,0);
xmax=LastValue(Highest(x));//find the highest x
x=IIf(x==xmax,0,x);//and make it 0
xmax=LastValue(Highest(x));//find the next highest x
medH=xmax;//save it
x=IIf(x==xmax,0,x);//and make it 0
xmax=LastValue(Highest(x));//find the next highest x
medH=(medH+xmax)/2;//calculate the median
//Repeat the same procedure for troughs
xx=IIf(Ref(L,-1)==LLV(L,3),Ref(L,-1),100000);
xx=IIf(y>Ly-n,xx,100000);
xxmin=LastValue(Lowest(xx));
xx=IIf(xx==xxmin,100000,xx);
xxmin=LastValue(Lowest(xx));
medL=xxmin;
xx=IIf(xx==xxmin,100000,xx);
xxmin=LastValue(Lowest(xx));
medL=(medL+xxmin)/2;
Title="medH="+WriteVal(medH)+", medL="+WriteVal(medL);

The medH, medL are already defined and may be used in Plot statements 
or further calculations.

I tried to avoid loops for better [?] understanding.
Dimitris
--- In amibroker@xxxxxxxxxxxxxxx, <dennisconn@xxxx> wrote:
> Hi to all,
> 
> I want to thank everyone who's responded for their help - I really 
do appreciate the fact that you've been willing to take the time to 
help with this.
> 
> But there's been some confusion as to the nature of my median 
calculation request, so I'm posting this in an attempt to better 
define the nature of the problem.
> 
> For those who are familiar with Joe Dinapoli's Oscillator 
Predictor, that is what I'm attempting to recreate in AFL. For those 
who are not familiar with it, his Predictor calculates a price at 
which one would take profits in a highly overbought or oversold 
condition, based on the average overbought or oversold level of an 
oscillator. In other words, if a person knows ahead of time the price 
at which a market would be highly overbought or oversold, a pre-
placed order to exit at that price will catch spike moves and get one 
out BEFORE the market takes back the profits one has. Using only 
daily charts, Joe looks for the average value of the three highest 
peaks to determine a reasonable approximation of the average 
realistic overbought level. He does the same for the three lowest 
troughs to determine the average realistic oversold level.
> 
> *** I prefer to use the median value of the last four peaks (or 
troughs) in order to eliminate the most extreme values from the 
calculation. ***
> 
> His lookback range is roughly six months (120 bars) or so. The 
oscillator he uses is non-normalized, so the troughs will ALL be 
negative values.
> 
> For my purposes, I define a peak in the oscillator as the highest 
value reached during an excursion above zero. A trough is the lowest 
value reached during an excursion below zero. With that in mind, I 
have already created two arrays; one array contains all the peaks, 
the other contains the troughs. If a given day in either array is NOT 
a peak or trough based on the above definition, then that day's array 
element = ZERO. So here's an example of what my trough array would 
look like over the last 20 bars:
> 
> 0, 0, -11, 0, 0, 0, -13, 0, -7, 0, -9, 0, 0, 0, -4, 0, -12, 0, 0, -2
> 
> Here is the brainteaser (to me):
> 
> Let's assume my lookback range is just these last 20 bars; 
obviously, the four lowest troughs are -11, -13, -9, and -12. I need 
to find a way to pick those four values out of the array in order to 
find the median value (which will equal -11.5).
> 
> That's IT - that's ALL I'm trying to do - I'm pretty sure I can 
figure out everything else I need to do.
> 
> Now I know it's possible to allow the user to input the number 
manually, by way of "Param", and that's all well and good. But I want 
to program it for user convenience (as well as cutting down on user 
error), and also be able to plot the resulting OB/OS price 
predictions as bands on the price chart (which will be all the more 
effective when Tomasz gets around to allowing plotting of indicators 
beyond the last bar!).
> 
> Anyway, again, ALL I need is a bit of direction on the median 
calculation in order to have the formula handle everything in the 
Oscillator Predictor's output.
> 
> If I ever get this figured out, or someone else does, I'll be sure 
to post it to the AFL library - this could be a very useful tool for 
capturing profits, especially if the output correlates with some 
other projection method, like Cardwell's positive and negative 
reversals in the RSI, or Fibonacci projections, or whatever other 
tool.
> 
> If you've managed to read this far, then...
> 
> Thanks again & best regards,
> 
> DC
> 
> [Non-text portions of this message have been removed]





------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Check AmiBroker web page at:
http://www.amibroker.com/

Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.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/