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

[EquisMetaStock Group] Re: How to count the number of occurences ?



PureBytes Links

Trading Reference Links


Thanks Andrew.  It's good to see my code being dissected. :)

I've used a minimum of 0.001% change in the Peak()/Trough() functions 
to make sure no peak or trough misses out.  This can be defined as a 
user input, but introduces other problems because percentages go 
haywire as they approach the zero line.

The Peak/Trough functions can be replaced by code that is not using 
hindsight, but it doesn't make much difference here anyway because of 
the LastValue() functions used.  In any case I've removed them from 
the code below.

====================
Peak/Trough averages
====================
---8<---------------------

{ v2.0 - Average of three highest/lowest
         peaks/troughs over last x months }

{ ©Copyright 2005 Jose Silva
  For personal use only
  http://www.metastocktools.com }

{ Oscillator/Indicator }
x:=MACD();

{ User inputs }
lbMth:=Input("Lookback months",1,12,6);
choose:=Input("Choose:  [1]Price,  [2]Oscillator/Indicator",1,2,1);

{ Choose Price or Osc/Ind }
x:=If(choose=1,C,x);

{ Restrict search to lookback period }
StMnth:=LastValue(Month())-lbMth;
StYear:=If(StMnth<1,
 LastValue(Year())-1,LastValue(Year()));
StMnth:=If(StMnth<1,StMnth+12,StMnth);
lookback:=Year()>StYear
 OR (Year()=StYear AND (Month()>StMnth
  OR Month()=StMnth
   AND DayOfMonth()>=LastValue(DayOfMonth())));

{ Highest/2nd/3rd peak values }
ignore:=LastValue(Lowest(x));
pk:=Ref(x,-1)>x AND Ref(x,-1)>Ref(x,-2);
pk:=If(lookback,
 ValueWhen(1,pk,Ref(x,-1)),ignore);
pk1:=LastValue(Highest(pk));
pk2:=LastValue(Highest(If(pk=pk1,ignore,pk)));
pk3:=LastValue(Highest(
 If(pk=pk1 OR pk=pk2,ignore,pk)));

{ Lowest/2nd/3rd trough values }
ignore:=LastValue(Highest(x));
tr:=Ref(x,-1)<x AND Ref(x,-1)<Ref(x,-2);
tr:=If(lookback,
 ValueWhen(1,tr,Ref(x,-1)),ignore);
tr1:=LastValue(Lowest(tr));
tr2:=LastValue(Lowest(If(tr=tr1,ignore,tr)));
tr3:=LastValue(
 Lowest(If(tr=tr1 OR tr=tr2,ignore,tr)));

{ Average of last 3 peak/trough values }
avgPk:=(pk1+pk2+pk3)/3;
avgTr:=(tr1+tr2+tr3)/3;

{ Plot on price chart }
x;avgPk;avgTr

---8<---------------------

Changing the output to dynamic should prove to be an interesting 
exercise, best left to whoever needs it.  :)


jose '-)
http://www.metastocktools.com



--- In equismetastock@xxxxxxxxxxxxxxx, "Andrew Tomlinson" 
<andrew_tomlinson@xxxx> wrote:
> 
> Jose
> 
> Some very cool coding. 
> 
> I liked Peak(1,x,.001) to define a line that resets at every new
> peak. Any specific reason for using .001? (I note that the ZigZag
> function won't allow a number less than .01)
> 
> Highest(If(pk=pk1,0,pk)) is very elegant as a way of defining a data
> array excluding the highest value. I tend not to think of using
> highest() or lowest() because they include all loaded data, but
> that's not a problem when the data array is already restricted in
> time, as in this case.
> 
> Are there any issues surrounding the use of Peak() that I should be
> aware of? I don't use ZigZag based functions much because of the
> problems.
> 
> I would like to change this into a dynamic band so I can see the
> effect over time. I'll take a crack at it myself first, but any
> hints welcome!
> 
> Thank you Jose
> 
> Andrew







------------------------ Yahoo! Groups Sponsor --------------------~--> 
Has someone you know been affected by illness or disease?
Network for Good is THE place to support health awareness efforts!
http://us.click.yahoo.com/Rcy2bD/UOnJAA/cosFAA/BefplB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/equismetastock/

<*> To unsubscribe from this group, send an email to:
    equismetastock-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/