1. 首页
  2. 易语言 - EasyLanguage
  3. ELC&ADE
  4. 关于全局变量 Help with Global Variables
  1. 首页
  2. 易语言 - EasyLanguage
  3. 关于全局变量 Help with Global Variables

关于全局变量 Help with Global Variables

本文提供有关全局变量的一切相关学习,包括简明指导手册、下载地址、常见问题等。

To a newbie global variables may seem too difficult to attempt so this wiki is intended to simplify them.  First of all, the great Bamboo, inventer of ADE/ELC also created great documentation for global variables which he called  A Concise Global Variables Manual. Unfortunately Bamboo no longer posts and his document has become out of date due to the release of version 2-2.  So I have updated it to include the more important 2-2 features.  Other than that, the document is pure Bamboo.  My updated version is listed below under Children.  Newbies should read it now before proceeding. 

It is also important to visit JohnBr’s Global Variables 2.2 Download thread.  To use Global Variables you must go there and follow the download directions.  The thread also includes a list of what’s new in 2.2 and a demonstration workspace which can be very useful for learning.  It also includes GlobalVariable.dll Version 2.2 – Documentation which is the complete definition of Global Variables. 

Note that globals variables are frequently abbreviated as GV or gv or GVs.

Another useful document by Bamboo on GVs is this Forum post in which he explains the difference between GV, ADE, and ELC.  If you are unclear about which is best for you in some case be sure to read this post.

Here are a few GV FAQs:

Q: I have this statement and it won’t verify:

value1 = GVGetNamedString("myGVstring", -1);

A: The problem is that GVGetNamedString returns a string value and value1 is a number.

Q: So I changed it to:

vars: MyString("");
...
MyString = GVGetNamedString("myGVstring", -1);

and it still won’t verify!

A: The -1 is an error code which will be assigned to MyString in the event that this statement results in an error during run time.  But -1 is a number and cannot be assigned to MyString which is a string.   So a valid statement is:

MyString = GVGetNamedString("myGVstring", "-1");

Q: Do GVs retain their values when I close Tradestation and then reopen it?
A: No, the values are lost when Tradestation is closed.

Q: Can I use GVs to transmit values between charts and Radarscreens?
A: Yes, this is an advantage that GVs have over ELC and ADE.

Q: Can you give me a simple example of the use of GVs?
A: Say you want to compute a value which indicates the overall direction of the market and transmit it so that it is available to any chart or radarscreen.  This example will broadcast the Momentum value of the S&P 500.  Open the Momentum Indicator and copy and paste it to a new indicator with a name of your choosing.  Add this one statement to the bottom of the new indicator:

value1 = gvSetNamedFloat("S&P500Momentum", Mom);

Now verify and insert this indicator in a chart which has the symbol $SPX.X and is set to ‘Update value intra-bar’ on the General tab of Format Indicator.  That indicator will now broadcast the S&P500Momentum GV each tick.

To use this value in a chart or radarscreen provide statements like these to any indicator:

vars: SP500Mom(0);
SP500Mom = gvGetNamedFloat("S&P500Momentum", -1);

Now you can use the variable SP500Mom as you like. Note that the name of the GV, S&P500Momentum, must be in quotes because it is a string.

Q: Why did you use a named GV in the above example rather than a numbered GV?
A: Since GVs are shared across your entire Tradestation system you must be careful that GV usage does not conflict. Say you used a numbered GV for S&P 500 Momentum. If you ever were to accidentially use a GV with the same number for something else you would have a conflict and that would be a bug which would be very difficult to find. If you were to re-use the name, S&P500Momentum, that would also be a conflict but it is less likely that you would choose this name twice. For that reason it is recommended to always use named GVs.

Q: Can you give a more advanced use of GVs?
A: Here is an example to show a way to sum values across the entire Nasdaq 100. Please read the thread first but here we will discuss the code:

vars: appID(numToStr(getAppInfo(aiAppID), 0)), row(getAppInfo(aiRow)),  
	rowStr(numToStr(row, 0)), gv(appID+"nas"), ma(0), k(0), sum(0), val(0); 
 
ma = averageFC(close, 50); 
if leftStr(symbol, 1)<>"$" then begin {For not $} 
	val = iff(close>ma, 1, 0); 
	value1 = gvSetNamedInt(gv+rowStr, val); 
end {For not $} else begin {For $} 
	sum = 0; 
	for k = 1 to row-1 begin 
		sum = sum + gvGetNamedInt(gv+numToStr(k, 0), -1);    
	end; 
	val = 100*sum/(row-1); 
end; {For $} 
 
plot10(val, ">50d");

Let’s start with the vars:

  • appID is a string variable which is set to a value which is unique for the radarscreen or chart which this code will run in.
  • row is a numeric which is set to a value corresponding to the radarscreen row that the code is currently running in.
  • rowStr is a string version of row.
  • gv is a string which will be used as a prefix for GV names.
  • Note that this gv prefix will provide unique GV names for this indicator which will not conflict with names created by other instances of this same indicator running in another radarscreen and operating on, for example, the Dow 30.

This indicator will compute a 50-day moving average on each of the NDX100 stocks and then use GVs to determine the percentage of them that have a close greater than the ma50. So this indicator will run on all of the NDX100 symbols and it does this one at a time and the normal order is to run the rows sequently from 1 to 100. This indicator requires a special symbol, such as $NDX.X, to be inserted in row 101. The reason for this will be shown shortly.

As the indicator runs on each symbol it sets the variable ‘val’ to either 0 or 1 depending on whether close is greater than ma50 for that symbol at the current time. It then creates a named GV of type Int and assigns val to it. The name of the GV is determined by concatenating the gv prefix with the string version of the row number. In this way 100 GVs are created as the indicator runs successively on each row of the radarscreen. Note also that this process is only being used on the 100 symbols which do not begin with $.

For the one row which has a symbol that does begin with $, row 101, a different process is used. A ‘for loop’ is employed which will generate the names of all 100 of the GVs which were set by the previous process by concatenating the gv prefix with the string value of k, the loop variable. The loop uses these names to sum the 0 or 1 values of all 100 GVs. Finally the percentage is calculated and plotted.

The reader can easily imagine how to revise this indicator for functions other than ma50.

更新于 2020年11月2日

这篇文章对您有帮助吗?

相关的话题

留言评论