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

Notification

Icon
Error

Options
Go to last post Go to first unread
teachme  
#1 Posted : Friday, September 7, 2007 11:38:18 PM(UTC)
teachme

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 10/1/2006(UTC)
Posts: 17

Thanks: 3 times
Was thanked: 1 time(s) in 1 post(s)
Dear Metastock Gurus,

I would like to know if it is possible to count the number of stocks above the 50-day moving average on any single day.

Currently, what I do is to use Explorer to screen out the stocks first and then copy and paste the result onto Microsoft Excel.

I would like to make the process more efficient. Is it possible to use global variable GV.dll to count the number of stocks?

I tried researching on GV.dll but due to my inexperience or plain stupidity, I cannot understand.

Can someone advise?


mstt  
#2 Posted : Sunday, September 9, 2007 10:44:35 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

Hi TM

I have an exploration that uses the GV DLL to do just what you want (2 explorations actually). It's too much of a battle to post the explorations and commentary here but I'd be happy to send it to you or anyone else who provides me with an email address or PM alias. The principle can be adapted to counting many different events. Advances and declines and new highs and lows are a couple of the more obvious uses for those that don't have access to broad market data.

Roy

MetaStock Tips & Tools

mstt  
#3 Posted : Sunday, September 9, 2007 11:29:53 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

Hi TM


With a little help from Wabbit I think my document should appear more or less as intended now. The two explorations below will allow you to count the number of securities closing above a 50 period EMA by using Mark Pyle's GV DLL.

One of the difficulties with the GV DLL is that it's not designed for accumulating results across multiple securities. In short, it does not store the dates associated with specific data cells and it therefore cannot handle missing bars or N/A values. When counting just one event per security this weakness can be overcome by the appropriate use of LastValue() - a tactic that nullifies the tendency of a GV data array to crab to the left by one cell every time a missing bar (missing date) is encountered in any security.

When counting across multiple historical bars all securities must first be screened to ensure that all bars within the period of interest have all dates in common. Just one missing bar will invalidate the entire GV data array. I can provide routines that help identify and filter out securities not meeting the necessary standard.

The Cum() function in MetaStock allows the accumulation of totals or counts serially (from one bar to the next) across a single data array. However there is no function in MetaStock to accumulate totals in parallel across a portfolio of securities. Essentially MetaStock always processes serially (function by function, line by line, bar by bar, security by security etc.).

To count across multiple securities it's necessary to be able to read a value on any given date or bar, and then calculate a new value and store it while still processing the same bar in the same data array, before MetaStock moves on to process the next bar. Counter explorations achieve this by the user setting up GV (global variable) data arrays for the required counters and then scanning all securities to read, calculate and store updated values for each security. These values are stored outside of MetaStock by the GlobalVar DLL. For practical reasons this DLL has been renamed GV.DLL.

Using LastValue() both when reading and calculating an update ensures that all available cells in the counter's GV data array hold the counter's current value. It does not matter that the same value is stored in multiple cells, only that the last cell always holds the current counter value. As the accumulator or counter is read for each security MetaStock loads the current value from the GV data array (or arrays) for every bar accessed.

When a counting exploration is completed the last security processed will provide the final count. Sometimes this will not be the number that appears at the end of the exploration report column. The reason for this is that a database is often sorted (and therefore processed) in an order that is not consistent with an alphabetical listing by name or symbol. The final counter value can be easily found by clicking on the appropriate report column header to sort it in ascending or descending order.

Remember that global variables are not stored from one MetaStock session to the next. Once MetaStock is closed all global variables are lost. That does not mean that values reported by an exploration are lost though; an exploration report can be retrieved at any time until that exploration is rerun.

The purpose of the first of the two explorations below is to reset the required global variables before use by the counter exploration (create and set all cells in GV data arrays to zero). Global variables can also be reset or displayed by using indicators but the process is simpler when a setup exploration is batched with the main exploration and scans only one security or index. Naming your explorations with the same name and then appending "Reset" or "Scan" ensures that a reset sequence is performed before the scan sequence (it appears first in the Explorers list of explorations and is therefore executed first). Additionally, the reset scan sets up global variables holding the most recent date of the scanned index or security. The reason for this will be explained shortly.

The second exploration marks events in column B and counts those events in column A. Global variables should always be reset (to zero) prior to running this scan. If not reset a counter will simply carry on from the previously stored value. Some additional checks are included in the "Test" variables of the second exploration. The main focus of these checks is to ensure that the counter is only applied to securities that traded on the most recent bar of the reference index or security. It's highly likely that a security that did not trade on the most recent trading day should not be included in the count. This certainly applies to advances, declines, new highs and new lows. A test for volume greater than zero is also included. This should only have an effect when non-traded days are auto-filled with a price from the previous trading day. Zero volume strongly suggests that no trading activity occurred. If the exploration is used with mutual funds or other issues that do not have volume the expression "AND V>0" should be commented out from each column and from the Filter section.

You might ask why the date check needs to be applied to each column as well as the filter section. That's a good question. The filter is only effective when processing the last bar of data for each security, and not only that, it's only effective AFTER the last bar has been processed by all column formulas. In other words the filter is not enabled until after all GV-based counters have been updated, and that's too late. Even though a security can be filtered out from an exploration report, its contribution to a counter must be inhibited in columns A-F as well (columns A-L for MS V10).

Code:
{EMA Status Count Reset}
{Roy Larsen}
{Col A: LastDay}
{Reset GV counter(s)}
A:=ExtFml("GV.SetVar","CountU",0);
{Store and plot last day parameter}
ExtFml("GV.SetVar","RunD",LastValue(DayOfMonth()));
{Col B: LastMnth}
{Store and plot last month parameter}
ExtFml("GV.SetVar","RunM",LastValue(Month()));
{Col C: last year}
{Store and plot last year parameter}
ExtFml("GV.SetVar","RunY",LastValue(Year()));
Filter enabled: No
Periodicity: Daily
Records required: 250
Code:
{EMA Status Count Scan}
{Roy Larsen}

{Col A: Counter}
{Count securities above 50-day MA}
{Date variables}
D:=DayOfMonth(); M:=Month(); Y:=Year();
LD:=LastValue(D); LM:=LastValue(M);
{Indicator to check price against}
MA:=Mov(C,50,E);
{Count securities meeting test conditions}
Count:=LastValue(ExtFml("GV.GetVar","CountU"));
RunD:=ExtFml("GV.GetVar","RunD");
RunM:=ExtFml("GV.GetVar","RunM");
RunY:=ExtFml("GV.GetVar","RunY");
Date:=RunD=D AND RunM=M AND RunY=Y;
Test:=LastValue(Date AND C>MA AND V>0);
NewCount:=If(Test,{Then} Count+1,{Else} Count);
ExtFml("GV.SetVar","CountU",NewCount);

{Col B: Status}
{Mark securities above 50-day MA}
{Date variables}
D:=DayOfMonth(); M:=Month(); Y:=Year();
LD:=LastValue(D); LM:=LastValue(M);
{Indicator to check price against}
MA:=Mov(C,50,E);
{Mark securities meeting test conditions}
RunD:=ExtFml("GV.GetVar","RunD");
RunM:=ExtFml("GV.GetVar","RunM");
RunY:=ExtFml("GV.GetVar","RunY");
Date:=RunD=D AND RunM=M AND RunY=Y;
LastValue(Date AND C>MA AND V>0);

{Filter}
{Discards a security when date or colB not matched}
RunD:=ExtFml("GV.GetVar","RunD");
RunM:=ExtFml("GV.GetVar","RunM");
RunY:=ExtFml("GV.GetVar","RunY");
D:=DayOfMonth(); M:=Month(); Y:=Year();
RunD=D AND RunM=M AND RunY=Y AND colB;

Filter enabled: Yes
Periodicity: Daily
Records required: 250


Regards

Roy
MetaStock Tips & Tools


[edited by wabbit: added code tags]

teachme  
#4 Posted : Saturday, September 15, 2007 3:16:15 AM(UTC)
teachme

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 10/1/2006(UTC)
Posts: 17

Thanks: 3 times
Was thanked: 1 time(s) in 1 post(s)
Hi mstt,

Thank you very much for your reply.

You have been most helpful. I hope you make plenty of $$$ from the market this year.

I will study your post with diligence
gawdib  
#5 Posted : Sunday, January 15, 2012 8:17:32 PM(UTC)
gawdib

Rank: Advanced Member

Groups: Registered, Registered Users, Unverified Users
Joined: 11/10/2005(UTC)
Posts: 31

Amazing. Roy, you do it again. Kudos
uasish  
#6 Posted : Friday, February 10, 2012 2:41:42 PM(UTC)
uasish

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 8/13/2005(UTC)
Posts: 170

Thanks: 7 times
Till now found Roy,Jose & Wabbit are the best in that sequence.
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.