Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EMA Cross Issue

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

    EMA Cross Issue

    Hello,

    I'm trying to have my strat enter upon a simple EMA cross and want it to enter whenever it does so, including midbar. So, I've set CalculateOnBarClose to false but when I try using "if (CrossAbove(EMA(20), EMA(50), 0))", it just doesn't enter. Using "if (CrossAbove(EMA(20), EMA(50), 1))" actually does work midbar (which I don't understand) but raises another issue. I'm using a breakeven stop in my code which moves to entry price once I'm +2 points in profit. The problem is if I'm long, hit +2 (raising my stop to even), then turn around and take out my stop in the same or next bar, my strat enters again because it's set to go long upon a cross 1 bar back.

    I would prefer to code the cross to current bar and just add EntriesPerDirection = 1 but it doesn't seem to want to cooperate. Have I missed something or is there a work around?

    Thanks,
    Dave

    #2
    Hello,

    Thank you for your note.

    We recieved the message and someone will follow up with you at the latest Monday.

    Thank you for your patience.
    BrettNinjaTrader Product Management

    Comment


      #3
      Dave you cannot use a lookback period of 0. To determine a cross over condition you need to check two bars or two points in time. At one point in time you need to see that it was below and on the other you need to see it was above. This determines your crossover so you have to use at least a lookback period of 1.

      If you do not want your strategy to reenter I suggest you use some bool variables and only reenable trading when you deem it acceptable by resetting the bool.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Hey Josh,

        Thanks for responding. I'm not too familiar with bools but I can probably just do this, right?:

        Code:
        if(EMA(20)[0] < EMA(50)[0] && EMA(20)[1] > EMA(50)[1])
        
        Go short
        This should enter a "cross" if it happens midbar or at the open of the next bar. Please correct me if I'm wrong.

        Thanks,
        Dave

        Comment


          #5
          Dave,

          Would not solve your issue. That would get you into a trade, if you get stopped out immediately, and then on the exact same bar the conditions to enter again become true again you would enter another trade.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Josh,

            Your right, of course. I didn't explain my issue correctly in my first post. For some reason, after taking out my stop and recrossing on the same bar, I was getting entries followed by immediate exits until the bar closed. Any idea why? Am trying to duplicate as I write this.

            Dave

            Comment


              #7
              Dave,

              I suggest you use TraceOrders = true and check out what may be happening with your orders in terms of fill prices and submission prices. Perhaps you are submitting a stop price that would close you out immediately which would cause such behavior.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Could be, sir. It's been awhile since I played with code and have gotten a little rusty. Thanks for the tip.

                Dave

                Comment


                  #9
                  Josh,

                  I can't tell exactly what's causing this to happen, but this is what I get in the output window with TraceOrders set to true:

                  5/4/2010 7:33:58 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='60 Cross' Mode=Ticks Value=12 Currency=0 Simulated=False
                  5/4/2010 7:33:58 AM Entered internal PlaceOrder() method at 5/4/2010 7:33:58 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='60 Cross' FromEntrySignal=''
                  5/4/2010 7:33:58 AM Entered internal PlaceOrder() method at 5/4/2010 7:33:58 AM: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='60 Cross'
                  5/4/2010 7:33:58 AM Ignored PlaceOrder() method: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='60 Cross' Reason='This was an exit order but no position exists to exit'
                  5/4/2010 7:33:59 AM Entered internal PlaceOrder() method at 5/4/2010 7:33:59 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='60 Cross' FromEntrySignal=''
                  5/4/2010 7:33:59 AM Ignored PlaceOrder() method at 5/4/2010 7:33:59 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='60 Cross' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
                  5/4/2010 7:33:59 AM Entered internal PlaceOrder() method at 5/4/2010 7:33:59 AM: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='60 Cross'
                  5/4/2010 7:33:59 AM Entered internal PlaceOrder() method at 5/4/2010 7:33:59 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='60 Cross' FromEntrySignal=''
                  5/4/2010 7:33:59 AM Ignored PlaceOrder() method at 5/4/2010 7:33:59 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='60 Cross' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
                  5/4/2010 7:33:59 AM Entered internal PlaceOrder() method at 5/4/2010 7:33:59 AM: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='60 Cross'
                  5/4/2010 7:33:59 AM Ignored PlaceOrder() method: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName=Sell' FromEntrySignal='60 Cross' Reason='There already is a matching, terminal exit order in place'
                  5/4/2010 7:33:59 AM Cancelled pending exit order, since associated position is closed: Order='609ecdfca5fe41c9b462642e690dd034/Sim101' Name='Profit target' State=Working Instrument='ES 06-10' Action=Sell Limit price=1205.75 Stop price=0 Quantity=1 Strategy='Cross60min' Type=Limit Tif=Gtc Oco='62d970b91d0a42548299532d20817e81' Filled=0 Fill price=0 Token='609ecdfca5fe41c9b462642e690dd034' Gtd='12/1/2099 12:00:00 AM'

                  It seems to enter (market), set my stop and target, and exit (market) immediately. The thing is is that based on my entry and exit signals, it's literally impossible for both to happen simultaneously - entry = 20/50 cross up for long; exit = close under 20.

                  Any ideas?

                  Thanks,
                  Dave

                  Comment


                    #10
                    Please post complete corresponding code. Thank you.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Please let me know if anything else is needed:

                      Code:
                      protected override void OnBarUpdate()
                                  // Condition set 1
                                  if (CrossAbove(EMA(20), EMA(50), 1))
                                  {
                                      entryOrder = EnterLong(DefaultQuantity, "2050 Cross");
                      				DrawArrowUp("", true, 0, Low[0] - 12 * TickSize, Color.Blue);
                      				DrawTriangleUp("My triangle up" + CurrentBar, false, 0, Low[0] - 12 * TickSize, Color.Blue);
                      				
                                  }
                      
                                  // Condition set 2
                                  if (CrossBelow(EMA(20), EMA(50), 1))
                                  {
                                      entryOrder = EnterShort(DefaultQuantity, "2050 Cross");
                      				DrawArrowDown("", true, 0, High[0] + 12 * TickSize, Color.Red);
                      				DrawTriangleDown("My triangle down" + CurrentBar, false, 0, High[0] + 12 * TickSize, Color.Red);
                      				
                                  }
                      
                                  // Condition set 3
                                  if (Close[1] < EMA(20)[1])
                                  {
                                      ExitLong("", "2050 Cross");
                      				
                                  }
                      
                                  // Condition set 4
                                  if (Close[1] > EMA(20)[1])
                                  {
                                      ExitShort("", "2050 Cross");
                      				
                                  }
                      Thanks, Josh.
                      Dave

                      Comment


                        #12
                        dsraider, please try use separate signal names for the long positions and short positions.

                        In addition, the code you posted doesn't quite match up with the TraceOrder output. The log indicates orders with a signal name of '60 Cross' while your strategy places orders with a signal name of '2050 Cross'.

                        Lastly, are you sure positions can't be closed as soon as they're opened? A long entry occurs when the 20 EMA crosses above the 50 EMA. A long exit occurs when the previous bar's close is less than the previous bar's 20 EMA. In your first post you indicate you are using CalculateOnBarClose = false. As such, the situation could play out like this:
                        • The 20 EMA goes above the 50 EMA -> long entry.
                        • If the previous bar's close is less than the previous bar's 20 EMA, the position would be exited immediately.

                        I have a feeling using different signal names could help solve the issue, but some code re-working may also be necessary.
                        AustinNinjaTrader Customer Service

                        Comment


                          #13
                          Dude, you're a genius. Thank you. The '60 Cross' v '2050 Cross' was something I just did to make it easier to read but you're obviously dead on with the exit code. Can't believe I didn't think of that. Hmm. Need to find a way where it can enter upon a cross and not exit until it closes under the 20 AFTER. Any suggestions?

                          Thanks,
                          Dave

                          Comment


                            #14
                            Trying the following. It complied and I think I should be covered:

                            Code:
                            if (Close[1] < EMA(20)[1] && Close[1] > Position.AvgPrice)
                                        {
                                            ExitLong("", "2050 Cross");
                            As far as the signal name is concerned, since I only allow one trade at a time, shouldn't ExitLong() ONLY kick in if I have an active long trade and vice versa for shorts? I built in a breakeven and trail which just follows whichever trade signals so would prefer to keep the one signal name.

                            Thanks,
                            Dave

                            Comment


                              #15
                              Hi Dave, you could also just work a BarsSinceEntry check in to activate the exit then - you're correct on the signal names, if there's no short and you issue a ExitShort it would just be ignored, your previous traces show exactly this happen.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              666 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              377 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              110 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              580 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X