PureBytes Links
Trading Reference Links
|
Hi Roy-
My fault re: misunderstanding on SMA
I'm using first day of the week as the mechanism to access the last
bar of the prior week. I am calculting the EMA using the weekly
CLOSE.
Since my post, I got rid of the cum(1)=2 logic.
The core formula is:
If(Cum(FIRSTDAYOFWEEK)=1,ValueWhen( 1, FIRSTDAYOFWEEK , Ref(C,-1)) *
( 1 - 2/9) + C * 2/9, ValueWhen( 1, FIRSTDAYOFWEEK , PREV ) * ( 1 -
2/9) + C * 2/9)
The updated code with my documentation is
below. Please let me know if my code is still confusing...
And thanks for all your contributions - reviewing the help you've
offered to others as well as myself has taught me a tremendous amount
about MetaStock.
With Best Regards-
Sam
_______________________________________________________________
{
.FIRSTDAYOFWEEK
}
FIRSTDAYOFWEEK:=
DayOfWeek()<Ref(DayOfWeek(),-1)
;
FIRSTDAYOFWEEK
_______________________________________________________________
{
.EMAMULTIPLIER
}
EMAPERIODS:= 8
;
EMAMULTIPLIER:= 2 / (1 + EMAPERIODS) {the
multiplier to use when calculating the
moving average.
To calculate exponential moving average =
last bar's calculated moving average * ( 1 -
( 2 / (1 + # of periods in the moving
average)) + Close * (2 / (1 + # of periods
in the moving average))
The "seed" (i.e. the first number in the
moving average data array) uses the close of
the bar prior to the FIRST FIRSTDAYOFWEEK}
;
EMAMULTIPLIER
_______________________________________________________________
{
.8PERIODWEEKLYEMA.T
This is the formula MetaStock uses to calculate an 8 period
exponential moving average
}
FIRSTDAYOFWEEK:= Fml( ".FIRSTDAYOFWEEK" )
;
EMAMULTIPLIER:= Fml( ".EMAMULTIPLIER" )
;
If(
Cum(FIRSTDAYOFWEEK)=1 {for each day of the
first full week}
{then},
ValueWhen( 1, FIRSTDAYOFWEEK , Ref(C,-1)) *
( 1 - EMAMULTIPLIER ) + C * EMAMULTIPLIER
{Use last week's close as the "seed" for
the exponential moving average}
{else},
ValueWhen( 1, FIRSTDAYOFWEEK , PREV ) * ( 1
- EMAMULTIPLIER ) + C * EMAMULTIPLIER
{use the previous calculated moving
average as the base.}
)
{
NOTE: The value of the first bar of the first full week (i.e.
FIRSTDAYOFWEEK AND Cum(FIRSTDAYOFWEEK)=1)) is displayed as N/A. This
value (i.e. Ref(C,-1)) * ( 1 - EMAMULTIPLIER ) + C * EMAMULTIPLIER)
is not displayed in the data array because PREV is still N/A (it has
not yet been assigned a value in the else statement). Although the
value is displayed as N/A on this bar, the calculated value is in
fact stored in the data array for this bar and used by the
ValueWhen/PREV statement in the next bar!
Each day, use the moving average value from the last trading day of
last week as the basis. This gives me an EMA that takes into
consideration the close of the current bar as the last value used in
calculating the EMA (i.e. use the current bar close as the weighted
value to use with last week's moving average). This matches
MetaStock's exponential moving average calculation.
}
_______________________________________________________________
{
.8PERIODWEEKLYEMA
}
FIRSTDAYOFWEEK:= Fml( ".FIRSTDAYOFWEEK" )
;
8PERIODWEEKLYEMA.T:= Fml( ".8PERIODWEEKLYEMA.T" )
;
8PERIODWEEKLYEMA:=
8PERIODWEEKLYEMA.T * ValueWhen( 8 ,
FIRSTDAYOFWEEK, 1 ) {Set data array to N/A
for first 7 weeks}
;
8PERIODWEEKLYEMA
_______________________________________________________________
--- In equismetastock@xxxxxxxxxxxxxxx, "Roy Larsen" <rlarsen@xxxx>
wrote:
> Hi Sam
>
> I thought you were looking for SMA. There's no way to calculate an
EMA using MFL without using one
> PREV.
>
> I'm curious about your code - Are you using first day of the week
because the EMA is of the weekly
> OPEN?
>
> Roy
>
>
> ----- Original Message -----
> From: "sabboushi2000" <Yahoo@xxxx>
> To: <equismetastock@xxxxxxxxxxxxxxx>
> Sent: Tuesday, February 10, 2004 5:03 PM
> Subject: Re: [EquisMetaStock Group] Simple Moving Average using
first bar of each week
>
>
> > Thanks Roy-
> > I did some exploration tests and my "8 period exponential moving
> > average using daily data with PREV" function took about 50 times
> > longer to calculate than a simple "C > O" (Explorate options set
to
> > load 50 periods for both tests).
> >
> > I have not been able to figure out how to calculate an exponential
> > moving average without a self-referencing formula - I don't see
how
> > cum can help me. The method I've devised uses the following 3
> > formulas (.FIRSTDAYOFWEEK, .8PERIODWEEKLYEMA.T
> > and .8PERIODWEEKLYEMA). I have pasted them below with the rather
> > tedious documentation that I write to myself.
> >
> > If anyone knows how to replace my formulas so that I can avoid
using
> > PREV, any help would be appreciated.
> >
> > With Regards-
> > Sam
> >
> > --------------------------------------------------------------
> > --------------------------------------------------------------
> >
> > {
> > .FIRSTDAYOFWEEK
> >
> > NOTE: NEED TO SELECT START DATE THAT IS 2 NON-HOLIDAY DAYS PRIOR
TO
> > THE NEXT WEEK FOR .8PERIODWEEKLYEMA TO WORK
> >
> > Setting Cum(1) = 2 flags the last bar of the first week assuming I
> > have chosen a start date per NOTE above. Needed because
> > 8PERIODWEEKLYEMA uses ValueWhen( 1, FIRSTDAYOFWEEK , PREV ) which
is
> > N/A on FIRSTDAYOFWEEK if PREV is N/A (i.e, I need to assign a
value
> > to the bar prior to the first real FIRSTDAYOFWEEK (i.e. Cum(1)=2
is
> > not a real FIRSTDAYOFWEEK) so that on the first real ValueWhen( 1,
> > FIRSTDAYOFWEEK , PREV ),PREV will have a valid Close value to use
> > }
> >
> > FIRSTDAYOFWEEK:=
> >
> > Cum(1)=2
> > OR
> > DayOfWeek()<Ref(DayOfWeek(),-1)
> > ;
> >
> > FIRSTDAYOFWEEK
> >
> > --------------------------------------------------------------
> >
> > {
> > .8PERIODWEEKLYEMA.T
> >
> > Start date must be 2 trading days before the first day of the
> > following week for this formula to work properly
> > }
> >
> > FIRSTDAYOFWEEK:= Fml( ".FIRSTDAYOFWEEK" )
> > ;
> >
> > If(
> > FIRSTDAYOFWEEK AND PREV = 0 {this should
> > be when Cum(1)=2}
> > {then},
> > C
> > {else},
> > ValueWhen( 1, FIRSTDAYOFWEEK , PREV ) * ( 1 - ( 2 / ( 8 +
1 ))) +
> > C * ( 2 / ( 8 + 1 )) {the formula MetaStock uses to
> > calculate an 8 period exponential moving average}
> > )
> >
> > --------------------------------------------------------------
> >
> > {
> > .8PERIODWEEKLYEMA
> > }
> >
> >
> > FIRSTDAYOFWEEK:= Fml( ".FIRSTDAYOFWEEK" )
> > ;
> >
> > 8PERIODWEEKLYEMA.T:= Fml( ".8PERIODWEEKLYEMA.T" )
> > ;
> >
> > 8PERIODWEEKLYEMA:=
> > 8PERIODWEEKLYEMA.T * ValueWhen( 8 , FIRSTDAYOFWEEK, 1 ) {Set
> > data array to N/A for first 7 weeks}
> > ;
> >
> > 8PERIODWEEKLYEMA
> >
> > --------------------------------------------------------------
> > --------------------------------------------------------------
> >
> >
> > --- In equismetastock@xxxxxxxxxxxxxxx, "Roy Larsen" <rlarsen@xxxx>
> > wrote:
> > > Sam
> > >
> > > For weekly values on daily charts I think you should stay away
from
> > the PREV based code. The
> > > difference in accuracy is minimal but the processing overheads
are
> > significant. I'm still hoping to
> > > come up with a method that doesn't need to Cum() all weekly data
> > but for now I still believe that's
> > > the best way to go.
> > >
> > > Roy
> > >
> > > ----- Original Message -----
> > > From: "sabboushi2000" <Yahoo@xxxx>
> > > To: <equismetastock@xxxxxxxxxxxxxxx>
> > > Sent: Monday, February 09, 2004 5:37 AM
> > > Subject: Re: [EquisMetaStock Group] Simple Moving Average using
> > first bar of each week
> > >
> > >
> > > > Thanks Roy - I appreciate your assistance.
> > > >
> > > > I don't understand the basis behind how the LastValue ( J +
PREV -
> > > > PREV ) converts J into a constant (or exactly what that means
to
> > > > MS).
> > > > Although this certainly works as you've outlined, I cannot
get it
> > to
> > > > work for my specific case:
> > > >
> > > > EMAPERIODS:= Fml(".EMAPERIODS"); {where EMAPERIODS is a
constant
> > > > (8)}
> > > >
> > > > J:= LastValue( EMAPERIODS + PREV - PREV );
> > > >
> > > > Mov( C , J , E )
> > > >
> > > > Error message: This variable or expression must contain only
> > > > constant data.
> > > >
> > > > Any ideas why your trick won't work for my case?
> > > >
> > > > With Regards-
> > > > Sam
> > > >
> > > >
> > > > --- In equismetastock@xxxxxxxxxxxxxxx, "Roy Larsen"
<rlarsen@xxxx>
> > > > wrote:
> > > > > Here are three versions of a weekly Monday CLOSE SMA. The
first
> > > > formula is less accurate than the
> > > > > other two because of the errors introduced by cumulating the
> > data
> > > > for every Monday on the chart then
> > > > > subtracting the unwanted Monday data. The advantage of this
> > code is
> > > > that it does not require PREV.
> > > > >
> > > > > The second and third formulas are more accurate because
there
> > is no
> > > > fractional component in the 'J'
> > > > > variable cumulations. This variable calculates the look-back
> > > > periods (in bars, not weeks) for Sum()
> > > > > or Mov(). In both formulas the 'J' variable is converted to
> > operate
> > > > legitimately as a constant in
> > > > > either the Sum() or Mov() functions.
> > > > >
> > > > > The last line of the third formula can be broken down as
> > follows.
> > > > >
> > > > > Mov( {moving average of}
> > > > > K*M, {data array includes only the CLOSE when Monday is
true}
> > > > > LastValue(J+PREV-PREV), {'J' periods in the form of a
> > constant
> > > > as required by Mov()}
> > > > > S) {simple moving average}
> > > > > *J/D; {multiply MA by look-back periods and divide
result
> > by
> > > > weeks}
> > > > >
> > > > > It is clear from the makeup of this last line that moving
> > between
> > > > different periodises on the one
> > > > > chart is rarely a matter of making simple changes. As you've
> > > > already discovered the data array needs
> > > > > to created, the sampling of that data needs to be managed,
and
> > the
> > > > number of look-back periods also
> > > > > need to adjusted to allow for holidays. The basic functions
just
> > > > can't cut it.
> > > > >
> > > > > Roy
> > > > >
> > > > > {Monday Close SMA 1}
> > > > > {for use on daily charts}
> > > > > D:=Input("Periods in Weeks",1,100,10);
> > > > > M:=DayOfWeek()<=ValueWhen(2,1,DayOfWeek());
> > > > > K:=ValueWhen(1,M,C);
> > > > > G:=Cum(M*K);
> > > > > X:=(G-ValueWhen(D+1,M,G))/D;
> > > > > X;
> > > > >
> > > > > {Monday Close SMA 2}
> > > > > {for use on daily charts}
> > > > > D:=Input("Periods in Weeks",1,100,10);
> > > > > M:=DayOfWeek()<=ValueWhen(2,1,DayOfWeek());
> > > > > J:=Cum(1)-ValueWhen(D+1,M,Cum(1));
> > > > > K:=ValueWhen(1,M,C);
> > > > > Sum(K*M,LastValue(J+PREV-PREV))/D;
> > > > >
> > > > > {Monday Close SMA 3}
> > > > > {for use on daily charts}
> > > > > D:=Input("Periods in Weeks",1,100,10);
> > > > > M:=DayOfWeek()<=ValueWhen(2,1,DayOfWeek());
> > > > > J:=Cum(1)-ValueWhen(D+1,M,Cum(1));
> > > > > K:=ValueWhen(1,M,C);
> > > > > Mov(K*M,LastValue(J+PREV-PREV),S)*J/D;
> > > > >
> > > > >
> > > > > ----- Original Message -----
> > > > > From: "sabboushi2000" <Yahoo@xxxx>
> > > > > To: <equismetastock@xxxxxxxxxxxxxxx>
> > > > > Sent: Friday, February 06, 2004 2:53 AM
> > > > > Subject: [EquisMetaStock Group] Simple Moving Average using
> > first
> > > > bar of each week
> > > > >
> > > > >
> > > > > > Hi-
> > > > > >
> > > > > > Am trying to use EOD daily data to create a Moving Average
> > for the
> > > > > > previous 3 weeks - by using the Close of the first bar of
each
> > > > > > week AND by using the MOV function.
> > > > > >
> > > > > > Example: Today is Wednesday. I want a simple 3 day
moving
> > > > average
> > > > > > using the following data:
> > > > > >
> > > > > >
> > > > > >
> > > > > > 2/17 Wednesday
> > > > > > 2/16
> > > > > > 2/15 Monday Close: 10
> > > > > >
> > > > > > 2/12 Friday
> > > > > > 2/11
> > > > > > 2/10
> > > > > > 2/9 Tuesday Close: 15
> > > > > > 2/8 Monday Holiday
> > > > > >
> > > > > > 2/5 Friday
> > > > > > 2/4
> > > > > > 2/3
> > > > > > 2/2
> > > > > > 2/1 Monday Close: 20
> > > > > >
> > > > > > The result I am looking for in this example would be (10
+ 15
> > +
> > > > > > 20) / 3 = 15
> > > > > >
> > > > > > (2/9 Tuesday was first trading day of that week since
Monday
> > was a
> > > > > > trading Holiday)
> > > > > >
> > > > > >
> > > > > > NOTE: I know I can create a formula to create this moving
> > average
> > > > > > without using the MOV function, but I don't really want to
> > create
> > > > a
> > > > > > simple moving average using 3 data points - I just used
this
> > as a
> > > > > > simple example for discussion purposes.
> > > > > >
> > > > > > I've tried using valuewhen to identify the first close
value I
> > > > want
> > > > > > MS8.01 EOD to use, wishing that the formula
would "increment"
> > to
> > > > each
> > > > > > previous bar that met the condition - but no such luck.
The
> > > > formula
> > > > > > I tried:
> > > > > > Mov(ValueWhen(1,FIRSTDAYOFWEEK, C) ,3 ,S )
> > > > > > where FIRSTDAYOFWEEK is a custom indicator that is true
on the
> > > > first
> > > > > > trading day of each week. My problem would be solved if
MS
> > > > > > incremented the "1" in my formula to 2 and 3 to get the
data
> > > > points I
> > > > > > want it to use - but of course I'm being naive...
> > > > > >
> > > > > > Any geniuses out there that can think outside my box?
> > > > > >
> > > > > > With Regards-
> > > > > > Sam
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > Yahoo! Groups Links
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > Yahoo! Groups Links
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> >
> >
> >
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
> >
> >
> >
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/
|