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 NullPointStrategies, Today, 05:17 AM
          0 responses
          48 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          126 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          66 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          42 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          46 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X