Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit trade after bar closes

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

    Exit trade after bar closes

    Hello, I tryed a lot of things to do this works but I can't.
    I want to know how exit the trade after the bar closes. I tryed with :

    if((Position.MarketPosition == MarketPosition.Short) && (BarsSinceEntryExecution() == 0))
    {
    ExitShort();
    }

    And that do this:


    So I tryed with:
    if((Position.MarketPosition == MarketPosition.Short) && (BarsSinceEntryExecution() == 1))
    {
    ExitShort();
    }

    And that do this:


    The thing that I want to do is this:


    #2
    Hello ematias,

    Was this in realtime that you had tested the condition? The code you have should be able to achieve your goal. You can use a Print in your specific test to see what is happening, for example:

    Print(Time[0] + " " + BarsSinceEntryExecution());

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello ematias,

      Was this in realtime that you had tested the condition? The code you have should be able to achieve your goal. You can use a Print in your specific test to see what is happening, for example:

      Print(Time[0] + " " + BarsSinceEntryExecution());
      Hello Jesse, first of all, thanks for your response.
      It was tested in playback but It's curious that in Market Analyzer that's works fine, so... I don't know what of those are ok.

      Comment


        #4
        Hello ematias,

        That may relate to the Calculate setting you used, if you used OnEachTick or PriceChange that could result in the order on the same bar. Please check that you are using OnBarClose. Alternatively you would need to add that print to see what the values are at the time of the entry and the bars following that to get a better idea of the problem.

        Comment


          #5
          Hi ematias,

          I recommend using this code:
          if (IsFirstTickOfBar) {

          Obviously, that will return true upon the first tick of the next bar. If so, then exit your position
          if (IsFirstTickOfBar) {
          if((Position.MarketPosition == MarketPosition.Short) {
          ExitShort();



          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          45 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          21 views
          0 likes
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          31 views
          1 like
          Last Post CaptainJack  
          Started by CarlTrading, 03-30-2026, 11:51 AM
          0 responses
          50 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 03-30-2026, 11:48 AM
          0 responses
          42 views
          0 likes
          Last Post CarlTrading  
          Working...
          X