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 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.
    JimNinjaTrader Customer Service

    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 cmtjoancolmenero, Yesterday, 03:58 PM
      5 responses
      27 views
      0 likes
      Last Post cmtjoancolmenero  
      Started by gbourque, Today, 06:39 AM
      2 responses
      14 views
      0 likes
      Last Post gbourque  
      Started by rexsole, Today, 08:39 AM
      0 responses
      4 views
      0 likes
      Last Post rexsole
      by rexsole
       
      Started by trilliantrader, Yesterday, 03:01 PM
      3 responses
      31 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by Brevo, Today, 01:45 AM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X