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

Expanding Rectangle

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

    Expanding Rectangle

    Hello there!


    Trying to create a new indicator, but having some troubles..

    Let say I have a condition:

    if (High [0] > High [1])
    DrawRectangle("AA" + CurrentBar, false, Time[1], High[1], Time[0].AddMinutes(60), Low[1], Color.Blue, color, opacity);

    Instead of this, I want to stop drawing my rectangle when the price will be below Low[1]. So I need to write down a specific condition and change Time[0].AddMinutes(60) for something like Time[???], but all my previous attempts were unsuccessful.

    How I will reference to that bar in the future in general? Since the rectangle function requires int DateTime or BarsAgo, how should I allocate DateTime of that previous bar, in order to call it later and compare its Low?


    Thanks a lot,
    Arthur
    Last edited by FxInception; 02-25-2014, 03:03 PM.

    #2
    Hello Arthur,

    The trouble with this is that you will not know when the price goes below Low[1] so you cannot set the time in the future.

    You may want to keep redrawing your rectangle until the price goes below the value that you are looking for. Doing it this way you may not want to use "CurrentBar" in your tag as this will cause many different rectangles to be drawn.

    If you wanted multiple rectangles then you may create a variable that increments after the price goes below the Low so that you can draw another rectangle.

    Let me know if you have any questions.
    JCNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_JC View Post
      Hello Arthur,

      The trouble with this is that you will not know when the price goes below Low[1] so you cannot set the time in the future.

      You may want to keep redrawing your rectangle until the price goes below the value that you are looking for. Doing it this way you may not want to use "CurrentBar" in your tag as this will cause many different rectangles to be drawn.

      If you wanted multiple rectangles then you may create a variable that increments after the price goes below the Low so that you can draw another rectangle.

      Let me know if you have any questions.
      Thanks JC,


      I was thinking about the redrawing option, but yes, since I need multiple rectangles, I refused this idea.

      What about the variable, which increments after my condition is met? I have no clue how to set it..


      Regards,
      Arthur

      Comment


        #4
        Hello Arthur,

        You may try something like:

        Code:
        private bool conditionMet = false;
        private int counter = 0;
        private int barConditionMet = 0;
        
        protected override void Initialize()
        {
              Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "PlotHigh"));
              Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "PlotLow"));
              Overlay				= true;
        }
        		
        
        protected override void OnBarUpdate()
        {
        	if(CurrentBar < BarsRequired)
        	{
        		PlotHigh.Set(High[0]);
        		PlotLow.Set(Low[0]);
        		return;
        	}
        				
        	if(High[0]>High[1] && !conditionMet)
        	{
        		barConditionMet = CurrentBar;
        		conditionMet = true;
        	}
        			
        	if( conditionMet )
        	{
        		PlotHigh.Set(High[CurrentBar-barConditionMet]);
        		DrawRegion("AA"+counter, CurrentBar-barConditionMet, 0,PlotHigh, Low[CurrentBar-barConditionMet],  Color.Blue, Color.Blue, 2);
        	}
        			
        	if ( conditionMet && Low[CurrentBar-barConditionMet] > Close[0])
        	{
        		conditionMet = false;
        		counter++;
        	}		
        }
        #region Properties
                [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                [XmlIgnore()]
                public DataSeries PlotHigh
                {
                    get { return Values[0]; }
                }
        	[Browsable(false)]
                [XmlIgnore()]
                public DataSeries PlotLow
                {
                    get { return Values[1]; }
                }
        #endregion
        JCNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_JC View Post
          Hello Arthur,

          You may try something like:

          Code:
          private bool conditionMet = false;
          private int counter = 0;
          private int barConditionMet = 0;
          
          protected override void Initialize()
          {
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "PlotHigh"));
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "PlotLow"));
                Overlay				= true;
          }
          		
          
          protected override void OnBarUpdate()
          {
          	if(CurrentBar < BarsRequired)
          	{
          		PlotHigh.Set(High[0]);
          		PlotLow.Set(Low[0]);
          		return;
          	}
          				
          	if(High[0]>High[1] && !conditionMet)
          	{
          		barConditionMet = CurrentBar;
          		conditionMet = true;
          	}
          			
          	if( conditionMet )
          	{
          		PlotHigh.Set(High[CurrentBar-barConditionMet]);
          		DrawRegion("AA"+counter, CurrentBar-barConditionMet, 0,PlotHigh, Low[CurrentBar-barConditionMet],  Color.Blue, Color.Blue, 2);
          	}
          			
          	if ( conditionMet && Low[CurrentBar-barConditionMet] > Close[0])
          	{
          		conditionMet = false;
          		counter++;
          	}		
          }
          #region Properties
                  [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                  [XmlIgnore()]
                  public DataSeries PlotHigh
                  {
                      get { return Values[0]; }
                  }
          	[Browsable(false)]
                  [XmlIgnore()]
                  public DataSeries PlotLow
                  {
                      get { return Values[1]; }
                  }
          #endregion
          Thank you very much, I almost got what I want!

          Still have a few question to clarify:

          1). What "!" signifies in "!conditionMet" expression?

          2). And since we have "private bool conditionMet = false", does
          "if (conditionMet)
          Draw..... " represents "if conditionMet = false" at the same time or not?
          I just can't construct a few logical sentences from what I see in the code, in order to be able to make it myself next time.

          3). And the last thing, which will make my puzzle complete. From the point when (High[0] > High[1]) till (Low[CurrentBar-barConditionMet] > Close[0]) - there are situations which fit the conditions in code. BUT, the new sequence will not be processed until the initial Low will be broken through. Could you suggest me please how to make those instances processed as well, regardless the fact that initial Low is not yet broken. I assume the counter makes it a bit tricky..


          Many thanks,
          Arthur

          Comment


            #6
            Hello Arthur,

            1. The ! expression is the "not" expression meaning that the condition is not met. It is the same thing as "conditionMet != true" basically.

            2. An "if" statement is only going to proceed forward if the condition is true so since "conditionMet" is just a bool it is short hand for "if ( conditionMet == true )" so that it is only drawing when it meets your condition.

            3. For that you will need to modify the code fairly significantly because you would be drawing/redrawing multiple objects at the same time. You may want to try something like a List or an Array so that you can loop through each instance of your conditions being true and check to see if the ending point has been reached yet.
            JCNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_JC View Post
              Hello Arthur,

              1. The ! expression is the "not" expression meaning that the condition is not met. It is the same thing as "conditionMet != true" basically.

              2. An "if" statement is only going to proceed forward if the condition is true so since "conditionMet" is just a bool it is short hand for "if ( conditionMet == true )" so that it is only drawing when it meets your condition.

              3. For that you will need to modify the code fairly significantly because you would be drawing/redrawing multiple objects at the same time. You may want to try something like a List or an Array so that you can loop through each instance of your conditions being true and check to see if the ending point has been reached yet.

              Ok, I've got it, thank you for clear explanation.

              Returning back to the code, I deleted those plots and sets, as I don't need them. So, that's what I left with.

              Code:
              private bool conditionMet = false;
              private int counter = 0;
              private int barConditionMet = 0;
              
              protected override void Initialize()
              {
                    Overlay  =  true;
              }
              	
              protected override void OnBarUpdate()
              {
              	if(CurrentBar < BarsRequired)
              	   return;
              				
              	if(High[0]>High[1] && !conditionMet)
              	{
              		barConditionMet = CurrentBar;
              		conditionMet = true;
              	}
              			
              	if(conditionMet)
              	{
              	  DrawRegion("AA"+counter, CurrentBar-barConditionMet, 0,PlotHigh, Low[CurrentBar-barConditionMet],  Color.Blue, Color.Blue, 2);
              	}
              			
              	if (conditionMet && Low[CurrentBar-barConditionMet] > Close[0])
              	{
              		conditionMet = false;
              		counter++;
              	}		
              }
              I'm not falimiar with the Lists and Arrays at all. Therefore, I was trying to find any other option to get what I need.

              Since the code draws rectangle when "conditionMet = true" and stops when it is false, and what I need is to start drawing another while it is still true - I was thinking to use the Most Recent Occurences and then compare the dates and times of those along with boolens. And then simply add the multiplier to a counter in order to not to mix them up.

              I've met two problems:

              1). ">" cannot be applied to System.DateTime operands
              2). It does not recognise the actual boolens like "conditionMet", stating that it does not exist for some reason

              That's what I've got:

              int MROTrue = MRO(delegate {return ConditionMet = True;}, 1, Bars.BarsSinceSession);
              int SecondMROTrue = MRO(delegate {return ConditionMet = True;}, 2, Bars.BarsSinceSession);
              int MROFalse = MRO(delegate {return ConditionMet = False;}, 1, Bars.BarsSinceSession);

              if ((Time[MROTrue] > Time[SecondMROTrue] > Time[MROFalse]) && (conditionMet))
              {
              DrawRectangle("AA" + counter * 1000000, false, CurrentBar-barConditionMet, 0, PlotHigh, Low[CurrentBar-barConditionMet], Color.Blue, Color.Blue, 2);
              }

              At the same time, in order to stop drawing that particular rectangle earlier, I believe I need to link "conditionMet = false" somehow with "AA" + counter * 1000000, which is something I'm not sure about as well.

              So I'm totally struggling now and will appreciate any help.


              Many thanks,
              Arthur
              Last edited by FxInception; 02-26-2014, 08:09 PM.

              Comment


                #8
                Hello Arthur,

                1. Inside of an "if" statement you can only compare two different items at a time so you may want to change your condition from:

                if ((Time[MROTrue] > Time[SecondMROTrue] > Time[MROFalse]) && (conditionMet))

                To:

                if ((Time[MROTrue] > Time[SecondMROTrue] && Time[SecondMROTrue] > Time[MROFalse]) && (conditionMet))

                2. The "conditionMet" variable that I gave in my example has a lower case "c" which is why it may not think it does not exist unless you have declared it as another variable. Programming in C# is case sensitive.

                I also see that the DrawRectangle arguments will need to be modified after you some changes are made to resolve the first two items.
                JCNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_JC View Post
                  Hello Arthur,

                  1. Inside of an "if" statement you can only compare two different items at a time so you may want to change your condition from:

                  if ((Time[MROTrue] > Time[SecondMROTrue] > Time[MROFalse]) && (conditionMet))

                  To:

                  if ((Time[MROTrue] > Time[SecondMROTrue] && Time[SecondMROTrue] > Time[MROFalse]) && (conditionMet))

                  2. The "conditionMet" variable that I gave in my example has a lower case "c" which is why it may not think it does not exist unless you have declared it as another variable. Programming in C# is case sensitive.

                  I also see that the DrawRectangle arguments will need to be modified after you some changes are made to resolve the first two items.
                  JC,

                  I've changed what you said as well as attempted to modify DrawRectangle several times, but still came up with nothing.

                  Code:
                  int MROTrue = MRO(delegate {return conditionMet;}, 1, Bars.BarsSinceSession);
                  int SecondMROTrue = MRO(delegate {return conditionMet;}, 2, Bars.BarsSinceSession);		
                  int MROFalse = MRO(delegate {return !conditionMet;}, 1, Bars.BarsSinceSession);
                  		
                  if ((Time[MROTrue] > Time[SecondMROTrue]) && (Time[SecondMROTrue] > Time[MROFalse]) && (conditionMet))
                  {    
                  	conditionMet[1] = true;
                  	DrawRectangle("AA" + counter*10000, false, CurrentBar-MROTrue, High[CurrentBar-MROTrue], 0, Low[CurrentBar-MROTrue], Color.Blue, Color.Blue, 2);
                  }			
                  if (conditionMet && Low[CurrentBar-MROTrue] > Close[0])
                  {
                  conditionMet = false;
                  counter++;
                  Being doing this, I thought that I have to get familiar with Arrays, as I suppose it will be crucial aspect of coding in future.

                  As far as I understand, we need them to link different conditions within the code to a single variable and apply the same action to those different conditions by refering just to that single variable. Correct me, if I'm wrong.

                  So, in the code you have initially provided, we have "private bool conditionMet = false", according to which the indicator starts or stops drawing rectangle.

                  I initialize the array:

                  Code:
                  private bool[] conditionMet = new bool[10]
                  Then I understand that if I have the actual condition, I write:
                  if (.......)
                  conditionMet[0] = true;

                  But what about: if(High[0]>High[1] && !conditionMet)
                  or if(conditionMet)??

                  Do I need to use separate index numbers for these ones? Or everything what was mentioned regarding conditionMet in your code will have the same index number and it will have to be different, only if I will add my extra comparisons? Planty of questions...

                  And by the way, having

                  Code:
                  if(High[0]>High[1] && !conditionMet)
                  {
                  	barConditionMet = CurrentBar;
                  	[I]conditionMet = true[/I];
                  , why I can't mention rectangle condition right after this, since the conditionMet is true?

                  Code:
                  if([I]conditionMet[/I])
                  	{
                  	  DrawRegion("AA"+counter, CurrentBar-barConditionMet, 0,PlotHigh, Low[CurrentBar-barConditionMet],  Color.Blue, Color.Blue, 2);
                  	}
                  but have to create a new "if" instead?


                  Many thanks,
                  Arthur
                  Last edited by FxInception; 03-01-2014, 10:15 AM.

                  Comment


                    #10
                    Hello Arthur,

                    Using the MRO like you are doing, you may want to use a BoolSeries instead of a normal array. Just using two variables you are going to be overlapping them if you have more than two occurrences of your condition being true. It is still going to be array like but since you are comparing it to the bars data series this will create an array object for each bar so that you can check back to see what bar your condition was true at.




                    When creating your a NinjaScript file it is always good to check to see if you are getting the values you would expect. Using the Print() method is a good way to understand what your code is doing if it is not doing something that you are expecting. Here is a good example of how you can debug your code that you may want to add in your code if your strategy is not behaving in the way that you would expect.




                    Note that there is a limitation of the draw rectangle if 2 or more points are outside of the visible range of the chart you may not see the DrawRectangle(). If you expand the range of the chart you should be able to see the draw rectangle to show up or if you switch to the DrawRegion() method that does not have this limitation.

                    Let me know if the BoolSeries will work better for what you are trying to accomplish.
                    JCNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Perr0Grande, Today, 08:16 PM
                    0 responses
                    2 views
                    0 likes
                    Last Post Perr0Grande  
                    Started by elderan, Today, 08:03 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post elderan
                    by elderan
                     
                    Started by algospoke, Today, 06:40 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post algospoke  
                    Started by maybeimnotrader, Today, 05:46 PM
                    0 responses
                    11 views
                    0 likes
                    Last Post maybeimnotrader  
                    Started by quantismo, Today, 05:13 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post quantismo  
                    Working...
                    X