Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Referencing past values of a definition

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

    Referencing past values of a definition

    Hello,

    I am new to C# and I am working to convert my code from my previous coding language into Ninjatrader.
    What I could do in my original code was I could reference previous values of definitions.
    For instance, I could make the strategy calculate a current value based upon a previous value of that value.

    Here is my code as of now:
    Note that I am trying to use [1] to reference the previous value of that definition.
    However, the code is not compiling and I cannot reference these values.

    Any help is appreciated, thank you!

    Code:
    if (myCCI > 0)
    {
    MT1 = (Math.Max(MT1[1], Median[0] - ATRCCI));
    }
    else
    {
    MT1 = (Math.Min(MT1[1], Median[0] + ATRCCI));
    }
    
    // Alignment of two different indicators
    if ((Close[0]> Stade) & (Close[0] > MT1))
    {
    double State2 = (StateUp);
    }
    else if ((Close[0] < Stade) & (Close[0] < MT1))
    {
    double State2 = StateDn;
    }
    else
    double State2 = State2[1];
    
    if (Math.Max(State2 != State2[1]))
    Math.Max(newState = bar);
    else
    Math.Max(newState = 0);

    #2
    Hello ArcticBiscuit,

    Thanks for your post.

    I see that you are trying to access the previous value held by a double variable.

    Double variables are only able to hold a single value at a time and any previous values will be overwritten when a new value is assigned to the double variable. This means that you would only be able to access the current value that is assigned to the double variable.

    If you would like to access previous values held by the double variable, you would need to change it to be a Series<double> variable. Then, you could access the current value assigned to the Series<double> or previous values that were held by that variable.

    For example, if you have a Series<double> variable called mySeriesDouble, you could access the current value by calling mySeriesDouble[0]. To access the previously held value, you would call mySeriesDouble[1].

    See this help guide page for more information about Series<double> variables and sample code: https://ninjatrader.com/support/help...t8/seriest.htm

    Let me know if I may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Originally posted by ArcticBiscuit View Post
      Hello,

      I am new to C# and I am working to convert my code from my previous coding language into Ninjatrader.
      What I could do in my original code was I could reference previous values of definitions.
      For instance, I could make the strategy calculate a current value based upon a previous value of that value.

      Here is my code as of now:
      Note that I am trying to use [1] to reference the previous value of that definition.
      However, the code is not compiling and I cannot reference these values.

      Any help is appreciated, thank you!

      Code:
      if (myCCI > 0)
      {
      MT1[B][COLOR=#e74c3c][0][/COLOR][/B] = (Math.Max(MT1[1], Median[0] - ATRCCI));
      }
      else
      {
      MT1[B][COLOR=#e74c3c][0][/COLOR][/B] = (Math.Min(MT1[1], Median[0] + ATRCCI));
      }
      
      // Alignment of two different indicators
      if ((Close[0]> Stade) & (Close[0] > MT1[COLOR=#e74c3c][B][0][/B][/COLOR]))
      {
      double State2 = (StateUp);
      }
      else if ((Close[0] < Stade) & (Close[0] < MT1[COLOR=#e74c3c][B][0][/B][/COLOR]))
      {
      double State2 = StateDn;
      }
      else
      double State2 = State2[1];
      
      if (Math.Max(State2 != State2[1]))
      Math.Max(newState = bar);
      else
      Math.Max(newState = 0);
      Make sure that MT1 is a Series<double> and you're all set.

      Looks like you were missing using the 'MT1[0]' slot position.
      You need the indexing '[0]' suffix to do that, see above.

      Last edited by bltdavid; 07-26-2022, 09:54 AM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      578 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      334 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      554 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      551 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X