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

Need method evaluated each second

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

    Need method evaluated each second

    Hello! If there's a way to evaluate some method exatly each second or more often? It's really important that strategy needs to work when nothing is changing and the OnBarUpdate is not evaluating. E.g. count pause time to in.

    Due to this page:

    I understood that if I set "On each tick" update method then the functions OnBarUpdate and OnMarketData will be being evaluated each tick (as I know a tick should be evaluated 50+ times at a second). I write simple function that writes down in a file date time when it is evaluating. And the output of both OnBarUpdate and OnMarketData are similar:

    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:18
    27.03.2017 15:35:22
    27.03.2017 15:35:23
    27.03.2017 15:35:23
    27.03.2017 15:35:24
    27.03.2017 15:35:24
    27.03.2017 15:35:24
    27.03.2017 15:35:24
    27.03.2017 15:35:25
    27.03.2017 15:35:26
    27.03.2017 15:35:26
    27.03.2017 15:35:26
    27.03.2017 15:35:26
    27.03.2017 15:35:26
    27.03.2017 15:35:28
    27.03.2017 15:35:29
    27.03.2017 15:35:29
    27.03.2017 15:35:29
    27.03.2017 15:35:29
    27.03.2017 15:35:29
    27.03.2017 15:35:30
    27.03.2017 15:35:30
    27.03.2017 15:35:30
    27.03.2017 15:35:30
    27.03.2017 15:35:30
    27.03.2017 15:35:30
    27.03.2017 15:35:32
    27.03.2017 15:35:35
    27.03.2017 15:35:35
    27.03.2017 15:35:35
    27.03.2017 15:35:35
    27.03.2017 15:35:35
    27.03.2017 15:35:37
    Look, this functions are evaluated several times at some seconds but they pass some second. For example this function wasn't evaluated on 19th, 20th, 21th, 27th, 31th, 33th, 34th and 36th seconds. I need each second. So how can I achieve this?
    Last edited by Defake; 03-27-2017, 05:01 AM.

    #2
    Originally posted by Defake View Post
    Hello! If there's a way to evaluate some method exatly each second or more often? It's really important that strategy needs to work when nothing is changing and the OnBarUpdate is not evaluating. E.g. count pause time to in.

    Due to this page:

    I understood that if I set "On each tick" update method then the functions OnBarUpdate and OnMarketData will be being evaluated each tick (as I know a tick should be evaluated 50+ times at a second). I write simple function that writes down in a file date time when it is evaluating. And the output of both OnBarUpdate and OnMarketData are similar:

    Look, this functions are evaluated several times at some seconds but they pass some second. For example this function wasn't evaluated on 19th, 20th, 21th, 27th, 31th, 33th, 34th and 36th seconds. I need each second. So how can I achieve this?
    Setup and use your own timer to handle things every second.

    Comment


      #3
      To build off kogonam's answer, here is a code sample from the help guide. We are happy to help with any specific questions that come up.

      Code:
      [FONT=Courier New]// http://ninjatrader.com/support/helpGuides/nt8/en-us/triggercustomevent.htm
      [/FONT][FONT=Courier New][COLOR=#0000ff]protected[/COLOR][COLOR=#0000ff] override[/COLOR][COLOR=#0000ff] void [/COLOR]OnBarUpdate()
      [/FONT] [FONT=Courier New]{
          [COLOR=#008000]// OnBarUpdate() only runs as bars are processed, which is not guaranteed to occur at a specific interval[/COLOR]
          [COLOR=#008000]// e.g., even on a 5 second bar series, there may be time periods where there are no updates due to low trading activity[/COLOR]
          [COLOR=#008000]// or could be buffered due to running Calculate.OnBarClose. Instead of trying to obtain the Close[0] value[/COLOR]
          [COLOR=#008000]// at some interval here, we are going to do it in our custom TimerEventProcessor[/COLOR]
      [/FONT] [FONT=Courier New]}
       
      [/FONT] [FONT=Courier New][COLOR=#008000]// This is the method to run when the timer is raised.[/COLOR]
      [/FONT] [FONT=Courier New][COLOR=#0000ff]private[/COLOR][COLOR=#0000ff] void[/COLOR] TimerEventProcessor(Object myObject, EventArgs myEventArgs)
      [/FONT] [FONT=Courier New]{
          [COLOR=#008000]// Do not process your code here but instead call the TriggerCustomEvent() method[/COLOR]
          [COLOR=#008000]// and process your code in the custom handler method e.g., our custom PrintThePrice()[/COLOR]
          [COLOR=#008000]// Doing so ensures all internal indexers are up-to-date[/COLOR]
          TriggerCustomEvent(PrintThePrice, Close[[COLOR=#ff6600]0[/COLOR]]);
      [/FONT] [FONT=Courier New]}
       
      [/FONT] [FONT=Courier New][COLOR=#008000]// Print the latest closing price with the current time[/COLOR]
      [/FONT] [FONT=Courier New][COLOR=#0000ff]private [/COLOR][COLOR=#0000ff]void[/COLOR] PrintThePrice([COLOR=#0000ff]object[/COLOR] price)
      [/FONT] [FONT=Courier New]{
          Print([COLOR=#800000]"The Last Bar's Closing Value as of "[/COLOR] + NinjaTrader.Core.Globals.Now +[COLOR=#800000] " was "[/COLOR] + price);
      [/FONT] [FONT=Courier New]}
       
      [/FONT] [FONT=Courier New][COLOR=#0000ff]private[/COLOR] System.Windows.Forms.Timer myTimer =[COLOR=#0000ff] new[/COLOR] System.Windows.Forms.Timer();
       
      [/FONT] [FONT=Courier New][COLOR=#0000ff]protected[/COLOR][COLOR=#0000ff] override[/COLOR][COLOR=#0000ff] void[/COLOR] OnStateChange()
      [/FONT] [FONT=Courier New]{
          [COLOR=#0000ff]if[/COLOR](State == State.SetDefaults)
          {
              Name =[COLOR=#800000] "SampleTriggerCustomEventTimer"[/COLOR];
          }
          [COLOR=#0000ff]else[/COLOR][COLOR=#0000ff] if[/COLOR](State == State.Configure)
          {
              [COLOR=#008000]// Adds the event and the event handler for the method that will[/COLOR]
              [COLOR=#008000]// process the timer event to the timer.[/COLOR]
              myTimer.Tick +=[COLOR=#0000ff] new[/COLOR] EventHandler(TimerEventProcessor);
       
              [COLOR=#008000]// Sets the timer interval to 5 seconds.[/COLOR]
              myTimer.Interval =[COLOR=#ff6600] 5000[/COLOR];
              myTimer.Start();
          }
       
          [COLOR=#0000ff]else[/COLOR][COLOR=#0000ff] if[/COLOR](State == State.Terminated)
          {
              [COLOR=#008000]// Stops the timer and removes the timer event handler[/COLOR]
              myTimer.Stop();
              myTimer.Tick -=[COLOR=#0000ff] new[/COLOR] EventHandler(TimerEventProcessor);
          }
      [/FONT] [FONT=Courier New]}[/FONT]
      Last edited by NinjaTrader_JessicaP; 03-27-2017, 11:35 AM.
      Jessica P.NinjaTrader Customer Service

      Comment


        #4
        Okay I made this with DispatcherTimer. Everything is working, thanks!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CortexZenUSA, Today, 12:53 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by CortexZenUSA, Today, 12:46 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by usazencortex, Today, 12:43 AM
        0 responses
        5 views
        0 likes
        Last Post usazencortex  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        168 responses
        2,266 views
        0 likes
        Last Post sidlercom80  
        Started by Barry Milan, Yesterday, 10:35 PM
        3 responses
        13 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Working...
        X