1. 首页
  2. 易语言 - EasyLanguage
  3. 全局变量简明手册 A Concise Global Variables Manual

全局变量简明手册 A Concise Global Variables Manual

这里有您想了解的关于全局变量的安装、工作原理、常见问题。

How do I install GlobalVariables?
 

  1. Click the ZIP file attached at the end of the article.
  2. Click Open.
  3. From the File menu select Extract All opening a File Download and click Open.
  4. Browse to a directory of your choice and click Next.
  5. Click Finish
  6. Copy GlobalVariable.dll from your unzip directory to your C:\WINDOWS\system32 directory (or equivalent).
  7. With Tradestation open click GV  2-2.ELD and then click Open and follow the Import Wizard to import the Global Variable functions into Tradestation.Included in the download is Global Variable 2.2.doc which is the complete Global Variable Document.

You don’t have to know anything about DLLs in order to use GlobalVariables. Just perform these installation steps, and you can call ordinary EasyLanguage functions to do the job.

How does it work?

For each data type (Integer, Float, Double, String, Boolean), you can store up to 10,000 numbered variables and 3,000 named variables. A numbered variable is identified by an integer (1, 2, 3, etc.). A named variable is identified by a unique name (e.g. “MyGlobalVar”).

(If you are unfamiliar with the terms “Float” and “Double”, a “Float” is a single-precision floating point number, and a “Double” is a double-precision floating point number. You can use “Double” as the equivalent of a numeric value in EasyLanguage.  A “Boolean” is a variable that has only two possible values: “true” or “false.”)

When you set the value of a global variable, you can retrieve that value from any other strategy or study in TradeStation. For example, you can use GVSetNamedDouble (“StopPrice”, StopPrice) in a strategy to store your stop price, and then use GVGetNamedDouble (“StopPrice”, -1) in an indicator to retrieve that stop price and display it on a chart.

Most GlobalVariable functions come in four flavors:

1. GVSet

{Type}(Location, Value) where {Type}

is Integer, Float, Double, String, or Boolean. Location is a unique integer that identifies the variable, and Value is the value you want to store. These functions set the value of a numbered global variable. For example, GVSetString(2, “Test”) sets the value of the global variable at location 2 to “Test”.

2. GVGet

{Type}(Location) where {Type}

is Integer, Float, Double,  String, or Boolean. These functions retrieve the value of the global variable at the specified integer location. For example, GVGetString(2) will return “Test” if you call it immediately after the above GVSetString call.

3. GVSetNamed

{Type}(Name, Value) where {Type}

is Int, Float, Double, String, or Bool. Name is a unique string that identifies the variable, and Value is the value you want to store. These functions set the value of a named global variable. For example, GVSetNamedDouble(“ProfitTarget”, 1000) sets the value of the global variable called “ProfitTarget” to 1000.

4. GVGetNamed

{Type}(Name, ErrorCode) where {Type}

is Int, Float, Double, String, or Bool. These functions retrieve the value of the global variable with the specified name. If there is an error, they return the specified ErrorCode. For example, GVGetNamedDouble(“ProfitTarget”, -1) will return 1000 if you call it immediately after the above GVSetNamedDouble call.
 NOTE: For numbered global variables the full words Integer and Boolean are used but for named global variables the shortened words Int and Bool are used.
The GV functions return a negative number if there is an error, except for

  1. GVGetString which returns an empty string if there is an error.
  2. GVGetNamed… which require you to specify an ErrorCode in the second argument, and this value will be returned if there is an error.
  3. GVGetBoolean which returns “false” if there is an error.
  4. GVGetNamedBool which generates a run-time error if there is an error.If you use unnamed global variables, you should probably avoid storing negative numbers in them, since there would be no way to distinguish certain negative numbers (-1, -2, -3) from error values.  If you use Boolean global variables you should probably avoid using numbered global variables since there is no way to distinguish a valid return of false from an error condition.

The return value of the GVSet… functions should be treated only as a status code. If it’s negative, there was an error. If it’s greater than or equal to zero, the function succeeded.

The GVGet

{Type} and GVSet{Type}

family of functions should be called only on the last bar of a chart; otherwise, they will return an error. Thus, you should always check LastBarOnChart before calling them:

{ Called in your strategy }

if LastBarOnChart then GVSetDouble(5, EntryPrice);

{ Called in your indicator }

if LastBarOnChart then EPrice = GVGetDouble(5);
select all 

The GVGetNamed

{Type} and GVSetNamed{Type}

family of functions do not have this restriction: they can be called on any bar.

What is the difference between named and numbered global variables?

Named global variables produce code that is easier to understand. Numbered global variables are faster. You can use both in your code if you wish, since the storage locations of named and numbered global variables are separate.

Note that you get 10,000 storage locations for each data type for numbered variables (except for strings, which provide only 3,000 storage locations). Thus, you can use the location 3 for both a Double and a String, and these are separate locations.

You also get 3,000 storage locations for each data type for named variables. Thus, you can use the name “MyGlobalVar” for both a Double and a String (although I wouldn’t recommend this).

Can I backtest GlobalVariables?

No, you can’t. The reason is that a global variable only remembers the value from the last GVSet… call. Global variables do not maintain any history, so you can’t use them to pass data from a historical bar in one chart to a historical bar in another chart. They are intended to pass data from one strategy or study to another in real time.

You should also be aware that synchronization problems can occur with global variables. In the example above, if your indicator executes the code for its last bar before your strategy does, then the indicator may not retrieve the value from the latest strategy bar. The complete GlobalVariables document suggests loading your studies and strategies in the preferred order to avoid this problem.

 What else?

 Other features are included in Global Variable 2.2.doc including functions to “reset” or “reinitialize” named global variables and to retrieve the name of a named global variable along with several other changes.  These changes are not described in this document because it is expected that they will be seldom used.

更新于 2020年5月21日

这篇文章对您有帮助吗?

相关的话题

留言评论