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 to trigger OnBarUpdate() to be called in Strategy()?

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

    How to trigger OnBarUpdate() to be called in Strategy()?

    I had no problem to let OnBarUpdate() be called in Indicator(), but it looks like it is not called in Strategy(). Why?
    I didn't use my own Indicator() in my Strategy(), I added a print() at the beginning of the OnBarUpdate() in order to verify OnBarUpdate() is called when each bar is updated. Is there anything I have missed? Thanks.

    #2
    Hi localappleseed, thanks for your post.

    You can call the Update() method in any other method in your script to force the OnBarUpdate method to be called. OnBarUpdate should automatically be called for every bar on the chart once you enable the strategy (only if Calculate = OnBarClose). Can you give a test script that is not doing this?

    I look forward to hearing from you.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi, Chris, thanks for quick reply.
      Here is the short script to verify OnBarUpdate() to be called.

      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class SampleLuke : Strategy
      {

      private string atmStrategyId = string.Empty;
      private string orderId = string.Empty;
      private bool isAtmStrategyCreated = false;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = NinjaTrader.Custom.Resource.NinjaScriptStrategyDes criptionSampleATMStrategy;
      Name = NinjaTrader.Custom.Resource.NinjaScriptStrategyNam eSampleATMStrategy;
      // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = false;
      }
      }

      protected override void OnBarUpdate()
      {

      // Compares the primary bar's low price to the 5-minute bar's low price
      Print("11111");
      }

      protected override void OnMarketDepth(MarketDepthEventArgs marketDepthUpdate)
      {
      // Print some data to the Output window
      Print("The m22222");
      }
      }
      }

      Comment


        #4
        Here is how I use Strategy().
        From a active Chart, right click to select Strategies -> In Strategies Window, select Calculate = On Bar Close -> Nothing printed from Output Window.

        Comment


          #5
          Hi, Chris,
          I tried two methods you suggested, so far no luck.

          1) I added Update() in this method:
          protected override void OnMarketDepth(MarketDepthEventArgs marketDepthUpdate)
          {
          Print("The m22222");
          Update();
          }

          2) I tried to set Calculate = OnBarClose, (this is done only in GUI setup), also I tried to initialize it using code "CalculateOnBarClose=false",
          None of them are working. CalculateOnBarClose is not defined in NT8. I saw it was used in NT7.

          Thanks.

          Comment


            #6
            Hello localappleseed, thanks for your reply.

            Make sure you are connected to a data provider, and that you can load up a chart of the selected instrument. Can you run this script and let me know if you get output?

            Attached Files
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              I am sure the data feed is working, I tried to use Indicator(0 which works as expected. It has output printed.

              Comment


                #8
                I compiled and applied your Test Strategy, still not working. So it looks like I didn't configure correctly.

                Comment


                  #9
                  Hi localappleseed, thanks for your reply.

                  Did you click Enable in the Strategy? That would be the only thing that would keep the strategy from running.
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    You got it, I never enabled it. It worked now. You guys are just super!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by andrewtrades, Today, 04:57 PM
                    1 response
                    5 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by chbruno, Today, 04:10 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post chbruno
                    by chbruno
                     
                    Started by josh18955, 03-25-2023, 11:16 AM
                    6 responses
                    436 views
                    0 likes
                    Last Post Delerium  
                    Started by FAQtrader, Today, 03:35 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post FAQtrader  
                    Started by rocketman7, Today, 09:41 AM
                    5 responses
                    19 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X