Skip to content

Candlestick: draw horizontal line if open equals close #3678

@swharden

Description

@swharden

antilles79 from the Discord (#1966) noted that candlestick plots have two issues:

  • candles at X=0 have unexpected width
  • candles where open equals close do not display a horizontal line
List<OHLC> price = new List<OHLC>();

price.Add(new OHLC(100, 103, 99, 102, new DateTime(1899, 12, 30), TimeSpan.FromDays(1)));
price.Add(new OHLC(100, 103, 99, 102, new DateTime(1899, 12, 31), TimeSpan.FromDays(1)));
price.Add(new OHLC(101, 103, 99, 101, new DateTime(1900, 1, 1), TimeSpan.FromDays(1)));
price.Add(new OHLC(100, 103, 99, 102, new DateTime(1900, 1, 2), TimeSpan.FromDays(1)));
price.Add(new OHLC(100, 103, 99, 102, new DateTime(1900, 1, 3), TimeSpan.FromDays(1)));

formsPlot1.Plot.Add.Candlestick(price);

image

These issues can probably be resolved by editing this section of code:

for (int i = 0; i < ohlcs.Count; i++)
{
OHLC ohlc = ohlcs[i];
bool isRising = ohlc.Close >= ohlc.Open;
LineStyle lineStyle = isRising ? RisingLineStyle : FallingLineStyle;
FillStyle fillStlye = isRising ? RisingFillStyle : FallingFillStyle;
float top = Axes.GetPixelY(ohlc.High);
float bottom = Axes.GetPixelY(ohlc.Low);
float center, left, right;
if (Sequential == false)
{
center = Axes.GetPixelX(ohlc.DateTime.ToNumber());
TimeSpan halfWidth = new((long)(ohlc.TimeSpan.Ticks * SymbolWidth / 2));
DateTime leftTime = ohlc.DateTime - halfWidth;
DateTime rightTime = ohlc.DateTime + halfWidth;
left = Axes.GetPixelX(leftTime.ToNumber());
right = Axes.GetPixelX(rightTime.ToNumber());
}
else
{
center = Axes.GetPixelX(i);
left = Axes.GetPixelX(i - (float)SymbolWidth / 2);
right = Axes.GetPixelX(i + (float)SymbolWidth / 2);
}
// do not render OHLCs off the screen
if (right < rp.DataRect.Left || left > rp.DataRect.Right)
continue;
float open = Axes.GetPixelY(ohlc.Open);
float close = Axes.GetPixelY(ohlc.Close);
// center line
using SKPath path = new();
path.MoveTo(center, top);
path.LineTo(center, bottom);
lineStyle.ApplyToPaint(paint);
rp.Canvas.DrawPath(path, paint);
// rectangle
SKRect rect = new(left, Math.Max(open, close), right, Math.Min(open, close));
fillStlye.ApplyToPaint(paint, rect.ToPixelRect());
rp.Canvas.DrawRect(rect, paint);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good First IssueThis issue has limited complexity and may be a good start for new contributorsHelp WantedScott won't do this soon, but PRs from the community are welcome!⚠️ HIGH PRIORITY

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions