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

Cross Above/Below Counter

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

    Cross Above/Below Counter

    Hello,

    Hope you guys are doing well,


    I am trying to work on a counter to count crosses in the last specified period of bars, I have done counters before but none were counting crosses, just green or red bars, but because of the nature of cross above or below, as you have to specify the lookback period as an int, so I can not include my desired look back.

    Pasting the code below :

    if (CurrentBar <= LookBack)
    return;

    int manycrosses = 0;
    int signalState = 0;


    if(RuleSetNumber == 1)
    for (var i = LookBack; i >= 0 ; i--)
    {
    if ((CrossAbove(SMA(Period1), SMA(Period2), i))
    || (CrossBelow(SMA(Period1), SMA(Period2), i)))
    manycrosses++;
    //(CountIf(() => (CrossAbove(SMA(Period1), SMA(Period2),i ) > 1)
    }

    if(RuleSetNumber == 2)
    for (var i = LookBack; i >= 0 ; i--)
    {
    if ((CrossAbove(EMA(Period1), EMA(Period2), i))
    || (CrossBelow(EMA(Period1), EMA(Period2), i)))
    manycrosses++;

    }



    if (manycrosses >= RequiredCount)
    signalState = 1;


    if( manycrosses < RequiredCount)
    signalState = 0;


    SignalState[0] = (double)signalState;
    ManyCrosses[0] = (double)manycrosses;​

    }




    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="LookBack", Order=1, GroupName="Parameters")]
    public int LookBack
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="RequiredCount", Order=2, GroupName="Parameters")]
    public int RequiredCount
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="minimumBarSizeTick", Order=3, GroupName="Parameters")]
    public int minimumBarSizeTick
    { get; set; }


    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Period1", Order=7, GroupName="Parameters")]
    public int Period1
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Period2", Order=8, GroupName="Parameters")]
    public int Period2
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="RuleSetNumber", Order=4, GroupName="Parameters")]
    public int RuleSetNumber
    { get; set; }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> SignalState
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> ManyCrosses
    {
    get { return Values[1]; }
    }

    }
    }​

    My problem is that it is counting max counter every time a cross happens in the last (Lookback) period, while I want to count every cross that happened 1 bar ago for x number of bars which is the lookback defined as an input.

    So again, I want to count how many crosses happen in (Lookback) and if the amount required happens I want the signal state to be 1, if not 0, while this code is signaling 1 every time a cross happens in the last 20 bars.

    please help.
    thanks,

    #2
    Hello moewqasem,

    I would suggest to just use the CountIf method for this type of use case. CountIf takes a lookback period and returns the number of times a condition becomes true in that lookback period. If you take a look at the help guide sample that is counting each time Close[0] > Open[0] throughout the lookback period, it will automatically increment the bars ago as it traverses the period you use. In your code you are using i as the lookback period for countif, insead of that you would just use the lookback period that you wanted it to loop over. It does the looping for you so you don't need for loops.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thank you for your prompt response,

      I tried applying the Countif, but seems that I am getting some errors, also, the example is demonstrating close[0] > x, while the cross function requires an int to define the lookback period, I dont really know how to write the code for that, i di the following but getting some errors :

      if (CountIf(() => (CrossAbove(SMA(Period1), SMA(Period2), 1, LookBack) > RequiredCount)))
      || (CrossBelow(SMA(Period1), SMA(Period2), 1, LookBack) > RequiredCount)))
      manycrosses++;​


      if you may help how to write the code,

      Thanks in advance

      Comment


        #4
        Hello moewqasem,

        The count if statement needs to be written like the following where 10 is the lookback period and 8 is the number of instances you are checking for. Close[0] > Open[0] is where you would place your condition. If you need more than one condition you need to use parenthesis to surround your conditions in that area


        Code:
        if (CountIf(() => Close[0] > Open[0], 10) > 8)
        Code:
        if (CountIf(() => (firstCondition || secondCondition) , 10) > 8)
        {
        
        }


        If you just want to know the count you can assign the result to a variable:

        Code:
        int result = CountIf(() => (firstCondition || secondCondition) , 10);
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          I wrote this but I get an error
          {
          if (CountIf(() => ((CrossAbove(SMA(Period1), SMA(Period2) || (CrossBelow(SMA(Period1), SMA(Period2))) , 10) > 8))))
          manycrosses++;
          }

          The error is No overload for method countif takes 1 arguments (CS1501)

          Comment


            #6
            Hello moewqasem,

            You don't need to increment a variable and your CrossAbove and Below are not formed correct. The count if returns the number of times the condition happened in the lookback period. I would suggest trying the third sample I provided

            Code:
            int result = CountIf(() => (firstCondition || secondCondition) , 10);

            This is what it would look with a cross condition

            Code:
            int result = CountIf(() => (CrossAbove(SMA(Period1), SMA(Period2), 1) || CrossBelow(SMA(Period1), SMA(Period2), 1)) , 10);


            The 10 is the only variable you need to adjust to match the lookback period you wanted to use.
            JesseNinjaTrader Customer Service

            Comment


              #7
              I appreciate you, hopefully this is the last error,



              A local variable named '' manycrosses'' cannot be declared in this scope because it would give a different meaning to ''manycrosses'' which is already used in parent or current scope to denote something else.

              ​​​​​​​CS0136



              IsSuspendedWhileInactive = true;
              LookBack = 20;
              RequiredCount = 3;
              minimumBarSizeTick =1;
              Period1 = 5;
              Period2 = 10;
              RuleSetNumber = 1;
              AddPlot(Brushes.Orange, "SignalState");
              AddPlot(Brushes.ForestGreen, "Crosses");

              }
              else if (State == State.Configure)
              {
              }
              else if (State == State.DataLoaded)
              {

              }
              }

              protected override void OnBarUpdate()
              {
              //Add your custom indicator logic here.

              if (CurrentBar <= LookBack)
              return;

              int manycrosses = 0;
              int signalState = 0;


              if(RuleSetNumber == 1)
              {
              int manycrosses = CountIf(() => (CrossAbove(SMA(Period1), SMA(Period2), 1) || CrossBelow(SMA(Period1), SMA(Period2), 1)) , LookBack);

              }

              if(RuleSetNumber == 2)
              {
              int manycrosses = CountIf(() => (CrossAbove(EMA(Period1), EMA(Period2), 1) || CrossBelow(EMA(Period1), EMA(Period2), 1)) , LookBack);

              }



              if (manycrosses >= RequiredCount)
              signalState = 1;


              if( manycrosses < RequiredCount)
              signalState = 0;


              SignalState[0] = (int)signalState;
              ManyCrosses[0] = (int)manycrosses;




              }




              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="LookBack", Order=1, GroupName="Parameters")]
              public int LookBack
              { get; set; }

              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="RequiredCount", Order=2, GroupName="Parameters")]
              public int RequiredCount
              { get; set; }

              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="minimumBarSizeTick", Order=3, GroupName="Parameters")]
              public int minimumBarSizeTick
              { get; set; }


              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="Period1", Order=7, GroupName="Parameters")]
              public int Period1
              { get; set; }

              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="Period2", Order=8, GroupName="Parameters")]
              public int Period2
              { get; set; }

              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="RuleSetNumber", Order=4, GroupName="Parameters")]
              public int RuleSetNumber
              { get; set; }

              [Browsable(false)]
              [XmlIgnore]
              public Series<double> SignalState
              {
              get { return Values[0]; }
              }

              [Browsable(false)]
              [XmlIgnore]
              public Series<double> ManyCrosses
              {
              get { return Values[1]; }
              }

              }
              }​

              Comment


                #8
                Hello moewqasem,

                the variable you used is being redefined because you used the type int before the variable names, you used manycrosses for three local variables. You need to define the variable once and then later set its value like the following:

                Code:
                int manycrosses = 0;
                if(RuleSetNumber == 1)
                {
                manycrosses = CountIf(() => (CrossAbove(SMA(Period1), SMA(Period2), 1) || CrossBelow(SMA(Period1), SMA(Period2), 1)) , LookBack);
                
                }
                
                if(RuleSetNumber == 2)
                {
                manycrosses = CountIf(() => (CrossAbove(EMA(Period1), EMA(Period2), 1) || CrossBelow(EMA(Period1), EMA(Period2), 1)) , LookBack);
                
                }​​
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Okay, that worked just as intended, the indicator! Perfectly!
                  It's because of your efforts and patience to be honest, I am new to coding and doing my best to learn, and you did teach me more than a few lessons throughout this conversation.

                  Thank you , I appreciate your support

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by burtoninlondon, Today, 12:38 AM
                  0 responses
                  9 views
                  0 likes
                  Last Post burtoninlondon  
                  Started by AaronKoRn, Yesterday, 09:49 PM
                  0 responses
                  14 views
                  0 likes
                  Last Post AaronKoRn  
                  Started by carnitron, Yesterday, 08:42 PM
                  0 responses
                  11 views
                  0 likes
                  Last Post carnitron  
                  Started by strategist007, Yesterday, 07:51 PM
                  0 responses
                  13 views
                  0 likes
                  Last Post strategist007  
                  Started by StockTrader88, 03-06-2021, 08:58 AM
                  44 responses
                  3,983 views
                  3 likes
                  Last Post jhudas88  
                  Working...
                  X