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

Exit on session close firing too early

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

    Exit on session close firing too early

    Hi. We are using NT 8.0.24.3 and doing back testing on our strategy using the Strategy Analyzer.
    Our strategy has 3 data series: 1-minute primary data series, 1-tick data series, and 240-minute (4 hours) data series.

    The issue I'm having is the Exit on session close firing too early. Timewise in 1-minute / 1-tick data series, its still 2021/09/10 2:08(AM).
    A Stop Limit order just got fulfilled and a Stop order was set up.
    Then suddenly an Exit on session close order budges in unexpectedly.

    Is this expected behaviour? Did I miss a setting?

    Thanks.


    Here is a snippet from the logs where it happens:

    Click image for larger version

Name:	ExitOnSessionCloseLog.JPG
Views:	143
Size:	463.9 KB
ID:	1173156


    Here is the result:

    (Red arrows are where the Stop Loss order are placed. But since entry orders got stopped very early by Exit session on close, then they just become sell orders which we aren't.)

    Click image for larger version  Name:	ExitOnSessionCloseResult.JPG Views:	3 Size:	155.2 KB ID:	1173150
    Last edited by cmarkb; 10-01-2021, 05:50 AM.

    #2
    Here is what I expected it would do using a 60-minute data series instead of 240-minute one.

    Log:

    Click image for larger version

Name:	ExitOnSessionCloseLogExpected.JPG
Views:	134
Size:	370.3 KB
ID:	1173155


    Result:

    Click image for larger version

Name:	ExitOnSessionCloseResultExpected.JPG
Views:	134
Size:	116.8 KB
ID:	1173154

    Comment


      #3
      Hello cmarkb,

      Thank you for your posts.

      I'm not having much luck replicating what you're seeing in your log there on my end. The chart shows the exit orders filled on the bar I would expect to see them, so the main issue is the time you're seeing them printed in the log, is that correct? What time is listed in the backtest results for those Exit on Session Close orders? Can you provide a reduced sample script that reproduces?

      Thanks in advance; I look forward to assisting you further.
      Kate W.NinjaTrader Customer Service

      Comment


        #4
        Hi Kate,

        Thanks for the reply.

        I've recreated the issue and log output. The attached script submits just one order where the issue happens (shown on previous post) and sets a stop loss.


        How the log looks:

        Click image for larger version  Name:	EarlyExitOnSessionCloseLog.JPG Views:	0 Size:	682.6 KB ID:	1173337


        Result:

        Click image for larger version  Name:	EarlyExitOnSessionCloseResult.JPG Views:	0 Size:	204.6 KB ID:	1173338
        Last edited by cmarkb; 10-03-2021, 02:41 AM.

        Comment


          #5
          Hello cmarkb,

          Thank you for your reply.

          What time is listed in the results for the Exit On Session Close order in the above screenshot? Looks to be on the correct bar on the chart as I'd expect.

          You mention an attached script but no script was attached to your post. Could you provide the script you're testing with so I may test on my end?

          Thanks in advance; I look forward to assisting you further.
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            Hi Kate,

            > What time is listed in the results for the Exit On Session Close order in the above screenshot? Looks to be on the correct bar on the chart as I'd expect.
            The Exit on Session Close was on 2:08am.

            Click image for larger version

Name:	EarlyExitLog.JPG
Views:	142
Size:	427.4 KB
ID:	1173470


            Ah! Looks like my attached script disappeared when I edited the post. I'll attach script here.

            Comment


              #7
              > The Exit on Session Close was on 2:08am.
              It was supposed to be 4:00am

              Comment


                #8
                Hello cmarkb,

                Thank you for your reply.

                I don't mean in your custom log - I mean in the actual backtest results in the Executions display of the Strategy Analyzer. What time is listed for the Exit on session close execution?

                Edit: Also, the script looks like it's still not getting attached to your replies. Are you exporting it from Tools > Export > NinjaScript Add-on and trying to upload the .Zip file to your reply as an attachment using the Upload Attachment button under the reply window?

                Thanks in advance; I look forward to assisting you further.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Here's the script. I don't know why it's disappearing. Yes, I exported it.
                  Attached Files

                  Comment


                    #10
                    Click image for larger version

Name:	script_attached.JPG
Views:	137
Size:	61.6 KB
ID:	1173499

                    Comment


                      #11
                      Here's the entire script just in case.

                      Code:
                      #region Using declarations
                      using System;
                      using System.Collections.Generic;
                      using System.ComponentModel;
                      using System.ComponentModel.DataAnnotations;
                      using System.Linq;
                      using System.Text;
                      using System.Threading.Tasks;
                      using System.Windows;
                      using System.Windows.Input;
                      using System.Windows.Media;
                      using System.Xml.Serialization;
                      using NinjaTrader.Cbi;
                      using NinjaTrader.Gui;
                      using NinjaTrader.Gui.Chart;
                      using NinjaTrader.Gui.SuperDom;
                      using NinjaTrader.Gui.Tools;
                      using NinjaTrader.Data;
                      using NinjaTrader.NinjaScript;
                      using NinjaTrader.Core.FloatingPoint;
                      using NinjaTrader.NinjaScript.Indicators;
                      using NinjaTrader.NinjaScript.DrawingTools;
                      using System.IO;
                      #endregion
                      
                      //This namespace holds Strategies in this folder and is required. Do not change it.
                      namespace NinjaTrader.NinjaScript.Strategies
                      {
                      public class TestEarlyExitOnSessionClose : Strategy
                      {
                      private RSI rsi;
                      private StreamWriter _LogWriter = null;
                      private DateTime _TheTime = DateTime.MinValue;
                      
                      private void WriteLog(string line, int bip = -1)
                      {
                      if (_LogWriter != null)
                      {
                      _LogWriter.WriteLine(line);
                      }
                      else
                      {
                      Print(line);
                      }
                      }
                      
                      protected override void OnStateChange()
                      {
                      if (State == State.SetDefaults)
                      {
                      Description = "Sample code showing early Exit On Session Close";
                      Name = "TestEarlyExitOnSessionClose";
                      
                      Calculate = Calculate.OnEachTick;
                      EntriesPerDirection = 1;
                      EntryHandling = EntryHandling.AllEntries;
                      IsExitOnSessionCloseStrategy = true;
                      ExitOnSessionCloseSeconds = 30;
                      IsFillLimitOnTouch = false;
                      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                      OrderFillResolution = OrderFillResolution.Standard;
                      IncludeTradeHistoryInBacktest = true;
                      Slippage = 0;
                      StartBehavior = StartBehavior.WaitUntilFlat;
                      TimeInForce = TimeInForce.Gtc;
                      TraceOrders = false; // NOTE: Should only be TRUE when testing
                      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                      StopTargetHandling = StopTargetHandling.PerEntryExecution;
                      BarsRequiredToTrade = 14;
                      
                      // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
                      // See the Help Guide for additional information
                      IsInstantiatedOnEachOptimizationIteration = true;
                      }
                      else if (State == State.Configure)
                      {
                      String basePath = "D:\\logs\";
                      Directory.CreateDirectory(basePath);
                      String logFilePath = System.IO.Path.Combine(basePath, String.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + ".log");
                      _LogWriter = new StreamWriter(logFilePath);
                      
                      _TheTime = new DateTime(2021, 09, 10, 2, 8, 0);
                      
                      SetStopLoss("testOrder", CalculationMode.Ticks, 10, false);
                      AddDataSeries(Data.BarsPeriodType.Tick, 1);
                      AddDataSeries(Data.BarsPeriodType.Minute, 60 * 4); // 4 hours
                      }
                      else if (State == State.DataLoaded)
                      {
                      rsi = RSI(BarsArray[2], 1, 3);
                      }
                      else if (State == State.Terminated)
                      {
                      if (_LogWriter != null)
                      {
                      _LogWriter.Flush();
                      _LogWriter.Close();
                      }
                      }
                      }
                      
                      protected override void OnBarUpdate()
                      {
                      if (BarsInProgress == 0 && CurrentBar < BarsRequiredToTrade)
                      return;
                      
                      if ((BarsInProgress == 0 && IsFirstTickOfBar) || BarsInProgress != 0)
                      {
                      WriteLog(String.Format("{0:dd/MM/yyyy HH:mm:ss.ffffff}", Bars.GetTime(CurrentBar)) + " BP=" + BarsPeriods[BarsInProgress].BarsPeriodType.ToString() + "-" + BarsPeriods[BarsInProgress].Value.ToString() + ", " + Calculate.ToString() + ", BIP=" + BarsInProgress + ", CurBar=" + CurrentBar + ", 1stTick=" + (IsFirstTickOfBar ? "Y" : "N") + ", O=" + Bars.GetOpen(CurrentBar) + ", H=" + Bars.GetHigh(CurrentBar) + ", L=" + Bars.GetLow(CurrentBar) + ", C=" + Bars.GetClose(CurrentBar) + (BarsInProgress == 2 && CurrentBars[2] > 0 ? ", rsi=" + rsi[0] : string.Empty));
                      }
                      
                      if (BarsInProgress == 0 && IsFirstTickOfBar && CurrentBars[2] > 2)
                      {
                      double startVal = rsi[0];
                      double endVal = rsi[1];
                      double slope = Slope(rsi, 1, 0);
                      double angle = Math.Atan(slope) * 180 / Math.PI;
                      
                      if (Bars.GetTime(CurrentBar) == _TheTime)
                      {
                      if (Position.MarketPosition == MarketPosition.Flat)
                      EnterLong(1, 1, "testOrder");
                      }
                      }
                      }
                      
                      protected override void OnOrderTrace(DateTime timestamp, string message)
                      {
                      WriteLog(string.Format("{0} OnOrderTrace: {1}", String.Format("{0:dd/MM/yyyy HH:mm:ss.ffffff}", timestamp), message));
                      }
                      
                      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                      {
                      WriteLog(String.Format("{0:dd/MM/yyyy HH:mm:ss.ffffff}", Bars.GetTime(CurrentBar)) + " OnOrderUpdate: " + order.ToString());
                      }
                      
                      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
                      {
                      WriteLog(String.Format("{0:dd/MM/yyyy HH:mm:ss.ffffff}", Bars.GetTime(CurrentBar)) + " OnExecutionUpdate: " + execution.ToString());
                      }
                      }
                      }
                      Last edited by cmarkb; 10-04-2021, 11:04 AM.

                      Comment


                        #12
                        Here are the screenshots for Executions and Orders tab.

                        Executions

                        Click image for larger version

Name:	executions.JPG
Views:	124
Size:	32.6 KB
ID:	1173510

                        Orders

                        Click image for larger version

Name:	orders.JPG
Views:	122
Size:	41.8 KB
ID:	1173511

                        Comment


                          #13
                          Hello cmarkb,

                          I'm not seeing the same running it on the same data on my end (note I am in the US Mountain time zone, so 2:08 am on your chart is equivalent to 12:08 pm on September 9th on mine):

                          Click image for larger version

Name:	2021-10-04_12-04-08.png
Views:	152
Size:	140.4 KB
ID:	1173521

                          Executions:

                          Click image for larger version

Name:	2021-10-04_12-04-51.png
Views:	120
Size:	29.9 KB
ID:	1173522

                          I'm seeing the stop executed on my end and it does not close the position at the end of the day.

                          I would ensure first that you have all necessary data for the backtest, including tick data for the 1 tick series. You can check the data you currently have in the Historical Data window under the Edit tab. Do you have both minute and tick data for the entire day for September 10th?

                          You can double check by downloading the data again from the Load tab of the Historical data window for the dates you're testing. Also, I would suggest since this is leaning toward being a data issue, to clear your cache and then test again once you've confirmed you have the proper historical data:

                          To delete the cache, follow the steps below.
                          • Shut down NinjaTrader.
                          • Open the Documents > NinjaTrader 8 > db folder.
                          • Delete the sub-folder named 'cache'.
                          • Restart NinjaTrader and test.

                          Please let us know if we may be of further assistance.
                          Kate W.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Kate,

                            Can you rerun it with Tick Teplay checked?

                            Comment


                              #15
                              Hello cmarkb,

                              Thank you for your reply.

                              Ah, that's definitely what's triggering the behavior - if you turn tick replay off it functions normally, however, with tick replay on I see the Exit on Session Close behavior.

                              I don't think this would be expected, so I'm going to reach out to our development team to have a look. We're tracking this under the number TS-2638. Just so you know, they've got a lot they are working on right now so it may be some time before I hear back.

                              Thanks in advance; I look forward to assisting you further.
                              Kate W.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by PaulMohn, Today, 05:00 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post PaulMohn  
                              Started by ZenCortexAuCost, Today, 04:24 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post ZenCortexAuCost  
                              Started by ZenCortexAuCost, Today, 04:22 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post ZenCortexAuCost  
                              Started by SantoshXX, Today, 03:09 AM
                              0 responses
                              15 views
                              0 likes
                              Last Post SantoshXX  
                              Started by DanielTynera, Today, 01:14 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post DanielTynera  
                              Working...
                              X