Global Variables in MC.Net ?

Questions about MultiCharts .NET and user contributed studies.
johnromeronc
Posts: 53
Joined: 18 Feb 2011
Has thanked: 11 times
Been thanked: 13 times

Global Variables in MC.Net ?

Postby johnromeronc » 14 Nov 2012

It is my understanding, that depending on where I define my variables in C#, they can be global in MC.Net across indicators, but what I am looking for is --> has anyone implemented or created some wrappers to use Global Variable 2.2, so I can share values with MC regular. I'm hoping not to have to get my head around this C++ dll. I've already had to do it on another dll, and I am not the sharpest tool in the shed. So there was quite a bit of trial and error.

John

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Global Variables in MC.Net ?

Postby riverTrader » 10 Dec 2012

Here you go.

Code: Select all

public static class LegacyGlobalVariables {
const string WildGvDllFilePath = @"C:\Program Files (x86)\..."; // your global variable path



[DllImport(WildGvDllFilePath)]
public static extern int GV_SetInteger(int iLocation, int iVal);

[DllImport(WildGvDllFilePath)]
public static extern int GV_GetInteger(int iLocation);

[DllImport(WildGvDllFilePath)]
public static extern int GV_SetNamedInt(string sIntVarName, int iVal);

[DllImport(WildGvDllFilePath)]
public static extern int GV_GetNamedInt(string sIntVarName, int iErrorCode);

[DllImport(WildGvDllFilePath)]
public static extern int GV_SetFloat(int iLocation, float fVal);

[DllImport(WildGvDllFilePath)]
public static extern float GV_GetFloat(int iLocation);

[DllImport(WildGvDllFilePath)]
public static extern int GV_SetNamedFloat(string sFloatVarName, float fVal);

[DllImport(WildGvDllFilePath)]
public static extern float GV_GetNamedFloat(string sFloatVarName, float fErrorCode);

[DllImport(WildGvDllFilePath)]
public static extern int GV_SetDouble(int iLocation, double dVal);

[DllImport(WildGvDllFilePath)]
public static extern double GV_GetDouble(int iLocation);

[DllImport(WildGvDllFilePath)]
public static extern int GV_SetSecsBack(int iArrayId, int iLocation, double dVal);

[DllImport(WildGvDllFilePath)]
public static extern double GV_GetSecsBack(int iArrayId, int iLocation);

[DllImport(WildGvDllFilePath)]
public static extern int GV_SetMinsBack(int iArrayId, int iLocation, double dVal);

[DllImport(WildGvDllFilePath)]
public static extern double GV_GetMinsBack(int iArrayId, int iLocation);

[DllImport(WildGvDllFilePath)]
public static extern int GV_SetNamedDouble(string sDoubleVarName, double dVal);

[DllImport(WildGvDllFilePath)]
public static extern double GV_GetNamedDouble(string sDoubleVarName, double dErrorCode);

[DllImport(WildGvDllFilePath)]
public static extern int GV_SetString(int iLocation, string sVal);

[DllImport(WildGvDllFilePath)]
public static extern int GV_SetNamedString(string sStringVarName, string sVal);

[DllImport(WildGvDllFilePath, EntryPoint = "GV_GetNamedString")]
static extern IntPtr GetNamedString(string sStringVarName, string sErrorCode);

[DllImport(WildGvDllFilePath, EntryPoint = "GV_GetString")]
static extern IntPtr GetString(int iLocation);

public static string GV_GetString(int iLocation) {
IntPtr ptr = GetString(iLocation);
return Marshal.PtrToStringAuto(ptr);
}

public static string GV_GetNamedString(string sStringVarName, string sErrorCode) {
IntPtr ptr = GetNamedString(sStringVarName, sErrorCode);
return Marshal.PtrToStringAnsi(ptr);
}
}

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Global Variables in MC.Net ?

Postby riverTrader » 10 Dec 2012

Caveat: Unless you are supporting legacy code or need interop with an external process (eg. TS) I would NOT recommend going down the path of using GlobalVariable as a crutch. Every variable you put in global variable must be marshalled (copied) from the legacy .dll into/out of mc.net every time you access it. It is fast enough -- unless you are using huge numbers of variables (the values of a time-series). Though it is virtually impossible to debug past the marshalling barrier; so if your app is setting a variable in GV and not returning a valid value later, good luck.

1.) The far better approach is to design your code so that you do not rely on global variables; create objects that hold data 'close' to their source and use properties to either the variables themselves, or to containers (eg. arrays, lists, sortedLists, dictionary, etc.) that hold the variables.
2.) explore the mc.net library and find interfaces that suit your purposes, then design your classes around those interfaces. (I only do this for classes I design for use within mc; i like to keep my code vendor neutral).
3.) If you find you truly need a global datastore for your application, design a static class to hold these and add public static variables to hold them. Bad practice; but global variables are not best practice either. The static class and its variable can be referenced much faster from the compiler than an instance class.
4.) if your problem is maintaining an interprocess layer between legacy code, keep in mind that .net 4.0 will bring with it shareable memory-mapped files; a nice new world.

VanVan
Posts: 10
Joined: 28 Dec 2012
Been thanked: 22 times

Re: Global Variables in MC.Net ?

Postby VanVan » 11 Jan 2013

GlobalVariables for all MultiCharts versions including MultiCharts .Net

For MC x32 and MC .Net x32
You need to have vcredist_x86_2010 installed

For MC x64 and MC .Net x64
You need to have vcredist_x64_2010 installed

What is possible:
It allows you to pass the data between signals/indicators within the same MultiCharts.exe process
(from chart to chart, from chart to scanner, etc.)

How to install:
1) Unzip the archive;
2) Copy COGlobalVariable.dll, COGlobalVariableWrapper.dll and Register.bat into
MultiCharts installation folder (the files should correspond to your MC version: For MC x32 and MC .Net x32 use the files from Win32 folder, for MC x64 and MC .Net x64 use х64 folder).
3) Right click on Register.bat in MultiCharts installation folder->Run as administrator.

In the zip file there is a МС .Net х64 example.

Currently implemented functions:
bool InitializeGV()
void UninitializeGV()
bool SetDataInt(String name, int _value)
int GetDataInt(String name)
bool SetDataLong(String name, long _value)
long GetDataLong(String name)
bool SetDataFloat(String name, float _value)
float GetDataFloat(String name)
bool SetDataDouble(String name, double _value)
double GetDataDouble(String name)
bool SetDataString(String name, String _value)
CString GetDataString(CString name)
where name – variable name, _value – its value.
Max string length for _value value of SetDataString function = 2048 symbols.
Maximal value of current data type is returned in case of unsuccessful attempt to get a variable value.
String function returns an empty string.

PowerLanguage equivalents:
GVGetNamedDouble
GVGetNamedInt
GVSetNamedDouble
GVSetNamedInt

For МС 32 bit you need to change the path to COGlobalVariableWrapper.dll in _GVLibWrap_ function.
Attachments
Gv_x32_x64.zip
(51.6 KiB) Downloaded 675 times

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: Global Variables in MC.Net ?

Postby vking » 14 Feb 2013

VanVan - Thanks for posting this. Any possibility of posting the source code for this?

Thanks

VanVan
Posts: 10
Joined: 28 Dec 2012
Been thanked: 22 times

Re: Global Variables in MC.Net ?

Postby VanVan » 18 Feb 2013

VanVan - Thanks for posting this. Any possibility of posting the source code for this?

Thanks
in Russian:
Для сборки вам нужно перенастроить пути к некоторым файлам в проекте

in English:
You will need to reconfigure the path to certain files in the project for the assembly
Attachments
GV_src.zip
(36.41 KiB) Downloaded 610 times

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: Global Variables in MC.Net ?

Postby vking » 18 Feb 2013

Thank you VanVan.. appreciate it.

Lutz
Posts: 8
Joined: 01 Nov 2006
Has thanked: 4 times

Re: Global Variables in MC.Net ?

Postby Lutz » 20 Apr 2013

Hello,

if I apply the example indicator _GlobalVars_ the CPU load goes up to nearly 99%. I use an old computer with WinXP. Can I avoid this high CPU load?

Regards

Lutz

maybe a stupid question, but I am a beginning programmer...

MarketPrep
Posts: 9
Joined: 24 Apr 2013
Has thanked: 2 times

Re: Global Variables in MC.Net ?

Postby MarketPrep » 02 Oct 2013

I am intrigued if this use of GlobalVariables will work with the backtesting engine? I've seen several posts which do not recommend the use of GlobalVariables with the backtesting engine.

If it will not is there any recommended way to pass information between SignalObjects using MC.Net?
Should we create our own classes containing static variables and then reference them via accessor methods?
Should we just create the static variable within the PowerLanguage.Strategy namespace such that it can be shared amongst all SignalObjects?

For instance Yesterday's OHLC would be helpful to be defined once and then accessed via methods vs needing to define it over and over in each Signal which is used in the backtesting engine.

Thoughts?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Global Variables in MC.Net ?

Postby Henry MultiСharts » 18 Oct 2013

MarketPrep,

You can have a look at C# Solution for Global Storage described here.


Return to “MultiCharts .NET”