Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trade open price of session based on relation to previous close

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

    Trade open price of session based on relation to previous close

    Hi,

    I am looking at a database of 1min bars. I want to trade the opening price of the session based on a relation between this opening price and the prior session's closing price (ignoring slippage related to catching an opening price etc). As a straightforward example, if the opening price was above the closing price I would buy. Could I do this in the strategy analyzer by comparing Close[0] to Open[-1]? Obviously I can't look forward in time if running the trade for real.

    Thanks,
    darmbk.

    #2
    Hello darmbk,

    Thanks for your post.

    On the first tick or bar close you can compare the open price Open[0] to the last session close using PriorDayOHLC. If you add intra-bar granularity to the script you can have this trigger on the first tick.

    For example:

    if (Open[0] > PriorDayOHLC().PriorClose[0])
    {
    // execute code
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I also want to condition an action (EnterLongLimit) during a bar based on the relation of the bar's open to the previous data.

      Can you elaborate on how to "add intra-bar granularity to the script" for backtest purposes?

      Comment


        #4
        Hello dbooksta,

        Where I have written //execute code this is the code that will be triggered when the condition is true. If want an order placed, then add an order call in place of that line.


        To add a tick interval to your chart use the Add() method.
        For example, add to the Initialize():
        Add(PeriodType.Tick, 1);

        And in OnBarUpdate():
        if (BarsInProgress == 1)
        {
        if (Postion.MarketPosition == MarketPosition.Flat && SMA(19)[0] > Closes[1][0])
        {
        EnterLongLimit(0, true, 1, Highs[0][0], "long1");
        }
        }
        This will use the second data series, the tick series to check to see if the SMA[0] is higher than Close[0]. This means it will be checking every tick to see if the SMA is higher than the close of the tick series. Then it places the order using the primary data series (45 min or whatever the chart is). This means that the limit order will fill once the 45 minute series has the close price hit the limit price. This will mean it will be at least until the next 45 minute bar before the order is placed.

        Below is a link to an official reference sample that demonstrates how to add intra-bar granularity.
        You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


        As well as a link to the help guide on the Add() method.


        A link to the help guide on BarsInProgress.


        And a link to the help guide on Multi-Time Frame & Instruments. Please see the section 'How Bar Data is Referenced', and 'Accessing the Price Data in a Multi-Bars NinjaScript'.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Is it possible to do this without tick data, since that is too large and slow for the backtests we want to try?

          E.g., suppose our smallest PeriodType is 1 minute, and we want to have an EnterLongLimit run during Bar[0] based on Open[0]. Can we reference Open[-1]?

          Or can we retroactively give ourself a position with an entry based on the OHLC[0], to which we can subsequently apply the Managed Order Methods like SetProfitTarget and Set StopLoss?

          Comment


            #6
            Hello dbooksta,

            It is not possible to detect the first tick of a bar in backtest without introducing tick data.

            From the help guide:
            "When indicators or strategies are running on historical data, OnBarUpdate() is only called on the close of each historical bar even if this property is set to false. This is due to the fact that with a historical data set, only the OHLCVT of the bar is known and not each tick that made up the bar."

            http://www.ninjatrader.com/support/h...onbarclose.htm

            Below is a link that describes the differences between backtest and real-time.

            http://www.ninjatrader.com/support/h...ime_vs_bac.htm


            Yes, adding tick data does cause the processing to take longer, however, this is required for what you are wanting to accomplish.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Then is there any means of retroactively giving ourselves an entry or position -- i.e., at Time[0] to add an entry or exit at some specified price at a Time prior to Time[0]?

              Comment


                #8
                Hello dbooksta,

                No it would not be possible to add in an execution after the that time had passed.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  What you say in the following is not true: We know for certain that the first tick of a bar is the Open of the bar. Therefore there should be a way to act on the open without recourse to tick data.

                  Originally posted by NinjaTrader_ChelseaB View Post

                  It is not possible to detect the first tick of a bar in backtest without introducing tick data.

                  From the help guide:
                  "When indicators or strategies are running on historical data, OnBarUpdate() is only called on the close of each historical bar even if this property is set to false. This is due to the fact that with a historical data set, only the OHLCVT of the bar is known and not each tick that made up the bar."

                  Comment


                    #10
                    Hello dbooksta,

                    That is the price of the first tick, yes. That is not detecting the first tick and triggering an action.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    633 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    364 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    105 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    567 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    568 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X