• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Draw rectangle syntax

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

  • 2Look4me
    started a topic Draw rectangle syntax

    Draw rectangle syntax

    Greetings,
    I'm trying to figure out the syntax then draw a rectangle; for instance with a triple crossover that will cover the highs and lows of that entire crossover as in the screenshot.

    What would I need for the startBarsAgo, startY, endBarsAgo, endY parameters to accomplish this?

    Thanks for the assistance.

    #region Variables
    privatebool colorBars = true;
    #endregion

    protectedoverridevoid Initialize()
    {
    Overlay = false;
    CalculateOnBarClose = false;
    }
    protectedoverridevoid OnBarUpdate()
    {
    double MA5 = EMA(Close,5)[0];
    double MA10 = EMA(Close,10)[0];
    double MA20 = EMA(Close,20)[0];
    bool CrossUp = MA5 > MA10 && MA10 > MA20;
    bool CrossDown = MA5 < MA10 && MA10 < MA20;

    if ( CrossUp ) {
    if (colorBars ) {
    BarColor = Color.Cyan;
    }
    }
    else if (CrossDown ) {
    if (colorBars ) {
    BarColor = Color.Violet;
    }
    }
    else if (colorBars) {
    BarColor = Color.DimGray;
    }
    DrawRectangle("Area"+ CurrentBar.ToString(), false, startBarsAgo, startY, endBarsAgo, endY, Color.Empty, Color.Blue, 2);
    Attached Files

  • 2Look4me
    replied
    Cal,

    Thanks for the prompt response and for taking a close look at it.
    Attached Files

    Leave a comment:


  • NinjaTrader_CalH
    replied
    2Look4Me,

    Can you please post your latest rendition of your code so I can review it on my end?

    Leave a comment:


  • 2Look4me
    replied
    Cal,

    It didn't draw the rectangles because I failed to check for enough bars, so I added:
    if (CurrentBar < 1)
    return;

    Now, somehow the drawn rectangles are not meeting my conditions, i.e. when Close >/< HighY/LowY, avgRange, etc and the rectangles are drawn on each individual candle, instead of being drawn over the met conditions. I have included a screenshot.
    Attached Files

    Leave a comment:


  • NinjaTrader_CalH
    replied
    2Look4Me,

    I would expect that you are not hitting the conditions at all then in this case.

    If you put Print() statements in the if statements do you see any prints being generated in the Output window?
    Code:
    if(Close[0] > EMA(200)[0] && Close[0] > HighY && avgRange && EMA(10)[0] > EMA(20)[0] && EMA(20)[0] > EMA(20)[1])
    {
    Print("Bull Bar Cross Up");
    crossBar = CurrentBar;
    }

    Leave a comment:


  • 2Look4me
    replied
    Cal,
    Thanks for taking the time to look thru the code and my apologies for not explaining myself clearly.

    The code compiles without errors, but the rectangles are not drawn out at all in the chart.

    As far as the screenshot goes, the rectangles were drawn manually to depict what I intended the code to do. Somehow the code must have an error in the logic.

    Leave a comment:


  • NinjaTrader_CalH
    replied
    2Look4me,

    I don't see anything wrong with the code or the screenshots. Could you please be a little more specific in what you are having trouble with?

    Leave a comment:


  • 2Look4me
    replied
    Hi Patrick,

    Thanks again for the script. As I try to use the same logic to draw the rectangles, I come across problems when I the script with some other parameters. I know that my logic and syntax aren't there, but I'm stuck

    A) Start bullish rectangle when
    Close > EMA(200) &&
    Close > Yesterday's High &&
    EMA(10) > Ema(20) &&
    EMA(20)[0] > Ema(20)[1] &&
    ATR (6) <=2

    Complete bullish rectangle as EMA(20) turns down
    EMA(20)[0] < EMA(20)[1]

    B) Start bearish rectangle when
    Close < EMA(200) &&
    Close < Yesterday's Low &&
    EMA(10) < Ema(20) &&
    EMA(20)[0] < Ema(20)[1] &&
    ATR (6) <=2

    Complete bearish rectangle as EMA(20) turns up
    EMA(20)[0] > EMA(20)[1]


    #region Variables
    privateint crossBar = 0;
    #endregion

    protectedoverridevoid OnBarUpdate()
    {
    double HighY = PriorDayOHLC().PriorHigh[0];
    double LowY = PriorDayOHLC().PriorLow[0];
    bool avgRange = ATR(10)[0] <= 2;

    //Draw Bullish Rectangle
    if(Close[0] > EMA(200)[0] && Close[0] > HighY && avgRange && EMA(10)[0] > EMA(20)[0] && EMA(20)[0] > EMA(20)[1])
    {
    crossBar = CurrentBar;
    }
    if (EMA(20)[0] <= EMA(20)[1]) //Complete Rectangle as EMA(20) turns down
    {
    crossBar = CurrentBar;
    }
    if(crossBar != 0)
    {
    DrawRectangle("Area"+crossBar, false, CurrentBar - crossBar, MIN(Low, CurrentBar - crossBar)[0], 0, MAX(High, CurrentBar - crossBar)[0], Color.Empty, Color.Blue, 2);
    }

    //Draw Bearish Rectangle
    if(Close[0] < EMA(200)[0] && Close[0] < LowY && avgRange && EMA(10)[0] < EMA(20)[0] && EMA(20)[0] < EMA(20)[1])
    {
    crossBar = CurrentBar;
    }
    if (EMA(20)[0] > EMA(20)[1]) //Complete Rectangle as EMA(20) turns up
    {
    crossBar = CurrentBar;
    }
    if(crossBar != 0)
    {
    DrawRectangle("Area"+crossBar, false, CurrentBar - crossBar, MIN(Low, CurrentBar - crossBar)[0], 0, MAX(High, CurrentBar - crossBar)[0], Color.Empty, Color.Orange, 2);
    }
    Attached Files

    Leave a comment:


  • 2Look4me
    replied
    Thanks again Patrick, that works.

    Leave a comment:


  • NinjaTrader_PatrickH
    replied
    Hello 2Look4me,

    Thank you for your response.

    I would change the conditions as well and work the DrawRectangle in as I have below:
    Code:
            #region Variables
            private int crossBar = 0;
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
    			
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			if(EMA(5)[0] > EMA(10)[0] && EMA(10)[0] > EMA(20)[0]
    				&& (EMA(5)[1] <= EMA(10)[1] || EMA(10)[1] <= EMA(20)[1]))
    			{
    				crossBar = CurrentBar;
    			}
    			
    			if(EMA(5)[0] < EMA(10)[0] && EMA(10)[0] < EMA(20)[0]
    				&& (EMA(5)[1] >= EMA(10)[1] || EMA(10)[1] >= EMA(20)[1]))
    			{
    				crossBar = CurrentBar;
    			}
    			
    			if(crossBar != 0)
    			{
    				DrawRectangle("Area"+crossBar, false, CurrentBar - crossBar, MIN(Low, CurrentBar - crossBar)[0], 0, MAX(High, CurrentBar - crossBar)[0], Color.Empty, Color.Blue, 2);
    			}
    		}

    Leave a comment:


  • 2Look4me
    replied
    Hi Cal,

    In trying to follow your suggestions, it doesn't seem like I got it right or even close to it!

    Thanks for your help.

    #region Variables
    privatedouble StoredBar;
    #endregion

    protectedoverridevoid OnBarUpdate()
    if ( CrossUp ) {
    StoredBar = CurrentBar;
    if (colorBars) {
    BarColor = Color.Cyan;
    }
    }

    else if (CrossDown ) {
    StoredBar1 = CurrentBar;
    if (colorBars ) {
    BarColor = Color.Violet;
    }
    }
    DrawRectangle("Area"+ CurrentBar.ToString(), 1, CurrentBar-StoredBar, 0, 0, Color.Blue);

    Leave a comment:


  • NinjaTrader_CalH
    replied
    Hello 2Look4Me,

    Thank you for your post.

    In your cross detections you would want to store the CurrentBar at the time to pass through the StartBarsAgo, and use CurrentBar - storedBar. This will be applied for each unique tag name for Rectangle.

    Your EndBarsAgo will remain 0 so that it stays active to the point where a new cross is detected.


    Let me know if this helps in your study

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by Hanstanz, Today, 04:44 AM
0 responses
4 views
0 likes
Last Post Hanstanz  
Started by juniortut, Today, 04:38 AM
0 responses
4 views
0 likes
Last Post juniortut  
Started by rojiinko, Today, 03:58 AM
0 responses
4 views
0 likes
Last Post rojiinko  
Started by mreanuzlazx, Today, 01:30 AM
0 responses
4 views
0 likes
Last Post mreanuzlazx  
Started by xinepev, Today, 12:33 AM
0 responses
4 views
0 likes
Last Post xinepev
by xinepev
 
Working...
X