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 NullPointStrategies, Today, 05:17 AM
          0 responses
          52 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          130 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          70 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          43 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          47 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X