Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Communication between two strategies
Collapse
X
-
Excellent, Bormir!
Expanding on your idea, I've attached my code that can Set and Get 5 different global values of type double.
Can these two methods (Set and Get) be overloaded to handle different types of data? (integer, string, boolean, etc)
Perhaps someone with more experience in C# can look into this.
BenAttached Files
Comment
-
For more information on what's happening here, you can read this:
and keep in mind, like I said, that all of your indicators derive from that class, e.g.:
Code:/// <summary> /// /// </summary> [Description("")] public class HJSStdDev : Indicator {
Comment
-
Ben, you are doing waaaaay too much work, this would create a dictionary you could access by a string-based key:
You are both missing something important in your files though. Yes, you can access the CODE from any indicator. If you want to access the EXACT SAME VALUES from any indicator, the variable needs to be static. Neither one is right or wrong, you just need to understand what you are trying to do. For my example code above, if you don't want the actual values to be shared across indicators, you would remove the static in front of Dictionary, and each indicator would have its own dictionary to use.Code:private void SetGlobalInt(string key, int val) { mDict.Add(key, val); } private int GetGlobalInt(string key) { return (int)mDict[key]; } private void SetGlobalDouble(string key, double val) { mDict.Add(key, val); } private double GetGlobalDouble(string key) { return (double)mDict[key]; } private void SetGlobalString(string key, string val) { mDict.Add(key, val); } private string GetGlobalString(string key) { return (string)mDict[key]; } private void SetGlobalBool(string key, bool val) { mDict.Add(key, val); } private bool GetGlobalBool(string key) { return (bool)mDict[key]; } private static Dictionary<string, object> mDict = new Dictionary<string, object>();
Comment
-
Guys,
Great you're exchanging ideas and try to resolve programming issues by the community. Highly appreciated.
However, please keep in mind that we at NT support draw a line on supporting NinjaScript for good reason: if you don't exactly know what you're doing you really can mess up complete NT.
In this case using static variables/collections/dictionaries is a valid idea, but you need to make sure you clean up resources properly (e.g. by overloading Dispose method), since any memory managed/held by a static will not get freed until you terminate (!) NT.
Just my 2 cents...
Comment
-
Dierk,Originally posted by NinjaTrader_Dierk View PostGuys,
Great you're exchanging ideas and try to resolve programming issues by the community. Highly appreciated.
However, please keep in mind that we at NT support draw a line on supporting NinjaScript for good reason: if you don't exactly know what you're doing you really can mess up complete NT.
In this case using static variables/collections/dictionaries is a valid idea, but you need to make sure you clean up resources properly (e.g. by overloading Dispose method), since any memory managed/held by a static will not get freed until you terminate (!) NT.
Just my 2 cents...
Probably a nice simple thread in the Reference Sample do the trick, Ray told us probably will be one soon.
Regards
Comment
-
Hi
I'm trying to develop a strategy based on the PNL of another strategy (among the other things).
I created my public static in Strategy1 and then in Strategy2 I called my public static from strategy 1.
Problem is, if I run Strategy1 on stock X, then I can use its PNL for strategy2 on stock X, but then if I run strategy2 on stock Y it will still take the PNL data of Strategy1 for stock X!
Is there a way to create a non static (I guess?!) variable, so that if I run Strategy2 on stock X, it calculates the PNL of Strategy1 for stock X, and if I run Strategy2 on stock Y, it calculates the PNL of Strategy1 for stockY and so on?
Many thanks to anyone who can shed a bit of light on this issue!
Originally posted by Agamenon View Postit´s very easy ct! you create in region vars in [Estrategy1] a var with "public static ....." example:
#region Variables
public static bool MessengerInterStrategicVar = false ;
#endregion
Now, you call, write or change your var in [Strategy2].
Example:
(Put the name of class or strategy1 after var name)
Strategy1.MessengerInterStrategicVar = true ;
test with debug print!
Print (" Var from remote strategy status:" + Strategy1.MessengerInterStrategicVar.ToString());
woalaa! It can work! I think soo!
remember to load both strategys in your chart!
Comment
-
I have been following this post with great interest! I need to use the PNL of a strategy in another strategy. I applied Agamenon's idea but then I realized (using print) that using the global variable I just get the final (last day) PNL, while I need the DataSerie (i.e., the PNL day by day).Originally posted by ct View PostAgamenon
It worked perfect. You are a sheer genius!
So, I'm trying to use "public DataSeries" and apparently I can access it, but when I run the second strategy (the one which uses the first strategy's PNL), I get the following error in the log: "Error on calling OnBarUpdate method: Strategy1: ADX[BarsAgo]; barsAgo out of valid range 0 through -2, was 0".
I guess the DataSeries size doesn't match... what do you guys think? How could I fix it? If I manage to make it work, I guess it would be helpful for many other members to be able to access DataSeries from different strategies.
Thank you everyone!
Stefy
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
633 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
364 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
105 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
567 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
568 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment