Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Indicator interacting with strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Indicator interacting with strategy

    Hi,
    I created a sample strategy and a sample indicator.
    I would like to know what's the correct way so that I can share data from the strategy to the indicator
    Can someone please point me in the right direction.
    Thanks

    Code:
    using System;
    
    namespace NinjaTrader.NinjaScript.Strategies {
    public class MyCustomStrategy : Strategy {
    
    public DateTime myDate;
    
    
    protected override void OnStateChange() {
    if (State == State.SetDefaults) {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "MyCustomStrategy";
    Calculate = Calculate.OnEachTick;
    
    } else if (State == State.Configure) {
    
    }
    }
    
    public void EnableThis() {
    this.IsEnabled = true;
    }
    
    public DateTime getMyTime() {
    return myDate;
    }
    
    protected override void OnBarUpdate() {
    myDate = DateTime.UtcNow;
    }
    }
    }
    Code:
    using NinjaTrader.NinjaScript.Strategies;
    
    namespace NinjaTrader.NinjaScript.Indicators {
    
    public class MyCustomIndicator : Indicator {
    
    private MyCustomStrategy testStrategy;
    
    protected override void OnStateChange() {
    if (State == State.SetDefaults) {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "MyCustomIndicator";
    Calculate = Calculate.OnBarClose;
    
    } else if (State == State.Configure) {
    testStrategy = new MyCustomStrategy();
    testStrategy.EnableThis();
    }
    }
    
    protected override void OnBarUpdate() {
    testStrategy.Update();
    
    Print("Date: " + testStrategy.getMyTime().ToString());
    }
    }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private MyCustomIndicator[] cacheMyCustomIndicator;
    public MyCustomIndicator MyCustomIndicator()
    {
    return MyCustomIndicator(Input);
    }
    
    public MyCustomIndicator MyCustomIndicator(ISeries<double> input)
    {
    if (cacheMyCustomIndicator != null)
    for (int idx = 0; idx < cacheMyCustomIndicator.Length; idx++)
    if (cacheMyCustomIndicator[idx] != null && cacheMyCustomIndicator[idx].EqualsInput(input))
    return cacheMyCustomIndicator[idx];
    return CacheIndicator<MyCustomIndicator>(new MyCustomIndicator(), input, ref cacheMyCustomIndicator);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.MyCustomIndicator MyCustomIndicator()
    {
    return indicator.MyCustomIndicator(Input);
    }
    
    public Indicators.MyCustomIndicator MyCustomIndicator(ISeries<double> input )
    {
    return indicator.MyCustomIndicator(input);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.MyCustomIndicator MyCustomIndicator()
    {
    return indicator.MyCustomIndicator(Input);
    }
    
    public Indicators.MyCustomIndicator MyCustomIndicator(ISeries<double> input )
    {
    return indicator.MyCustomIndicator(input);
    }
    }
    }
    
    #endregion

    #2
    Hello PWillard,

    Public properties in an indicator can be called from a host strategy (or host indicator).

    For this to work the Strategy Builder or with the MarketAnalyzer, the values would need to be set to public plots.


    If this is not necessary, any object type can be used, but Update() should be called first.


    On DateTime objects, (or any object types other than int, double, bool, string, enum unless it a custom class that has an explicit internal ToString()), need to use the XmlIgnore() attribute.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      is adding the strategy from "Strategies" a requirement for this to work or it can work by adding the strategy from indicator as "new MyCustomStrategy()"

      Comment


        #4
        Hello PWillard,

        The host script such as a strategy (or another indicator) must call the indicator. The new keyword cannot be used.

        You cannot call a strategy from another script the way an indicator can be called from another script.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CortexZenUSA, Today, 12:53 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by CortexZenUSA, Today, 12:46 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by usazencortex, Today, 12:43 AM
        0 responses
        5 views
        0 likes
        Last Post usazencortex  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        168 responses
        2,266 views
        0 likes
        Last Post sidlercom80  
        Started by Barry Milan, Yesterday, 10:35 PM
        3 responses
        13 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Working...
        X