Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enter at close.

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

    Enter at close.

    Hi:

    I would like to make a daily strategy that executes the entry orders at the close of the current bar.

    For example the next strategy:

    -Buy at close if the RSI is > 75.

    I would like to know if there is any way to know, for example 30 seconds before the end of the session, what is the RSI value and if the condition is true that send the order automatically. I need that the order will be executed at the end of the session, and not at the open of the session of the next day.

    I understand that I can close a position using ExitOnClose, but, could I open positions of any way?

    Thanks in advance.

    #2
    Hello Plaket,

    Thank you for your post.

    You can use a Time check condition to to check the time and then your condition for the RSI to submit the order.

    You could use the following if the close was at 5 PM:
    Code:
    if(ToTime(Time[0]) == 165930 && myRSIcondition)
    {
    //Place your order
    }
    For information on ToTime() please visit the following link: http://www.ninjatrader.com/support/h...nt7/totime.htm

    Please let me know if I may be of further assistance.

    Comment


      #3
      Thank you Patrick,

      I understand that this code only works if the property CalculateOnBarClose is false. Is it Ok?

      Thank you in advance.

      Comment


        #4
        Hello Plaket,

        Thank you for your response.

        That would be correct if we were using a data series such as 1 minute, as the 30 second close would never be reached.

        Comment


          #5
          Hi Patrick,

          Thank you for your response.

          What I need is a strategy that entry at market X seconds before the end of the current bar. I'm having problems with your code.

          For example, I have develop a trading strategy that buy or sell 30 seconds before the end of the current bar (if the conditions arre true). You can see this code in the screenshot called (code).

          How you can see the property CalculatedOnBarClose is false.

          The problem here is that using this code the entry order is executed at the first tick of the current bar and NOT 30 seconds before the end of the current bar.

          You can see in the screenshot called (problem) that using this code the entry order is executed at the first tick of the bar although the time is < that 153930.

          I would like to know if there is any way to open a position before the close of the current bar, and how I could do it.

          Thank you so much for your help.
          Attached Files

          Comment


            #6
            Hello Plaket,

            Thank you for your response.

            I have tested further on my end and found that the timestamp of the 1 minute bar is returned in the case of ToTime(Time[0]) or Time[0]. The 1 minute bar is timestamped with the close time of the bar so the moment the bar begins it falls into your conditions here.

            So we would need to add an additional data series to the code to check the time of let's say a 30 second bar instead of a 1 minute or the primary data series the strategy is applied to. For information on adding multiple time frames to your code and accessing the information please visit the following link: http://www.ninjatrader.com/support/h...nstruments.htm

            In this we actually need use the whole minute for the hora int and check the time of the 30 second close. Below is a simplified version of your strategy using this concept:
            Code:
                    #region Variables
                    private int hora = 092600; //Notice that this is a whole number and not the 30 second item as I originally detailed.
                    #endregion
            
                    
                    protected override void Initialize()
                    {
                       	CalculateOnBarClose = false;
            			Add(PeriodType.Second, 30);
                    }
            		
            		protected override void OnBarUpdate()
            		{
            			if(CurrentBars[0] < 5 || CurrentBars[1] < 5)
            				return;
            			
            			if(BarsInProgress == 1)
            			{
            				if(ToTime(Times[1][0]) == hora)
            					EnterLong(0, 1, "EnterLong");
            			}
            			if(BarsInProgress == 0)
            			{
            				if(BarsSinceEntry(0, "EnterLong", 1) >= 2)
            				{
            					ExitLong();
            				}
            			}
            		}
            Please let me know if you have any questions.

            Comment


              #7
              I understad you. Thank you so much for your help Patrick.

              Comment


                #8
                Hello Patrick:

                To understand how this code works, I'm trying to develop a simple trading strategy:

                I would like to buy at 1 minute before the end of the day (In this example the daily bar ends at 22:00:00) when the close of the daily bar will be higher than the close of the previous daily bar (at that moment: 21:59:00), and sell when the close of the daily bar will be lower than the close of the previous daily bar.

                To make this I have the next code:


                protected override void Initialize()
                {
                CalculateOnBarClose = false;
                Add(PeriodType.Minute, 1);
                ExitOnClose = false;
                }

                protected override void OnBarUpdate()
                {
                if( BarsInProgress == 1 && ToTime(Times[1][0]) == 215900)
                {
                if(Close[0] > Close[1])
                EnterLong();
                else
                EnterShort();
                }

                }

                The problem is that the buys and sells are not correct. You can see the screen shot of a backtest of this strategy on a FESX daily chart.

                Is there any way to deveolp a trading strategy with this simple rules?

                Thank you so much in advance.
                Attached Files

                Comment


                  #9
                  Hello,

                  Thank you for the snippet

                  In you're code it looks like you have the right condition setup for the long side, but you're referencing the 1 minute bars instead of the daily bars.

                  try using

                  if(Closes[0][0] > Closes[0][1])



                  Let me know if I can further help.
                  LanceNinjaTrader Customer Service

                  Comment


                    #10
                    Thank you for your answer Lancer.

                    What I need only is to buy at the close of the session when de close of this day is above the close of the previous day. The problem is that in NT the order to buy will be send at the open of the next bar and I need to open my position at the end of the day.

                    To solve this I'm trying to use a multi-time frame strategy with two bar objetcs (daily bar and 1 minute bar).

                    What I need to do is buy one minute before the end of the session when the condition will be true:

                    To make this I understand that I need to work with CalculateOnBarClose= false.

                    I have the next code but unfortunately does not work:

                    protected override void Initialize()
                    {
                    CalculateOnBarClose = false;
                    Add(PeriodType.Minute, 1);
                    ExitOnClose = false;
                    }

                    protected override void OnBarUpdate()
                    {
                    if(BarsInProgress == 1 && ToTime(Times[1][0]) == 215900)
                    {
                    if(Close[0] > Close[1])
                    EnterLong();
                    if(Close[0] < Close[1])
                    EnterShort();
                    }
                    }

                    I have tried your code (if(Closes[0][0] > Closes[0][1])) but it does not work neither.

                    is it possible make this of any way ?

                    Thank you so much in advance.
                    Last edited by Plaket; 08-23-2013, 08:15 AM.

                    Comment


                      #11
                      COBC = false will only work in real time trading.

                      In a backtest COBC will always be true.

                      In real time you could use if(Closes[0][0] > Closes[0][1])

                      In a backtest these closing prices would be from the day earlier.

                      If you wanted to do a backtest over this you would have to something like this

                      if(Close[0] > Closes[0][0]) //current 1 minute close > last closed daily bar
                      LanceNinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by Plaket View Post
                        Thank you for your answer Lancer.

                        What I need only is to buy at the close of the session when de close of this day is above the close of the previous day. The problem is that in NT the order to buy will be send at the open of the next bar and I need to open my position at the end of the day.

                        To solve this I'm trying to use a multi-time frame strategy with two bar objetcs (daily bar and 1 minute bar).

                        What I need to do is buy one minute before the end of the session when the condition will be true:

                        To make this I understand that I need to work with CalculateOnBarClose= false.

                        I have the next code but unfortunately does not work:

                        protected override void Initialize()
                        {
                        CalculateOnBarClose = false;
                        Add(PeriodType.Minute, 1);
                        ExitOnClose = false;
                        }

                        protected override void OnBarUpdate()
                        {
                        if(BarsInProgress == 1 && ToTime(Times[1][0]) == 215900)
                        {
                        if(Close[0] > Close[1])
                        EnterLong();
                        if(Close[0] < Close[1])
                        EnterShort();
                        }
                        }

                        I have tried your code (if(Closes[0][0] > Closes[0][1])) but it does not work neither.

                        is it possible make this of any way ?

                        Thank you so much in advance.
                        Your test condition should be comparing:
                        Current price on minute bars, Close[0] (as you are using the BarsInProgress filter), to the daily close on the preceding bar, Closes[0][1].
                        Code:
                        				if(Close[0] > Closes[0][1])
                        					EnterLong();
                        				if(Close[0] < Closes[0][1])
                        					EnterShort();

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        671 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        379 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        111 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        575 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        582 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X