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

Print statement depending on local time outside OnBarUpdate

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

    Print statement depending on local time outside OnBarUpdate

    Hi,

    I want my Strategy to make Print statements to the output window OUTSIDE THE ONBARUPDATE method when my local time is 8 o'clock (DateTime.Now.Hour) . So, I want that print statement no matter if there was a new bar or not. Unfortunately, when I activate the strategy there are no print statements in the output window.

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class XYZ: Strategy
    {
    
    public void PrintOnLocalTime()
    {
    if (DateTime.Now.Hour == 8)
    Print("......");
    }
    What am I doing wrong?


    Kirk
    Last edited by KirkHammett; 11-27-2021, 05:38 PM.

    #2
    Hello Kirk,

    In real-time you could use a timer.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks a lot Chelsea! I was able to make it work.

      I just have two questions for my understanding:

      1. Timer:

      Code:
      else if (State == State.Realtime)
      {[INDENT]Dispatcher.InvokeAsync(() =>
      {[/INDENT][INDENT=2]myTimer = new System.Windows.Threading.DispatcherTimer();
      myTimer.Interval = TimeSpan.FromMilliseconds(TimerInterval);
      myTimer.Tick += TimerEventProcessor;
      myTimer.IsEnabled = true;[/INDENT][INDENT]});[/INDENT]
       
       }
      I changed the code from "ChartControl.Dispatcher.InvokeAsync(() =>" to "Dispatcher.InvokeAsync(() =>" because I want the timer to trigger my custom method outside of a chart. Why doesnt it simply work without it or with a method like "private void Timer()"? Why do I need "Dispatcher.InvokeAsync(()"? I read through the NT help guide but as far as I understand the Dispatcher is just for UI and I am not using UI here.

      2. Indicator

      In the indicator you created a series of the CurrentBar
      Code:
      protected override void OnBarUpdate()
      {[INDENT]myDoubleSeries[0] = CurrentBar;[/INDENT]
       
       }
      Why do I need a series here? I tried it with a class level private int variable instead of myDoubleSeries[0]. But when I added the indicator to my strategy the prints showed me "0" instead of the current bar.

      Thanks a lot for your help!

      Regards
      Kirk
      Last edited by KirkHammett; 11-30-2021, 03:31 PM.

      Comment


        #4
        Hello KirkHammet,

        A DispatcherTimer needs a dispatcher, which is available on a UI thread of a chart, and where using ChartControl.Dispatcher.InvokeAsync would be needed. If you want to use a timer without using ChartControl.Dispatcher, you can consider using a different timer.

        The example attached will not use the same kind of timer.

        The use of the Series<double> is there just to demonstrate reading Series values from TriggerCustomEvent, from the timer event method.
        Attached Files
        JimNinjaTrader Customer Service

        Comment


          #5
          Thanks a lot for your prompt answer Jim. The Dispatcher topic is clear to me now. But I still have a problem with understanding the Series<double> topic.

          If I create a private int variable at the indicators class level and assign the CurrentBar to it in OnBarUpdate, I am only getting "0" as prints and not the current bar. I am only getting the correct Current Bar value in my print statements if I create a private series variable and assign the CurrentBar to it in OnBarUpdate. I just don't understand why I need the series variable.

          So the following modification in your example prints only "0" for myBar. If myBar is a series<int> it works. Why is that so?

          Code:
          public class SampleCustomEvents : Indicator
          {
          private int myBar;
          
          
          protected override void OnBarUpdate()
          {
          myBar = CurrentBar;
          }
          
          private void MyCustomHandler(object state)
          {
          Print(myBar);
          }
          }


          Thanks a lot for your help!

          Comment


            #6
            Hello KirkHammett,

            It would be helpful to attach source code exports similar to what we are sharing so we can see the full context of your test script.

            It is not clear if you are using TriggerCustomEvent, and how you have the timer set up.

            You may compare with the attached script which uses the same timer in post#4 and also prints a private int and a Series<double> value.
            Attached Files
            JimNinjaTrader Customer Service

            Comment


              #7
              Hi Jim!

              Thanks a lot for your response. With your code the myInt prints are correct.

              However, if I just change the Code in Line 99 by making the Print(mySeries[0]) as a comment, the myInt prints are wrong: I am getting a 0 instead of the current bar. See below the file.

              Why does it behave that way? This makes no sense to me at all.
              Attached Files

              Comment


                #8
                Hello KirkHammett,

                I have received your note.

                If you were testing out an event that was not unsubscribed that event may still be firing old code. Try restarting the platform, and testing the indicator again. I also suggest changing the name of the indicator so you can be sure you are testing the same version.

                Below is a demonstration of my test.

                Demo - https://drive.google.com/file/d/1P_6...w?usp=drivesdk
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Thank you very much Jim!

                  Changing the name solved the problem in the Chart. But I have the same problem if I add the indicator to a strategy and enable the strategy.

                  I duplicated the SampleSMACrossOver Strategy, added the SampleTimerTest in DataLoaded and enabled the strategy (synchronize account, immediately submit). I am getting "0" no matter if I change the name of the indicator in the script, restart NT8 etc. However addingthe indicator on the Chart I am getting the correct values. See below the example.

                  Why is the problem still occuring in the Strategy but not on the Chart? I need it in the strategy, so I would very much appreciate if you have any solutions here as well.

                  Thanks a lot for your efforts!

                  Attached Files

                  Comment


                    #10
                    Hello KirkHammett,

                    Thanks, this detail helps clarify.

                    The indicator is not plotting anything or updating Series<T> objects, so OnBarUpdate is not being called for the indicator when it is added in the strategy.

                    Note from the Help Guide:

                    Hosted indicators will need to be accessed by the hosting script to ensure OnBarUpdate functionality. This can be done by: 1) Calling Update on the hosted indicator within the host script, 2) Including a plot in the hosted indicator and accessing the plot in the host script, 3) Including a plot in the hosted indicator and adding the indicator to the chart with AddChartIndicator (strategies only)



                    In regards to the note Series<T> are also applicable, as these are what are used for plots.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Jim this was the problem! Calling SystemTimer().Update() in OnBarupdate of the strategy solved all problems! Thanks a lot for your help!

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by sidlercom80, 10-28-2023, 08:49 AM
                      168 responses
                      2,262 views
                      0 likes
                      Last Post sidlercom80  
                      Started by Barry Milan, Yesterday, 10:35 PM
                      3 responses
                      10 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by WeyldFalcon, 12-10-2020, 06:48 PM
                      14 responses
                      1,428 views
                      0 likes
                      Last Post Handclap0241  
                      Started by DJ888, 04-16-2024, 06:09 PM
                      2 responses
                      9 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by jeronymite, 04-12-2024, 04:26 PM
                      3 responses
                      41 views
                      0 likes
                      Last Post jeronymite  
                      Working...
                      X