Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit all position on the given time.

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

    #31
    Hi Cheslea,

    Indeed we see an Exit on Session Close order submitted at 15:34:01 and as seen on a chart as executed at 15:35. No complains there.

    What we DONT see AT ALL anywhere is the submission of the Exit from the position order at 15:00 clearly spelled out in the script.
    In the example provided I was long at 15:00 holding one contract so why no orders were received ?

    // Set 2
    if ((Position.MarketPosition == MarketPosition.Long)
    // EXIT
    && ((IsFalling(AuLLMA1.LLMA) == true)
    || (Times[1][0].TimeOfDay == new TimeSpan(15, 0, 0))))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }

    That is where real problem is and what I am trying to resolve and implement.

    Cheers

    Comment


      #32
      Hello kazbek966,

      You will need to add debugging prints beside the order method to confirm that is reached. We must first confirm, with prints, that the order method is reached if we are to expect an order submission.

      If it is not reached (the print beside the order method is not seen,) we suggest printing out the values used in the condition controlling that order method. This can be done outside the condition or in a separate Condition Set that does not have any conditions controlling it (so the values always print.) The resources below can show you how strategy logic can be checked with prints.

      Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

      Debugging in the Strategy Builder - https://drive.google.com/file/d/1mTq...w?usp=drivesdk

      Debugging Demo - https://drive.google.com/file/d/1rOz...w?usp=drivesdk

      Chelsea had also requested that you add the specific print:

      Code:
      Print(string.Format("{0} | BIP: {1}, {2} {3} | (Position.MarketPosition: {4} == MarketPosition.Long) && ( (IsFalling(AuLLMA1.LLMA): {5} == true) || (Times[1][0].TimeOfDay: {6} == new TimeSpan(15, 0, 0): {7}) ) )", BarsInProgress, BarsArray[BarsInProgress].BarsPeriod.Value, BarsArray[BarsInProgress].BarsPeriod.BarsPeriodType, Position.MarketPosition, IsFalling(AuLLMA1.LLMA), Times[1][0].TimeOfDay, new TimeSpan(15, 0, 0) ));
      Please set up the prints to confirm the order method is reached, and if it is not reached, check the conditions controlling it.

      Let us know if you have questions on interpreting the output.

      Comment


        #33
        Hi Jim,

        To troubleshoot I decided to run my script on primary 8 min regular bar chart with recommended 1 min additional data series used as a timer for exit and compared it with RENKO primary chart supported by same 1 min additional data series.
        Click image for larger version

Name:	ExitOnTime1.PNG
Views:	171
Size:	133.6 KB
ID:	1192653
        Strategy was working as intended on 8 min data series with exit at 15:01 ( which can be seen in the attached figure ), while no exit at 15:01 was observed with RENKO primary series.
        This observation clearly indicates that EXIT on Time does not work with RENKO main series even in the presence of 1 MIN additional series. So the question is what additional instructions need to be scripted in the ExitLong(Convert.ToInt32(DefaultQuantity), "", ""); so it understood what to do.

        Cheers

        Comment


          #34
          Hello kazbek966,

          This likely indicates the condition set to submit the order is not evaluating as true.

          Note, that your conditions are looking for exactly 15:00. On a movement based chart, it would be unlikely for a bar to close at exactly 15:00 unless this is the end of the session.

          May we have the output from the prints so that we may further assist?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #35
            Hi Chelsea,
            I actually submitted prints to relevant times as an TXT file attachment . Did you receive them ? Just in case attaching them again.
            As far as I understood the Strategy builder function I instructed to Exit position starting at 15.00. That order was executed at 15:00:01 on 8 min chart

            1/24/2022 3:00:01 PM Strategy 'AuLLMAwTradingExiTimeLong/257591040': Entered internal SubmitOrderManaged() method at 1/24/2022 3:00:01 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

            As for RENKO nothing was happening till our beloved 3:34 Exit on Session Close that also received order with 1 second delay. We can forgive it for this lack of punctuality.

            1/24/2022 1:59:01 PM Strategy 'AuLLMAwTradingExiTimeLong/257591041': Entered internal SubmitOrderManaged() method at 1/24/2022 1:59:01 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

            1/24/2022 3:34:01 PM CancelAllOrders: BarsInProgress=0
            1/24/2022 3:34:01 PM CancelAllOrders: BarsInProgress=1
            1/24/2022 3:34:01 PM Strategy 'AuLLMAwTradingExiTimeLong/257591041': Entered internal SubmitOrderManaged() method at 1/24/2022 3:34:01 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Exit on session close' FromEntrySignal=''

            So again the question is why the Exit on Time order was received and properly executed on 8 MIn chart and completely missed on RENKO. The closure of bars in Both 8 MIN and RENKO charts is happening outside 15.00 marker and solely condition of exit is hitting 15:00 mark.
            Both of cases are using 1 MIN chart as a timer so it is inconceivable to me that timer is working on 1 MIN series in 8 MIN chart and suddenly decided not to function on RENKO. I believe some modification/specification in Exit order is needed. Please let me know whether you received an attachment.

            Cheers




            Attached Files
            Last edited by kazbek966; 03-07-2022, 09:21 AM.

            Comment


              #36
              Hello kazbek966,

              May I confirm you have added prints to print all value used in the conditions that submit the order?

              I am not seeing any output from Print() in the txt file you have provided.

              It's possible that on the 8 minute chart, a bar closed at 15:00, since it is a time based bar, and that on the renko chart no bar closed at exactly 15:00 since this is not a time based bar.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #37
                Hi Chelsea, indeed 8 min chart falls exactly on 15:00. When conducting same troubleshooting on 7 and 13 min series , exit on time was ignored as well and Exit on Session close was executed.
                So lets go back to square 1 and revisit basics. To the best of my understanding in the initial posting on current subject it was recommended to add 1 MIN additional series to primary series of our choice in order to exit on time within reasonable 1 MIN accuracy. As seen with RENKO , 7 and 13 min series this suggestion does not work. Mind you 15:00 exit time is being monitored on 1 MIN series so why this simple condition is being ignored is a bit of a mystery.
                So what steps need to be taken in order for EXIT order to recognize 15:00 time ?
                Cheers

                Comment


                  #38
                  Hello kazbek966,

                  What doesn't work?

                  The added 1 minute series is not updating OnBarUpdate with BarsInProgress 1?
                  Have you added prints to determine this?

                  The steps that would need to be taken to trigger an exit at exactly 15:00 would be a time based bar like 1 minute being added to the script, and logic to submit the exit order processed when OnBarUpdate is processing that BarsInProgress. A 1 minute bar would close every minute and would have a bar close at 15:00 (if the trading hours are still in session).





                  If you want to know why the strategy is behaving as it is, it would be necessary to use prints to debug as previously suggested.
                  You can print the time of the bar and BarsInProgress to confirm the 1 minute series is updating OnBarUpdate. You can print all values used in the condition to verify the condition evaluated as true.
                  I am happy to assist with analyzing the output from prints if you provide these.

                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #39
                    Hi Chelsea,

                    Currently, I placed additional 1 MIN series according to Strategy Builder options given to me without delving into OnbarUpdate and BarsinProgress.

                    }
                    else if (State == State.Configure)
                    {
                    AddDataSeries("MNQ 03-22", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                    }
                    else if (State == State.DataLoaded)
                    {
                    DATETIME = new Series<DateTime>(this);
                    AuLLMA1 = AuLLMA(Median, 1, 1);
                    }
                    }

                    protected override void OnBarUpdate()
                    {
                    if (BarsInProgress != 0)
                    return;

                    if (CurrentBars[0] < 1)
                    return;

                    // Set 1
                    if ((Position.MarketPosition == MarketPosition.Flat)
                    && (IsRising(AuLLMA1.LLMA) == true)
                    // Time
                    && ((Times[0][0].TimeOfDay <= new TimeSpan(14, 55, 0))
                    && (Times[0][0].TimeOfDay >= new TimeSpan(13, 30, 0))))
                    {
                    EnterLong(Convert.ToInt32(DefaultQuantity), "");
                    }

                    // Set 2
                    if ((Position.MarketPosition == MarketPosition.Long)
                    // EXIT
                    && ((IsFalling(AuLLMA1.LLMA) == true)
                    || (Times[1][0].TimeOfDay == new TimeSpan(15, 0, 0))))
                    {
                    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
                    }



                    Am I correct to assume that current placing of 1 MIN Series is the culprit of observed behaviour ?
                    By the way I put suggested PRINT in the { } after above mentioned portion of the script that resulted in the output I submitted as TXT file.
                    Was the placement of the PRINT correct ?

                    Comment


                      #40
                      Hello kazbek966,

                      That would be correct. The Strategy Builder is not able to trigger actions when a secondary series is processing in OnBarUpdate.

                      The script must be unlocked and coded by hand.

                      The BarsInProgress check is preventing the script from processing when BarsInProgress is 1 (the minute series) and this would need to be removed or modified.

                      if (BarsInProgress != 0)
                      return;

                      A print would be placed one line above the condition set. This would print the time of the bar, BarsInProgress, as well as all values used in the condition set.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #41
                        Good to know, Cheslea, that I 've been knocking to the closed door.
                        That being said I explored suggested links and have a couple of questions related to suggested topics of dataseries and BarsInProgress.
                        First of all there is obvious difference in syntax between script and Strategy Builder.
                        While in script barsinprogress are defined as

                        if (BarsInProgress == 0)

                        In Strategy they are defined as

                        if (BarsInProgress != 0)
                        return;
                        if (CurrentBars[0] < 1)
                        return;

                        In any case I presume these 4 lines specify dealing with main data series, and my task is to make sure that strategy understand that it needs to handle time based exit using 1 additional 1Min series. So I tried to improvise a bit and put this code together.

                        protected override void OnBarUpdate()
                        {
                        if (BarsInProgress != 0)
                        return;

                        if (CurrentBars[0] < 1)
                        return;

                        Print(string.Format("{0} | BIP: {1}, {2} {3} | (Position.MarketPosition: {4} == MarketPosition.Long) && ( (IsFalling(AuLLMA1.LLMA): {5} == true) || (Times[1][0].TimeOfDay: {6} == new TimeSpan(15, 0, 0): {7}) ) )", BarsInProgress, BarsArray[BarsInProgress].BarsPeriod.Value, BarsArray[BarsInProgress].BarsPeriod.BarsPeriodType, Position.MarketPosition, IsFalling(AuLLMA1.LLMA), Times[1][0].TimeOfDay, new TimeSpan(15, 0, 0) ));

                        // Set 1
                        if ((Position.MarketPosition == MarketPosition.Flat)
                        && (IsRising(AuLLMA1.LLMA) == true)
                        // TIME
                        && ((Times[0][0].TimeOfDay >= new TimeSpan(17, 0, 0))
                        || (Times[0][0].TimeOfDay < new TimeSpan(15, 0, 0))))
                        {
                        EnterLong(Convert.ToInt32(DefaultQuantity), "");
                        }

                        // Set 2
                        if ((Position.MarketPosition == MarketPosition.Long)
                        && (IsFalling(AuLLMA1.LLMA) == true))
                        {
                        ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
                        }

                        else if (BarsInProgress != 1)
                        return;

                        if (CurrentBars[1] < 1)
                        return;

                        // Set 3
                        if ((Position.MarketPosition == MarketPosition.Long)
                        && (Times[1][0].TimeOfDay == new TimeSpan(15, 0, 0)))
                        {
                        ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
                        }

                        }

                        The idea was to create separate condition designed to handle Time based exit on 1 MIN series while specifying that it is 1MIn series strategy must be looking for.
                        It did not work as planned and thats where your input will be helpfull. By the way as seen in the script I put PRINT in its correct location and got exactly the same output as before.

                        Finally while I do understand the meaning of "if (BarsInProgress != 0) " - work with main series , I am a bit unsure about meaning of "if (CurrentBars[0] < 1)".

                        Cheers




                        Comment


                          #42
                          Hello kazbek966,

                          With the code at the top of OnBarUpdate:
                          Code:
                          if (BarsInProgress != 0)
                          return;
                          This return would stop evaluating any code unless the series is the primary series (BarsInProgress 0). Meaning no actions could happen on any other added series.

                          BarsInProgress 0 is the primary chart series. BarsInProgress 1 would be the first series added with AddDataSeries(), BarsInProgress 2 would be the second series added with AddDataSeries(), etc..

                          CurrentBar is the number of the bar (starting with bar 0 at the left of the chart) that is currently being updated in OnBarUpdate(). CurrentBars (plural) is a collection of CurrentBar values for each series added with AddDataSeries. The index is the BarsInProgress or BarsArray index.

                          CurrentBars[0] would be the number of the bar being processed for the primary chart series. CurrentBars[1] would be the number of the bar being processed for the first added series with AddDataSeries().

                          https://ninjatrader.com/support/help...urrentbars.htm
                          Last edited by NinjaTrader_ChelseaB; 03-08-2022, 09:34 AM.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #43
                            Thanks Chelsea, now a bit more clear. Are there any available examples of strategy working with multiple series so I did not reinvent bicycle.
                            The theory is clear , but would be nice to have some examples to emulate.
                            Cheers

                            Comment


                              #44
                              Hello kazbek966,

                              There is a reference sample for intra-bar granularity that has the basic setup, but no actions written for the added series.


                              The AdvanceDecline and Spread indicators add multiple series with different instruments which you might find helpful, but these are complex.
                              NYSE Advancing Issues/Declining Issues Uses historical tick data to calculate the ^ADV index minus the ^DECL index to give a simulation of the ^ADD index. Requires a data feed that supports indexes and provides data for the ^ADV (NYSE Advancing Issues) and ^DECL (NYSE Declining Issues). Update June 16th, 2020 &#8211; Corrected stroke attributes to [&#8230;]

                              Plots the Spread of two instruments The first instrument is the primary chart data series. The secondary instrument is specified by the parameter "Symbol2". The Parameters "Qty1" and "Qty2" are multipliers which are applied to each instrument&apos;s price before adding the two; default values are "Qty1=1" and "Qty2=&dash;1" which will simply plot the difference between [&#8230;]
                              Chelsea B.NinjaTrader Customer Service

                              Comment


                                #45
                                Using you First Example

                                To close the position of the strategy at a specific time in OnBarUpdate():

                                if (ToTime(Time[0]) == 90000)
                                {
                                if (Position.MarketPosition == MarketPosition.Long)
                                ExitLong();
                                if (Position.MarketPosition == MarketPosition.Short)
                                ExitShort();
                                }

                                This would close an open position at 9:00 AM​

                                I created a version with a variable time input, which I have watched it executed exits as expected except the time will be off about a 60 to 120 seconds

                                Today It did not create my exits at 8:26 AM on 2 different strategies that were in positions prior to the Exit Time Settings

                                Exit1TimeInput = DateTime.Parse("08:26", System.Globalization.CultureInfo.InvariantCulture) ;


                                [NinjaScriptProperty]
                                [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
                                [Display(Name="Exit 1 Time Input", Description="Use Time Exit 1 Time Input", Order=32, GroupName="Parameters")]
                                public DateTime Exit1TimeInput
                                { get; set; }


                                MY STRATEGY RUNS ON A RENKO STYLE BAR, THE STRATEGY ALSO HAS A SECONDARY 1 MINUTE DATA
                                SO I USED Times[1][0] to base the condition TimeOfDay on the 1 minute Secondary data

                                /// TimeExit1
                                if ((Position.MarketPosition == MarketPosition.Long)
                                && (LongTrades == true)
                                && (TimeExit1 == true)

                                && (Times[1][0].TimeOfDay == Exit1TimeInput.TimeOfDay))

                                {
                                ExitLong(Convert.ToInt32(Position.Quantity), @"LTimeExit1", @"");
                                PerBarExitONEmade = true;
                                }

                                SO I CANNOT FIGURE OUT WHY is NOT exact time set 08:26 AM and this may be why did not exit my positions today. The prior witness exits that were off 60-120 seconds may have been lagging more today for something in the condition using Times[1][0]

                                Thanks for any help
                                DTSSTS

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by NullPointStrategies, Today, 05:17 AM
                                0 responses
                                50 views
                                0 likes
                                Last Post NullPointStrategies  
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                126 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                69 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                42 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                46 views
                                0 likes
                                Last Post TheRealMorford  
                                Working...
                                X