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

Exit on close of entry bar

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

    Exit on close of entry bar

    For the following code for example

    HTML Code:
    if (Close[0] > Close[10])
                {
                    EnterLong(DefaultQuantity, "Long");
                }
    I would like to have positions closed at the close of the entry bar. How can I do that without another price series?

    Thanks in advance

    #2
    Hello ricko,

    Thanks for your post.

    Can you clarify what type of bars you are using? What time frame or bar size?

    Is this backtesting, market replay or live data?

    If market replay or live data, what is CalculateOnBarClose set to?
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,

      I am using only daily bars and this is for backtesting only.

      CalculateOnBarClose = true;

      Thanks for the quick reply.

      Comment


        #4
        Hello ricko,

        Thanks for your reply.

        When backtesting your strategy, the code will only execute once per bar at the end of the bar. If orders are placed then they will be entered on the next bar. I know of no way to provide for an exit at the end of the bar entered in backtesting without using another dataseries to provide more opportunities for the entries and exits to be executed as you wish.

        Even though you have indicated you don't want to add another dataseries, if you want to accomplish your backtesting goals it is what you would need to do and in that regard, here is a reference strategy showing how to do that: http://ninjatrader.com/support/forum...ead.php?t=6652
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          OK, I got that. How to apply an exit at the next open then?

          Comment


            #6
            Hello ricko,

            Thanks for your post.

            I may be misunderstanding the context of your question but using just daily bars in backtesting you would not be able to specify an operation on the opening of a bar. The code executes at the end of the bar and any order placed would be done on the next bar. All you could do would be to place the one order and then on the next bar have the code exit your order.

            For example:

            if (your entry conditions are true)
            {
            // enter long order here
            saveBarNumber = CurrentBar; // save the current bar number to be used later
            }

            if (Position.MarketPosition == MarketPosition.long && CurrentBar - saveBarNumber >= 1)
            {
            // exit long order here
            }
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Thanks. Last question on this. Can I accomplish the same with

              if (BarsSinceEntry("Long") > 0)
              {
              ExitLong("Long");
              }
              I tried and it appears it does not work.

              Comment


                #8
                Hello ricko,

                Thanks for your reply.

                It should work if you named your entry "Long".
                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by bmartz, Today, 09:30 AM
                2 responses
                11 views
                0 likes
                Last Post bltdavid  
                Started by f.saeidi, Today, 11:02 AM
                1 response
                3 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Started by geotrades1, Today, 10:02 AM
                4 responses
                12 views
                0 likes
                Last Post geotrades1  
                Started by rajendrasubedi2023, Today, 09:50 AM
                3 responses
                16 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Started by lorem, Today, 09:18 AM
                2 responses
                11 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Working...
                X