Announcement

Collapse
No announcement yet.

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.
    https://ninjatrader.com/support/foru...238#post812238

    If this is not necessary, any object type can be used, but Update() should be called first.
    https://ninjatrader.com/support/help...nt8/update.htm

    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.
    https://ninjatrader.com/support/help...eattribute.htm
    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 yodaler, Today, 06:12 PM
        0 responses
        2 views
        0 likes
        Last Post yodaler
        by yodaler
         
        Started by op1031, Today, 05:16 PM
        0 responses
        1 view
        0 likes
        Last Post op1031
        by op1031
         
        Started by fscabrera03, Today, 01:18 PM
        1 response
        12 views
        0 likes
        Last Post fscabrera03  
        Started by BottomShark77, Today, 12:20 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_ShawnB  
        Started by bowling, Today, 06:16 AM
        3 responses
        30 views
        0 likes
        Last Post NinjaTrader_ShawnB  
        Working...
        X