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

ExitOnClose = !Historical?

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

    ExitOnClose = !Historical?

    Is correct when I use: ExitOnClose = !Historical;

    I for backtesting use minute data.
    ExitOnClose apparently work only tick data.
    But in real strategy i want to close positions and orders exactly when session close.

    Is

    Code:
    ...Initialize()
    {
        ExitOnClose = false; // initially disable ExitOnClose
        Add(PeriodType.Minute,1)
        ...
    
    ...OnBarUpdate()
    {
        ExitOnClose = !Historical; // when real data, use ExitOnClose!
    
        if (BarsInProgress == 1 && Historical) // minute data, "hand" cancel in simulation on minute data
        {
            if (ToTime(Time[0]) == 161400))
            {
                CancelOrder(stopOrder);
                ...
    correct?


    I can use too
    Code:
    ...Initialize()
    {
        Add(PeriodType.Minute,1)
        Add(PeriodType.Second,1)
        ...
    
    ...OnBarUpdate()
    {
        if (BarsInProgress == 1 && Historical) // minute data
        {
            if (ToTime(Time[0]) == 161400))
            {
                CancelOrder(stopOrder);
                ...
    
        if (BarsInProgress == 2) // second data
        {
            if (ToTime(Time[0]) == 161459))
            {
                CancelOrder(stopOrder);
                ...
    What solution is better?
    Is second solution safe?

    Both strategies are simplified...

    Thanks for help Petr
    Last edited by svopex; 01-28-2014, 06:12 PM.

    #2
    Hello svopex,

    Thank you for your post.

    ExitOnClose works on all bars types. What are you seeing that indicates it does not work on minute bars?

    ExitOnClose = !Historical will not work as desired.

    Comment


      #3
      I read this here:

      "...
      ...
      ...
      Last, ExitOnClose requires a tick of data for the order to process. If the session closes and there is no incoming data the close order will not be transmitted.
      "

      See in picture on my strategy too.
      This happens occasionally - exit on close is not performed.

      Supplement:
      I test second solution.
      When i use "Add(PeriodType.Second, 1);" and have for backtesting only minute data,
      OnBarUpdate never called.
      Attached Files
      Last edited by svopex; 01-28-2014, 06:13 PM.

      Comment


        #4
        Hello svopex,

        Thank you for your response.

        That would be the expected behavior of ExitOnClose, as it has exited after the close of the session. In backtesting the bars are all historical, so the Session End is not detected until after the last bar of the session closes.

        As you detailed in your original post, using a smaller bar series such as a 1 second bar can assist in exiting the position or cancelling the order before the session close.

        Comment


          #5
          Hello Patrick,

          First solution:
          Position closed in the picture by my CancelOrder command, not by ExitOnClose.
          ExitOnClose work 10x before this day, but on the picture ExitOnClose do not work.
          I don't now why. 10x after this day ExitOnClose work again withou problem - in backtest,
          I don't now why...
          Note: I use minute data (I mean source data file contain only minute data, not mean minute series).
          It's mystery for me.

          Second solution:
          If I use smaller bar series as a 1 second, it's work ok only when I use second data.
          If I use minute data (I mean source data file contain only minute data, not mean minute series),
          when I add "Add(PeriodType.Second,1)" to Initialize(), OnBarUpdate never called.
          I solve it, when I backtesting, I use "Add(PeriodType.Minute,1)" and close on the 16:14 and
          when i run strategy in real trading (i have tick data in real trading),
          i use "Add(PeriodType.Second,1)" and close on the 16:14:59. This is my solution.
          I switch this metod using boolean variable as parameter of strategy.

          Petr

          Comment


            #6
            Hello Patrick,

            i write information about candles time to log (by write function in OnBarUpdate)
            and here is interesting thing, see this:
            ...
            ...
            ...
            29-01-2014 - 15:59:58-11-2-155958-Flat-order!=null - second series
            29-01-2014 - 15:59:59-11-2-155959-Flat-order!=null - second series
            29-01-2014 - 16:00:00-11-0-160000-Flat-order!=null - 15 minute series
            29-01-2014 - 16:00:00 - change stop order
            29-01-2014 - 16:00:00-11-1-160000-Flat-order!=null - minute series
            29-01-2014 - 16:00:00-11-2-160000-Flat-order!=null - second series
            ...
            ...
            ...
            29-01-2014 - 16:13:55-11-2-161355-Flat-order!=null - second series
            29-01-2014 - 16:14:00-11-1-161400-Flat-order!=null - minute series
            29-01-2014 - 16:14:00-11-2-161400-Flat-order=null - second series
            ...
            ...
            ...
            29-01-2014 - 16:14:56-11-2-161456-Flat-order=null - second series
            29-01-2014 - 16:14:57-11-2-161457-Flat-order=null - second series
            29-01-2014 - 16:14:58-11-2-161458-Flat-order=null - second series
            END

            My question:
            Why do not OnBarUpdate called in times:
            16:13:56, 16:13:57, 16:13:58, 16:13:59 a 16:14:59?

            Second graph in atachment.

            Between 16:13:55 and 16:14:00 no data, ok, OnBarUpdate called only when data arrive from exchange. It's correct.

            Why data not arrived in the 16:14:59? Why not called OnBarUpdate in the 16:14:59?
            Because cadle (16:14:59) have same open and close level?
            But candle on the 16:14:00 have same open and close level too and OnBarUpdate called.

            Ok.
            Is exist way, that I close position on the time 16:14:59 safe always?

            Is possible way use C# timer + TriggerCustomEvent
            (on 15 min series on the 16:00:00 I run timer with 15 minute interval and I close position when timer tick)?
            Or have use OnMarketData()?

            Thanks Petr
            Attached Files
            Last edited by svopex; 01-30-2014, 10:51 AM.

            Comment


              #7
              Hello Petr,

              Thank you for your response.

              I would like to note the CancelOrder() does not close positions, it cancels orders that have not filled: http://www.ninjatrader.com/support/h...ancelorder.htm

              Seconds data is driven by tick data, if there is no tick there is no bar. Minute data is driven by time but seconds data is not. Please review the following link for information on how bars are built: http://www.ninjatrader.com/support/h..._are_built.htm

              it would not be considered safe to always exit on one time alone, you may instead wish to use greater than (>=) that time rather than equal (==) to that time.

              You could use a StopWatch to trigger the desired event: http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

              Please let me know if you have any questions.

              Comment


                #8
                You write: "Minute data is driven by time but seconds data is not."

                OnBarUpdate() called in realtime (Historical == false) every minute always?
                If data doesn't arrived from market, OnBarUpdate() is called too?

                And in backtest (Historical == true) OnBarUpdate() isn't called always?
                If I import minute data from ASCII file and there isn't minute candle in data,
                NinjaTrader doesn't call OnBarUpdate in this minute, is it true?
                Last edited by svopex; 07-05-2015, 09:24 AM.

                Comment


                  #9
                  Hello,

                  In realtime an OnBarUpdate() event would require data.

                  In the backtest if a minute bar was missing there would be no OnBarUpdate for that minute.
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    What do you mean "In realtime an OnBarUpdate() event would require data."?

                    When arrive data from market (and when is correct time), OnBarUpdate() is called?

                    Comment


                      #11
                      Hello svopex,

                      Thanks for your question.

                      When arrive data from market (and when is correct time), OnBarUpdate() is called?

                      Correct.
                      Paul H.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by kujista, Today, 05:44 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post kujista
                      by kujista
                       
                      Started by ZenCortexCLICK, Today, 04:58 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post ZenCortexCLICK  
                      Started by sidlercom80, 10-28-2023, 08:49 AM
                      172 responses
                      2,281 views
                      0 likes
                      Last Post sidlercom80  
                      Started by Irukandji, Yesterday, 02:53 AM
                      2 responses
                      18 views
                      0 likes
                      Last Post Irukandji  
                      Started by adeelshahzad, Today, 03:54 AM
                      0 responses
                      8 views
                      0 likes
                      Last Post adeelshahzad  
                      Working...
                      X