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

BarsSinceEntryExecution() for Exit Trigger

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

    BarsSinceEntryExecution() for Exit Trigger

    Hi,
    I am working on a strategy where I want my positions to close after certain bars elapsed since entry. Works fine until I set the entries per direction to a number greater than 1. In this case the positions are closed out together after the last entry was X bars ago. Is there a method I can use in Strategy Builder that handles the entries as unique ones and exits each separately after they were entered X bars ago? Tried few methods but with no success so far.
    Thanks!


    #2
    Hello Balage0922,

    Thank you for your post.

    I'd say the best way to deal with this would be to use Signal Names with your orders. There is an overload for BarsSinceEntryExecution() that takes a signal name and will check how many bars it's been since that particular entry executed. I'd set up a couple of these for your different orders and then if it's been more than X bars close that specific entry by using it's Signal Name as the FromEntrySignal of the exit. Let's assume we have 2 long entries called myEntry1 and myEntry2:

    Code:
    if (BarsSinceEntryExecution("myEntry1") > 5)
    {
        ExitLong("myEntry1");
    }
    if (BarsSinceEntryExecution("myEntry2") > 5)
    {
        ExitLong("myEntry2");
    }
    Here's a link to our help guide where it mentions this overload:



    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Thanks Kate, will check this!

      Comment


        #4
        Kate, I filled out the unique signal names and changed entries per direction to 2/unique entries. The results are the same, did I miss something?
        Attached Files

        Comment


          #5
          Hello Balage0922,

          Thank you for your post.

          You need to have different signal names for each of the two entries, and two exits, one for each entry. Have you named the entries the same thing?

          Thanks in advance; I look forward to assisting you further.
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            Hi Kate,
            yes, I named them Nr.1 & Nr.2, forgot to mention.

            Comment


              #7
              Hello Balage0922,

              Thank you for your reply.

              Could you provide a code snippet showing the conditions for your exits and the exits themselves?

              Thanks in advance; I look forward to assisting you further.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Sure, here it comes:
                // Set 1
                if (CrossBelow(PCRISlow1.SlowFisherTransform, PCRI_Trigger, 1))
                {
                EnterLong(Convert.ToInt32(DefaultQuantity), @"Nr.1");
                }

                // Set 2
                if (BarsSinceEntryExecution(0, @"Nr.1", 0) >= Exit_Bars)
                {
                ExitLong(Convert.ToInt32(DefaultQuantity), @"", @"Nr.1");
                }

                // Set 3
                if (CrossBelow(PCRISlow1.SlowFisherTransform, PCRI_Trigger, 1))
                {
                EnterLong(Convert.ToInt32(DefaultQuantity), @"Nr.2");
                }

                // Set 4
                if (BarsSinceEntryExecution(0, @"Nr.2", 0) >= Exit_Bars)
                {
                ExitLong(Convert.ToInt32(DefaultQuantity), @"", @"Nr.2");
                }

                }

                Comment


                  #9
                  Hello Balage0922,

                  Thank you for your reply.

                  I had to change this up a little to test since I don't know what your PCRISlow indicator is or your value for PCRI_Trigger, but in my testing this is working as expected - it's just a little hard to see from your screenshot that they actually are exiting on different bars.

                  Here's what I got in my testing:

                  Click image for larger version

Name:	2020-02-10_1208.png
Views:	263
Size:	58.5 KB
ID:	1086834​​​​​​​

                  Thanks in advance; I look forward to assisting you further.
                  Kate W.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks Kate for checking, what are the order handling settings you used?

                    Comment


                      #11
                      Hello Balage0922,

                      Thank you for your reply.

                      I used this:

                      EntriesPerDirection = 1;
                      EntryHandling = EntryHandling.UniqueEntries;

                      But it also works the same with this:

                      EntriesPerDirection = 2;
                      EntryHandling = EntryHandling.AllEntries;

                      Please let us know if we may be of further assistance to you.
                      Kate W.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by pechtri, 06-22-2023, 02:31 AM
                      9 responses
                      122 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by frankthearm, 04-18-2024, 09:08 AM
                      16 responses
                      66 views
                      0 likes
                      Last Post NinjaTrader_Clayton  
                      Started by habeebft, Today, 01:18 PM
                      1 response
                      5 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by benmarkal, Today, 12:52 PM
                      2 responses
                      14 views
                      0 likes
                      Last Post benmarkal  
                      Started by f.saeidi, Today, 01:38 PM
                      1 response
                      8 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Working...
                      X