Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to index the last bar before EMA cross above?

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

    How to index the last bar before EMA cross above?

    Hi ,

    Now I know how to index the first bar after EMA1 cross above EMA2 :


    (CrossAbove(EMA1, EMA2, 1)


    But how to index the last bar that before EMA1 cross above EMA2?

    Or is any function that I can get 3 or 5 bars around the cross happened,for example bar[-2] bar[-1] bar[0] bar[1] bar[2]

    So that I can add the condition:If there is any green bar with the 5 bars around the cross happen,I go long.
    Last edited by williamzz; 06-22-2019, 07:36 AM.

    #2
    Hello williamzz,

    If you want to know the value of the EMA1 from the previous bar when the cross occurs, this would be:

    EMA1[1]

    The 1 is a bars ago index representing 1 bar ago and is used the same for getting the Time[1], Close[1], Open[1], High[1], Low[1], etc..


    If you want the value of the EMA1 from the bar prior to the cross on a later bar, record the CurrentBar number when the cross occurs, then use math.
    private int crossBar;

    if ((CrossAbove(EMA1, EMA2, 1))
    {
    crossBar = CurrentBar;
    }

    // then later
    Print(EMA1[CurrentBar - crossBar - 1]);

    Below I am also including a link to a forum post with helpful tips about getting started with NinjaScript.
    https://ninjatrader.com/support/foru...040#post786040
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yes,it is helpful.

      Can I ask a further question:

      https://ninjatrader.com/support/foru...at-current-bar

      Now I'm using:

      if (CrossAbove(EMA1, EMA2, 1))
      {

      EnterLong(_posSize, LongPos);
      }

      But the Enterlong action always occurs after 1 or 2 bars after the cross happened.

      How can make the Enterlong action occurs in the current bar that when the cross just happen?

      Thank you so much!!!
      Last edited by williamzz; 06-25-2019, 09:40 AM.

      Comment


        #4
        Hello williamzz,

        Respectfully we request that you avoid creating multiple posts for the same inquiry.

        Would you like to work in this thread or in the other thread?

        When Calculate is set to OnBarClose, then the cross is not checked until the bar is closed and the order is submitted at the open of the new bar.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Got you!Thank you!Yes I will avoid it!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          77 views
          1 like
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          40 views
          0 likes
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          63 views
          2 likes
          Last Post CaptainJack  
          Started by CarlTrading, 03-30-2026, 11:51 AM
          0 responses
          63 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 03-30-2026, 11:48 AM
          0 responses
          53 views
          0 likes
          Last Post CarlTrading  
          Working...
          X