Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy State

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

    Strategy State

    I have a custom strategy that uses a custom indicator.

    Both strategy and indicator are setup for Calculate.OnEachTick and check the IsFirstTickOfBar to do single bar stuff.

    I need to be able to detect in both indicator and strategy if I am running on historical data in order to prevent a few things form happening (mostly TTS voice notifications).

    The problem is when a strategy is started on live data it is run against X amount of data prior to the current price in the market to get a base for populating the bars etc for running the strategy.

    I check to verify the state is not State.Historical however when running on this "pre" data it appears the state is State.Realtime.

    How can I determine that this "realtime" is not actually real time on current market price but the strategy setting itself up to start so I can avoid the unnecessary TTS voice commands etc?

    Thanks.

    #2
    Hello fxRichard, and thank you for your question. Calling

    Code:
    [FONT=Courier New]
    protected override void OnBarUpdate()
    {
        if (State == State.Historical)
        {
            return;
        }[/FONT]
    is usually sufficient to prevent any historical bars from processing, including the "pre" bars you mentioned. Would it be possible to send us a stripped down copy of your code so we can look into this in greater detail?
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_JessicaP View Post
      Hello fxRichard, and thank you for your question. Calling

      Code:
      [FONT=Courier New]
      protected override void OnBarUpdate()
      {
          if (State == State.Historical)
          {
              return;
          }[/FONT]
      is usually sufficient to prevent any historical bars from processing, including the "pre" bars you mentioned. Would it be possible to send us a stripped down copy of your code so we can look into this in greater detail?
      Thanks Jessica, I already have this but it's still processing the pre bars loaded and I print the state to the log it shows real-time.

      I'll copy paste the snippets once I am back on the computer later. Thanks

      Comment


        #4
        Originally posted by NinjaTrader_JessicaP View Post
        Hello fxRichard, and thank you for your question. Calling

        Code:
        [FONT=Courier New]
        protected override void OnBarUpdate()
        {
            if (State == State.Historical)
            {
                return;
            }[/FONT]
        is usually sufficient to prevent any historical bars from processing, including the "pre" bars you mentioned. Would it be possible to send us a stripped down copy of your code so we can look into this in greater detail?
        Hello Jessica,

        My code snippets are as follows:

        Strategy:
        Code:
        protected override void OnBarUpdate()
        {
        	if (State == State.Historical)
        		return;
        
               ... do stuff here
        Indicator:
        Code:
        protected override void OnBarUpdate()
        {
        	if(State == State.Historical)
        		return;
        	
        	// To verify our time and state....	
        	Print("Time: " + Time[0] + " State: " + State);
        	...do more stuff
        If I attach the strategy to a chart then enable it in the console I get the following output in the NinjaScript Output window:

        Code:
        Time: 9/18/2016 5:01:00 PM State: Realtime
        Time: 9/18/2016 5:02:00 PM State: Realtime
        Time: 9/18/2016 5:03:00 PM State: Realtime
        ....same lines for every minute up until the current day and time
        Take note that the current time that I ran this was Sept 22 5:48pm thus every bar was processed as "RealTime".

        Is there any way to determine if the Indicator is attached to a strategy and to NOT process certain events unless it is on LIVE market data?

        Comment


          #5
          In addition, if I add a little code to print the first OnBarUpdate in the indicator and print all state changes I get the following flow:

          Code:
          9/22/2016 6:01:00 PM: Current State is State.SetDefaults
          9/22/2016 6:01:00 PM: Current State is State.Configure
          9/22/2016 6:01:00 PM: Current State is State.DataLoaded
          9/22/2016 6:01:00 PM: Current State is State.Historical
          Enabling NinjaScript strategy 'CTA Strategy 1/xxxxxxx: On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=Unique entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On each tick IsUnmanaged=False MaxRestarts=4 in 5 minutes
          9/22/2016 6:01:00 PM: Current State is State.Transition
          9/22/2016 6:01:00 PM: Current State is State.Realtime
          OnBarUpdate
          Meaning no bars are handled in the Historical state, the bars loaded into the chart are all calculated and run as RealTime....

          Comment


            #6
            Hello fxRichard,

            I had forgotten about the new NT8 Transition state. This code will ensure that only Realtime or later states are executed. While Realtime is currently the last state, this code will continue working if newer states are added at a later point.

            Code:
            [FONT=Courier New]
                    protected override void OnBarUpdate()
                    {
                        if (State < State.Realtime)
                        {
                            return;
                        }
                        Print("The current time is " + Time[0].ToString());
                    }[/FONT]
            Sample output on 1 tick bars with 12:49 on my clock set to UTC

            Code:
            The current time is 23-Sep-16 12:49:53
            The current time is 23-Sep-16 12:49:56
            The current time is 23-Sep-16 12:49:56
            The current time is 23-Sep-16 12:49:57
            The current time is 23-Sep-16 12:50:00
            The current time is 23-Sep-16 12:50:07
            The current time is 23-Sep-16 12:50:07
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Thanks Jessica this helps however we still have the problem that if I have a strategy attached to a chart, it processes all of the bars on the chart with a state of "Realtime".

              With this in mind the custom indicator used in the strategy has no way of knowing if the bars are actually "Realtime" or historical bars on the chart.

              My guess is either this is a bug in NT8 or there is some other way to determine this that I have missed.

              Comment


                #8
                It looks like something local to your system is occurring, since you and I are getting different results.

                While the code we were using earlier would produce results like yours, the code sample I just posted should absolutely not be posting data on any historical bars. The code we were using earlier would not exclude the transition state, where the code I just posted will.

                If you tried the code sample I posted and were still getting printouts from historical bars, please send me your log and trace files, templates, and other files, so that I may look into what occurred.
                You can do this by going to the Control Center-> Help-> Email Support.
                Please reference the following ticket number in the body of the email: 1577235
                Please include Attn:NinjaTrader_JessicaP in the body of your e-mail.
                IMPORTANT: Please also tick all the boxes in the "Other Files" section.
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Let me put together a simple sample. My indicator and strategy are built in Visual Studio and span many files etc.

                  I'll post the example here shortly.

                  Comment


                    #10
                    Ok so I have narrowed it down:

                    If you have an indicator added to a chart in a strategy, the indicator processes ALL data in state Realtime from what it appears.

                    The notes in NT for AddChartIndicator state the following:

                    Tip: If you are adding an indicator which is dependent on the correct State of the indicator, you will need to ensure that you are also calling the indicator from the strategy in OnBarUpdate(), otherwise your indicator will only process in State.RealTime for performance optimizations.
                    This can be read in one of two ways:

                    1) If I add an indicator to the chart in a strategy in the State.Configure area ONLY bars with State.Realtime will be processed.

                    2) If I add an indicator to the chart in a strategy in the State.Configure area ALL bars on the chart will be processed by the added indicator as "Realtime".

                    I would expect #1 to be the case but #2 is what I am seeing. If I remove the AddChartIndicator call in the State.Configure area and just call the indicator in the strategy OnBarUpdate it still processes all bars as "Realtime".

                    I'll add the 2 files in the next post but this can easily be created if you create a new strategy and new indicator with Calculate.OnEachTick. Use the dummy wizard code and add the following few lines to each:

                    ExampleStateIndicator
                    Code:
                    protected override void OnBarUpdate()
                    {
                        if(IsFirstTickOfBar)
                            Print("INDICATOR - Time: " + Time[0] + " State: " +  State);
                    }
                    ExampleStateStrategy
                    Code:
                    ...in OnStateChange
                      else if (State == State.Configure)
                      {
                          AddChartIndicator(ExampleStateIndicator());
                      }
                    ...
                    protected override void OnBarUpdate()
                    {
                        if(IsFirstTickOfBar)
                            Print("STRATEGY - Time: " + Time[0] + " State: " +  State);
                    }
                    Hopefully there are no typos as I am writing this post on my Mac so could not copy/paste the example code.

                    If you do this and watch the output you'll see the strategy correctly processes the bars but the indicator processes all as Realtime.

                    Is this by design or how can we get the indicator to process the bars correctly as the strategy does with correct state?

                    Comment


                      #11
                      If you still need the log/trace files etc let me know Jessica. The post above should clarify everything. I have also attached the raw indicator and strategy examples I put together to outline what's going on.

                      Thanks for your help.
                      Attached Files

                      Comment


                        #12
                        Hello, and thank you for your report. This behavior was confirmed on our end. We will be investigating this further. Please keep an eye on the NinjaTrader 8 Release Notes page for updates and bugfixes.



                        Jessica P.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Jessica, so this is a bug that I need to provide a temporary workaround for correct?

                          Comment


                            #14
                            I have talked with some of our developers and other people in charge of the product and reviewed the specifications, and this is the way the NT8 state engine is currently designed to work.

                            To expand on the help guide tip, indicators added through AddChartIndicator from a parent will have their OnBarUpdate event called in the State.Realtime state while the parent’s OnBarUpdate is being called in earlier states such as State.Historical and State.Transition, unless the parent contains a reference to the indicator in its own OnBarUpdate method such as the following :

                            Code:
                            [FONT=Courier New]// in parent
                            protected override void OnBarUpdate()
                            {
                                double myVal = ExampleIndicator()[0];
                            }[/FONT]
                            As an example, code such as

                            Code:
                            [FONT=Courier New]// in child
                            protected override void OnBarUpdate()
                            {
                                Print("Child Indicator State: " + State);
                            }[/FONT]
                            Will print “Child Indicator State: Realtime” for each of the parent’s Historical and Transition bars without the parent calling ExampleIndicator() during OnBarUpdate, and will report being in earlier states with ExampleIndicator() being called.

                            Please let us know if there are any other ways we can help.
                            Jessica P.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks Jessica, I'm out to lunch but will verify and test what you posted once back. I appreciate your time and help.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by swestendorf, Today, 11:14 AM
                              2 responses
                              5 views
                              0 likes
                              Last Post NinjaTrader_Kimberly  
                              Started by xiinteractive, 04-09-2024, 08:08 AM
                              4 responses
                              12 views
                              0 likes
                              Last Post xiinteractive  
                              Started by Mupulen, Today, 11:26 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post Mupulen
                              by Mupulen
                               
                              Started by Sparkyboy, Today, 10:57 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by TheMarlin801, 10-13-2020, 01:40 AM
                              21 responses
                              3,917 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Working...
                              X