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():
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(); } }
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.
Comment