Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Builder - Entering on current bar + Time limit

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

    Strategy Builder - Entering on current bar + Time limit

    Hello,

    Is there a way to set in the strategy builder to enter a position as the current ema is crossing the mid bollinger band rather than waiting for 1 bar to close?
    Also, is there a way to set a limit on entering trades since last position? For example, disallowing an entries if an exit position was taken in the last 30 min.

    #2
    Hello pattern,

    Thanks for your post.

    You could consider setting the Calculate mode of the script to OnPriceChange or OnEachTick instead of OnBarClose if you want the logic of the script to calculate intrabar.

    This could be set in the 'Default Properties' screen of the Strategy Builder of in the Strategy Properties UI when enabling the script.

    When the strategy is set to use Calculate.OnBarClose, logic will only process at the close of a bar.

    Calculate: https://ninjatrader.com/support/help.../calculate.htm

    Limiting when a strategy enters trades would require adding your own custom logic to the script.

    If you are running the strategy on say 1-minute bars, you could consider using BarsSinceExitExecution() (Misc folder > Bars since exit) to check if 30 1-minute bars (30 minutes) have passed since the last exit and then call your entry order method to place another order.

    BarsSinceExitExecution(): https://ninjatrader.com/support/help...texecution.htm
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon,

      The Calculate method, would that still solve the real time crossing of an ema over another? Because the lowest it lets you set it at is only 1 bar ago and if I put zero it will still still as 1 bar ago

      Comment


        #4
        Hello pattern,

        Thanks for your notes.

        Yes, you could detect a CrossAbove or CrossBelow condition on the currently forming bar before the bar closes by setting the Calculate mode to either Calculate.OnPriceChange or Calculate.OnEachTick and setting the CrossAbove/CrossBelow condition's lookback period to 1.

        This will allow the strategy to detect a crossover intrabar when the condition becomes true instead of only at the close of a bar.

        Calculate: https://ninjatrader.com/support/help.../calculate.htm
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Hello Brandon,

          You can only set the whole strategy to calculate on bar or tick, etc. and not by individual scenarios within the strategy?

          Comment


            #6
            Hello pattern,

            Thanks for your notes.

            Logic can be separated between Calculate.OnEachTick and Calculate.OnBarClose using IsFirstTickOfBar. Please note that a hosted script will inherit the Calculate mode of the script that hosts it. You can take the following approach to differentiate logic between OnBarClose and OnEachTick processing.

            Please see this reference sample which demonstrates a technique used for those who need to separate their logic to calculate some values on each tick and others only on the close of a bar. You will set your host script to Calculate.OnEachTick and use if(IsFirstTickOfBar) and place all code that needs to calculate once every bar within that condition check. Then place all code that you want to calculate OnEachTick outside of the IsFirstTickOfBar condition check.

            SampleEnterOnceExitEveryTick:
            https://ninjatrader.com/support/helpGuides/nt8/separating_logic_to_either_cal.htm
            Last edited by NinjaTrader_BrandonH; 12-26-2023, 03:16 PM.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Hi Brandon,

              Sorry , I don't understand and the reference link you gave is not working.

              Comment


                #8
                Hello pattern,

                Thanks for your notes.

                You will need to set your host script to Calculate.OnEachTick and use if(IsFirstTickOfBar) and place all code that needs to calculate once every bar within that condition check.

                Then, place all code that you want to calculate OnEachTick outside of the IsFirstTickOfBar condition check.

                The reference sample could be found below which demonstrates separating logic between Calculate.OnEachTick and Calculate.OnBarClose.

                I have also fixed the link in the previous post.

                SampleEnterOnceExitEveryTick:
                https://ninjatrader.com/support/helpGuides/nt8/separating_logic_to_either_cal.htm
                Last edited by NinjaTrader_BrandonH; 12-26-2023, 03:17 PM.
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  Originally posted by NinjaTrader_BrandonH View Post
                  Hello pattern,

                  Thanks for your post.

                  You could consider setting the Calculate mode of the script to OnPriceChange or OnEachTick instead of OnBarClose if you want the logic of the script to calculate intrabar.

                  This could be set in the 'Default Properties' screen of the Strategy Builder of in the Strategy Properties UI when enabling the script.

                  When the strategy is set to use Calculate.OnBarClose, logic will only process at the close of a bar.

                  Calculate: https://ninjatrader.com/support/help.../calculate.htm

                  Limiting when a strategy enters trades would require adding your own custom logic to the script.

                  If you are running the strategy on say 1-minute bars, you could consider using BarsSinceExitExecution() (Misc folder > Bars since exit) to check if 30 1-minute bars (30 minutes) have passed since the last exit and then call your entry order method to place another order.

                  BarsSinceExitExecution(): https://ninjatrader.com/support/help...texecution.htm
                  I'm having a bit of trouble doing this. Would you do this if you want the current bar to be more than 2 bars since the last exit?
                  Click image for larger version

Name:	image.png
Views:	326
Size:	34.0 KB
ID:	1283755

                  Comment


                    #10
                    Hello pattern,

                    Thanks for your notes.

                    This would not be the correct way to create a BarsSinceExitExecution() condition in the Strategy Builder.

                    BarsSinceExitExecution() would need to be compared to a numeric value (the number of bars to wait).

                    On the left side of the Condition Builder you would select Misc > 'Bars since exit' and set the 'Exit Executions Ago' to 0.

                    In the center you would select "Greater" or "Greater equal" depending on which comparison you want to use.

                    On the right side of the Condition Builder you would select Misc > Numeric value and set the value to 2.

                    See this help guide page for more information about BarsSinceExitExecution(): https://ninjatrader.com/support/help...texecution.htm

                    See this forum thread for more information about using BarsSinceExitExecution() in the Strategy Builder: https://forum.ninjatrader.com/forum/...43#post1166743
                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment


                      #11
                      Originally posted by NinjaTrader_BrandonH View Post
                      Hello pattern,

                      Thanks for your notes.

                      This would not be the correct way to create a BarsSinceExitExecution() condition in the Strategy Builder.

                      BarsSinceExitExecution() would need to be compared to a numeric value (the number of bars to wait).

                      On the left side of the Condition Builder you would select Misc > 'Bars since exit' and set the 'Exit Executions Ago' to 0.

                      In the center you would select "Greater" or "Greater equal" depending on which comparison you want to use.

                      On the right side of the Condition Builder you would select Misc > Numeric value and set the value to 2.

                      See this help guide page for more information about BarsSinceExitExecution(): https://ninjatrader.com/support/help...texecution.htm

                      See this forum thread for more information about using BarsSinceExitExecution() in the Strategy Builder: https://forum.ninjatrader.com/forum/...43#post1166743
                      I had done that originally but was not working

                      Click image for larger version

Name:	image.png
Views:	360
Size:	116.4 KB
ID:	1283765

                      Comment


                        #12
                        Hello pattern,

                        Thanks for your notes.

                        The BarsSinceExitExecution should be placed in a condition group (set to 'If Any') as noted on the forum thread linked in my previous email.

                        You would need to check if BarsSinceExitExecution() is greater than 2 and check if BarsSinceExitExecution() is equal to -1.

                        Please review this forum post which contains an image showing what this might look like in a Strategy Builder strategy.
                        Please elaborate on how to use barssinceexit in NT8 Strategy Builder based on the above case. Setting it in SB is very confusing, even after reading many threads it is still vague. Please provide lines that need to be added to the Sets. Thank you.


                        Further, if the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true or the order is being ignored for other reasons.

                        To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

                        In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

                        Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

                        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                        https://ninjatrader.com/support/foru...121#post791121​​
                        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Yesterday, 05:17 AM
                        0 responses
                        65 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        139 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        75 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        45 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        50 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X