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

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 RookieTrader, Today, 07:41 AM
        1 response
        4 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by kujista, Today, 05:44 AM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by elderan, Yesterday, 08:03 PM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by samish18, Yesterday, 08:57 AM
        8 responses
        25 views
        0 likes
        Last Post samish18  
        Started by DJ888, 04-16-2024, 06:09 PM
        3 responses
        10 views
        0 likes
        Last Post NinjaTrader_Erick  
        Working...
        X