Questions on Global Variables

Questions about MultiCharts and user contributed studies.
PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Questions on Global Variables

Postby PD Quig » 26 Aug 2013

I'm attempting to use global variables to pass exit order conditions from one chart to another. Chart #1 calculates the spread (close Data1 - close Data2) between two instruments and launches an order for the Data1 instrument when the criteria are met.

Chart #2 is basically a slave chart that contains only the Data2 instrument. The strategy running on Chart #2 does no calculations--it simply waits for order signals and parameters to be passed to it via global variables and then it executes orders on Data2 of the spread pair.

I downloaded the Global Variable 2.2 word document and structured my code after its examples, but still must be missing some things:

1. The "return" variables that I am using in setting named integer and named Boolean global variables only return zero--even though the GVs are being created.
2. It's not clear to me whether when using the GV wrappers you still need to include a LastBarOnChart critierion.
3. Because the _Rtn variables are always = 0, the order execution does not happen for Data1. When I comment out the test the exit order is executed.
4. Can a local variable use the same name as a global variable?
5. Are there any housekeeping best practices when it comes to resetting global variables?

Here's the test Chart #1 GV setting code:

Code: Select all

{*************************************************************************************************************************************************************
Name : $Arb_Exit_Order_EMD_GV_Test
Last Modified Date : 08/26/2012
**************************************************************************************************************************************************************}

[IntrabarOrderGeneration = true];

//************************************************************************************************************************************************************
// inputs and variable initialization section
//************************************************************************************************************************************************************

inputs:
Exit_Target (188.0),
Position_Size (1),
Trade_Direction ("Long"),
StartTime (930);

variables:
Delta (0),
Get_GV_Position_Size (0),
Get_GV_Trigger_Long_Target (false),
int GV_Position_Size_Rtn (0),
int GV_Trigger_Long_Target_Rtn (0);


//************************************************************************************************************************************************************
// calculations
//************************************************************************************************************************************************************

Delta = close data1 - close data2;


//************************************************************************************************************************************************************
// target order execution section
//************************************************************************************************************************************************************

if LastBarOnChart then begin

If CurrentTime >= StartTime then begin

if Trade_Direction = "Long" and Delta >= Exit_Target then begin

GV_Position_Size_Rtn = GVSetNamedInt("GV_Position_Size",Position_Size); // set global variable to position size value
GV_Trigger_Long_Target_Rtn = GVSetNamedBool("GV_Trigger_Long_Target",True); // set long exit trigger global variable to true

if GV_Position_Size_Rtn > 0 and GV_Trigger_Long_Target_Rtn > 0 then begin // test for successful write of global variable before executing

sell Position_Size shares next bar at market;
end;

Get_GV_Position_Size = GVGetNamedInt("GV_Position_Size",-9999); // for output to see if GV is created
Get_GV_Trigger_Long_Target = GVGetNamedBool("GV_Trigger_Long_Target"); // for output to see if GV is created
end;
end;
end;


//************************************************************************************************************************************************************
// diagnostic print section
//************************************************************************************************************************************************************


Print(
"Date= ", date,
" Time= ", time,
" Delta= ", Delta,
" Exit_Target= ", Exit_Target,
" Position_Size= ", Position_Size,
" Get_GV_Position_Size= ", Get_GV_Position_Size,
" Get_GV_Targ_Trigger_Long= ", Get_GV_Trigger_Long_Target,
" GV_Position_Size_Rtn= ", GV_Position_Size_Rtn,
" GV_Trigger_Long_Target_Rtn= ", GV_Trigger_Long_Target_Rtn
);

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Questions on Global Variables

Postby bowlesj3 » 27 Aug 2013

I just checked the return values of the GVSetNamedInt in the documentation (see below). It appears a return value of zero could occur if it executed properly. Testing > -1 may fix it.
GVSetNamedInt
IntVRtn = GVSetNamedInt( “My Order Number”, 12345 ) ;
{ Where “My Order Number” is the global integer variable name, and 12345 is the stored value. If the EasyLanguage variable IntV is set to a non-negative integer value by this assignment statement then a global variable was created with the name “My Order Number” and this variable was set to 12345.

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Questions on Global Variables

Postby PD Quig » 27 Aug 2013

Good catch. I was assuming that the memory storage location had to be > 0 and read too quickly. Thanks.

BTW: If you--or anyone else--know of any sources that discuss global variable use beyond the bare bones 2.2 Word doc please advise. There really seems to be little out there.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Questions on Global Variables

Postby bowlesj3 » 28 Aug 2013

Yeah, that is a great idea. Has anyone tried a Google search "global variables forum" or "global variables discussion"? I can't right at the moment. In a rush.

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Questions on Global Variables

Postby PD Quig » 28 Aug 2013

Tried your search suggestions along with several other Google searches. Also, looked through the indices of all the books that came up in the Amazon "Easy Language TS" search... no luck.

This functionality appears to be a well-kept secret.

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Questions on Global Variables

Postby PD Quig » 14 Sep 2013

Follow-up to this thread:

Having failed to find any good documentation or even much in the way of code samples that could illuminate some global variable (GV) best practices, I have spent the past several weeks coding, testing, and executing strategies that perform pair trades in a live account. I hope the attached signals code will give a jump start to others attempting a simple GV implementation.

I took a "master / slave" approach where the master signal 1) does all the calculations 2) sends order triggers to the slave signal and 3) launches orders for instrument ABC. The slave signal is a simple receiver and launches orders for instrument XYZ based on the the triggers it receives triggers from the master.

The code for Arbitrage_Master_v1.0 cannot be compiled as-is because I have stripped out all the variables and logic associated with entry, target, stop calculations, and text and trend line plotting--leaving the basic shell. This template could no doubt be optimized for better execution, but I left it verbose for clarity sake.

The Arbitrage_Slave_v1.0 can be compiled as is.

Some notes / caveats:

1. This code was developed for use in futures markets using the standard MultiCharts / Interactive Brokers interface. Execution has been perfectly acceptable in small size (1-5 contracts per trade). Based on my analysis of time & price tick flow data, the system is getting next tick execution 95 percent of the time.

2. For simplicity sake, the code attached supports only one trade per automation. If you want to turn the automation on and execute multiple consecutive trades, you will need to extend the code.

3. I use two chart windows (one for each instrument) in my pair trade workspace. If the slave signal is left in Status = "On" when MultiCharts is shut down, MC will throw a pink warning dialogue upon start-up. This is caused by the slave looking for a global variable that has not been created yet. This poses no problem--somebody more clever than me can figure out how to squelch this dialogue.

Anyway, I hope this helps. If you see any opportunities for improvements please sing out.

PDQ
Attachments
Pair_Trade_Master_and_Slave.pla
(41.59 KiB) Downloaded 403 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Questions on Global Variables

Postby TJ » 14 Sep 2013

This might be of interest to those who need to rely on GV for their analysis

GlobalVariables GV Latency Tester
viewtopic.php?f=5&t=10780

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Questions on Global Variables

Postby bowlesj3 » 14 Sep 2013

I did a test on GV speed once. It was very simple and there is no need to make it more complex than this. In a program outside MC send a GV variable out then immediately get it back and time the difference. After doing this test I realized immediately that they are way way faster than MC mainly because MC has a lot of study code to go through before it gets around to pulling in the GV. So what I am saying is (if you are sending something out to GV in one study or one chart and picking it up later in another study or another chart), the GV will be out there waiting long before your other study gets around to grabbing it (way way way before). It may be a bit different on the 64 bit system but the GVs may be faster there too. If you are concerned about speed it is best to put as much as you can into a single study (if you can). Studies are loaded into memory and executed alphabetically within chart so you might want to place them in a specific order of execution if they are within the same chart. Studies are executed randomly across charts due to the multi-threading (I did tests to discover they are random across charts.). Overall if one is concerned with speed, getting the fastest computer you can afford with the most memory and fastest memory you can afford is the general rule (something on my to-do list).

It may be that the named GVs are a bit slower than the numbered GVs. I put all my named GVs out at the start of the trading day in alphabetical order hoping there is a binary search used to speed finding them (I do this before MC starts). Unfortunately there is no way of finding out if a binary search is used. The send and receive of named GVs is so fast I don't bother using numbered GVs at all. I wish I could get my EL code to go that fast.
Last edited by bowlesj3 on 14 Sep 2013, edited 1 time in total.

PD Quig
Posts: 191
Joined: 27 Apr 2010
Location: San Jose
Has thanked: 67 times
Been thanked: 10 times

Re: Questions on Global Variables

Postby PD Quig » 14 Sep 2013

Thanks, TJ and JB both for the latency info! For real-time execution I run only one very simple indicator and the master signal on one chart, with only the slave signal on the other chart. I'm sure C#, C++, or VB code could be much faster, but from what I've seen I can live with the execution throughput I'm getting with MC and IB. BTW: in a month I have yet to get a GV return code < 0.

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: Questions on Global Variables

Postby sptrader » 14 Sep 2013

Thanks, TJ and JB both for the latency info! For real-time execution I run only one very simple indicator and the master signal on one chart, with only the slave signal on the other chart. I'm sure C#, C++, or VB code could be much faster, but from what I've seen I can live with the execution throughput I'm getting with MC and IB. BTW: in a month I have yet to get a GV return code < 0.
*******************************************************************

* If you don't mind my asking, what are the 2 instruments that you like to use to spread trade ??
I've thought about trying it for several years but never got around to it..


Return to “MultiCharts”