Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enter trade 1 bar after EMA crossover

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

    Enter trade 1 bar after EMA crossover

    Hi, I am trying to finalize the settings in Strategy Builder for my automated strategy and i would like to have some help for creating a setting for entering a trade the next bar after the EMA 8 and EMA 12 has crossed.

    I would also like to have some help on setting for my trade exit which is the next 2 EMA cross.

    See my image...

    Many thanks for your help.
    Attached Files

    #2
    In the Strategy Builder -> go inside a Set -> Conditions panel -> [ If All ] -> add -> (1) Position.MarketPosition = MarketPosition.Flat -> add -> (2) Dialog Left: chose EMA (set period 8) -> Dialog Center: chose "cross above" ->Dialog Right: EMA (set period 12)
    Actions panel -> EnterLong.

    In the Strategy Builder -> go inside a Set -> Conditions panel -> [ If All ] -> add -> (1) Position.MarketPosition = MarketPosition.Long -> add ->​ (2) Dialog Left: chose EMA (set period 8) -> Dialog Center: chose "cross below" ->Dialog Right: EMA (set period 12)
    Actions panel -> ExitLong.
    Last edited by KonstantinosNT; 02-09-2023, 01:51 AM.

    Comment


      #3
      Hello McGyver270,

      Thanks for your post.

      KonstantinosNT is correct. You could follow the steps they shared to set up the conditions and actions that you described.

      See the help guide documentation below for more information.

      Conditions: https://ninjatrader.com/support/help...on_builder.htm
      Actions: https://ninjatrader.com/support/help...t8/actions.htm

      Here is a link to our publicly available training video, 'Strategy Builder 301', which you might find helpful.

      Strategy Builder 301 — https://www.youtube.com/watch?v=_KQF2Sv27oE&t=13s

      Please let me know if I may assist further.
      <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


        #4
        Thank you both of you for the follow up. Seem to work just fine.

        Got another quick question, if i wanted to add a time filter to my strategy: ex: 9:30am to 15:30pm with theses conditions:

        Times[Default input][0].TimeOfDay >= new TimeSpan(9, 30, 0)
        Times[Default input][0].TimeOfDay <= new TimeSpan(15, 30, 0)

        Should i create another set just for this ? if not where do i put in theses codes?

        Thank you.

        Comment


          #5
          Hello McGyver270,

          Thanks for your note.

          If you would like to use the Time Filter condition for your Entry method, you would put the Time Filter in the same Set that you are calling your Entry method in. Same goes for if you want to use the Time Filter for your Exit methods.

          Or, something you could consider is creating a bool variable (initially set to false) on the Inputs and Variables screen of the Strategy Builder. In Set 1, create your Time Filter and flip the bool to true in the Actions section.

          In Set 2, create your Entry order conditions and include a condition checking if the bool is true and call your Entry method.

          In Set 3, create your Exit method conditions and include a condition checking if the bool is true, and call your Exit method.

          In Set 4, create another Time Filter checking if the Time is outside of your first Time Filter and set the bool back to false.

          By doing this, the Entry and Exit orders will only occur when the bool is true and the bool will only be true when the first Time Filter is within the specified times. If the time is outside of the first Time Filter the bool will be false and trades won't be placed.

          Inputs and Variables: https://ninjatrader.com/support/help...ariablesScreen
          Time Filter: https://ninjatrader.com/support/help...ateTimeFilters
          Actions: https://ninjatrader.com/support/help...t8/actions.htm

          Please let me know if I may assist further.
          <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


            #6
            Hi Brandon,

            Thank you for being so helpful. Appreciate it !

            Hugues

            Comment


              #7
              Hello - I need to make some modifications to the set up. Can i use Strategy builder to construct a set up like this :

              Conditions : If EMA 8 cross above EMA 12

              Actions : Enter LONG at the open of the next candle(the candle just after the candle that has the EMA's cross). And exit at the open of the candle that follow the candle that has EMA 8 cross below EMA 12.

              Conditions : If EMA 8 cross below EMA 12

              Actions :​ Enter SHORT at the open of the next candle(the candle just after the candle that has EMA's cross). And exit at the open of the candle that follow the candle that has EMA 8 cross above EMA 12

              See the attached image for more clarity.

              Many thanks!
              Hugues
              Click image for larger version  Name:	Trade setup.jpg Views:	0 Size:	125.0 KB ID:	1236616
              Last edited by McGyver270; 02-23-2023, 08:59 AM.

              Comment


                #8
                Hello McGyver270,

                Thanks for your notes.

                You could consider creating a condition that checks if the EMA(8) indicator (Indicators folder > EMA > Period > 8) crosses above the EMA(12) indicator (Indicators folder > EMA > Period > 12) with a 'Look back period' of 1 and call your EnterLong() order (Order management folder > Enter long position).

                This would mean the strategy checks if the EMA(8) indicator crosses above the EMA(12) indicator 1 bar ago and then submits the long entry order.

                A similar condition would be created that checks if EMA(8) crosses below EMA(12) with a 'Look back period' of 1 and call your EnterShort() order (Order management > Enter short position).

                This means the strategy checks if the EMA(8) crosses below EMA(12) 1 bar ago and submits the short entry order.

                When calling an order in the opposite direction, Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position. This information can be seen on the Managed Approach help guide page linked below.

                Managed Approach: https://ninjatrader.com/support/help...d_approach.htm

                Please note that if you use Calculate mode OnBarClose, this means that the strategy code executes once at the end of each bar and will perform calculations on completed bars. If your strategy determines to enter an order, the order would be placed on the next bar. In this mode, your strategy will operate the same historically as it does when connected to real-time (or replay) data.

                See the help guide documentation below for more information.

                How to create a crossover condition:https://ninjatrader.com/support/help...sOverCondition
                Calculate: https://ninjatrader.com/support/help.../calculate.htm

                Please let me know if you have further questions.
                <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
                  Many thanks Brandon. Your help is much appreciated !

                  Do you happens to know why after one trade, it stopped working. My strategy is supposed to give me 2 trades today but after 1 trade, when my strategy exit my position, i got an ''Exit on session close''.

                  I have set a time filter condition for my strategy which is 9:30 to 15:30(all my position must be closed at 15:30).
                  Times[Default input][0].TimeOfDay >= new TimeSpan(9, 30, 0)
                  Times[Default input][0].TimeOfDay <= new TimeSpan(15, 30, 0)​

                  They are placed in the condition of set 1 and set 2.

                  Thank you.
                  Hugues

                  Comment


                    #10
                    Hello Hugues,

                    Exit on session close automatically exits the position and cancels orders. You can enable or disable this.


                    NinjaTrader Community, A common inquiry is that the Exit on close didn't work in a NinjaScript Strategy because there is a position after the exit on close should have occurred. When viewing the log we often find that the Exit on close does indeed exit the position shortly before the end of the session (based on the Exit on


                    By 2 trades, do you mean two entry orders in the same direction scaling in to the position?
                    Do you mean an entry then an exit then another entry after the first exit?

                    To allow for multiple entries into the same position, set EntriesPerDirection to 2.


                    If the second entry is after the position has closed, this is allowed. The logic may be preventing this.
                    Debug using prints to understand why the condition is not evaluating as true a second time.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Chelsea,

                      I mean an entry then another entry after the first exit.

                      My time filter doesn't work correctly. The second trade should have closed at 15:30 as per my instruction. Not 17:00. See the attached image. Thank you ! Hugues

                      SET UP 1
                      Click image for larger version

Name:	TimeFilter.jpg
Views:	655
Size:	75.9 KB
ID:	1236856


                      SET UP 2
                      Click image for larger version

Name:	TimeFilter2.jpg
Views:	644
Size:	77.8 KB
ID:	1236855


                      Click image for larger version

Name:	Trade setup2.jpg
Views:	666
Size:	142.1 KB
ID:	1236853
                      Attached Files

                      Comment


                        #12
                        Hello McGyver270,

                        Your screenshots do not show any exits. There are two entries, which would reverse the position, not exit it. Are you asking why the EnterShort() did not get submitted and reverse the position?

                        Likely, the other part of the condition did not evaluate as true, and there as no cross below.

                        Have you debugged by adding prints in a blank condition set to print the current and previous bar values for the series used in your cross above?

                        If not, follow the steps in the video 'Debugging using prints with the Strategy Builder' linked in the forum post below and provide the output from the prints.



                        I cannot see the times you have selected. Click ok and just take a screenshot of the conditions page without the condition builder open. And ensure the window is wide enough to see the entire condition.​
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Chelsea,

                          I'm just asking why my second trade(Long) has not exited at 15:30 as per my TIME FILTER. The screenshot show that my second trade has exited at session close(17:00) which i don't understand why as there are Time Filter in place. All my trade should take place between 9:30am to 15:30pm. There are no trade that should be triggered before 9:30 and all my trade should be closed at 15:30(if there are any trade open).

                          EnterShort() did get submited and the position has reversed to go long(the second trade). My issue is not there. My problem is about the TimeFilter thad did not do the job as the second trade has run until 17:00(session close).

                          Here is the screenshots of of the conditions page without the builder open.

                          Click image for larger version

Name:	TimeFilter.jpg
Views:	667
Size:	203.3 KB
ID:	1236863




                          Comment


                            #14
                            Hello McGyver270,

                            A Time filter allows a condition to be true or false between a time range. This does not submit an exit order.

                            You would need to submit an exit order at 15:30 in a different condition set if you want to exit the position.

                            Your time filter only applies the entry conditions that submit the entry.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Hello Chelsea,

                              Ok i see. I misunderstood the utility of the time filter then. Thank you for your explanation, that make sense now. I will do a search on the forum for the exit order at a specific time.

                              Hugues

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              64 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