Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to add a line of code as a variable?

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

    How to add a line of code as a variable?

    Hi,
    I have a strategy that uses

    if (Close[0]>Open[0]
    && Close[1]>Open[1])

    as a condition.


    I want to be able to add more lines of Close>Open to that logic for a longer lookback...in order to test which is the most effective number of consecutive rising bars. Potentially going all the way to e.g. [10].

    I cant do it with an integer, as changing the lookback period to [3] is not equal to having three Open>Close in a row...as one implies a strong deviation...while the other one could simply be a small rise.


    Can anyone tell me how I can do that?

    Thanks.

    #2
    Loop it?

    Create a loop and check the open-close condition and break once the condition is not met.

    In Variables:
    int cnt; //counter variable

    In OBU:
    if(FirstTickOfBar) //use this if COBC = false
    {
    cnt = 0;

    for(int i = 1; i <= 10; i ++) loop to check 10 prior bars
    {
    if(Close[i] > Open[i])
    cnt ++;
    else
    break;
    }

    if(cnt > 8) //use your own variable instead of 8
    do something;
    else
    don't do it;
    }

    This might do the trick if I understand your dilemma.

    Dan
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      Hello nikolaalx,

      Thank you for your post.

      eDanny hit the nail on the head there. Please let me know if you need any assistance implementing eDanny's example.

      Comment


        #4
        Hi,
        yes you have understood right, however I am a complete newbie and not sure I understand completely.

        If I understood right, here is what I should do:


        1. Add the variable integers first as follow:

        private int i = 0;
        private int cnt;

        2. Then add the line of code that will loop in the OnBarUpdate

        for(int i = 1; i <= 10; i ++)
        (( this is something I am not sure of...as I never had any experience with adding a line of code that begins with "for(...)" . I assume this is the loop function, right))) ?

        and all the remainign conditions.

        I also have a question about : In OBU:
        if(FirstTickOfBar) //use this if COBC = false
        {
        cnt = 0;


        Why would I want to reset the counter at the beginning of each bar? Isnt the point to keep track of the counter and reset it only on a specific event? One would be beginning of the day and the other rule would be whne a bar closes in opposite direction from the counting logic.



        Originally posted by eDanny View Post
        Create a loop and check the open-close condition and break once the condition is not met.


        for(int i = 1; i <= 10; i ++) loop to check 10 prior bars
        {
        if(Close[i] > Open[i])
        cnt ++;
        else
        break;
        }

        if(cnt > 8) //use your own variable instead of 8
        do something;
        else
        don't do it;
        }

        Dan

        Comment


          #5
          I have added the following code :

          if(FirstTickOfBar)
          {
          pcount = 0;

          for(int i = 1; i <= 10; i ++)
          {
          if(Close[i] > Open[i])
          pcount ++;
          }


          if(pcount == LookBack)
          Variable9 = 0;
          else if (pcount != LookBack)
          Variable9 = 1;
          }




          if(FirstTickOfBar)
          {
          lcount = 0;

          for(int i = 1; i <= 10; i ++)
          {
          if(Close[i] < Open[i])
          lcount ++;
          }


          if(lcount == LookBack)
          Variable10 = 0;
          else if (lcount != LookBack)
          Variable10 = 1;
          }


          And also added the Variable9/10 == 0 as a condition for the entries, but it does not seem to be working properly.

          Comment


            #6
            Your original post seemed to require consecutive rising or falling bars. The "else break;" in my example tells the loop to quit when the test fails and the counter will stop and have an accurate count of the consecutive bars. Your version keeps looping till all bars are checked and therefore the count can be off. The counter needs to be reset every time the test begins because you are testing the latest 10 bars at the first tick of a new bar. Maybe I don't understand what you are actually trying to do.

            Dan
            Last edited by eDanny; 08-29-2014, 10:36 AM.
            eDanny
            NinjaTrader Ecosystem Vendor - Integrity Traders

            Comment


              #7
              nikolaalx, can you please update your code to include the break sequence as eDanny had noted and then recheck your outcome from the script?

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              574 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              332 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              101 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              553 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              551 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X