Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how to run certain calculation logic every tick, while only running entry calculation

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

    how to run certain calculation logic every tick, while only running entry calculation

    Hi,
    I´m going back to your reference sample from here:


    I can´t agree with this part of the code which is saying:
    /* FirstTickOfBar specifies that this section of code will only run once per bar, at the close of the bar as indicated by the open of the next bar. NinjaTrader decides a bar is closed when the first tick of the new bar comes in, therefore making the close of one bar and the open of the next bar virtually the same event. */

    For me the close and open are the time driven events and not tick driven. So I can´t never make the open with my own first tick!
    Is there some other time logic which can be implemented?? I mean to have part of the stop code in tick by tick and the entry to position on bar update specified with some timer in seconds??
    Thx.

    Best regards,

    vb

    #2
    Hello linzvb, and thank you for your question.

    Just to make sure I understand your question correctly, you are asking if you can execute an event on both the first and last tick of a bar?

    If this is the case, since we can not predict in advance when a bar is about to close, we must store information and use it at the first tick of a bar. I am attaching a copy of the C# code from that reference code where I implement such a solution and annotate it with my initials and the date I begin an annotated section. Please let us know if there are any other ways we can help.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi Jessica,

      many thanks for your answer. Unfortunately doesn't solve the closing bar problem... See below some 1MIN bars from 10YR T-Note(ZNU6):

      DateTime Open High Low Close
      2016-08-22 15:11:00, 132.171875, 132.203125, 132.15625, 132.171875
      2016-08-22 15:10:00, 132.140625, 132.15625, 132.140625, 132.15625
      2016-08-22 15:09:00, 132.109375, 132.15625, 132.109375, 132.15625

      Now you can clearly see that the last tick of the bar and the first tick of the bar are not the same events! Not ticks but the time closes the bars. If we take the time of 2016-08-22 15:11:00:000:000 then all ticks which arrive until this time will be included calc. within this OHLC bar. All ticks which arrive after this time will be included in the next bar.

      And that´s the point! With your logic I will need to wait until the first tick will arrive but this is exactly what I want to do. I will that my strategy update exact on this time - 15:11:00:000:000 and if a buy signal is true then I will send the order and the first tick will be my trade.

      Let my know if you have any question please. Thx.

      Best regards,

      VB

      Comment


        #4
        Hello,
        NinjaTrader is event driven there are not time events though. NinjaTrader uses Ticks for the event. The way that the bars close is it checks the time of each tick and if the tick's time is later than the time the bar was supposed to close at then it closes the bar and creates a new bar. Historically it doesn't work this way as we are not getting real time ticks and NinjaTrader can check the times of the OHLC values. Running in real time though the bar closes with the first tick of the new bar.
        If you are wanting to have an event occur at a specific time you would need to create a custom timer event and check for the time within that event. An example of how you could do this would be the built in BarTimer indicator. I would recommend reviewing that indicator as a reference.
        Cody B.NinjaTrader Customer Service

        Comment


          #5
          Hi Cody,

          I find the timer code. See below:

          protected override void OnBarUpdate()
          {
          if (timer == null)
          {
          if (DisplayTime())
          {
          timer = new System.Windows.Forms.Timer();
          timer.Interval = 100;
          timer.Tick += new EventHandler(OnTimerTick);
          timer.Enabled = true;
          }
          }
          if(barTimeLeft.Seconds <= secondsToAlert)
          {
          Alert("COB", NinjaTrader.Cbi.Priority.High, "Close of Bar.", "Alert1.wav", secondsToAlert + 1, Color.Black, Color.Yellow);
          }
          }


          Wanted ask if the timer is running within OnBarUpdate() method then will update every incoming tick isn´t it??

          Best regards,

          vb

          Comment


            #6
            One vital part of the BarTimer code that is missing from the excerpt you sent is the OnTimerTick method. I'll post that here because it pertains to the answer to your question.

            Code:
            [FONT=Courier New]        private void OnTimerTick(object sender, EventArgs e)
                    {
                        if (DateTime.Now.Subtract(lastTimePlot).Seconds >= 1 && DisplayTime())
                        {
                            ChartControl.ChartPanel.Invalidate();
                            lastTimePlot = DateTime.Now;
                        }
                    }[/FONT]
            To answer your question, while OnTimerTick has an opportunity to fire off every System.Windows.Forms.Timer() tick - which it requires to run, and which are different from NinjaTrader ticks since they happen at regular intervals rather than on market data updates - it will take a different action on the last windows tick of a bar. In our case, it will not update the clock during the last second of a bar.

            You will need to modify that conditional to suit your needs, so that the condition is only ever true during the last tick of a bar. The Timer.TickEvent approach Cody recommended is still necessary, rather than simply adding this condition to OnBarUpdate, since NinjaTrader only updates when it has incoming data. In a slow moving market, several seconds may go by without any market data changes. Without a mechanism like Timer.TickEvent we can not guarantee an event at a regular interval.

            Another link which will help you understand how this solution works is this publicly available MSDN documentation on Timer.TickEvent available here,



            We are happy to help in any way we can.
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Hi Jessica,

              Cody noted:
              An example of how you could do this would be the built in BarTimer indicator. I would recommend reviewing that indicator as a reference.

              Unfortunately cant find any BarTimer.cs or BarTimer indicator file... Can you provide it please?
              Thx.

              vb

              Comment


                #8
                I am happy to. My copy of (My) Documents\NinjaTrader 7\bin\Custom\Indicator\@BarTimer.cs is attached. Please let us know if there are any other ways we can help.
                Attached Files
                Jessica P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by yertle, Yesterday, 08:38 AM
                7 responses
                28 views
                0 likes
                Last Post yertle
                by yertle
                 
                Started by bmartz, 03-12-2024, 06:12 AM
                2 responses
                21 views
                0 likes
                Last Post bmartz
                by bmartz
                 
                Started by funk10101, Today, 12:02 AM
                0 responses
                6 views
                0 likes
                Last Post funk10101  
                Started by gravdigaz6, Yesterday, 11:40 PM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by MarianApalaghiei, Yesterday, 10:49 PM
                3 responses
                11 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X