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

How to enter on the open and exit on the close of a bar

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

    How to enter on the open and exit on the close of a bar

    I'm doing some backtesting using some of the Period features for analysis purposes and want to cause trades to consist of one bar, with the entry on the open and the exit on the close. How do I do this?

    I tried:

    protected override void OnBarUpdate()
    {
    EnterLong(DefaultQuantity, "Long1");
    ExitLong("Exit Long1", "Long1");
    }

    but this had two problems:
    1. The exit occurred on the open of the next bar instead of on the close of the entry bar.
    2. NT skipped every other bar. It would not do an entry on bars 1, 2, 3, etc. It entered on bar 1, 3, etc.

    Thanks

    #2
    Hi brooksrimes,

    In backtesting, that behavior is expected as each bar is treated as a single instance.

    If you want to accomplish this on live data, you can use CalculateOnBarClose=false; in combination with FirstTickOfBar, or perhaps use a multi-series strategy.

    FirstTickOfBar - http://www.ninjatrader-support.com/H...FirstTickOfBar

    Multi-Series - http://www.ninjatrader-support.com/H...ameInstruments
    TimNinjaTrader Customer Service

    Comment


      #3
      <In backtesting, that behavior is expected as each bar is treated as a single instance.>

      "It's not a bug, it's a feature." - Microsoft

      I need this for backtesting, not real time.

      It's very basic stuff to be able to enter and exit on the same bar. It's easy in Tradestation or Amibroker. If NT can't do it, IMHO someone should be working on it.

      I believe in the early versions of Amibroker, they couldn't do this either but got requests for it, and created system variables for BuyPrice, SellPrice, ShortPrice & CoverPrice and gave users control.

      If I can't get the closing price in Ninja Trader, can you at least suggest code and settings to enter on the open of every bar and exit on the open of the next bar? In other words:

      Trade 1 - entry at bar 1 open price, exit at bar 2 open price
      Trade 2 - entry at bar 2 open price, exit at bar 3 open price
      etc.

      I tried setting "Entries Per Direction" from 1 to 2, but that didn't work. It made some trades last 1 bar and some last 2 bars.

      Thanks

      Comment


        #4
        Hi brooksrimes,

        To clarify, the reason this is done is so that NinjaTrader can simulate fills in a conservative way. The tick by tick sequence is not available when looking at a, for example, historical 5min bar. So there is no guarantee that certain prices were followed by others. For example, there's no way to know when the High of a bar was accomplished, in comparison to it's Low.

        Adding granularity to the strategy could be helpful in backtesting.
        The details of doing so are detailed here - http://www.ninjatrader.com/support/f...ead.php?t=6652

        You could add a smaller smaller series, and use limit orders for the Open[0] or Close[0] of the larger series.
        TimNinjaTrader Customer Service

        Comment


          #5
          I understand the desire to be conservative and agree completely with not allowing entry or exit on high or low, but the restrictions in place on open, close and entering and exiting on the same bar are unnecessarily restrictive, IMHO.

          Using a 2nd timeframe is not a good solution as it introduces unneeded complexity.

          It's really hard to believe that there is no easy way to code:

          Trade 1 - entry at bar 1 open price, exit at bar 2 open price
          Trade 2 - entry at bar 2 open price, exit at bar 3 open price
          etc.

          Could you confer with your colleagues?

          Originally posted by NinjaTrader_Tim View Post
          Hi brooksrimes,

          To clarify, the reason this is done is so that NinjaTrader can simulate fills in a conservative way. The tick by tick sequence is not available when looking at a, for example, historical 5min bar. So there is no guarantee that certain prices were followed by others. For example, there's no way to know when the High of a bar was accomplished, in comparison to it's Low.

          Adding granularity to the strategy could be helpful in backtesting.
          The details of doing so are detailed here - http://www.ninjatrader.com/support/f...ead.php?t=6652

          You could add a smaller smaller series, and use limit orders for the Open[0] or Close[0] of the larger series.

          Comment


            #6
            Hi brooksrimes,

            The method mentioned before is the supported way of achieving what you are seeking. Unfortunately, there is not another way to do this in both backtesting and live.
            TimNinjaTrader Customer Service

            Comment


              #7
              Originally posted by brooksrimes View Post
              .....
              It's very basic stuff to be able to enter and exit on the same bar. It's easy in Tradestation or Amibroker. If NT can't do it, IMHO someone should be working on it.

              I believe in the early versions of Amibroker, they couldn't do this either but got requests for it, and created system variables for BuyPrice, SellPrice, ShortPrice & CoverPrice and gave users control.

              If I can't get the closing price in Ninja Trader, can you at least suggest code and settings to enter on the open of every bar and exit on the open of the next bar? In other words:

              Trade 1 - entry at bar 1 open price, exit at bar 2 open price
              Trade 2 - entry at bar 2 open price, exit at bar 3 open price

              100% ! ! ! ! ! !
              So many years passed, requesting this feature, and somehow NT doesnt consider this, even to be user-control-able option. I cant understand why... even such ignorance, that dev.teams doesnt say the reason, and NT_TIM couldnt answer arguable to your replies, because you are 100% right.

              we say about OPEN and CLOSE, and they talk about HIGH/LOW...

              In all platforms this IS DOABLE (TradeStation, ThinkOrSwim, AmiBroker)... and I am surprised why the notorius NT doesnt even "ALLOW" that.
              Last edited by ttodua; 09-10-2017, 02:50 AM.

              Comment


                #8
                Originally posted by selnomeria View Post
                100% ! ! ! ! ! !
                So many years passed, requesting this feature, and somehow NT doesnt consider this, even to be user-control-able option. I cant understand why... even such ignorance, that dev.teams doesnt say the reason, and NT_TIM couldnt answer arguable to your replies, because you are 100% right.

                we say about OPEN and CLOSE, and they talk about HIGH/LOW...

                In all platforms this IS DOABLE (TradeStation, ThinkOrSwim, AmiBroker)... and I am surprised why the notorius NT doesnt even "ALLOW" that.
                Hello selnomeria,

                Thank you for your post.

                This behavior could be performed when using Tick Replay in NinjaTrader 8 or even just replaying the market through Playback/Market Replay in NinjaTrader 7 or NinjaTrader 8.

                If you are referring to the fill type and how it uses highs and lows, then in NinjaTrader 7 it was technically possible to create your own Fill Type based on the Documents\NinjaTrader 7\bin\Custom\Type\@DefaultFillType.cs and @LiberalFillType.cs files.
                However, this is not possible in NinjaTrader 8.

                Please let me know if you have any questions.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Irukandji, Yesterday, 02:53 AM
                2 responses
                17 views
                0 likes
                Last Post Irukandji  
                Started by adeelshahzad, Today, 03:54 AM
                0 responses
                3 views
                0 likes
                Last Post adeelshahzad  
                Started by CortexZenUSA, Today, 12:53 AM
                0 responses
                3 views
                0 likes
                Last Post CortexZenUSA  
                Started by CortexZenUSA, Today, 12:46 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by usazencortex, Today, 12:43 AM
                0 responses
                5 views
                0 likes
                Last Post usazencortex  
                Working...
                X