static/global variables
static/global variables
20 Feb 2017, 13:56
Hello. Is it possible that one instance of the cBot that is running on a symbol is getting the value of the same variable that is running on another symbol. The variable is marked as static. Does the static attribute means that the variable becomes global? I have an issue on that front so I need to sort that out.Thank you.
Replies
Jonkey
29 Oct 2018, 12:11
using System; using System.Linq; using System.Collections.Generic; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class MyRobot: Robot { [Parameter("Vol Qty", DefaultValue = 10000, MinValue = 1000, Step = 1000)] public int volQty { get; set; } private string currentName; // ########################################################################################################### ON START ########## protected override void OnStart() { if (CoolClass.instances != null) { CoolClass.instances.Clear(); } for (int i = 0; i < 10; i++) { string name = string.format("name{0}", i.toString()); CoolClass.Initializer(i, name); } } protected override void OnTick() { // uses static dictionary in CoolClass to juggle stuff around currentName = "name5" if (CoolClass.instances[currentName].myName != "name5") { Stop(); } } // ############################################################################################################ ON STOP ########## protected override void OnStop() { foreach (var position in Positions) { if (Symbol.Code == position.SymbolCode) { ClosePosition(position); } } } } // ################################################################################################################################### // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< CUSTOM CLASS >>>>>>>>>> // ################################################################################################################################### public class CoolClass { public static Dictionary<string, CoolClass> instances; public int myNumber; private string myName; private bool isInitialized; // ######################################################################################################## CONSTRUCTOR ########## private CoolClass() { isInitialized = false; } // ################################################################################################### CONFIRM INSTANCE ########## private static bool ConfirmInstance(string name) { if (instances == null) { instances = new Dictionary<string, CoolClass>(); } if (!instances.ContainsKey(name)) { instances.Add(name, new CoolClass()); } return true; } // ######################################################################################################## INITIALISER ########## public static void Initializer(int number, string name) { if (ConfirmInstance(name)) { if (!instances[name].isInitialized) { instances[name].myNumber = number; instances[name].myName = name; instances[name].isInitialized = true; } } } public static void SomeMethod() { //Do something } } }
Would anyone mind considering this example code and following questions? In particular the static dictionary "instances" in the custom class... I am pretty sure it will not conflict across my instances... but any clarification will help...
Hallucination 1. (True or False or explain) The static Dictionary "instances" in MyRobot running on EURUSD may conflict with a MyRobot running on AUDUSD as they both share memory for CoolClass.instances? (as Dictionary Keys of the same name may write over each others values possibly?)
Hallucination 2. (True or False or explain) Duplicated robots named MyRobot_EURUSD to run on EURUSD and MyRobot_AUDUSD to run on AUDUSD will not conflict with each other as they are compiled to separate executables and each of their Dictionary's hold their own space in memory?
Thank you in advance. (Note: if there are syntactical and logic errors in the example code, no need to point them out... i would just like to understand more about the behaviour in memory of the static dictionary)
@Jonkey
zhitnikroman
21 Feb 2017, 13:36
A static attribute in cAlgo means that ALL launched instances consider this variable as global. Thread closed.
@zhitnikroman