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

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.
        Chris L.NinjaTrader Customer Service

        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:




              Chris L.NinjaTrader Customer Service

              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
                  Paul H.NinjaTrader Customer Service

                  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

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by wzgy0920, 04-20-2024, 06:09 PM
                    2 responses
                    27 views
                    0 likes
                    Last Post wzgy0920  
                    Started by wzgy0920, 02-22-2024, 01:11 AM
                    5 responses
                    32 views
                    0 likes
                    Last Post wzgy0920  
                    Started by wzgy0920, 04-23-2024, 09:53 PM
                    2 responses
                    61 views
                    0 likes
                    Last Post wzgy0920  
                    Started by Kensonprib, 04-28-2021, 10:11 AM
                    5 responses
                    193 views
                    0 likes
                    Last Post Hasadafa  
                    Started by GussJ, 03-04-2020, 03:11 PM
                    11 responses
                    3,235 views
                    0 likes
                    Last Post xiinteractive  
                    Working...
                    X