PDA

View Full Version : Communication between two strategies


ct
11-25-2007, 11:54 PM
What is the easiest/most efficient way to share a value between two strategies on the SAME chart? Can I share a global value? I saw the post about using this on different charts but wasn't sure if it was true for the same chart as well.

I wasn't sure what was supported under the Ninja Trader implementation and thought I would ask before I beat my head against a rock.

Thanks/danke

Cliff

NinjaTrader_Dierk
11-26-2007, 12:09 AM
Unfortunately this is beyond of what we support. However, you might see value in trying the concept of "static" in C#.

ct
11-26-2007, 12:13 AM
Dierk

Guten morgan. Thanks for the fast reply.

ok, I will give it a try. I wasn't sure if "not supported" was "doesn't work" or is just unsupported. Sounds like it may be ok.

Thanks/danke

Cliff

ct
11-26-2007, 01:35 AM
Dierk

Here is what I received from my technical expert.

"There is no way to add global variable,
instead a very simple custom indicator with one property can be developed, which can be set and retrieved from any other indicator or strategy."

Does it look like this will work to you?

NinjaTrader_Dierk
11-26-2007, 01:38 AM
Statement is incorrect: Since NinjaScript is C# you can add any global/static variable you like by custom coding.

But again: this is beyond of what we support. Please contact a NinjaScript consultant if you need further support on that matter. http://www.ninjatrader.com/webnew/partners_onlinetrading_NinjaScript.htm

ct
11-26-2007, 01:41 AM
Dierk

OK, thanks for the help.

Cliff

Agamenon
11-26-2007, 01:44 PM
itī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! :o
remember to load both strategys in your chart! :D

ct
11-26-2007, 01:45 PM
Agamenon

Kewl. I will give that a try. Many thanks!!!

ct
11-27-2007, 11:33 PM
Agamenon

It worked perfect. You are a sheer genius!

sbgtrading
12-17-2007, 02:44 PM
This apparently only works with strategies?

I tried the technique on two indicators...the "remote" indicator does not compile since it contains a reference to a variable that's defined in the "primary" indicator.

Any way around this? Do I need to define it as a "public static" variable in the "remote" indicator?

Pete S
12-17-2007, 08:24 PM
I have not tried this myself, but you could try defining it in UserDefinedMethods, which is a partial base class all of your indicators use.

Dierk -- if you're still following this discussion, is OnBarUpdate always called from the same thread or would this code need to be made threadsafe? I've wondered about that for my own uses. From watching the output it looks like the strategies are called one after the other.

NinjaTrader_Dierk
12-17-2007, 10:30 PM
I did no follow this thread in detail but all strategy event methods are executed in the same thread.

Bormir
12-20-2007, 04:24 PM
PeteS,
I tried this out , and it seemed to work. Here is a code snippet:
partial class Indicator
{
double wrkVal = 0;
public double testMethod()
{
wrkVal = wrkVal + 1.0;
return wrkVal;
}
}


With this I can finally access wrkVar from other indicators.

bormir

zoltran
12-20-2007, 07:45 PM
Bormir, where did you place this?

In userdefinedmethods?

Bormir
12-21-2007, 05:28 AM
zoltran,
I made a copy of UserDefinedMethods and named it TestUserMethods. I have attached the whole test file for you to look at. It is interesting. It looks like you can access wrkVar directly from any indicator

zoltran
12-21-2007, 06:25 AM
Interesting, ..thanks !

sbgtrading
12-21-2007, 06:28 AM
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.

Ben

Pete S
12-21-2007, 06:32 AM
For more information on what's happening here, you can read this:
http://msdn2.microsoft.com/en-us/library/wa80x488(VS.80).aspx

and keep in mind, like I said, that all of your indicators derive from that class, e.g.:

/// <summary>
///
/// </summary>
[Description("")]
public class HJSStdDev : Indicator
{

Pete S
12-21-2007, 06:53 AM
Ben, you are doing waaaaay too much work, this would create a dictionary you could access by a string-based key:

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>();
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.

NinjaTrader_Dierk
12-21-2007, 07:00 AM
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...

PrTester
12-21-2007, 07:08 AM
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...

Dierk,

Probably a nice simple thread in the Reference Sample do the trick, Ray told us probably will be one soon.

Regards

NinjaTrader_Dierk
12-21-2007, 07:10 AM
The problem is that this will not be an "simple thread" since covering memory management issues always is a challenge. That's why we held of for now.

PrTester
12-21-2007, 08:04 AM
The problem is that this will not be an "simple thread" since covering memory management issues always is a challenge. That's why we held of for now.


Got it

Regards,

stefy
05-15-2009, 07:19 AM
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! :)



itī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! :o
remember to load both strategys in your chart! :D

zweistein
05-16-2009, 10:11 PM
you could use a list of pairs.
each pair is a instrument,pnl combination.
look at MSDN for a pair defintion (MultiListSpace? if I remember well)

The list becomes your global static.

Becomes more complex if you have multiple accounts

stefy
08-19-2009, 12:09 PM
Agamenon

It worked perfect. You are a sheer genius!

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).
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