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

[EquisMetaStock Group] Re: Metastock Coding - how to count the number of day based on given condition?



PureBytes Links

Trading Reference Links

Eric, the trick here is to count *calendar days*, and not just data 
bars.

Try this adaptation of the DaysSince indicator from
http://www.metastocktools.com/index.html#metastock:

=========================
Calendar days since event
=========================
---8<------------------------------

{ DaysSince last Nth event function.
  Calendar day count is independent of
   any chart periodicity or missing data.

 ©Copyright 2003~2006 Jose Silva.
  The grant of this license is for personal use
   only - no resale or repackaging allowed.
  All code remains the property of Jose Silva.
  http://www.metastocktools.com }

{ User Inputs }
nth:=Input("Calendar days since Nth [1~1000] event",1,1000,1);
plot:=Input("[1]DaysSince,  [2]EventSignals,  [3]DayCount",1,3,1);
limit:=Input("Zero reset calendar days from year",1,2100,2000);

{ Event example }
today:=LastValue(C);
event:=Cross(C,today) OR Cross(today,C);

{ Gregorian calendar }
LimLeap:=Frac(limit/4)=0 AND Frac(limit/100)<>0
 OR Frac(limit/400)=0;
NoCount:=limit*365+Int(limit/4)
 -Int(limit/100)+Int(limit/400)-LimLeap;
leap:=Frac(Year()/4)=0 AND Frac(Year()/100)<>0
 OR Frac(Year()/400)=0;
y:=Year()*365+Int(Year()/4)
 -Int(Year()/100)+Int(Year()/400)-NoCount;
m:=
 If(Month()=2,31-leap,
 If(Month()=3,59,
 If(Month()=4,90,
 If(Month()=5,120,
 If(Month()=6,151,
 If(Month()=7,181,
 If(Month()=8,212,
 If(Month()=9,243,
 If(Month()=10,273,
 If(Month()=11,304,
 If(Month()=12,334,
 -leap)))))))))));
DayNr:=y+m+DayOfMonth();

{ Restrict Nth to max events }
nth:=
 LastValue(If(nth>Cum(event),Cum(event),nth));

{ Days since Nth event }
DaysSince:=DayNr-ValueWhen(nth,event,DayNr);

{ Plot in own window }
If(plot=1,DaysSince,If(plot=2,event,DayNr))

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


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




--- In equismetastock@xxxxxxxxxxxxxxx, chichungchoi <no_reply@xxx> 
wrote:
>
> Does anyone know how to code the following condition?
> 
> If current closing price is 10, then I would like to look for the
> price from the historical data, which was cross above or below 10
> level, and select the first one to meet the requirement.
> After that it returns the  number of day back for this condition
> from the current period?
> Such as
> 
> 23 Feb, 2006      10  [Today]
> 21 Feb, 2006      11
> 20 Feb, 2006      9.5 
> 
> then return 2 days from 23 Feb 2006
> 
> Does anyone have any suggestion?
> Thank you in advance
> Eric







------------------------ Yahoo! Groups Sponsor --------------------~--> 
Try Online Currency Trading with GFT. Free 50K Demo. Trade 
24 Hours. Commission-Free. 
http://us.click.yahoo.com/RvFikB/9M2KAA/U1CZAA/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/