Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close Price is not real

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

    Close Price is not real

    Hello,

    I write this very simple program, to get Close[0] and Close[1] Prices at the close of each bar, but the Close prices does not correspond to the real prices at the start:

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Prueba Con Cierres
    /// </summary>
    [Description("Prueba Con Cierres")]
    public class News8PruebaClose : Strategy
    {
    #region Variables
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    CalculateOnBarClose = true;

    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    // protected override void OnStartUp()
    protected override void OnBarUpdate()

    {


    Print("Close[0] = " + Close[0] + " Date Time = " + DateTime.Now);
    Print("Close[1] = " + Close[1] + " Date Time = " + DateTime.Now);


    }



    #region Properties



    #endregion
    }
    }


    When I put this on the CL Chart, the result on the output window is:

    **NT** Enabling NinjaScript strategy 'News8PruebaClose/e353c398539b4edb9be0714bff109320' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=StopStrategy DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=True CancelExitOrdersOnDisable=True CalculateOnBarClose=True MaxRestarts=4 in 5 minutes
    Close[0] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.04 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.04 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.03 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.03 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.06 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.08 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.06 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.07 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.08 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.06 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.07 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.06 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.04 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.04 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.04 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.04 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.04 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.05 Date Time = 12/12/2016 9:14:54 PM
    Close[0] = 51.02 Date Time = 12/12/2016 9:14:54 PM
    Close[1] = 51.04 Date Time = 12/12/2016 9:14:54 PM

    It obviously is not correct data, and it is not got at the close of each bar (see the same time in all samples), so I can not understand what is going wrong here.

    Please help,

    Thanks

    #2
    On the close of the very first bar, there is no previous bar.

    Thus, on the close of the very first bar, accessing the value at Close[1] (aka, the previous bar's close) does not make sense.

    You should use CurrentBar to check that enough bars exist (at least 2, in your case) so that you can avoid premature processing inside your OnBarUpdate().

    Comment


      #3
      Thank you for your response bltdavid.

      As I understand, when we use CalculateOnBarClose = true, we should expect that the script start to work after the Close of the very first bar, but in this case, this is starting with the start of the script, before the close of the bar, it do not wait for the close of the bar, which should be an error in my opinion.

      BR,
      Federico

      Comment


        #4
        Originally posted by federicoo View Post
        Thank you for your response bltdavid.

        As I understand, when we use CalculateOnBarClose = true, we should expect that the script start to work after the Close of the very first bar, but in this case, this is starting with the start of the script, before the close of the bar, it do not wait for the close of the bar, which should be an error in my opinion.

        BR,
        Federico
        Hopefully this helps your opinion and helps you believe there is no error.

        From your first post, I had no idea what you were talking about. Did you have a 1 tick chart? I didn't see which timeframe for the chart.

        Your follow up post leads me to believe you want no "historical processing" (NT term), please add this to your script.

        And if you printed Time[0] and Time[1], not DateTime.Now, you would get the Time of those bars.

        Code:
        protected override void OnBarUpdate()
        
        {
        
        [B]if (Historical) return;[/B]
        
        Print("Close[0] = " + Close[0] + " Date Time = " + DateTime.Now);
        Print("Close[1] = " + Close[1] + " Date Time = " + DateTime.Now);
        
        
        }

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        574 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        333 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