Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting the Indicator previos value

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

    Getting the Indicator previos value

    Hello,

    I'm testing an strategy, but when I print the SMA value I get the SMA value of the previous day. Only one order per day

    protected override void Initialize()
    {
    // Add a 1 Day Bars object to the strategy
    Add(PeriodType.Day, 1);

    // Note: Bars are added to the BarsArray and can be accessed via an index value
    // E.G. BarsArray[1] ---> Accesses the 5 minute Bars object added above

    // Add simple moving averages to the chart for display
    // This only displays the SMA's for the primary Bars object on the chart
    Add(SMA(10));

    SetTrailStop(CalculationMode.Ticks, 20);
    CalculateOnBarClose = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy
    // We only want to process events on our primary Bars object (index = 0) which is set when adding
    // the strategy to a chart
    if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
    return;

    if (ToTime(Time[0]) >= ToTime(21, 15, 0) && Position.MarketPosition != MarketPosition.Flat)
    {
    if (Position.MarketPosition == MarketPosition.Long)
    ExitLong();
    if (Position.MarketPosition == MarketPosition.Short)
    ExitShort();

    }

    if (Position.MarketPosition == MarketPosition.Flat && ToTime(Time[0]) >= ToTime(15, 31, 0)
    && (Time[0].Month != time.Month || Time[0].Day != time.Day))
    {
    if (SMA(BarsArray[1], 10)[0] < Input[0])
    {
    EnterLong(1, "Long");
    Print("Time: " + Time[0].ToString("yyyy-MM-dd HH:mm:ss") + " SMA: " + SMA(BarsArray[1], 10)[0].ToString() + " Input: " + Input[0].ToString());
    }
    else if (SMA(BarsArray[1], 10)[0] > Input[0])
    {
    EnterShort(1, "Short");
    Print("Time: " + Time[0].ToString("yyyy-MM-dd HH:mm:ss")+ " SMA: " + SMA(BarsArray[1], 10)[0].ToString() + " Input: " + Input[0].ToString());
    }
    time = Time[0];
    }

    }

    Best Regards
    Attached Files

    #2
    Hello David,

    Thank you for writing in. Please remember that the additional data series you load only loads the same number of days as the primary data series, not the same number of bars. Please check your data series settings on the chart you apply the strategy to and check the "Days to load" property.

    As an example, if I have 5 days to load set, my SMA(BarsArray[1], 10) is not going to be valid because we don't have 10 day bars yet (you would have to leave the program running for another 5 days before your numbers would start lining up in theory).

    Please make sure your "Days to load" value is set to 10 or greater.

    Also be sure when you compare the SMA value printed from your strategy, to compare it against a 1 day chart (with the same number of days loaded) with calculate on bar close set to false, and the SMA with a period of 10 applied to it.

    Then your numbers should line up.

    Please let me know if this does not resolve the issue or if I may be of further assistance.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      Hello,

      I set "Days to load" = 30

      This line:

      Print("Time: " + Time[0].ToString("yyyy-MM-dd HH:mm:ss") + " SMA: " + SMA(BarsArray[1], 10)[0].ToString() + " Input: " + Input[0].ToString());

      Print the Input[0] of the current day en tick value, but this SMA(BarsArray[1], 10)[0] Print the SMA(10) of the previous day

      Best Regards

      Comment


        #4
        Hello davidgamo,

        This is a note that I am currently investigating this issue in greater detail and will follow up with you tomorrow morning.

        Thank you for your patience in the meantime.
        Michael M.NinjaTrader Quality Assurance

        Comment


          #5
          Hello davidgamo,

          Thank you for your patience. Using the same logic I compared two strategies.
          The first was applied to a 1 day ES 09-15 chart and did the following:
          Code:
          Print("Actual SMA Value (1 day): " + SMA(10)[0]);
          The second was applied to a 30 minute ES 09-15 chart, had an added 1 day data series and did the following:
          Code:
          Print("Incorrect SMA Value (1 day):" + SMA(BarsArray[1], 10)[0]);
          Both values were the exact same every single time. There is no way that you are getting the SMA value from yesterday and the Input[0] from today using the code you provided unless something is wrong with your historical data. I recommend adjusting the way you are performing your test.

          If you would like be sure, please navigate to Tools -> Historical Data Manager -> Edit -> Right click on every instrument and select "Delete" then retest.

          Please let me know if I may be of further assistance anytime.
          Michael M.NinjaTrader Quality Assurance

          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
          553 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