Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Entry at day close

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

    Entry at day close

    Hi!

    I'm struggling to enter at the day close correctly.

    I have two data series:
    1. timeframe day
    2. timeframe 1m

    So, what is the approach:
    1. On day timeframe I analyze specific metrics as signals to enter
    2. On a minute timeframe I enter and exit positions

    In the strategy I have
    Code:
                
    endDateTime_RTH = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 14, 59, 0);    
    if(BarsInProgress == 0)
                {
    if(Condition1 > 120){
    LongCon1 = true}
    
    if(ConditionExit <= 0.5)
    {
      LongExit = true
    }
    }
    if(BarsInProgress == 1)
                {
                    if(LongCon1
                        && Time[0] == endDateTime_RTH
                        )
                    {
                        EnterLong (0, 1, RegularLong);
                    }
    
                    if(Position.MarketPosition == MarketPosition.Long
                        && Time[0] == endDateTime_RTH
                        && LongExit
                        ){
    
                        ExitLong();​​
    }​
    
    ​
    but results are still not correct, what I'm missing here? How to correctly enter and exit and day close?

    Thank you!

    #2
    Hello mrboltuae,

    Thank you for your post.

    What are the results you are seeing vs. the results you are expecting to see? You could always add Print() statements to further debug your strategy's behavior and understand why it is not behaving as expected:


    As for placing trades at specific times, you could implement a time filter to limit your trades to specific hours. This concept is demonstrated in the following reference sample:


    Please let us know if we may be of further assistance.

    Comment


      #3
      Hi, Emily

      As you can see I already implemented such timefilter, so, don't see any value here.

      As per expectations: now orders open at next candle Open, not the Close, see image below. So, order should have opened at 25.10 close, not 26.10 open.
      Click image for larger version

Name:	image.png
Views:	100
Size:	41.7 KB
ID:	1282627

      Comment


        #4
        Hello mrboltuae,

        Thank you for your reply.

        Are you seeing this happen in real-time, historical (backtest), or both? What is the "Calculate" property for your strategy set to?


        I would expect this behavior to happen in historical processing or in real-time with calculate set to Calculate.OnBarClose because the condition would be evaluated at the close of a bar and intrabar actions would not be available. For historical backtests, if you'd like intrabar fill prices, you must use the High Order Fill Resolution or submit orders to a more granular data series, with 1-tick being the most granular option. More information may be found at the following links:Please let us know if we may be of further assistance.

        Comment


          #5
          yes, it's in historical backtest, with OnBarClose.

          I do understand what you are saying, that's why from the begining I mentioned that I have already two data series, and it doesn't help, I'm trying to understand at what time I should place entry and exit as I need to wait for close daily bar and then enter.

          Comment


            #6
            Hello mrboltuae,

            Thank you for your reply.

            When are you specifically looking to enter? You mention "day close" though do you mean on the last 1 minute bar of the session or at a different time? A bar's close is signaled by the first tick of the next bar, which means the close of a daily bar is signaled by the first tick of the next session. If you want to enter/exit at the close of a day, it is expected that it would occur on the very first tick of the next session. Please clarify exactly when you are looking to enter/exit so I may better understand and assist you. Keep in mind that (depending on the instrument you are trading) there may be a session break at the close of the day and trades may not be allowed at that time until the next session begins.

            I look forward to your reply.

            Comment


              #7
              Ok, Emily.

              I'm trying to enter/exit at 15.00 Chicago time.The instrument is NQ.

              Btw, also wanted to ask: Ninja is not good platform for swing trading, like when you hold position for 3-4 days? I'm running some strategy, with such swing trading, and faced with issue when connection was lost and after positions were not showen in positions tab, so, trying to understand how to handle it in real environment.

              Comment


                #8
                Originally posted by mrboltuae View Post
                Ok, Emily.

                I'm trying to enter/exit at 15.00 Chicago time.The instrument is NQ.

                Btw, also wanted to ask: Ninja is not good platform for swing trading, like when you hold position for 3-4 days? I'm running some strategy, with such swing trading, and faced with issue when connection was lost and after positions were not showen in positions tab, so, trying to understand how to handle it in real environment.
                If your primary (daily) data series is using RTH, I suspect your added 1-minute data series is also using RTH. To enter on the 1-minute candle stamped at 15:00, you will need to add the 1-minute series with a trading hours template that includes that time, such as the ETH trading hours template. There are a few method signatures for AddDataSeries() that allow you to specify the tradingHoursName:

                With that said, this warning should also be kept in mind: "If your NinjaScript object is using AddDataSeries() allowing to specify a tradingHoursName, please keep in mind that: An indicator / strategy with multiple DataSeries of the same instrument will only process realtime OnBarUpdate() calls when a tick occurs in session of the trading hour template of all added series. Any ticks not processed will be queued and processed as a tick comes in for all subsequent DataSeries."

                As for holding positions for multiple days, especially if they are managed by automated strategies, the platform must remain open and connected for the strategy logic to properly manage the position. If the connection is lost, the strategy won't be connected to manage the position. If you ever have questions about live orders/positions, you should immediately reach out to your broker's orders desk for assistance.

                Please let us know if we may be of further assistance.

                Comment


                  #9
                  I even did this:
                  Code:
                              else if (State == State.Configure)
                              {    
                                  AddDataSeries(null, Data.BarsPeriodType.Minute, 1);
                              }​
                              startDateTime_RTH = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 08, 30, 0);
                              endDateTime_RTH = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 14, 58, 0);    
                              endDateTime_RTH2 = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 14, 59, 0);  
                  
                              if(BarsInProgress == 0 && Time[0] >= startDateTime_RTH && Time[0] <= endDateTime_RTH)
                              {​
                           <HERE I'm calculating metrics on Day close>
                              if(BarsInProgress == 1){
                                  if(Time[0] == endDateTime_RTH2
                                      && LongExit
                                      ){
                                      ExitLong();
                  
                                  }            
                  
                                  if(LongCon1
                                      && Time[0] == endDateTime_RTH2
                                      )
                                  {
                                      EnterLong (0, IQ, RegularLong);
                                  }​
                  } ​
                  So, I guess no need to add another trading hours if I want to enter last minute at session close, right?

                  Still struggling to find an error, in Trades tab I see that trades entries like 00:00:00

                  Comment


                    #10
                    Originally posted by mrboltuae View Post
                    I even did this:
                    Code:
                     else if (State == State.Configure)
                    {
                    AddDataSeries(null, Data.BarsPeriodType.Minute, 1);
                    }​
                    startDateTime_RTH = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 08, 30, 0);
                    endDateTime_RTH = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 14, 58, 0);
                    endDateTime_RTH2 = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 14, 59, 0);
                    
                    if(BarsInProgress == 0 && Time[0] >= startDateTime_RTH && Time[0] <= endDateTime_RTH)
                    {​
                    <HERE I'm calculating metrics on Day close>
                    if(BarsInProgress == 1){
                    if(Time[0] == endDateTime_RTH2
                    && LongExit
                    ){
                    ExitLong();
                    
                    }
                    
                    if(LongCon1
                    && Time[0] == endDateTime_RTH2
                    )
                    {
                    EnterLong (0, IQ, RegularLong);
                    }​
                    } ​
                    So, I guess no need to add another trading hours if I want to enter last minute at session close, right?

                    Still struggling to find an error, in Trades tab I see that trades entries like 00:00:00
                    I previously mentioned using print statements to debug your strategy. What prints have you tried so far? What values print out if you print the values for startDateTime_RTH, endDateTime_RTH, and endDateTime_RTH2? Please print the time of the current bar when you call EnterLong() and ExitLong() as well to compare the time of the bar with the times used in your variables and conditions to get a deeper understanding of the strategy's behavior.

                    If you are unsure of the meaning of the output, please right-click the NinjaScript Output and save the output to a text file that may be attached to your reply.

                    Thank you for your time and patience.

                    Comment


                      #11
                      Hi, Emily

                      So, what I tried already
                      1. "If your primary (daily) data series is using RTH, I suspect your added 1-minute data series is also using RTH. To enter on the 1-minute candle stamped at 15:00, you will need to add the 1-minute series with a trading hours template that includes that time, such as the ETH trading hours template. There are a few method signatures for AddDataSeries() that allow you to specify the tradingHoursName"

                      Added Data series with new trading hours
                      AddDataSeries(null, new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1 }, "CME US Index Futures ETH");
                      2. Main trading hours for Day timeframe is "CME US Index Futures RTH"

                      Given that:
                      Code:
                                  else if (State == State.Configure)
                                  {    
                                      AddDataSeries(null, new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1 }, "CME US Index Futures ETH");
                                  }    ​
                      
                          protected override void OnBarUpdate()
                              {
                      
                                  startDateTime_RTH = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 08, 30, 0);
                                  endDateTime_RTH = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 16, 00, 0);    
                                  endDateTime_RTH2 = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 16, 00, 0);​
                      
                      if(BarsInProgress == 0 && Time[0] == endDateTime_RTH)
                                  {​
                      <CALCULATING DAY METRICS>
                      }
                      
                      if(BarsInProgress == 1){
                                      if(LongCon1
                                          && Time[0] == endDateTime_RTH2
                                          )
                                      {
                                          EnterLong (0, IQ, RegularLong);
                                      }​
                      What I got: trades exit at 17.01 (don't understand why) and entry is 00:00

                      Click image for larger version

Name:	image.png
Views:	114
Size:	36.0 KB
ID:	1282860
                      Click image for larger version

Name:	image.png
Views:	92
Size:	13.9 KB
ID:	1282861​​

                      Comment


                        #12
                        Originally posted by mrboltuae View Post
                        Hi, Emily

                        So, what I tried already
                        1. "If your primary (daily) data series is using RTH, I suspect your added 1-minute data series is also using RTH. To enter on the 1-minute candle stamped at 15:00, you will need to add the 1-minute series with a trading hours template that includes that time, such as the ETH trading hours template. There are a few method signatures for AddDataSeries() that allow you to specify the tradingHoursName"

                        Added Data series with new trading hours
                        AddDataSeries(null, new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1 }, "CME US Index Futures ETH");
                        2. Main trading hours for Day timeframe is "CME US Index Futures RTH"

                        Given that:
                        Code:
                         else if (State == State.Configure)
                        {
                        AddDataSeries(null, new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1 }, "CME US Index Futures ETH");
                        } ​
                        
                        protected override void OnBarUpdate()
                        {
                        
                        startDateTime_RTH = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 08, 30, 0);
                        endDateTime_RTH = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 16, 00, 0);
                        endDateTime_RTH2 = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 16, 00, 0);​
                        
                        if(BarsInProgress == 0 && Time[0] == endDateTime_RTH)
                        {​
                        <CALCULATING DAY METRICS>
                        }
                        
                        if(BarsInProgress == 1){
                        if(LongCon1
                        && Time[0] == endDateTime_RTH2
                        )
                        {
                        EnterLong (0, IQ, RegularLong);
                        }​
                        What I got: trades exit at 17.01 (don't understand why) and entry is 00:00

                        Click image for larger version  Name:	image.png Views:	0 Size:	36.0 KB ID:	1282860
                        Click image for larger version  Name:	image.png Views:	0 Size:	13.9 KB ID:	1282861​​
                        "What I got: trades exit at 17.01 (don't understand why) and entry is 00:00"

                        In this case, please see the questions from my previous reply:
                        I previously mentioned using print statements to debug your strategy. What prints have you tried so far? What values print out if you print the values for startDateTime_RTH, endDateTime_RTH, and endDateTime_RTH2? Please print the time of the current bar when you call EnterLong() and ExitLong() as well to compare the time of the bar with the times used in your variables and conditions to get a deeper understanding of the strategy's behavior.

                        If you are unsure of the meaning of the output, please right-click the NinjaScript Output and save the output to a text file that may be attached to your reply.

                        You have advised what behavior you got (what times the entries/exits happened) but not what the prints are showing you for the time values used in your conditions. Please try adding prints and let me know the results from the output if you still do not understand why the exit was at 17:01 and the entry was 00:00.

                        Thank you for your time and patience.​

                        Comment


                          #13
                          Hi, Emily

                          I solved the problem, I just used one main data series with 1 minute time frame and then filtered it in the strategy, thank you!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by argusthome, 03-08-2026, 10:06 AM
                          0 responses
                          86 views
                          0 likes
                          Last Post argusthome  
                          Started by NabilKhattabi, 03-06-2026, 11:18 AM
                          0 responses
                          48 views
                          0 likes
                          Last Post NabilKhattabi  
                          Started by Deep42, 03-06-2026, 12:28 AM
                          0 responses
                          29 views
                          0 likes
                          Last Post Deep42
                          by Deep42
                           
                          Started by TheRealMorford, 03-05-2026, 06:15 PM
                          0 responses
                          32 views
                          0 likes
                          Last Post TheRealMorford  
                          Started by Mindset, 02-28-2026, 06:16 AM
                          0 responses
                          67 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Working...
                          X