Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trading off # of candles

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

    Trading off # of candles

    How would I go about coding a strategy that entered a trade after a certain number of candles in a row.... like shorting after 6 subsequent red candles.

    Not sure what I would use from the condition builder to make this happen?

    #2
    Hello ScottieDog,

    Thank you for writing in.

    If you wish to create a condition that checks for 6 down bars in a row, you can check for the following:
    Close[0] < Close[1] // current bar is less than previous
    Close[1] < Close[2] // previous bar less than two bars ago
    Close[2] < Close[3] // two bars ago less than three bars ago
    Close[3] < Close[4] // three bars ago less than four bars ago
    Close[4] < Close[5] // four bars ago less than five bars ago
    Close[5] < Close[6] // five bars ago less than six bars ago

    If all of the above are true, this would denote there were six down bars in a row.
    Attached Files
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by ScottieDog View Post
      How would I go about coding a strategy that entered a trade after a certain number of candles in a row.... like shorting after 6 subsequent red candles.

      Not sure what I would use from the condition builder to make this happen?
      You would have to code it from scratch: the Strategy Builder would not be able to handle it.

      #Variables
      Code:
      private int counter = 0; //class variable to store count
      #OnBarUpdate
      Code:
      counter = (FirstTickOfBar && Close[0] < Open[0]) ? counter++ : 0; //test for red bar and up counter or reset it as necessary
      if (counter >= 6) MakeMegaBucks();
      That code takes account of the fact that the data is being streamed and not a static space, and so makes efficient use of the data stream. You could always instead ignore the true nature of your data and use an inefficient "for loop" on every bar, which unfortunately is usually the answer that you would be given.
      Last edited by koganam; 04-09-2016, 12:38 PM.

      Comment


        #4
        Thanks.

        Is there a way of coding this so that I can have a user set preference for number of bars I want to choose? I´ve set a user defined input INT, but how to apply the conditions to it, for # of bars before an entry? Would I set a user variable for each in the condition builder?

        Originally posted by NinjaTrader_ZacharyG View Post
        Hello ScottieDog,

        Thank you for writing in.

        If you wish to create a condition that checks for 6 down bars in a row, you can check for the following:
        Close[0] < Close[1] // current bar is less than previous
        Close[1] < Close[2] // previous bar less than two bars ago
        Close[2] < Close[3] // two bars ago less than three bars ago
        Close[3] < Close[4] // three bars ago less than four bars ago
        Close[4] < Close[5] // four bars ago less than five bars ago
        Close[5] < Close[6] // five bars ago less than six bars ago

        If all of the above are true, this would denote there were six down bars in a row.

        Comment


          #5
          Thanks. Saw your reply after my post #4.

          I like the idea of the counter. Thanks. Is there somewhere I can read more about this?




          Originally posted by koganam View Post
          You would have to code it from scratch: the Strategy Builder would not be able to handle it.

          #Variables
          Code:
          private int counter = 0; //class variable to store count
          #OnBarUpdate
          Code:
          counter = (FirstTickOfBar && Close[0] < Open[0]) ? counter++ : 0; //test for red bar and up counter or reset it as necessary
          if (counter >= 6) MakeMegaBucks();
          That code takes account of the fact that the data is being streamed and not a static space, and so makes efficient use of the data stream. You could always instead ignore the true nature of your data and use an inefficient "for loop" on every bar, which unfortunately is usually the answer that you would be given.

          Comment


            #6
            Originally posted by ScottieDog View Post
            Thanks. Saw your reply after my post #4.

            I like the idea of the counter. Thanks. Is there somewhere I can read more about this?
            I am not sure what you asking. How to implement a counter?

            Such is a concomitant part of almost all data-driven and event-driven programming, which ultimately is all that NT is. It receives data, which receipt is handled by the arrival of a tick event.

            All that I have done here is that instead of receiving/accumulating the data, then iterating backward over it (a "for loop"), I have matched my processing to the data model, and handled the data as it came in. Dynamic data, dynamic processing.

            ref: https://en.wikipedia.org/wiki/Data-driven_programming

            Comment


              #7
              Originally posted by koganam View Post
              You would have to code it from scratch: the Strategy Builder would not be able to handle it.

              #Variables
              Code:
              private int counter = 0; //class variable to store count
              #OnBarUpdate
              Code:
              counter = (FirstTickOfBar && Close[0] < Open[0]) ? counter++ : 0; //test for red bar and up counter or reset it as necessary
              if (counter >= 6) MakeMegaBucks();
              That code takes account of the fact that the data is being streamed and not a static space, and so makes efficient use of the data stream. You could always instead ignore the true nature of your data and use an inefficient "for loop" on every bar, which unfortunately is usually the answer that you would be given.
              hey, koganam,
              why do you use FirstTickOfBar in this part of code?

              counter = (FirstTickOfBar && Close[0] < Open[0]) ? counter++ : 0; //test for red bar and up counter or reset it as necessary
              if (counter >= 6) MakeMegaBucks();

              thanks

              Comment


                #8
                Originally posted by Laimis View Post
                hey, koganam,
                why do you use FirstTickOfBar in this part of code?

                counter = (FirstTickOfBar && Close[0] < Open[0]) ? counter++ : 0; //test for red bar and up counter or reset it as necessary
                if (counter >= 6) MakeMegaBucks();

                thanks
                So that each bar processes the counter once and only once, regardless the setting for CalculateOnBarClose.

                Comment


                  #9
                  Originally posted by koganam View Post
                  So that each bar processes the counter once and only once, regardless the setting for CalculateOnBarClose.
                  Thanks, but all I get is zero
                  in variable:
                  private int counter = 0;

                  on bar update

                  counter = (FirstTickOfBar && Close[0] > SMA(period)[0]) ? counter++ : 0;
                  Print(counter);

                  i get zero I really need some advise...
                  thank you

                  Comment


                    #10
                    Hello Laimis,

                    I would suggest taking a look at the documentation for the ? operator: https://msdn.microsoft.com/en-us/lib...v=vs.100).aspx

                    Take a look at the statement koganam provided:
                    Code:
                    counter = (FirstTickOfBar && Close[0] > SMA(period)[0]) ? counter++ : 0;
                    In order for the counter to increment, the condition that is being evaluated must be true. If it is not true, it'll reset the counter variable to 0.

                    I would suggest placing a print before this statement to check the values of Close[0] and SMA(period)[0].

                    Example:
                    Code:
                    if (FirstTickOfBar)
                    {
                         Print("Close[0] " + Close[0]);
                         Print("SMA(period[0] " + SMA(period)[0]);
                    }
                    
                    counter = (FirstTickOfBar && Close[0] > SMA(period)[0]) ? counter++ : 0;
                    Zachary G.NinjaTrader Customer Service

                    Comment


                      #11
                      thanks. I tested the code again using your advice.
                      and it seems, some how counter++ doesn't add to counter = 0. then i changed it to counter+1, it works now.
                      do you have any ideas why is that so? nor urgent, but I'm learning C#, so it helps when I understand it .

                      thanks again

                      Comment


                        #12
                        Hello Laimis,

                        Thank you for your patience.

                        It looks like using counter++ would, indeed, not return the expected output in this scenario.

                        What's happening is that the code is evaluating what counter is (0), increments it by 1 (0 -> 1), but then assigns counter back to its original value (0).

                        You would be able to use counter + 1 (as you have explained) OR you can do ++counter rather than counter++.
                        Zachary G.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ZacharyG View Post
                          Hello Laimis,

                          Thank you for your patience.

                          It looks like using counter++ would, indeed, not return the expected output in this scenario.

                          What's happening is that the code is evaluating what counter is (0), increments it by 1 (0 -> 1), but then assigns counter back to its original value (0).

                          You would be able to use counter + 1 (as you have explained) OR you can do ++counter rather than counter++.
                          Yes, of course. Unary incrementer. Must increment before assignation.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          601 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          347 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          103 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          559 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          558 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X