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

CrossAbove function clarification

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

    CrossAbove function clarification

    Final Objective: Create a signal (plot an orange triangle) when there is a price pullback and then a cross above of the 34 EMA. Eventually I want to verify that the price went below the 34 EMA and came back and cross above the 34 EMA within 10 bars or less of crossing the 34 EMA to the downside.

    I am trying to use the CrossAbove function in the following manner:
    if(CrossAbove(Close,EMA(34),2))
    BSignal[0]= Low[0]-1;

    I get different behaviors when I use Close vs Close[0]. When I use Close I get some plot signals. When I use Close[0], I don't get a single signal. What is the difference between the way?

    When using the Close syntax, I have questions why I am not getting a signal. In the screenshot I have highlighted two scenarios where I did not get a signal. I would like to understand why those candles did not generate a signal.




    Also, is there a way to delete the signal plot?

    Example:
    if (BSignal[1] > 0)
    Then BSignal[1] = Delete/Remove/Reset

    When I do this, I am noticing that BSignal[1] is always equal to 0.
    Can I access the Plot history using BSignal[1]?
    How can I check the BSignal[1] has a value?
    How can I remove BSignal[1] value?

    Thanks in advance.
    JG
    Attached Files

    #2
    Hello [email protected],

    Thanks for your post.

    I get different behaviors when I use Close vs Close[0]. When I use Close I get some plot signals. When I use Close[0], I don't get a single signal. What is the difference between the way?
    Close is a Price Series, so a Series<double> of Close values. Close[0] is the most recent Close value in the Price Series. Checking if(CrossAbove(Close,EMA(34),2)) will see if the Close series of data points has crossed above the EMA's series of data points looking back 2 bars. If there was a across it would be true for 2 bars since you are looking back 2 bars for a cross.

    I'm not exactly sure what I am looking for on your chart to see the the cross signals. If you are referring to the gold triangles drawn, I do see these when a cross has occurred. It should also be worth noting that small visual differences in the chart and data points could be misleading to when you should expect the cross to occur. (For example, line thickness of a plot)

    Checking your logic is working correctly will involve adding prints so you understand what values are being used to process your logic as it is iterating. I would recommend adding prints for these values and for CurrentBar so you can better see the indexes of the iterating bars. The Data Box on a chart can also be enabled to show BarsAgo and Bar Indexes which can help while reviewing your logic. Additional debugging tips to monitor your logic are linked below.

    Debugging tips - https://ninjatrader.com/support/help...script_cod.htm

    I've included a small snippet where you can see bar indexes where a CrossAbove is true.

    Code:
    if (CrossAbove(Close, SMA(14), 2))
    {
        Print(CurrentBar);
    }
    As for deleting a signal plot, I may not necessarily recommend modifying historical plot values. Plots are series of points which are useful for say a moving average where you have data points for each bar. If you are looking to have the plot show an "active" or "inactive" state, I would recommend plotting 1 or -1 for the plot value. On a new bar the plot would be developing a new data point and would not have the include plot value.

    Associated documentation for working with Price Series and for CrossAbove are also linked below.

    Working with Price Series - https://ninjatrader.com/support/help...ice_series.htm

    CrossAbove - https://ninjatrader.com/support/help...crossabove.htm

    Please let me know if I can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thank you Jim for the clarification.
      As I continue to move forward with my indicator/signal, I have the following question.
      The code below is functioning and it is the same code for two different EMA cross above signals.

      Code:
      protected override void OnBarUpdate()
      
              {
      
      
                  if (CurrentBar < FirstMovingAverage)
      
                      return;
      
      
      
                  if (FirstCheckForCrossAbove)
      
                  {
      
                      if(i1CrossDownwardBar != 0)
      
                          i1CrossDownwardBar = i1CrossDownwardBar + 1;
      
                      else 
      
                          if(CrossBelow(Close,EMA(FirstMovingAverage),1))
      
                          {
      
                              i1CrossDownwardBar = 1 ;
      
                              //TwoBSignal2[0]= High[0]+1;
      
                          }
      
      
      
                      if(i1CrossDownwardBar > 0 && i1CrossDownwardBar <= FirstCrossLookBack)
      
                      {
      
                          if(CrossAbove(Close,EMA(FirstMovingAverage),1))
      
                          {
      
                              TwoB1stCrossAboveSignal[0]= Low[0]-1;
      
                              i1CrossDownwardBar = 0;
      
                          }
      
                          if (i1CrossDownwardBar == FirstCrossLookBack)
      
                              i1CrossDownwardBar = 0;
      
                      }
      
                  }
      
      
      
      
      
      
      
      
      
      
      
                  if (SecondCheckForCrossAbove)
      
                  {
      
                      if(i2CrossDownwardBar != 0)
      
                          i2CrossDownwardBar = i2CrossDownwardBar + 1;
      
                      else 
      
                          if(CrossBelow(Close,EMA(SecondMovingAverage),1))
      
                          {
      
                              i2CrossDownwardBar = 1 ;
      
                              //TwoBSignal2[0]= High[0]+1;
      
                          }
      
      
      
                      if(i2CrossDownwardBar > 0 && i2CrossDownwardBar <= SecondCrossLookBack)
      
                      {
      
                          if(CrossAbove(Close,EMA(SecondMovingAverage),1))
      
                          {
      
                              TwoB2ndCrossAboveSignal[0]= Low[0]-1;
      
      
                              i2CrossDownwardBar = 0;
      
                          }
      
                          if (i2CrossDownwardBar == SecondCrossLookBack)
      
                              i2CrossDownwardBar = 0;
      
                      }
      
                  }
      }

      For the first signal I am checking the cross above agains the EMA(34) and the second one against EMA(89).
      Both signals use CrossLookBack value to make sure the dip below the EMA occurs a certain number of bars back before it crosses above the EMA back again; my way of controlling the speed in which the pullback occurs.

      Now there is a scenario where I get a signal for the EMA(34) and bar later I get a signal for EMA(89). The EMA(89) comes one or two bars afterwards. If I want to generate a third signal when the cross above EMA(34) and EMA (89) is within 1 bar of each other, how I would go about this.


      This is my attempt but it is not working.

      Code:
       if(i2CrossDownwardBar > 0 && i2CrossDownwardBar <= SecondCrossLookBack)
      
                      {
      
                          if(CrossAbove(Close,EMA(SecondMovingAverage),1))
      
                          {
      
                              TwoB2ndCrossAboveSignal[0]= Low[0]-1;
                              if (TwoB1stCrossAboveSignal[1] > 0)
                                       TwoBothCrossAboveSignal[0] = Low[0]-2;
      
      
                              i2CrossDownwardBar = 0;
      
                          }
      
                          if (i2CrossDownwardBar == SecondCrossLookBack)
      
                              i2CrossDownwardBar = 0;
      
                      }
      
                  }

      I can't go back on the series to see if the first plot generated a signal one bar ago.

      Thanks,
      Javier G

      Comment


        #4
        Hello Javier,

        I would recommend using an integer to count after the cross has occurred. Reset the integer to 1 when the first cross occurs. If your second cross happens when the your counter is less than 2, your third condition will be true.

        For example:

        Code:
        if (CONDITION1)
        {
            myCounter = 1;
        }
        
        myCounter++;
        
        if (myCounter < 2 && CONDITION2)
        {
            // Do your action here
        }
        To move forward, I would recommend creating a simple test script where you can model counting logic, and then once you know it is working, you can implement that logic in your strategy.

        If you have issues with the implementation, debugging steps should be taken so you can monitor the strategy logic as it is processing. This would involve adding prints for the values used to evaluate your conditions so you can see why they are not becoming true. (Please see Debugging Tips posted in my previous reply.)

        Also if it interests you, we could have a representative of our EcoSystem reach out with information on NinjaScript Consultants who would be happy to create this logic for you.

        Please let me know if we can be of further assistance.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by trilliantrader, 04-18-2024, 08:16 AM
        4 responses
        18 views
        0 likes
        Last Post trilliantrader  
        Started by mgco4you, Today, 09:46 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by wzgy0920, Today, 09:53 PM
        0 responses
        9 views
        0 likes
        Last Post wzgy0920  
        Started by Rapine Heihei, Today, 08:19 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by Rapine Heihei, Today, 08:25 PM
        0 responses
        10 views
        0 likes
        Last Post Rapine Heihei  
        Working...
        X