Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I close positions 5 bars after opened

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

    How do I close positions 5 bars after opened

    Hey, I'm currently switching over from thinkorswim to ninjatrader 8 and I need to know the code I need to add to ninjascript or how I add it to the strategy builder. But I would like to close all short positions 5 bars after the position was opened. (or 5 mins or would be even better If I could close the position on the next 5 min increment, so if short position was opened at 10:22 it will close at 10:25)
    Here is the current code
    // Set 1
    if (CrossAbove(ParabolicSAR1, Close, 1))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 2
    if (CrossAbove(ParabolicSAR1, Close, 1))
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
    }

    Also what does the 1 mean after the para crossabove the close?

    Thank you so much for your time and help!

    #2
    Welcome to the forums xNICK!

    NinjaScripts are event based and are calculated based on incoming tick data, the Calculate mode, and the data series they are applied against. If you would like to add time based events, a timer must be added. An example is linked below.

    SampleCustomEvents - https://ninjatrader.com/support/help...to_output_.htm

    If you would like to exit a position a few bars after you have entered, I would suggest using BarsSinceEntryExecution. This will return the number of bars since the entry has been filled. Entry Executions Ago represents previous entries. This is set under Misc in the Strategy Builder's Condition Builder.

    BarsSinceEntryExecution is documented here - https://ninjatrader.com/support/help...yexecution.htm

    CrossAbove's/CrossBelow's look back period parameter checks a certain number of bars back where the value was below/above before it had crossed. So a value of 1 would check if the cross occured between the last bar and the current bar. A value of 2 would check if the cross had occurred between 2 bars ago and the current bar. I may suggest running some simple tests where you can observe the behavior with different lookback values.

    CrossAbove documentation can be found here - https://ninjatrader.com/support/help...crossabove.htm

    I will also include some tips for setting up various conditions and actions in the Strategy Builder.

    Conditions - https://ninjatrader.com/support/help...on_builder.htm

    Actions - https://ninjatrader.com/support/help...us/actions.htm

    The information provided is publicly available.

    Please let me know if I can be of further assistance.

    Comment


      #3
      Perfect this code closes the positions after 5 bars have pasted! Thank you for your assistance! I still would rather to have them close on 5 minute increments but I guess I need to read up a bit more.
      // Set 1
      if (CrossAbove(ParabolicSAR1, Close, 1))
      {
      EnterShort(Convert.ToInt32(DefaultQuantity), "");
      }

      // Set 2
      if (BarsSinceEntryExecution()==5)
      {
      ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
      }

      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
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      48 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X