logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Jose  
#1 Posted : Saturday, September 24, 2005 11:50:34 PM(UTC)
Jose

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/19/2005(UTC)
Posts: 1,065
Location: Koh Pha-Ngan, Earth

Was thanked: 2 time(s) in 2 post(s)
Programming code to signal the true start of a trading week is relatively easy in MetaStock. However, code that signals the true last day of the trading week (or EOW), has been one of MetaStock's Holy Grail. The difficulty lies in detecting that last trading day of the week when we all start our weekend a day or more early. DayOfWeek()=5 (Friday) does not work when the last day of the week falls on a Thursday, Wednesday (or even Monday & Tuesday with some illiquid securities). How is it possible for MetaStock to know about these irregular EOW signals? Well, with a little hindsight. Correct start & end of week signals for daily/intraday charts, independent of any missing chart data: MetaStock -> Tools -> Indicator Builder -> New -> Copy and paste formula below. [code:1:e3449e1bcb] ======================= Week's true Start & End ======================= ---8<--------------------------- { Week's true start & end of week (EOW) v2.0 } { Confirms EOW signal at start of following week, and signals EOW in retrospect.} { Signals independent of any missing chart data} { For daily & intraday charts } { ©Copyright 2002~2005 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 input } EOW:=Input("Final week's end: [1]True, [2]Current (dynamic)",1,2,1); { Calendar Week counter } limit:=2000; {do not change limit year} 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(); WkCount:=Int((DayNr-1)/7)+(Year()>=limit); { Week's start signal } WeekStart:=WkCount>Ref(WkCount,-1); { Week's end signal } { Choice of Static/Dynamic last EOW signal } EOW:=If(EOW=2,Cum(1)=LastValue(Cum(1)), Cum(1)=LastValue(Cum(1)) AND DayOfWeek()=5); { Detect other than daily/intraday charts } detect:=LastValue(Cum( WkCount=ValueWhen(2,1,WkCount)))=0; { Make EOW signal visible for weekly+ charts } WeekEnd:=PeakBars(1,-(If(detect=0, WeekStart OR Cum(IsDefined(WeekStart))=1, Cum(1)/2=Int(Cum(1)/2))),1)=0 OR EOW; WeekEnd:=If(detect=0,WeekEnd,1); { Plot signals in own window } WeekStart;-WeekEnd ---8<--------------------------- [/code:1:e3449e1bcb] Now, what's so important about needing to plot accurate start/end of week signals? This becomes essential when building weekly indicators for daily & intraday charts - they require accurate start & end-of-week signals. The above basic indicator can be easily referenced by other MetaStock formulae, making the process of designing weekly indicators a lot easier.
Jose  
#2 Posted : Saturday, September 24, 2005 11:52:14 PM(UTC)
Jose

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/19/2005(UTC)
Posts: 1,065
Location: Koh Pha-Ngan, Earth

Was thanked: 2 time(s) in 2 post(s)
Here is another basic example, weekly Open/High/Low/Close for daily & intraday charts: [code:1:fa94da8131] =========== Weekly OHLC =========== ---8<--------------------------- { Weekly OHLC for daily/intraday charts v2.0 } { Requires "Week's true start & end" indicator } { ©Copyright 2002~2005 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 } pds:=Input("Week's OHLC: [1]Current, [2]Previous",1,2,1); EOW:=Input("Final week's end: [1]True, [2]Current (dynamic)",1,2,2); { Week's true Start } WkStart:= FmlVar("Week's true start & end","WEEKSTART"); { Choice of Static/Dynamic last EOW signal } EOW:=If(EOW=2,Cum(1)=LastValue(Cum(1)), Cum(1)=LastValue(Cum(1)) AND DayOfWeek()=5); { Week's true End (EOW) } WkEnd:=EOW OR FmlVar("Week's true start & end","WEEKEND"); { Week's Open } wkOp:=ValueWhen(pds,WkStart,O); wkOp:=ValueWhen(1,wkOp>0,wkOp); { Week's High } Hi:=HighestSince(1,WkStart,H); wkHi:=If(pds=1,Hi, ValueWhen(1,WkStart,Ref(Hi,-1))); wkHi:=ValueWhen(1,wkHi>0,wkHi); { Week's Low } Lo:=LowestSince(1,WkStart,L); wkLo:=If(pds=1,Lo, ValueWhen(1,WkStart,Ref(Lo,-1))); wkLo:=ValueWhen(1,wkLo>0,wkLo); { Week's Close } wkCl:=ValueWhen(pds,WkEnd,C); wkCl:=ValueWhen(1,wkCl>0,wkCl); { Plot on price chart } wkOp;wkHi;wkLo;wkCl ---8<--------------------------- [/code:1:fa94da8131] Note that with the weekly OHLC indicator, the High/Low of the week are always dynamic in the current week, since their values cannot be known until the end of the trading week without cheating Father Time... With these two basic code components as reference for other code, just about any true weekly indicator can be coded in MetaStock (no requests please). :) jose '-) http://www.metastocktools.com .
Jose  
#3 Posted : Sunday, September 25, 2005 10:41:16 AM(UTC)
Jose

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/19/2005(UTC)
Posts: 1,065
Location: Koh Pha-Ngan, Earth

Was thanked: 2 time(s) in 2 post(s)
And here's an updated version of the weekly OHLC, now with more options: [code:1:4971e188f6] =========== Weekly OHLC =========== ---8<--------------------------- { Weekly Open/High/Low/Close v3.0 } { For daily/intraday charts } { Requires "Week's true start & end" indicator } { ©Copyright 2002~2005 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 } pds:=Input("Week's OHLC: [1]Current, [2]Previous",1,2,1); EOW:=Input("Final week's end: [1]True, [2]Current (dynamic)",1,2,2); plot:=Input("plot: [1]Price, [2]Week's start/end signals",1,2,1); plotOp:=Input("plot weekly Open? [1]Yes, [0]No",0,1,1); plotHi:=Input("plot weekly High? [1]Yes, [0]No",0,1,1); plotLo:=Input("plot weekly Low? [1]Yes, [0]No",0,1,1); { Week's true Start } WkStart:= FmlVar("Week's true start & end","WEEKSTART"); { Choice of Static/Dynamic last EOW signal } EOW:=If(EOW=2,Cum(1)=LastValue(Cum(1)), Cum(1)=LastValue(Cum(1)) AND DayOfWeek()=5); { Week's true End (EOW) } WkEnd:=EOW OR FmlVar("Week's true start & end","WEEKEND"); { Week's Open } wkOp:=ValueWhen(pds,WkStart,O); wkOp:=ValueWhen(1,wkOp>0,wkOp); { Week's High } Hi:=HighestSince(1,WkStart,H); wkHi:=If(pds=1,Hi, ValueWhen(1,WkStart,Ref(Hi,-1))); wkHi:=ValueWhen(1,wkHi>0,wkHi); { Week's Low } Lo:=LowestSince(1,WkStart,L); wkLo:=If(pds=1,Lo, ValueWhen(1,WkStart,Ref(Lo,-1))); wkLo:=ValueWhen(1,wkLo>0,wkLo); { Week's Close } wkCl:=ValueWhen(pds,WkEnd,C); wkCl:=ValueWhen(1,wkCl>0,wkCl); { Select price plots } wkOp:=If(plotOp,wkOp,wkCl); wkHi:=If(plotHi,wkHi,wkCl); wkLo:=If(plotLo,wkLo,wkCl); wkOp:=If(plot=1,wkOp,WkStart); wkHi:=If(plot=1,wkHi,-WkEnd); wkLo:=If(plot=1,wkLo,WkStart); wkCl:=If(plot=1,wkCl,-WkEnd); { Plot price on chart, signals in own window } wkOp; { Open: Violet} wkHi; { High: Blue } wkLo; { Low: Red } wkCl; { Close: Green } ---8<--------------------------- [/code:1:4971e188f6] jose '-) http://www.metastocktools.com .
Patrick  
#4 Posted : Monday, September 26, 2005 3:19:13 AM(UTC)
Patrick

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/8/2004(UTC)
Posts: 2,266

Was thanked: 1 time(s) in 1 post(s)
Thanks this is great :D You should record a training video for us ... Please [-o< If you have the time for it of course ;) Patrick :mrgreen: PS: Any ideas on how to turn this into a dll ... To make it easier to use and so that even a beginner could use it ...
Jose  
#5 Posted : Monday, September 26, 2005 10:26:08 AM(UTC)
Jose

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/19/2005(UTC)
Posts: 1,065
Location: Koh Pha-Ngan, Earth

Was thanked: 2 time(s) in 2 post(s)
Patrick wrote:
You should record a training video for us ... Please [-o<
I'd love to, Patrick, but I really can't seem to find the extra time that I would need to make a reasonable video. Not to mention the fact that I would make a lousy video director or producer, and putting me in front of the camera would risk breaking it... :) As for the proposed Weekly.dll, I'm not able to be of much help here either, as I'm not a programmer. I guess a Weekly.dll should first incorporate the basics shown in the "Week's true Start & End" indicator. That is, a calendar function to take care of missing data bars, and/or a way to determine the true start & end of each week. Once this is in place, it would just be a matter of using the week's start/end data reference points only. Roy has done a lot of work on weekly indicators - perhaps there could be some pointers waiting there. http://www.metastocktips.co.nz/other_formulas.html jose '-) http://www.metastocktools.com
Jose  
#6 Posted : Tuesday, September 27, 2005 5:23:36 PM(UTC)
Jose

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/19/2005(UTC)
Posts: 1,065
Location: Koh Pha-Ngan, Earth

Was thanked: 2 time(s) in 2 post(s)
Testing the weekly EMA to breaking point, Roy Larsen has found a rare instance in a highly irregular chart where an End Of Week (EOW) signal is missing. When two or more Start Of Week (SOW) signals follow each other without a break in between (e.g., data on a Monday and no other data until the following Monday), the EOW signal in between the two SOW signals fails to plot. For this example, the first SOW and EOW signals would be on the same bar on that first Monday. I would strongly advise against trading these kind of illiquid charts that may trigger the occasional EOW missing signal. For those that insist on looking at these charts, below is a second version of the "Week's true Start & End" indicator that takes care of this problem, at the expense of slowing things down with two PREV functions. For clients that require speedy processing, I also offer a custom DLL version of this indicator - it plots EOW signals correctly without the use of any PREV functions. [code:1:52f052c140] ========================= Week's true Start & End(2) ========================= ---8<--------------------------- { Week's true start & end of week (EOW) vPREVx2} { Confirms EOW signal at start of following week, and signals EOW in retrospect. Signals independent of any missing chart data. For use with highly irregular & sporadic (lots of missing data) daily & intraday charts. Use v2.0 signals for normal (liquid) charts.} { ©Copyright 2002~2005 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 input } EOW:=Input("Final week's end: [1]True, [2]Current (dynamic)",1,2,1); { Calendar Week counter } limit:=2000; {do not change limit year} 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(); WkCount:=Int((DayNr-1)/7)+(Year()>=limit); { Week's start signal } WeekStart:=WkCount>Ref(WkCount,-1); { Choice of Static/Dynamic last EOW signal } EOW:=If(EOW=2,Cum(1)=LastValue(Cum(1)), Cum(1)=LastValue(Cum(1)) AND DayOfWeek()=5); { Week's end signal } WeekEnd:=LastValue(Ref(WeekStart,1)+PREV-PREV) OR EOW; { Plot signals in own window } WeekStart;-WeekEnd ---8<--------------------------- [/code:1:52f052c140] jose '-) http://www.metastocktools.com .
Users browsing this topic
Guest (Hidden)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.