Announcement

Collapse
No announcement yet.

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.

    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

            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.

                  Comment


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

                    Comment


                      #11
                      Hello, I am trying to do something like this too. I want to force OnBarUpdate() to be called in my Strategy.

                      I have a button on the chart, and in the OnButtonClick(), I addded a call to

                      Update();

                      But I have a log first line in OnBarUpdate() and it does not print after clicking the button.

                      My feed is currently connected. I want to be able to have my button do something even on the weekend with no ticks, and that something happens in OnBarUpdate(). I tried to call it directly from OnButtonClick(), but that is a different thread and does not seem to work, so I need to do something in that function that will cause OnBarUpdate() to be called.



                      Last edited by DerkWehler; 12-01-2024, 12:03 AM.

                      Comment


                        #12
                        Originally posted by DerkWehler View Post
                        I want to be able to have my button do something even on the weekend with no ticks, and that something happens in OnBarUpdate(). I tried to call it directly from OnButtonClick(), but that is a different thread and does not seem to work, so I need to do something in that function that will cause OnBarUpdate() to be called.
                        I don't think you will ever get OnBarUpdate() to execute when no
                        bars have closed, ie, meaning no data series require updating.

                        You want OnBarUpdate() to run with State.Realtime on weekends
                        with no ticks? That is absolutely something that should not happen.

                        Note that calling Update() does not always 'force' OnBarUpdate()
                        to run either. Re-read the documentation very carefully.

                        If you want to execute something from your OnButtonClick(), then
                        you need to solve your 'runs in a different thread' problem. There
                        are NinjaScript examples that exist to help you with that. Solving
                        that issue sounds like your best solution.

                        Just my 2˘.


                        Comment


                          #13
                          Your link was just to a google search. I looked at several, but they are not about my dilemma. Do you know of any specific examples that should how to make something happen OnButtonClick which lies in the normal program thread?


                          Comment


                            #14
                            The proper way to perform asynchronous work would be to use a dispatcher,
                            such as Dispatcher.InvokeAsync(), as mentioned on the page here.

                            I'm suggesting you try that approach inside your OnButtonClick().

                            Something like this,

                            Code:
                            private void OnButtonClick(object sender, RoutedEventArgs rea)
                            {
                                Dispatcher.InvokeAsync((() =>
                                {
                                .... your code goes in here ....
                                }));
                            }

                            Comment


                              #15
                              Hello DerkWehler,

                              If the logic block contains any code using a series, call TriggerCustomEvent() first to ensure all series are updated.
                              Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              19 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              119 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              63 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              41 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              45 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X