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 Limit a Strategy Trade Entry to Only ONE Trade?

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

    How Do I Limit a Strategy Trade Entry to Only ONE Trade?

    I have created a strategy that includes entries that are triggered when my Strategy criteria are met. However, once my profit target is met and that position is closed, if the entry qualifications are met with the next opening candle, another entry is triggered. How can I limit trade entries to only the FIRST one and not have subsequent trades entered. Thank you.

    #2
    Hello Lightheart,

    Thank you for your note.

    This would need to be controlled with logic.

    Are you wanting to reset this on each new session or only allow one entry ever unless the strategy is disabled and re-enabled?

    If you are wanting to only allow one entry unless the strategy is disabled/re-enabled you can compare BarsSinceEntryExecution() to be greater than -1.

    Below is a public link to the help guide on BarsSinceEntryExecution()


    You could also use a bool.
    Below is a public link to a 3rd party educational site on boolean.
    Test the bool type, which holds true or false. A bool occupies 1 byte of memory.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      ". . . Are you wanting to reset this on each new session or only allow one entry ever unless the strategy is disabled and re-enabled?"

      I would like to be able to disable and re-enable the strategy and let it trigger another entry if I feel conditions are right to do so.

      Thanks,

      Comment


        #4
        As I do not know how to code, and am only able to use the Strategy Builder, I am dead in the water with your answer. I don't know how to incorporate the BarsSinceEntryExecution command.

        Thanks.

        Comment


          #5
          Hello Lightheart,

          In the Strategy Builder the Condition and Actions page is where a condition would be added.

          In the Condition Builder this method can be found in Misc > 'Bars since entry'. This would be compared with Misc > Numerical value.

          Below is a link to the Strategy Builder 301 course.


          And a link to a forum post with helpful information about getting started with NinjaScript.


          You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello Lightheart,

            Thank you for your note.

            This would need to be controlled with logic.

            Are you wanting to reset this on each new session or only allow one entry ever unless the strategy is disabled and re-enabled?

            If you are wanting to only allow one entry unless the strategy is disabled/re-enabled you can compare BarsSinceEntryExecution() to be greater than -1.

            Below is a public link to the help guide on BarsSinceEntryExecution()


            You could also use a bool.
            Below is a public link to a 3rd party educational site on boolean.
            Test the bool type, which holds true or false. A bool occupies 1 byte of memory.
            Hello Chelsea,

            Did you mean> ... compare BarsSinceEntryExecution() to be equal to -1 ?


            Code:
            protected override void OnBarUpdate()
            {
                if (CurrentBar < BarsRequiredToTrade)
                    return;
            
                // Only enter if at least 10 bars has passed since our last entry
                if ((BarsSinceEntryExecution() > 10 || BarsSince EntryExecution() [B]== [/B]-1) && CrossAbove(SMA(10), SMA(20), 1))
                    EnterLong();
            
            }

            Comment


              #7
              Hello Cormick,

              Yes, it would be compared to be equal to -1 to place the entry in the Strategy Builder.
              if (BarsSinceEntryExecution() == -1)
              EnterLong();

              (In an unlocked script it would be greater than -1 if calling return.)
              if (BarsSinceEntryExecution() > -1)
              return;
              EnterLong();


              You could also use your own bool variable (I'll call ActionTriggeredOnce for this example).

              In the first condition set compare Misc -> First bar of session, equals, Misc -> True.
              For the action set the ActionTriggeredOnce to false.

              In the condition set to place the order require the bool to be false and add the conditions for your entry.
              For the action submit the entry.
              Then also add a second action to set ActionTriggeredOnce to true.

              Below is a link to an example that uses bools.
              Hi, To improve a strategy, I would like the condition to enter a trade to be triggered only after a second crossing happens. Meaning, for instance we have a sthocastics crossing, but the strategy would only trigger when a crossing between 2 emas happen. Would the looking back N bars work? Can it be done within the builder
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Awesome thanks!

                Just a quesstion that popped:
                Why no curly brackets are required to wrap around
                Code:
                return;
                and
                Code:
                 EnterLong();
                in :
                Code:
                if (BarsSinceEntryExecution() > -1)
                return;
                EnterLong();
                While curly brackets are required for ExitLongStopMarket( ...) for example in :
                Code:
                // Set 2
                if ((Position.MarketPosition == MarketPosition.Long)
                && (StopLossModeLong == 0))
                [B]{[/B]
                ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), (Position.AveragePrice + (-10 * TickSize)) , @"LONG STOP", "");
                [B]}[/B]

                Comment


                  #9
                  Hello Cormick,

                  When the curly braces are not used, it is a single line statement (also called single line block).
                  Whatever the next statement is, is the action block for the conditional.

                  Below is a link to an educational site found through a google search of 'c# single line statement'.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Ok thanks for the 'single line statement' reference, I didn't know that was what to look for.

                    Following this example:
                    https://stackoverflow.com/a/65676992/10789707

                    Code:
                    //this is correct format.
                    if(true)
                        doX();
                    else
                        doY();
                    
                    //this is not.
                    if(true)
                        doX();
                        Console.WriteLine(); <- this is not part of if statement.
                    else
                        doY();
                    here Console.WriteLine() is considered as an independent statement. Hence 'if' for the else is lost.

                    When you put curly braces it considers as a single block -

                    Code:
                    //this is correct format.
                    
                    if(true)
                    {
                        doX();
                        Console.WriteLine();
                    }
                    else
                        doY();


                    To make sure I understand it right:

                    Are the 2 codes below the same?

                    Code:
                    if (BarsSinceEntryExecution() > -1)
                         return;
                         EnterLong();
                    Code:
                    if (BarsSinceEntryExecution() > -1)
                    {
                        return;
                    }
                    else
                    {
                        EnterLong();
                    }
                    Because I interpret/understand the 1st code as the 2nd.

                    Meaning, if the condition 'there is/are previous entry-ies prior to current entry' is TRUE, then don't execute any actions that follow.
                    Otherwise, (if there is/are no previous entry-ies prior to current entry), then Enter long.

                    If the 2 codes above are equivalent, can we use the 2nd code instead of the 1st?

                    The second code is more consistent (works in every case (single and multiple line statements)), while the 1st is shorter but requires to know about 'single line statement'.

                    Thanks a lot!

                    Comment


                      #11
                      Hi Cormick, thanks for your reply.

                      Yes, those two examples you posted are equivalent. Feel free to use any style you feel most comfortable with. This is C# and as long as it's compiling and the logic works you can use whatever style you would like.

                      Best regards,
                      -ChrisL
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi Chris,

                        Thank you for your reply.

                        Great help thanks.

                        Be well!

                        Comment


                          #13
                          I tried to add the rule for the strategy to avoid entering the deal one more time on the same candle. But this made the strategy to stop entering the deals at all. Please advise where I made a mistake?

                          Here is a part of the code where as I understood these lines have to be:


                          Code:
                          protected override void OnBarUpdate()
                          {
                          
                          if (BarsInProgress != 0)
                          return;
                          
                          if (CurrentBars[0] < 1)
                          return;
                          
                          
                          // Set 1
                          
                          if (Delta11.Buys[0] > Zero+500)
                          if (BarsSinceEntryExecution() > -1)
                          {
                          return;
                          }
                          else​
                          {
                          EnterLong(Convert.ToInt32(DefaultQuantity), "1");
                          }
                          
                          // Set 2
                          
                          if (Delta11.Sells[0] < Zero-500)
                          if (BarsSinceEntryExecution() > -1)
                          {
                          return;
                          }
                          else​
                          {
                          EnterShort(Convert.ToInt32(DefaultQuantity), "1");
                          }
                          
                          }​
                          Last edited by Assalt; 12-18-2023, 10:56 AM.

                          Comment


                            #14
                            Hello Assalt,

                            Are you certain the condition is evaluating as true?

                            if (Delta11.Buys[0] > Zero+500)

                            Is Delta11.Buys[0] greater than Zero+500?

                            What are the values of these?

                            Use prints to understand behavior. Print the time of the bar and all values in the condition.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Hello Chelsea,

                              If I exclude from the code this part

                              Code:
                              if (BarsSinceEntryExecution() > -1)
                              {
                              return;
                              }
                              else
                              Strategy buys when indicator Delta11 hits 500 and sells when hits -500.

                              After I add the above part of the code is stops entering the market.​

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Mizzouman1, Today, 07:35 AM
                              3 responses
                              17 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by RubenCazorla, Today, 09:07 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by i019945nj, 12-14-2023, 06:41 AM
                              7 responses
                              82 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by timmbbo, 07-05-2023, 10:21 PM
                              4 responses
                              158 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by tkaboris, Today, 08:01 AM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X