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

ExitOnSession Close not working properly

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

    ExitOnSession Close not working properly

    Hello,

    I wanted to code a method to exit strategy before session close to avoid margin increase on the last hour of the session.

    However, it ignores my efforts and I will post a screen here and explain what has been done.

    1) Exit name column: Only 6 trades were exited using ADX_XSM_SessionClose name, which suggests it has been following my formula.
    While much more trades were closed using default system's Exit on session close function (suggested by the name), and that ALSO suggests that my formula for Exit BEFORE session close was IGNORED.

    How I coded that Exit BEFORE session close:
    1.1) In OnBarUpdate method, I first define this condition:
    Code:
                    STmFxXCond = ((endTime.AddMinutes(-50).TimeOfDay <= Time[0].TimeOfDay) ? true : false); // Condition: Stop At 50 Minutes Before Session Close Exit
    To get endTime, I use this method:

    Code:
    sessionIterator.GetNextSession(Time[0], true);
    beginTime = sessionIterator.ActualSessionBegin; // DateTime Of Session Begin
    endTime = sessionIterator.ActualSessionEnd; // DateTime Of Session End
    I ran prints and this method works correctly - it gets correct beginTime and endTime values.

    1.2) Exit method that is written after all entry conditions and variations:
    Code:
    if (Position.MarketPosition == MarketPosition.Long && STmFxXCond)
    {
    ExitLong(Qty, NameXL, NameELS);
    ExitLong(Qty, NameXL, NameELM);
    }
    Similar code is used for ExitShort, where Qty is variable for quantity, NameXL is variable for Exit Long Name, NameELS/NameELM are variables for Entry Long. So here the logic is simple: if we have a long position and STmFxXCond is True, then it should execute ExitLong command. But, sadly, it doesn't, at least most of the times.

    In my timezone, endTime should be 24:00:00 (or 0:00:00) or midnight. So, the default NinjaTrader method exit trades 1-4 seconds before session close.

    My method uses earlier exit, but still it doesn't exit closer to 50 minutes (or 23:10:00 in my timezone). I understand, that using calculation on bar close instead of each tick I accept the risk that there may no be a bar from 23:10:00 till the end of the session, but from personal experience, activity before session close increases.

    I also used TraceOrders = true, but, as you can see from output window - no lines have been generated.

    So the focal question is: How to get that formula to work, that my exits would 1) use my formula correctly and generate 0 default Ninja exits with name "Exit on session close" and 2) exit closer to 23:10:00 (if 50 minutes to session close are set) than several minutes or seconds to session close?

    #2
    Hello UltraNIX,

    I have an example accompanied by a video that demonstrates the Exit on session close does work, and how to prevent new positions from being entered after the Exit on session close event has occurred.
    NinjaTrader Community, A common inquiry is that the Exit on close didn't work in a NinjaScript Strategy because there is a position after the exit on close should have occurred. When viewing the log we often find that the Exit on close does indeed exit the position shortly before the end of the session (based on the Exit on



    With your script, are you changing the Exit on session close or Exit on close seconds in State.SetDefaults? If so, remove the instance of the Strategy and add a new instance as the defaults are only pulled when adding a new instance of the strategy. Print the Exit on session close and seconds in State.DataLoaded to know what these are set to at run time.

    If you asking about a condition and submitting an order, print all values used in the condition along with the time of the bar. Enable TraceOrders and remove and re-add an instance of the Strategy to ensure this is enabled.

    Below is a link to a forum post that demonstrates using prints to understand behavior.


    Post the output from Prints and TraceOrders saved to a text file and we will be happy to assist with analyzing the output.


    The screenshot you have provided is showing orders with the name Exit on session close. Can you further clarify the issue in the screenshot?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello UltraNIX,

      I have an example accompanied by a video that demonstrates the Exit on session close does work, and how to prevent new positions from being entered after the Exit on session close event has occurred.
      NinjaTrader Community, A common inquiry is that the Exit on close didn't work in a NinjaScript Strategy because there is a position after the exit on close should have occurred. When viewing the log we often find that the Exit on close does indeed exit the position shortly before the end of the session (based on the Exit on



      With your script, are you changing the Exit on session close or Exit on close seconds in State.SetDefaults? If so, remove the instance of the Strategy and add a new instance as the defaults are only pulled when adding a new instance of the strategy. Print the Exit on session close and seconds in State.DataLoaded to know what these are set to at run time.

      If you asking about a condition and submitting an order, print all values used in the condition along with the time of the bar. Enable TraceOrders and remove and re-add an instance of the Strategy to ensure this is enabled.

      Below is a link to a forum post that demonstrates using prints to understand behavior.


      Post the output from Prints and TraceOrders saved to a text file and we will be happy to assist with analyzing the output.


      The screenshot you have provided is showing orders with the name Exit on session close. Can you further clarify the issue in the screenshot?
      I downloaded your ExitOnCloseTest and PreventEntryAfterExitOnCloseExample strategies. Yes, it does prevent entry, but I already had this functionality in my strategy.

      However, Exit part is still not working.

      Can you share how to do this:

      ]With your script, are you changing the Exit on session close or Exit on close seconds in State.SetDefaults? If so, remove the instance of the Strategy and add a new instance as the defaults are only pulled when adding a new instance of the strategy. Print the Exit on session close and seconds in State.DataLoaded to know what these are set to at run time.
      My IsExitOnSessionCloseStrategy = true is in State.SetDefaults.

      But regardless of that, I want to use it as a "last resort". What I mean, with having condition of:

      Code:
      STmFxXCond = ((endTime.AddMinutes(-50).TimeOfDay <= Time[0].TimeOfDay) ? true : false); // Condition: Stop At 50 Minutes Before Session Close Exit
      It should NEVER go to the session close with a position, as it would exit on the NEXT BAR after endTime.AddMinutes(-50).TimeOfDay <= Time[0].TimeOfDay. So, IF THIS WORKED, ExitOnClose Seconds part would be irrelevant. But it doesn't.

      Here, take a look at the screenshot. I took prints, then exported text files and imported to Excel to see where is the flaw:

      Greenish background - longs, Reddish background - shorts.

      As you can see in Column D, Position.MarketPosition == MarketPosition.Long: True, which, of course, means that I have a long position open.

      As you can see in Column B, SExVaXLCond: True (Exit variation condition) is True, and rightfully so, as 23:10:16 is less than 50 minutes to the session close (my timezone: midnight)

      As you can see in Column F, SExitXLCond: True (Exit condition) is True, and rightfully so, as the formula is: (((Position.MarketPosition == MarketPosition.Long) && SExVaXLCond) ? true : false)

      And finally, I adjusted calculation that calls ExitLong command:

      Code:
      if (SExitXLCond)
      {
      ExitLong(Qty, NameXL, NameELS);
      ExitLong(Qty, NameXL, NameELM);
      }
      So, you can see, that all 3: Column B, D, and F conditions remain TRUE until the end of the day, which SHOULD trigger ExitLong command, BUT IT DOESN'T.
      Attached Files

      Comment


        #4
        Hello UltraNIX,

        To confirm, you are not inquiring at all about the Exit on session close, and instead you want to know why your own logic condition is not evaluating as true or why order submitted with TraceOrders are not being filled, is this correct?

        May I have the output from the TraceOrders and prints?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Yes, if my exit logic worked, there would be zero exits with name "Exit on session close" (default NinjaTrader method) and all those should be replaced with my own logic.

          What do you want to print and trace? I already included printscreen of NinjaScript Output window in the first post, which was blank, but Trace Orders was enabled. So either I did something wrong, or I need to print additional data. Let me know what to print, I will do that.

          Comment


            #6
            And, as you can see from Excel screenshot in my post #3, it evaluates as true, but then it fails to execute ExitLong orders.

            Comment


              #7
              Hello UltraNIX,

              The information needs to be printed (or written to text file) by the strategy.

              Please review the video linked in the post about how to use prints to understand behavior I have linked in post #2.

              Print the time of the bar, print all values used in the condition outside of the condition.

              Enable TraceOrders.

              Save the output from the output window to a text file.

              Attach the text file with your reply.

              With TraceOrders and prints we can determine exactly why and when a condition will be true or false, and with TraceOrders we can see exactly why and when orders are being submitted, ignored, or automatically cancelled.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                I have TraceOrders enabled, and 0 text in Output Window appears.

                I included a screen of it in my initial post with blank output window. Don't know what else to do.

                Also, if you refer to my post in #3, you see that everything evaluates just fine after 23:10:00 - all 5 bars are evaluating TRUE.

                But ExitLong doesn't trigger.

                Comment


                  #9
                  Hello TraceOrders,

                  Is TraceOrders set to true in State.SetDefaults (in which case remove the instance and add a new instance so the defaults are pulled) or is this set to true in State.Configure?

                  If you have a print in the action block of the condition do you see the print appear?

                  The TraceOrders would let you know if the order was submitted.

                  Unfortunately, your screenshot does not provide enough information for me to know how the condition would evaluate or what the condition is as there are no comparison operator labels. Should these values be true or false? Nor would this confirm that the information is being produced by the strategy and not by you manually. Printing to the output window would provide the necessary information, in text file form to review.

                  As a example of a print that would tell you how the condition would evaluate:
                  Print(string.Format("{0} | SMA(7)[1]: {1} < SMA(14)[1]: {2} & SMA(7)[0]: {3} > SMA(14)[0]: {4}", Time[0], SMA(7)[1], SMA(14)[1], SMA(7)[0], SMA(14)[0]));

                  With just this print, I would be able to tell if there was a cross above by looking at the comparative operator labels like > or < and &.
                  Last edited by NinjaTrader_ChelseaB; 12-14-2020, 11:22 AM.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Finally, TraceOrders started to work, when moved to State.Configure. Thanks!

                    I was able to save same output window results in several days. I had to split in bunches of several days, as there was too much of data for the output window to handle.

                    I am uploading 3. [Edit: I was unable to upload due to 1 MB of max file size restriction, so I uploaded on WeTransfer: https://we.tl/t-CFo9ZO07IW]

                    There were 4 trades during this period, that "went wrong":
                    Entry: 2020-09-14 04:36:39, Exited at 23:59:59
                    Entry: 2020-09-22 03:08:58, Exited at 23:59:59
                    Entry: 2020-10-07 22:14:38, Exited at 23:59:57
                    Entry: 2020-10-09 08:36:02, Exited at 23:59:59

                    However, there was 1 trade during that period, that worked according to my condition.
                    Entry: 2020-09-21 22:54:01, Exited at 23:14:49 (100% correct)

                    Tell me if you see what needs to be fixed from those logs.

                    Comment


                      #11
                      Hello UtraNix,

                      These may be the wrong files..

                      The first print appears to start on 9/17 and not 9/14.

                      2020-09-17 03:11:26 Strategy 'AI_ADX_200807_O/-1': Entered internal SubmitOrderManaged() method at 2020-09-17 03:11:26: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ADX_ESM_MinMaxATR' FromEntrySignal=''

                      The last file also does not have anything on 10/9 at 8:36 AM.

                      It jumps from 8:18:03 to 23:10:16 with nothing in between.

                      2020-10-09 08:18:03 Strategy 'AI_ADX_200807_O/-1': Ignored SubmitOrderManaged() method at 2020-10-09 08:18:03: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ADX_XLM_SessionClose' FromEntrySignal='ADX_ELM_FastCandle' Reason='This was an exit order but no position exists to exit'
                      2020-10-09 23:10:16 | SExitXLCond: True | SExitXSCond: False

                      It does not appear the output you have provided matches the orders you are suggesting. Was this from the wrong script?
                      Are you viewing the output from a different strategy?

                      With the print:
                      2020-09-17 06:04:52 | SExitXLCond: False | SExitXSCond: True

                      Just to confirm the condition is:
                      if (SExitXLCond == true && SExitXSCond == true)

                      And there are no other values in that condition, correct?


                      Are you able to reduce the amount of data you are testing over to just a day or a few days and still reproduce the behavior?
                      When debugging, it is always important to reduced the data, reduce the code to just the relevant code being addressed, and print all of the involved values.


                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        As I was splitting into parts as there was too much of text, probably some text was cut. Uploading 2 more files. There you could see 09-14 and 10-09 orders, hopefully. https://we.tl/t-sYrslGl2VR

                        No, the output is exactly from that same strategy.

                        About that print:
                        Code:
                         if(SExitXLCond || SExitXSCond)
                        {
                        Print(Time[0] + " | SExitXLCond: " + SExitXLCond + " | SExitXSCond: " + SExitXSCond);
                        }
                        So the logic is - if either SExitXLCond or SExitXSCond evaluates TRUE, only then I will get a print. (in order not to clutter Output window). And, therefore show, that it evaluates, but is not executed.

                        Which is derived from:
                        Code:
                         SExitXLCond = (((Position.MarketPosition == MarketPosition.Long) && SExVaXLCond) ? true : false); // Condition: Exit- Long
                        SExitXSCond = (((Position.MarketPosition == MarketPosition.Short) && SExVaXSCond) ? true : false); // Condition: Exit- Long;
                        and those are derived from:
                        [CODE] SExVaXLCond = exitOnCloseWait;
                        SExVaXSCond = exitOnCloseWait;

                        I had more conditions, but just removed those as now I am interested in correct application on exit execution.

                        If you need anything else, just tell me.

                        And yes, when I reduced testing period from 10/7 to 10/16 (the log I am attaching now), it still reproduces the same unwanted behavior. Entry: 2020-10-09 08:36:02, Exit: 23:59:59.

                        Comment


                          #13
                          Hello UltraNIX,

                          Just to confirm, the behavior cannot be reproduced if testing over just two days of data?

                          Also, the | in the print is to indicate an OR correct?

                          Again in this latest file you have provided there is order at 4:36 on 9/14. It jumps from 4:23:14 to 18:02:03.

                          2020-09-14 04:23:14 Strategy 'AI_ADX_200807_O/-1': Entered internal SubmitOrderManaged() method at 2020-09-14 04:23:14: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ADX_ELM_RSIInd' FromEntrySignal=''

                          2020-09-14 18:02:03 | SExitXLCond: True | SExitXSCond: False

                          I may be overlooking this.
                          Are you able to find the TraceOrder for the order at 2020-09-14 04:36:39 in the output you have provided me?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ChelseaB View Post
                            Hello UltraNIX,

                            Just to confirm, the behavior cannot be reproduced if testing over just two days of data?

                            Also, the | in the print is to indicate an OR correct?

                            Again in this latest file you have provided there is order at 4:36 on 9/14. It jumps from 4:23:14 to 18:02:03.

                            2020-09-14 04:23:14 Strategy 'AI_ADX_200807_O/-1': Entered internal SubmitOrderManaged() method at 2020-09-14 04:23:14: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ADX_ELM_RSIInd' FromEntrySignal=''

                            2020-09-14 18:02:03 | SExitXLCond: True | SExitXSCond: False

                            I may be overlooking this.
                            Are you able to find the TraceOrder for the order at 2020-09-14 04:36:39 in the output you have provided me?
                            1. Regardless of the period (2 days, 6 days, 10 days or a year) - nothing changes. Exit order doesn't trigger in as it 'is supposed to' by calculation.

                            2. " | " in print is just separator, so it's just for visual purposes. only place where OR is defined in if statement ( if(SExitXLCond || SExitXSCond) )

                            3. As for "jumping" from one timestamp to another. I cannot control this. Text is generated in Output Window only under 2 conditions:
                            1) When if(SExitXLCond || SExitXSCond) is TRUE. Then it prints which of the conditions is True.
                            2) When TraceOrders generates some code (and that should only happen when order is triggered/filled/cancelled/ignored/etc.).

                            So, if(SExitXLCond || SExitXSCond) is FALSE, or nothing happens with orders, there should be no prints.

                            4. About that 2020-09-14 04:23:14 order. Strange things. When I set period from 2020-09-12 to 2020-09-18 - it doesn't appear in the list. First order is of 2019-09-17. However. If I change period from 2020-09-10 to 2020-09-20, the 2020-09-14 trade now appears in the trade list.

                            5. As for 2020-09-14 again.
                            I am attaching text of the log of 2020-09-14.

                            and error is:
                            Code:
                            Ignored SubmitOrderManaged() method at 2020-09-14 18:02:03: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ADX_XLM_ChopInd' FromEntrySignal='ADX_ESM_' Reason='SignalName does not have a matching FromEntrySignal to exit'
                            Maybe there is the real issue? As "FromEntrySignal='ADX_ESM_" shouldn't be like this.

                            It's my structure of naming, where ADX is 3 digit abbreviation, then "_" separator, then "ESM" (or Entry Short Market abbreviation), then "_" separator, and then there should be Entry Signal name, but it isn't.

                            When order was submitted, code was:
                            Code:
                            2020-09-14 04:23:14 Strategy 'AI_ADX_200807_O/-1': Entered internal SubmitOrderManaged() method at 2020-09-14 04:23:14: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ADX_ELM_RSIInd' FromEntrySignal=''
                            So, still, "FromEntrySignal='', which is blank, is that ok? and, if initial entry order had: SignalName='ADX_ELM_RSIInd', then Exit order on 2020-09-14 18:02:03 should have "FromEntrySignal='ADX_ELM_RSIInd" INSTEAD OF "FromEntrySignal='ADX_ESM_".

                            Not sure if this is an issue, but it looks like it might be.

                            What is interesting, that in some cases this FromEntrySignal works fine and it goes without an error, but in the given examples it is not the case.

                            To paint the full picture, I can share formulas of how those Exit Names are derived:

                            Code:
                            NameXLCond = (STmFxXCond == true ? "SessionClose" : SRSIIXLCond == true ? "RSIInd" : SChoIXLCond == true ? "ChopInd" : SVWAPXLCond == true ? "VWAPInd" : "");
                            NameXSCond = (STmFxXCond == true ? "SessionClose" : SRSIIXSCond == true ? "RSIInd" : SChoIXSCond == true ? "ChopInd" : SVWAPXSCond == true ? "VWAPInd" : "");
                            NameXL = NameStrat + "XLM_" + NameXLCond;
                            NameXS = NameStrat + "XSM_" + NameXSCond;
                            Where in NameXLCond or NameXSCond those STmFxXCond and so on are conditions for Exit. If those are true, then the name is given according to them. I am using multiple exit conditions, so whichever evaluates true, the name is given and then it is supposed to exit.

                            NameStrat is defined in the beggining of the strategy, where all variables are instantiated:
                            Code:
                            private string        NameStrat = "ADX_";
                            Similar procedure is with Entry signal names.
                            Attached Files

                            Comment


                              #15
                              Hello UltraNIX,

                              If the behavior does not change, then you can test over much smaller data sets and focus on a lot less output. Reducing the amount of data, amount of logic, helps to zero in on the issue without all of the other data there to have to go through. This saves valuable time.

                              Try testing over a single day, ensure you can reproduce, and the output is going to be much much smaller and easier to analyze.


                              If the vertical line is a separator, then you are not including operator labels in the print to know how the condition is suppose to evaluate.

                              As an example that would use a print with an OR:
                              Print(string.Format("{0} | SMA(7)[1]: {1} < SMA(14)[1]: {2}||SMA(7)[0]: {3} > SMA(14)[0]: {4}", Time[0], SMA(7)[1], SMA(14)[1], SMA(7)[0], SMA(14)[0]));

                              This would let us know in the print that the SMA7 of 1 bar ago must be less than the SMA(14) of 1 bar ago OR the SMA(7) of 0 bars ago must be greater than the SMA(14) of 0 bars ago.
                              By printing all of the variable, comparison, and logical operator labels with the values, this lets us know how the condition should evaluate.

                              By looking at just the print of:
                              2020-09-14 18:02:03 | SExitXLCond: True | SExitXSCond: False
                              There is no indication that both values must not be true and that these are joined with an || (OR) logical operator.
                              I would assume that these are joined with && (AND) as there are no labels.


                              In the example I have provided you with a video, notice that the prints appear on every bar and not just when the condition is true.
                              May I confirm you have reviewed the video in this post that demonstrates how to use prints to understand behavior?

                              If there is no traceorders information for an order submitted at 4:36 on 9/14. Mostly likely you are looking at output form another strategy and not from the strategy that has produced this output.


                              After changing the data, if an order is not appearing in the TraceOrders output, then it is not being submitted due to the logic. If you want to know why the order is not submitted, print all the values in the conditions that submit the order outside of the condition (so it prints on every bar).

                              You can focus on another order in the shortend data set that is also experiencing the issue.


                              With the error:
                              "Ignored SubmitOrderManaged() method at 2020-09-14 18:02:03: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ADX_XLM_ChopInd' FromEntrySignal='ADX_ESM_' Reason='SignalName does not have a matching FromEntrySignal to exit'"

                              This is telling you that an ExitLong() was called but was ignored. The from entry signal for the order did not match any positions opened with an entry order with that signal name. There were no positions opened with an entry order with the name ADX_ESM_. That means the entry either was never submitted, never filled, or the position has already been closed.

                              The condition evaluated as true, but the order was ignored as it was invalid.


                              If the FromEntrySignal is blank this will exit orders that have a blank signal name.

                              If there is an entry filled with the signal name ADX_ELM_RSIInd, then to exit that position the from entry signal of the exit must also be ADX_ELM_RSIInd. They have to match.

                              If the string is not what you expect, print all the values used in the conditions that set the string. Debug it. This will tell you what is evaluating as true or false and why the condition to concatenate with the string was triggered or not triggered.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rdtdale, Today, 01:02 PM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by alifarahani, Today, 09:40 AM
                              3 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by RookieTrader, Today, 09:37 AM
                              4 responses
                              19 views
                              0 likes
                              Last Post RookieTrader  
                              Started by PaulMohn, Today, 12:36 PM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by love2code2trade, 04-17-2024, 01:45 PM
                              4 responses
                              41 views
                              0 likes
                              Last Post love2code2trade  
                              Working...
                              X