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)
|
Making Constants Available Globally
This exercise demonstrates how to take a standard indicator with one or more Input() functions and modify it to use global variables instead of having to rely on Input() function defaults. A Compensated Linear Regression Slope indicator is used in each exploration column, and a conventional version of the indicator is also shown below for your interest.
Inputs to a specially constructed indicator are used to set a selection of constants which are then saved as global variables. This same indicator may be used to immediately retrieve these constants and plot them on an open chart for verification. It can be helpful to plot a sine wave or some other obvious signal in addition to the user’s constants because horizontal lines might not be visible when un-scaled.
The indicator should be deleted from the chart once the constants have been verified as correct. It can be pulled down again at any time the "global" constants need to be changed. Global variables are lost when MetaStock is closed so constants stored by this method must be set up prior to use in each MetaStock session.
Making an asterisk {*} the first character of the "* GV Set Constants" indicator ensures that this indicator resides at the top of the drop-down indicator list and is therefore easy to find.
The use of LastValue() when calling global variables makes these values available as constants, and they can then be used as "periods" parameters.
The procedure for making global variables available as "global constants" is as follows.
1. Pull down the "* GV Set Constants" indicator onto any open chart
2. Set required values for all constants using the "Parameters" window.
3. Refresh the chart.
4. Verify that displayed constants are the correct values (place cursor on any plot to view data window).
5. Delete the "* GV Set Constants" indicator from open chart(s).
Never use Fml("* GV Set Constants") to access any global variables. This will restore saved constants back to the (probably unwanted) Input() defaults. Until you are confident in using the above procedures it can be reassuring to have an extra indicator that only DISPLAYS global variables and CAN NOT change them. See below for "* GV Get Constants".
If you need assistance in adapting this method to your particular requirements then please write to me directly at rlarsen@man.quik.co.nz .
Roy
MetaStock Tips & Tools
{----------8<----------}
{* GV Set Constants}
{GV.DLL courtesy of Mark Pyle}
{© 2006 Roy Larsen}
{www.metastocktips.co.nz}
{Use to set constants as global variables.}
{Pull down, set values, refresh chart,}
{verify, delete indicator, use constants.}
GV1:=Input("Set Constant 1 - GV1",-999,999,20);
GV2:=Input("Set Constant 2 - GV2",-999,999,20);
GV3:=Input("Set Constant 3 - GV3",-999,999,20);
GV4:=Input("Set Constant 4 - GV4",-999,999,20);
GV5:=Input("Set Constant 5 - GV5",-999,999,20);
GV6:=Input("Set Constant 6 - GV6",-999,999,20);
{Set user constants to GV.DLL}
A:=ExtFml("GV.SetVar","GV1",GV1);
A:=ExtFml("GV.SetVar","GV2",GV2);
A:=ExtFml("GV.SetVar","GV3",GV3);
A:=ExtFml("GV.SetVar","GV4",GV4);
A:=ExtFml("GV.SetVar","GV5",GV5);
A:=ExtFml("GV.SetVar","GV6",GV6);
{Get user constants from GV.DLL}
GV1:=LastValue(ExtFml("GV.GetVar","GV1"));
GV2:=LastValue(ExtFml("GV.GetVar","GV2"));
GV3:=LastValue(ExtFml("GV.GetVar","GV3"));
GV4:=LastValue(ExtFml("GV.GetVar","GV4"));
GV5:=LastValue(ExtFml("GV.GetVar","GV5"));
GV6:=LastValue(ExtFml("GV.GetVar","GV6"));
{Plot user constants}
GV1; GV2; GV3; GV4; GV5; GV6;
{Plot sine wave to ensure indicator visibility}
Sin(Cum(5));
{----------8<----------}
{----------8<----------}
{Compensated LRS}
{© 2003-2006 Roy Larsen}
{www.metastocktips.co.nz}
Pds:=Input("LRS Periods",1,999,10);
LinRegSlope(WC(),Pds)*1000/WC();
{----------8<----------}
{----------8<----------}
{Exploration notes}
{Sample exploration to demonstrate the use of global variables with purpose-built "indicator" formulas}
{Col A:}
Pds:=LastValue(ExtFml("GV.GetVar","GV1"));
LinRegSlope(WC(),Pds)*1000/WC();
{Col B:}
Pds:=LastValue(ExtFml("GV.GetVar","GV2"));
LinRegSlope(WC(),Pds)*1000/WC();
{Col C:}
Pds:=LastValue(ExtFml("GV.GetVar","GV3"));
LinRegSlope(WC(),Pds)*1000/WC();
{Col D:}
Pds:=LastValue(ExtFml("GV.GetVar","GV4"));
LinRegSlope(WC(),Pds)*1000/WC();
{Col E:}
Pds:=LastValue(ExtFml("GV.GetVar","GV5"));
LinRegSlope(WC(),Pds)*1000/WC();
{Col F:}
Pds:=LastValue(ExtFml("GV.GetVar","GV6"));
LinRegSlope(WC(),Pds)*1000/WC();
Filter enabled Yes
Periodicity Daily
Records required 100
{----------8<----------}
{----------8<----------}
{* GV Get Constants}
{© 2006 Roy Larsen}
{www.metastocktips.co.nz}
{Get user constants from global var DLL}
GV1:=LastValue(ExtFml("GV.GetVar","GV1"));
GV2:=LastValue(ExtFml("GV.GetVar","GV2"));
GV3:=LastValue(ExtFml("GV.GetVar","GV3"));
GV4:=LastValue(ExtFml("GV.GetVar","GV4"));
GV5:=LastValue(ExtFml("GV.GetVar","GV5"));
GV6:=LastValue(ExtFml("GV.GetVar","GV6"));
{Plot user constants}
GV1; GV2; GV3; GV4; GV5; GV6;
{Plot sine wave to ensure indicator visibility}
Sin(Cum(5));
{----------8<----------}
|