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

Setting an Indicator Variable from a Strategy

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

    Setting an Indicator Variable from a Strategy

    Hi,

    I was just wondering why this code doesn't work - I have the strategy and the indicator added to a chart. From the Print() statement I can tell the variable is getting set in the indicator, but the plot is just plotting 1's.

    Code:
    public class MyCustomStrategy : Strategy {
            #region Variables
                int myValue = 0;
            #endregion
    
            protected override void Initialize(){
                CalculateOnBarClose = true;
            }
    
            protected override void OnBarUpdate(){
                myValue++;
                MyCustomIndicator().TestValue = myValue;
                if(myValue == 10)
                    myValue = 0;    
            }
        }
    }
    Code:
    public class MyCustomIndicator : Indicator
        {
            #region Variables
                private int testValue = 0;
            #endregion
    
            protected override void Initialize() {
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                CalculateOnBarClose    = true;
            }
    
            protected override void OnBarUpdate() {
                  Print(testValue);    
                Values[0].Set(testValue);
            }
    
            #region Properties
            public int TestValue {
                get { return testValue; }
                set { testValue = Math.Max(1, value); }
            }

    #2
    Hi spinalwiz,

    Please take a look at your indicator region properties and use this snippet:

    Code:
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] TestValue[/SIZE]
    [SIZE=2]{[/SIZE]
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] { [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] testValue; }[/SIZE]
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]set[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] { testValue = Math.Max([/SIZE][/SIZE][SIZE=2][SIZE=2][SIZE=2][COLOR=red]0[/COLOR][/SIZE][/SIZE][/SIZE][SIZE=2][SIZE=2], value); }[/SIZE]
    [SIZE=2]}[/SIZE]
    [/SIZE]
    Tested this briefly on my end and it correctly plots a '0' on the chart.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand,

      Good spot, although I dont think I explained myself very well. Ignoring the rest of the code, this is the part I can't get to work:

      In the Strategy:
      Code:
      MyCustomIndicator().TestValue = 5;
      And in my Indicator:

      Code:
      Values[0].Set(testValue);
      But it just plots 0s for some reason. The strange part is that if I add to the indicator:

      Code:
      Print(testValue);
      Then in the output window it prints:

      0
      5
      0
      5
      0
      5
      0
      5

      This is the code I have in Properties:
      Code:
              public int TestValue {
                  get { return testValue; }
                  set { testValue = Math.Max(0, value); }
              }

      Comment


        #4
        Hi spinalwiz,

        Thanks, please take a look then at the following reference sample - http://www.ninjatrader-support2.com/...ead.php?t=4991

        You want to use a DataSeries class to have up-to-date values...

        Have a great day!
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Hi,

          Thank you for your reply. I am now using a DataSeries although it unfortunately still does not work. The example you gave was for getting an indicator variable from a strategy. I need to set an indicator variable from a strategy.

          The code I have in my strategy is:
          Code:
          MyCustomIndicator().MyData.Set(5);    
          MyCustomIndicator().Update();
          And in my Indicator I have:
          Code:
          Values[0].Set(myData[0]);
          My Properties section looks like this:
          Code:
          public DataSeries MyData
                  {
                      set { myData = value; }
                      get { return myData; }
                  }

          Comment


            #6
            Hi spinalwiz, unfortunately this is not supported.
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by PaulMohn, Today, 12:36 PM
            2 responses
            16 views
            0 likes
            Last Post PaulMohn  
            Started by Conceptzx, 10-11-2022, 06:38 AM
            2 responses
            53 views
            0 likes
            Last Post PhillT
            by PhillT
             
            Started by Kaledus, Today, 01:29 PM
            0 responses
            3 views
            0 likes
            Last Post Kaledus
            by Kaledus
             
            Started by yertle, Yesterday, 08:38 AM
            8 responses
            37 views
            0 likes
            Last Post ryjoga
            by ryjoga
             
            Started by rdtdale, Today, 01:02 PM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Working...
            X