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

How does CalculateOnBarClose work?

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

    How does CalculateOnBarClose work?

    I have a constant volume (1000 contracts) bars chart. I would like to execute an order when the close of the current bar is higher than the close of the previous bar. And I would close the order after 10 bars.

    I notice the code is excute more frequent even I set CalculateOnBarClose=true. Is anyone know what I did wrong? Below is the test strategy.

    #region Variables
    // Wizard generated variables
    private int ticksBeforeFiringOrder = 10; // Default setting for TicksBeforeFiringOrder
    // User defined variables (add any user defined variables below)
    private int nbar=0;
    #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 OnBarUpdate()
    {
    Print ("OnBarUpdate: CurrentBar"+CurrentBar.ToString()+ "; nbar="+nbar.ToString()+"; Close[0]="+Close[0].ToString()+ "; Close[1]="+Close[1].ToString());

    if (Close[0] > Close[1])
    {
    EnterLong (10, "TLong");
    }
    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (nbar > 10) {
    nbar=0;
    ExitLong();
    }
    else
    nbar++;
    }

    }

    #2
    xewoox,

    You will need to debug your code. Please try using TraceOrders = true. http://www.ninjatrader-support2.com/...ead.php?t=3627
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      xewoox,

      You will need to debug your code. Please try using TraceOrders = true. http://www.ninjatrader-support2.com/...ead.php?t=3627
      But there is not problem with the code. I just not sure what should I be expected if CalculateOnBarClose is set to true, then should I be expect OnBarUpdate() be called after each bar close. But what I see is not quite what I expected. Therefore I attach the code. And the strategy is executing on a constant volume bar charts (1000 contracts). I am not asking for debugging code. But rather to ask how does the setting of CalculateOnBarClose affect the OnBarUpdate() being called.

      Comment


        #4
        If you have CalculateOnBarClose = true you will run your strategy logic once at the end of the bar. If you have it to false it will calculate once for each incoming tick. During backtesting you will always be running once at the end of each bar because you do not know the intrabar information.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          If you have CalculateOnBarClose = true you will run your strategy logic once at the end of the bar. If you have it to false it will calculate once for each incoming tick. During backtesting you will always be running once at the end of each bar because you do not know the intrabar information.
          Does it means that if I initialize CalculateOnBarClose to true? I should be expect the OnBarUpdate(0 be called when the bar is closed. I want to understand is that if I am using volume bar, it does not seems to behave when you use constant volume bar. I run the code up there with 1 min bar, it behaves as you said. I am not sure by design or something not being document for constant volume bar.

          Comment


            #6
            xewoox,

            I am not understanding what you mean. Please post your Output Window prints and what you think is wrong along with a screenshot of your chart.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Josh View Post
              If you have CalculateOnBarClose = true you will run your strategy logic once at the end of the bar. If you have it to false it will calculate once for each incoming tick. During backtesting you will always be running once at the end of each bar because you do not know the intrabar information.
              Does it means that if I initialize CalculateOnBarClose to true? I should be expect the OnBarUpdate(0 be called when the bar is closed. I want to understand is that if I am using volume bar, it does not seems to behave when you use constant volume bar. I run the code up there with 1 min bar, it behaves as you said. I am not sure by design or something not being document for constant volume bar.

              Comment


                #8
                Please see my previous post.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  It seems to be ok right now. I still don't understand what happen. I wish I have keep the log. Because just like it behave not I expect. Thanks for your patient

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Max238, Today, 01:28 AM
                  4 responses
                  36 views
                  0 likes
                  Last Post Max238
                  by Max238
                   
                  Started by r68cervera, Today, 05:29 AM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by geddyisodin, Today, 05:20 AM
                  1 response
                  11 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by timko, Today, 06:45 AM
                  2 responses
                  14 views
                  0 likes
                  Last Post NinjaTrader_ChristopherJ  
                  Started by habeebft, Today, 07:27 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post habeebft  
                  Working...
                  X