Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 PaulMohn, Today, 12:36 PM
          2 responses
          16 views
          0 likes
          Last Post PaulMohn  
          Started by Conceptzx, 10-11-2022, 06:38 AM
          2 responses
          53 views
          0 likes
          Last Post PhillT
          by PhillT
           
          Started by Kaledus, Today, 01:29 PM
          0 responses
          4 views
          0 likes
          Last Post Kaledus
          by Kaledus
           
          Started by yertle, Yesterday, 08:38 AM
          8 responses
          37 views
          0 likes
          Last Post ryjoga
          by ryjoga
           
          Started by rdtdale, Today, 01:02 PM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_LuisH  
          Working...
          X