Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Simplest way to share source code between indicators and/or strategies?
Collapse
X
-
Simplest way to share source code between indicators and/or strategies?
I have a piece of trade entry/exit logic code that is to be identical between my strategy and an indicator that shows the strategy's current and historical signals. Normally, I perfect the code in the indicator and then cut and and paste it into the strategy. This works well enough, but is a little tedious. Is there a simple way for two indicators to share source code? C# normally allows for include files, but I can't figure out how to do that with Ninjascript. Thanks!Tags: None
-
Hello gbourque,
An easy way to do that would be to use an indicator. You can put a calculation inside an indicator and then call that indicator from the strategy to return a value. You could also make a partial class for Strategy where you put custom methods. I would likely suggest using an indicator just for simplicity.
Can you provide some details on what your code is doing and what value its returning to the strategy? That would help to point a direction on how to go about implementing that in the indicator.
-
Well, the code in question is just code in OnBarUpdate(). It is in OnBarUpdate() in my indicator and strategy. It references and sets many variables in the strategy and indicator classes which they have in common. It wouldn't work to return just one value.
Comment
-
Hello gbourque,
From the description that still sounds like an indicator would be best. If you need to run OnBarUpdate to reach the conclusion you should keep that in an indicator and then expose whatever data the strategy needs as public plots or properties.
Any ongoing value should be exposed as a Plot, the strategy could get the current value by calling the indicator and accessing that plot. Any other variable that needs exposed can be exposed by making a public properties and using the update method in the property. That is shown in this sample: https://ninjatrader.com/support/help...alues_that.htm
By using an indicator you can make use of the platforms caching and also any other indicator or strategy can call the indicator to access those values.
Comment
-
I did redesign my indicator and strategy in the manner you suggested. It works great and should be much easier to maintain going forward. Thanks!
What I learned:- Expose any objects (variables) in the indicator class which the strategy needs by declaring them public. In my case this was just two bools, TakeLong and TakeShort.
- Sync the inputs on the strategy and indicator as much as possible and appropriate.
- Add an indicator object variable in the strategy class.
- Load the indicator (in State.DataLoaded) by calling its constructor, which will return an indicator object which you assign to the variable. The constructor will take as parameters the properties of your indicator which are [NinjaScriptProperty] and [Browsable(true)]. Look in the generated code in your indicator for namespace NinjaTrader.NinjaScript.Strategies. You will see an example of the call to the constructor.
- In OnBarUpdate() in the strategy, call Update() on the indicator before anything else, this will make sure OnBarUpdate() code is executed in the indicator first. E.g: assuming your indicator object is named MyIndicator, call MyIndicator.Update().
- Bonus: Create a bool property in the strategy which when unchecked causes no real trades to be made. This will allow you to keep the indicator visible, while effectively submitting no trades. Otherwise, the indicator goes away when you disable the strategy at the Ninjascript level. I like to keep the indicator up because it shows me the trades that would have made a lot of money if I hadn't chickened out and turned my strategy off. If you do this, be sure to plot something on the chart which tells you whether trading is live or not.
Last edited by gbourque; 08-09-2023, 09:46 PM.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
54 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
130 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
72 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
44 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
49 views
0 likes
|
Last Post
|

Comment