Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Refer to the last candle of a type -- need help

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

    Refer to the last candle of a type -- need help

    Hello all,

    I want to be able to refer to the last green or red candle that occurred while I am in a long or short position respectively, such that I am able to deploy actions upon said candle. For example, if I am in a long position, I want my code to reference the last green candle after every bar close and place a stop loss at the open of said candle.

    I know I can refer x bars back, but in this scenario I have no way of knowing how many bars back the last green candle occurred, therefore I can't refer to it.

    Is there a way I can do this in the strategy builder? That would be my preference, as I am not a strong programmer, but if manual code is my only option then bring it on, thank you.

    #2

    Comment


      #3
      I'm sorry, but what does this obscure bug have to do with what I'm asking? Could you give me a little more to go on, please?

      Thank you.

      Comment


        #4
        Hi, lunardiplomacy, thanks for your post.

        To check if a bar is green would be if(Close[0] > Open[0]). When that condition occurs, save CurrentBar to a variable. Then you can reference that variable when Position.MarketPosition == MarketPosition.Long.

        To check if you have a green bar and the position is long if would be written:

        Code:
        if(Position.MarketPosition == MarketPosition.Long)
        {
            if(Close[0] > Open[0])
            {
        
            }
        }
        Best regards,

        -ChrisL
        Last edited by NinjaTrader_ChrisL; 12-01-2019, 05:11 PM.

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          Hi, lunardiplomacy, thanks for your post.

          To check if a bar is green would be if(Close[0] > Open[0]). When that condition occurs, save CurrentBar to a variable. Then you can reference that variable when Position.MarketPosition == MarketPosition.Long.

          To check if you have a green bar and the position is long if would be written:

          Code:
          if(Position.MarketPosition == MarketPosition.Long)
          {
          if(Close[0] > Open[0])
          {
          
          }
          }
          Best regards,

          -ChrisL
          Right, that would be to check if the current bar is green. But, I want to be able to find the LAST green bar, whether that is the current bar or not. i.e. say there is a green bar and then five red bars, I want to be able to the ohlc of the green bar five bars ago. is this possible with the strategy builder?

          Comment


            #6
            Originally posted by NinjaTrader_ChrisL View Post
            Hi, lunardiplomacy, thanks for your post.

            To check if a bar is green would be if(Close[0] > Open[0]). When that condition occurs, save CurrentBar to a variable. Then you can reference that variable when Position.MarketPosition == MarketPosition.Long.

            To check if you have a green bar and the position is long if would be written:

            Code:
            if(Position.MarketPosition == MarketPosition.Long)
            {
            if(Close[0] > Open[0])
            {
            
            }
            }
            Best regards,

            -ChrisL
            Do you see what I am saying? I understand how to create a green bar in a long trade as a variable, but then how would I reference the ohlc of said bar as an action, i.e. a stop loss.

            Comment


              #7
              Hi lunardiplomacy, thanks for your reply.
              To check for a green bar 1 bar ago it would be written Close[1] > Open[1]. We have this documentation page on accessing bars:




              Comment


                #8
                Originally posted by NinjaTrader_ChrisL View Post
                Hi lunardiplomacy, thanks for your reply.
                To check for a green bar 1 bar ago it would be written Close[1] > Open[1]. We have this documentation page on accessing bars:



                Thank you for putting up with my persistence, Chris, and for being patient with me. So, if I am understanding correctly, I should check for a green bar x bars back by checking that the previous bar is green repeatedly until the conditions for a green bar are met? Coding this 10 times over perhaps, assuming the last green bar would almost always fall within that range? I figured I could do that initially, it just seemed rather lacking in elegance, so I was wondering if there was a more efficient way to do it.

                Also, one last question and then I'll stop bothering you. I know you can reference a previous bar by including its index number in brackets, as in Close[10] which would yield the bar that occurred 10 bars ago, but is it possible to do something like this -- Close[0:10], in order to process some condition between the current bar and the bar 10 bars ago? For example if I wanted to find the low of the last 10 bars I could do Low[0:10] -- I know this doesn't actually work, but if you could teach me how something like this translates to C# it would be much appreciated, it's not necessarily related to the initial question.

                Thanks, Chris

                Comment


                  #9
                  Hello lunardiplomacy,

                  Thanks for your reply.

                  Regarding, "So, if I am understanding correctly, I should check for a green bar x bars back by checking that the previous bar is green repeatedly until the conditions for a green bar are met? Coding this 10 times over perhaps, assuming the last green bar would almost always fall within that range? I figured I could do that initially, it just seemed rather lacking in elegance, so I was wondering if there was a more efficient way to do it." As Chris previously advised, check to see that you are in a position and then check each new candle for Close[0] > open[0] as the latest green candle and when true, you can employ your actions.

                  Regarding, "...if I wanted to find the low of the last 10 bars I could do Low[0:10] -- I know this doesn't actually work, but if you could teach me how something like this translates to C# it would be much appreciated, it's not necessarily related to the initial question."
                  T
                  here are a couple of ways:

                  1) In C# you could use a for loop:

                  [I]double myLow = double.MaxValue; // set the variable myLow to the maximum possible double value.
                  myLow = int.ma
                  for (int i = 0; i <= 10; i++)
                  {
                  if (Low < myLow) // cycle through 0 to 10 bars ago to find the lowest
                  {
                  myLow = Low[0]
                  }
                  }


                  2) A better way would be to use the MIN() method: https://ninjatrader.com/support/help...inimum_min.htm

                  double myLow = MIN(Low, 10)[0]; // pull the current value of lowest value of the Low data series from the last 10 bars

                  Comment


                    #10
                    Hi again, Paul,

                    Thank you for pointing out the MIN() method, that is precisely what I was looking for. Also, thank you for your help with the simple horizontal lines indicator, it came out swimmingly.

                    Comment


                      #11
                      Hi,

                      I'm trying to find the last green and red candles. Based on the above example, I'm able to find the last green candle with myHigh, but myLow doesn't seem to be finding the last red candle. Could you please help?

                      Thanks

                      double myLow = double.MinValue;

                      myLow = int.MinValue;

                      for (int index = 0; index < 20; index++)

                      {

                      if (Closes[1][index] < Opens[1][index])

                      {

                      myLow = Closes[1][0];

                      }

                      }



                      double myHigh = double.MaxValue;

                      myHigh = int.MaxValue;

                      for (int index = 0; index < 20; index++)

                      {

                      if (Closes[1][index] > Opens[1][index])

                      {

                      myHigh = Closes[1][0];

                      }

                      }

                      Comment


                        #12
                        Hello AgriTrdr,

                        Thank you for your inquiry.

                        This code is looping through the last 20 bars to find the occurrences. If you are just looking for the latest instance of a red and green bar on the chart, the loop is not necessary. You can simply detect when Close[0] > Open[0] for a green bar and opposite for a red bar, and save your myHigh and myLow values when detected.

                        //green bar
                        if (Close[0] > Open[0])
                        myHigh = Close[0];

                        //red bar
                        if (Close[0] < Open[0])
                        myLow = Close[0];

                        As the strategy processes all the available bars it save the latest instance of each to their respective variables.

                        Comment


                          #13
                          Thank you! Do I need to add double at for myLow and myHigh?

                          //green bar
                          double myHigh = 0;
                          if (Close[0] > Open[0])
                          myHigh = Close[0];

                          //red bar
                          double myLow = 0;
                          if (Close[0] < Open[0])
                          myLow = Close[0];​

                          Comment


                            #14
                            Hello AgriTrdr,

                            If you define it that way it is going to be reset back to 0 on each pass of OnBarUpdate. It may be better to declare them as the class level.

                            Comment


                              #15
                              That worked. Thank you!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              46 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              126 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              66 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              42 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              46 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X