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 after n bars

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

    Exit after n bars

    Hi
    I want to exit trades which have been open for >9 bars.
    (Therefore, no trade will be longer than 10 bars)
    E.g. If trade has been open >9 bars since signal, exit position.

    What code do I use to get the result?
    Where do I put the code? Under Initialize() or OnBarUpdate() ?

    Would also be grateful if you showed how I enter a trade if 3 bars have elapsed after a signal has been made.
    E.g. If it has been 5 bars since Close > SMA, enter Long.
    And If it has been 5 bars since Close < SMA, enter Short.
    Last edited by asi001; 05-03-2017, 01:17 PM.

    #2
    Thank you for your question asi001 . One of my colleagues will be available to answer your question shortly. To help us give you better advice, if possible please take a screen shot of your attempt so far and attach it to a reply.

    To send a screenshot with Windows 7 or newer I would recommend using Window's Snipping Tool.
    Click here for instructions
    Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.
    Click here for detailed instruction
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi Jessica,
      I am not a programmer but I feel this is an easy one so a programmer wouldn't mind doing it for me.

      Comment


        #4
        Hello asi001,

        Thank you for your note.

        To exit your position after a specified number of bars, I suggest using the BarsSinceEntry() method which allows you to return the number of bars that have elapsed since the last specified entry. You will want to utilize BarsSinceEntry() from within the OnBarUpdate() method which is called whenever a bar is updated. In contrast, the Initialize() method is called only once when starting a strategy.

        I've provided a link to the BarsSinceEntry() page on our Help Guide for additional information on how to work with this method in your programming logic:
        http://ninjatrader.com/support/helpG...sinceentry.htm

        Please let us know if we may provide further assistance.

        Comment


          #5
          Thank you Chris,

          how about how I enter a trade if 5 bars have elapsed after a signal has been made.
          E.g. If it has been 5 bars since Close > SMA, enter Long.
          And If it has been 5 bars since Close < SMA, enter Short.

          Comment


            #6
            Hello asi001,

            Thank you for the reply.

            For your example of entering a position after 5 bars have elapsed since entry, you would want to utilize the following code within the OnBarUpdate() method:

            if (BarsSinceEntry() > 5 && Close[0] > SMA(14, 3)[0])
            EnterLong();


            if (BarsSinceEntry() > 5 && Close[0] < SMA(14, 3)[0])
            EnterShort();

            This is just a code sample, however, and is intended for educational purposes. You would still need additional custom programming to be written into OnBarUpdate() for placing your entry orders for example.

            Please let me know if you have any additional questions.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CortexZenUSA, Today, 12:53 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Started by CortexZenUSA, Today, 12:46 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Started by usazencortex, Today, 12:43 AM
            0 responses
            5 views
            0 likes
            Last Post usazencortex  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            168 responses
            2,265 views
            0 likes
            Last Post sidlercom80  
            Started by Barry Milan, Yesterday, 10:35 PM
            3 responses
            11 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Working...
            X