Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to compare indicator value being the same reading over x bars back

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

    How to compare indicator value being the same reading over x bars back

    Hi.
    I have an indicator that plots price levels. I need to compare last x bars back if reading is the same.
    How can i do that?
    indicatorValue[0] =indicatorValue[1]

    I looked at countif function but it says it doesnt work with multi series

    #2
    Hello tkaboris,

    Thank you for your post.

    This could likely be done using a C# For loop. Here is an example snippet that would check the value of indicatorValue for 0 bars ago and loop through up to the value from 5 barsAgo. If the values are not equal in the comparison, the int notEqual will increment by one. By the end of the loop, if notEqual is greater than 0, it will print "Not all values from 0 barsAgo to 5 barsAgo were equal!"


    Code:
    int notEqual = 0;
    for (int i = 1; i <=5; i++)
    {
    Print(string.Format("{0}indicatorValue[0]: {1} indicatorValue[{2}]: {3}", CurrentBar, indicatorValue[0], i, indicatorValue[i]));
    if (indicatorValue[0] != indicatorValue[i])
    {
    notEqual++;
    }
    }
    if (notEqual > 0)
    Print("Not all values from 0 barsAgo to 5 barsAgo were equal!");
    For more details on looping commands, we have the following page in the help guide:


    Otherwise, this is a general C# concept that is not specific to NinjaTrader. You could search for publicly available resources about For loops, such as the following:
    W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


    Please let us know if we may be of further assistance.

    Comment


      #3
      Thank you.

      So do i need this loop where my entry conditions are? or is there a way i can keep this loop somewhere in onbarupdate and just reference in entry logic with
      Actually i entry if line is 0 continuastly for the last 5 bars..
      if (notEqual == 0)
      EnterLong
      How would this be done and to have it accessible?
      Last edited by tkaboris; 07-19-2023, 09:06 AM.

      Comment


        #4
        Hello tkaboris,

        Thank you for your reply.

        You could add entry conditions based on the value of notEqual if you'd like. I added on to the end of the snippet here:
        Code:
        int notEqual = 0;
        for (int i = 1; i <=5; i++)
        {
        Print(string.Format("{0}indicatorValue[0]: {1} indicatorValue[{2}]: {3}", CurrentBar, indicatorValue[0], i, indicatorValue[i]));
        if (indicatorValue[0] != indicatorValue[i])
        {
        notEqual++;
        }
        }
        if (notEqual > 0)
        Print("Not all values from 0 barsAgo to 5 barsAgo were equal!");
        else if (notEqual == 0)
        EnterLong;
        Ultimately, I suggest testing something like this to see how it behaves and monitor if it is behaving as expected when testing on a simulated account. You can also add Print() statements to better understand your script's behavior.

        Please let us know if we may be of further assistance.​

        Comment


          #5
          Thank you
          For some reason its not opening orders when i add int notEqual = 0; It seems its not reading that condition
          I need to check to start counting from barindex2 and back for 10 bars, is this the right way then?


          Code:
          #region Bounce Trade Entry Logic Here
                         #region Not equal loop for trend magic
          
                          int notEqual = 0;
                          for (int i = 2; i <=10; i++)
                          {
          //                Print(string.Format("{0}TrendMagicModified1[0]: {1} TrendMagicModified1[{2}]: {3}", CurrentBar, TrendMagicModified1[0], i, TrendMagicModified1[i]));
                          if (TrendMagicModified1[0] != TrendMagicModified1[i])
                          {
                          notEqual++;
                          }
                          }
                          if (notEqual == 0)
          //                Print("Values from Trend Magic are equal in the last 10 bars");
          
                          #endregion            
                          if(BounceTrade)                                                
                          {
                              if
                                  ((TrendMagicModified1.BullOrBear[1] == 0) && (TrendMagicModified1.BullOrBear[2] == 1)
          
                                  &&  (notEqual == 0))
                              {                        
                                  BounceTradeLong = true;        
          
                              }
                               else
                              {
                                  BounceTradeLong = false;
                              }                        
          
                          }​
          if(longson)
          {
          EnterLong(1,PositionSize, "TML");
          }

          Comment


            #6
            Hello tkaboris,

            Thank you for your reply.

            I suggest adding Print() statements to understand why your strategy might not be behaving as expected. For example, here are some of your conditions:
            Code:
             if(BounceTrade)
            {
            if
            ((TrendMagicModified1.BullOrBear[1] == 0) && (TrendMagicModified1.BullOrBear[2] == 1)
            
            && (notEqual == 0))
            {
            BounceTradeLong = true;
            
            }​
            The following prints could be added both inside and outside of the conditions to evaluate the values being used and if the condition is ever met:
            Code:
            ​
            Print(CurrentBar + "BounceTrade: " + BounceTrade);
            if(BounceTrade)
            {
            Print("BounceTrade is true! BounceTrade: " + BounceTrade + " CurrentBar: " + CurrentBar);
            Print(string.Format("{0} TrendMagicModified1.BullOrBear[1]: {1} TrendMagicModified1.BullOrBear[2] {2} notEqual: {3}", CurrentBar, TrendMagicModified1.BullOrBear[1], TrendMagicModified1.BullOrBear[2], notEqual));
            if
            ((TrendMagicModified1.BullOrBear[1] == 0) && (TrendMagicModified1.BullOrBear[2] == 1)
            
            && (notEqual == 0))
            {
            Print(string.Format("{0} Condition met!  TrendMagicModified1.BullOrBear[1]: {1} == 0 && TrendMagicModified1.BullOrBear[2]: {2} == 1 && notEqual: {3} == 0" CurrentBar, TrendMagicModified1.BullOrBear[1], TrendMagicModified1.BullOrBear[2], notEqual));
            BounceTradeLong = true;
            Print("BounceTradeLong set to true: " + BounceTradeLong);
            
            }


            For more details and tips on using prints to debug your code:


            Please let us know if we may be of further assistance.

            Comment


              #7
              hi (notEqual == 0) inside the loop is showing as 0, however when in condition its not seeing it. As soon as i remove from the condition strategy opens as normal. I think my syntax is wrong.. Can you please look?
              Thank you

              Comment


                #8
                Hello tkaboris,

                Thank you for your reply.

                You mentioned "when in condition its not seeing it" - please elaborate. What are the results of your print statements? If you print notEqual both inside of the loop and outside of the loop, are the values the same or different? I would be glad to take a look at the output of your print statements or any error messages you are getting and guide you through the debugging process, though it is important to understand that the support department does not offer hands-on debugging services. I will need more information, such as snippets, output information, error messages, etc so that I can see what you are seeing when trying to compile/run your script.

                To save the output from the NinjaScript Output window, you may right-click and select Save As. Give the file a name and save, then attach the file to your reply.

                I appreciate your patience.

                Comment


                  #9
                  hi inside loop i get this message
                  Values from Trend Magic are equal in the last 10 bar
                  and inside condition i dont get any messages...

                  Comment


                    #10
                    Hello tkaboris,

                    Thank you for your reply.

                    Have you tried the example prints I suggested in post number 6 of this thread? I added them in after your print that says "Values from Trend Magic are equal in the last 10 bars" and I am curious what shows up in the output window when those are added in. If it is more convenient, please make a copy of your script by opening it in the NinjaScript Editor. Right-click the background of the script and select Save As. Give the copy a new name and use the copy of the script to test the prints from post number 6. Please let me know what you see in the NinjaScript Output window after adding those prints.

                    I look forward to your reply and hearing the results.

                    Comment


                      #11
                      hi it prints these values

                      I attached a code for that too.
                      Thank you
                      2980 TrendMagicModified1.BullOrBear[1]: 1 TrendMagicModified1.BullOrBear[2] 1 notEqual: 0
                      2981BounceTrade: True
                      BounceTrade is true! BounceTrade: True CurrentBar: 2981
                      2981 TrendMagicModified1.BullOrBear[1]: 1 TrendMagicModified1.BullOrBear[2] 1 notEqual: 3
                      Values from Trend Magic are equal in the last 10 bars
                      2982BounceTrade: True
                      BounceTrade is true! BounceTrade: True CurrentBar: 2982
                      2982 TrendMagicModified1.BullOrBear[1]: 1 TrendMagicModified1.BullOrBear[2] 1 notEqual: 0
                      2983BounceTrade: True
                      BounceTrade is true! BounceTrade: True CurrentBar: 2983
                      2983 TrendMagicModified1.BullOrBear[1]: 1 TrendMagicModified1.BullOrBear[2] 1 notEqual: 3
                      2984BounceTrade: True
                      BounceTrade is true! BounceTrade: True CurrentBar: 2984
                      2984 TrendMagicModified1.BullOrBear[1]: 1 TrendMagicModified1.BullOrBear[2] 1 notEqual: 3
                      2985BounceTrade: True
                      BounceTrade is true! BounceTrade: True CurrentBar: 2985
                      2985 TrendMagicModified1.BullOrBear[1]: 1 TrendMagicModified1.BullOrBear[2] 1 notEqual: 3
                      condition 2
                      2986BounceTrade: True
                      BounceTrade is true! BounceTrade: True CurrentBar: 2986
                      2986 TrendMagicModified1.BullOrBear[1]: 0 TrendMagicModified1.BullOrBear[2] 1 notEqual: 3
                      2987BounceTrade: True
                      BounceTrade is true! BounceTrade: True CurrentBar: 2987
                      2987 TrendMagicModified1.BullOrBear[1]: 0 TrendMagicModified1.BullOrBear[2] 0 notEqual: 3
                      2988BounceTrade: True
                      BounceTrade is true! BounceTrade: True CurrentBar: 2988
                      2988 TrendMagicModified1.BullOrBear[1]: 0 TrendMagicModified1.BullOrBear[2] 0 notEqual: 3​
                      Attached Files

                      Comment


                        #12
                        Hello tkaboris,

                        Thank you for your reply.

                        Based on the prints you are seeing, the following condition is not being met:

                        if ((TrendMagicModified1.BullOrBear[1] == 0) && (TrendMagicModified1.BullOrBear[2] == 1) && (notEqual == 0))

                        This seems to make sense; for example, the only time I am seeing a value of 0 for TrendMagicModified1.BullOrBear[1] and a value of 1 for TrendMagicModified1.BullOrBear[2], notEqual has a value of 3. It seems that the entire condition is not being met, which means additional prints inside of the condition are not being triggered.

                        2986 TrendMagicModified1.BullOrBear[1]: 0 TrendMagicModified1.BullOrBear[2] 1 notEqual: 3

                        The two times that the prints show notEqual as 0, both values from the indicator are 1 so the condition is not fully met:

                        2980 TrendMagicModified1.BullOrBear[1]: 1 TrendMagicModified1.BullOrBear[2] 1 notEqual: 0
                        2982 TrendMagicModified1.BullOrBear[1]: 1 TrendMagicModified1.BullOrBear[2] 1 notEqual: 0


                        You may have to consider the condition being used and if that needs to be modified, or test your strategy more until the condition is actually met to see if the additional logic is executed as expected:
                        Code:
                        Print(string.Format("{0} Condition met! TrendMagicModified1.BullOrBear[1]: {1} == 0 && TrendMagicModified1.BullOrBear[2]: {2} == 1 && notEqual: {3} == 0" CurrentBar, TrendMagicModified1.BullOrBear[1], TrendMagicModified1.BullOrBear[2], notEqual));
                        BounceTradeLong = true;
                        Print("BounceTradeLong set to true: " + BounceTradeLong);
                        Thank you for your time and patience.​​​

                        Comment


                          #13
                          Hi, the condition is working. if i remove equal0 condition it opens trade, and when i enable condition it doesnt open trade.
                          There is a condition met

                          Values from Trend Magic are more then 0 in the last 5 bars
                          TrendMagicModified1.BullOrBear[1]0
                          TrendMagicModified1.BullOrBear[2]1​

                          Would this line be correct if i want to start counting from 2 bars back? for (int i = 2; i <=5; i++)
                          Because when line is same and when it changes on bar[1] and I need to check from bar before that to see if previous line value were the same

                          https://prnt.sc/2U--Z-RPlu-D (equal0 not enabled)

                          https://prnt.sc/fC6vWSrqqQJW (equal0 enabled)
                          Last edited by tkaboris; 07-19-2023, 01:58 PM.

                          Comment


                            #14
                            Hello tkaboris,

                            Thank you for your reply.

                            I am not understanding what you want to check for in your conditions. For example, here is the condition I have been using as an example from your snippet you provided earlier:

                            if ((TrendMagicModified1.BullOrBear[1] == 0) && (TrendMagicModified1.BullOrBear[2] == 1) && (notEqual == 0))

                            What this is checking for is if TrendMagicModified1.BullOrBear from 1 bar ago is equal to 0 AND that TrendMagicModified1.BullOrBear from 2 bars ago is equal to 1 AND that notEqual is 0.
                            For notEqual to be 0, you are checking the value of TrendMagicModified1[0] with the values from some previous bars for TrendMagicModified1. Is it even possible/does it make logical sense for TrendMagicModified1.BullOrBear[1] to be a value of 0 and TrendMagicModified1.BullOrBear[2] to be 1 and for TrendMagicModified1 to be the same for several bars in a row?

                            Hi, the condition is working. if i remove equal0 condition it opens trade, and when i enable condition it doesnt open trade.
                            There is a condition met

                            Where are you removing equal0? This does not make sense to me and I need more context. What condition are you enabling/disabling that does or doesn't open a trade? What condition are you referring to that is met?

                            Would this line be correct if i want to start counting from 2 bars back? for (int i = 2; i <=5; i++)
                            I need more context; please provide a code snippet of what you are referring to OR provide an explanation written out of what you are looking for so I may provide you with a sample condition.

                            I look forward to your reply.

                            Comment


                              #15
                              HI this is the condition
                              ((TrendMagicModified1.BullOrBear[1] == 0) && (TrendMagicModified1.BullOrBear[2] == 1)
                              && (notEqual == 0)
                              )

                              my forloop is below.
                              And snippet is below explaining on the graphic


                              int notEqual = 0;
                              for (int i = 1; i <=5; i++)
                              {
                              // Print(string.Format("{0}TrendMagicModified1[0]: {1} TrendMagicModified1[{2}]: {3}", CurrentBar, TrendMagicModified1[0], i, TrendMagicModified1[i]));
                              if (TrendMagicModified1[0] != TrendMagicModified1[i])
                              {
                              notEqual++;
                              }
                              }
                              if (notEqual > 0)
                              Print("Values from Trend Magic are more then 0 in the last 5 bars");
                              else if (notEqual == 0)
                              Print("notEqual"+ notEqual);
                              Print("TrendMagicModified1.BullOrBear[1]"+TrendMagicModified1.BullOrBear[1]);
                              Print("TrendMagicModified1.BullOrBear[2]"+TrendMagicModified1.BullOrBear[2]);
                              #endregion​​

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              64 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              139 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              75 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              45 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              50 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X