Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stumped in OnRender()

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

    Stumped in OnRender()

    Hey guys,

    I've been staring at this for three days and I can't figure out what I've done wrong.

    I'm trying to convert a MACD surrounded by Bollinger Bands - I'm sure you've seen the indicator all over. The first thing the old Plot() override does is color the zero line depending on whether the MACD is above/below the zero line. Color it red when the MACD is below the zero line and blue when it is above. (See the first image.)

    Still looking at the first image, see how on the left side, the color of the zero line is wrong.? (It is blue and the MACD is below the zero line.) Now check it out when I click the left horizontal scroll button in the bottom left hand of the screen one time. (See the second image)

    Now, in the second image, it is correct and not only that, it colored like 20 bars when I only moved the chart one bar. My first inclination is to think I'm doing something wrong but I have no idea what it could be. Here is my OnRender():
    Code:
    		protected override void OnRender( ChartControl chartControl, ChartScale chartScale )
    		{
    			if( base.Bars == null
                                || ChartBars.FromIndex < 1 ) return;
    
    			base.OnRender( chartControl, chartScale );
    				
              	        SharpDX.Direct2D1.Brush lineBrush = null;
    			SharpDX.Direct2D1.Brush lastLineBrush = null;
    			
    			// y = the zero line  (I'm sure there's an easier way)
    			int y = (ChartPanel.Y + ChartPanel.H) - ((int) (((0 - ChartPanel.MinValue) / (ChartPanel.MaxValue-ChartPanel.MinValue)) * ChartPanel.H));
    			
    			int LastX = chartControl.CanvasLeft;
    			int barPaintWidth = chartControl.GetBarPaintWidth( ChartBars );
    			int bars = ChartControl.SlotsPainted;
    			
    			bool lineUp = false;
    			int x = ChartControl.CanvasRight;
    			int x1 = ChartControl.CanvasRight;
    			
    			bool firstBarPainted = false;
    			
    			while( bars >= 0 )
          		        {
    				int idx = ((ChartControl.LastSlotPainted - ChartControl.SlotsPainted) + 1) + bars;
    
    				int slotsPainted = ChartControl.SlotsPainted;
    				x1 = (((ChartPanel.W - chartControl.BarMarginLeft) - (int)(barPaintWidth/2)) - ((ChartControl.SlotsPainted - 1) * (int)ChartControl.BarWidth)) + ((ChartControl.SlotsPainted - 1) * ((int)ChartControl.BarWidth));
    				
    				double macdVal = dotSeries.GetValueAt(idx);
    				
    				x = chartControl.GetXByBarIndex( ChartBars, idx );
    				double prevMacdVal = dotSeries.GetValueAt(idx-1);
    				
    				// Draw zero line (blue for uptrend, red for downtrend)
    				if( (prevMacdVal <= 0 && macdVal > 0) 
    				     || (prevMacdVal >= 0 && macdVal < 0) )	// Did the macd just cross upwards/downwards over the zero line?
    				{																	// Yes...
    					if( prevMacdVal <= 0 
    			                    && macdVal > 0 )						// Did macd just cross upwards above the zero line?
    					{															// Yes...
    						lineBrush = UpLineColor.ToDxBrush(RenderTarget);
    						lineBrush.Opacity = 10f;
    						lineUp = true;
    					}
    					else if( prevMacdVal >= 0 					// Or did macd just cross downwards below the zero line?
    			            	            && macdVal < 0 )							
    					{
    						lineBrush = DownLineColor.ToDxBrush(RenderTarget);
    						lineBrush.Opacity = 10f;
    						lineUp = false;
    					}
    
    					// We're finally ready to fill in our line from the last crossover
    					RenderTarget.DrawLine( new SharpDX.Vector2( ChartControl.CanvasLeft, y), new SharpDX.Vector2(LastX, y), lineBrush, ZeroLineWidth );
    		        	         LastX = x;																// Save crossover point.
    				}
    				
    				if( lineUp )
    				{
    					lastLineBrush = UpLineColor.ToDxBrush(RenderTarget);
    				}
    				else
    				{
    					lastLineBrush = DownLineColor.ToDxBrush(RenderTarget);
    				}
    				RenderTarget.DrawLine( new SharpDX.Vector2( LastX, y), new SharpDX.Vector2(x, y), lastLineBrush, ZeroLineWidth );
    				--bars;
    			}
    			if( lastLineBrush != null ) {
    				lastLineBrush.Dispose();
    			}
    			if( lineUp )
    			{
    				lastLineBrush = UpLineColor.ToDxBrush(RenderTarget);
    			}
    			else
    			{
    				lastLineBrush = DownLineColor.ToDxBrush(RenderTarget);
    			}
    			RenderTarget.DrawLine( new SharpDX.Vector2( LastX, y), new SharpDX.Vector2(x, y), lastLineBrush, ZeroLineWidth );
    			
    			if( lastLineBrush != null ) {
    				lastLineBrush.Dispose();
    			}
    			if( lineBrush != null ) {
    				lineBrush.Dispose();
    			}
    			
    		}
    If someone can see what I've done wrong you would make my day!
    Thank you (in advance!)!!

    Note: the original author used the loop "while( bars >= 0 )". I've also tried "for( int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++ )" and the same thing happens - you scroll one bar and the line on half the chart changes when only one bar should change.
    Attached Files

    #2
    Hi traderpards,

    As this is a logic inquiry, I would not be able to debug the code to find cause of the behavior on your behalf.

    However, this thread will remain open for any community members that would like to assist.


    My suggestion would be to use prints one step at a time.

    Below are some general suggestions you may find helpful.

    The 0 line, appears to be several lines. You would want to check the x and y values used for each segment of the line and make sure you have the right number of lines making up this larger line.

    When you mention that the line is being colored for 20 bars and this was unexpected, this may indicate that the line was longer than expected and should be broken into smaller line segments.

    Basically, for a single line that is rendered (instead of using a line plot) the entire line will be one color. This means to have a line appear to be different colors, you need several smaller lines.

    You may find it easier to use a line plot and just color particular bars of the plot the colors you would like.

    Last, the bars variable is declared outside of the code you have posted, so any viewer would not be able to know at what value this is initialized or if it is ever changed in another part of the code. The behavior of the loop depends on the value of that variable.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for looking at this Chelsea!

      I figured it out. Next time I'll post my question before I stare at it for three days...

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Stanfillirenfro, Today, 07:23 AM
      1 response
      2 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by cmtjoancolmenero, Yesterday, 03:58 PM
      2 responses
      20 views
      0 likes
      Last Post cmtjoancolmenero  
      Started by olisav57, Yesterday, 07:39 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by cocoescala, 10-12-2018, 11:02 PM
      7 responses
      943 views
      0 likes
      Last Post Jquiroz1975  
      Started by oviejo, Today, 12:28 AM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X