Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-09

TAChart CandleStick Chart

7 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Leonardo M. Ramé

Hi, I inherited TOpenHighLowCloseSeries to implement CandleStick Charts: http://en.wikipedia.org/wiki/Candlestick_chart

I only implemented the Draw method, but it would be nice to have this properties in the final version also:

CandleColor: fill color of default or UP candle.
DownCandleColor: fill color of down candle

By now I use LinePen.Color and DownLinePen.Color as color of brush (fill of candle color) and the border is allways clBlack.

Can someone include it in TAMultiseries?

// interface

{ TCandleStickChart }

TCandleStickChart = class(TOpenHighLowCloseSeries)
public
   procedure Draw(ADrawer: IChartDrawer); override;
end;
// implementation
procedure TCandleStickChart.Draw(ADrawer: IChartDrawer);
   function MaybeRotate(AX, AY: Double): TPoint;
   begin
     if IsRotated then
       Exchange(AX, AY);
     Result := ParentChart.GraphToImage(DoublePoint(AX, AY));
   end;
   procedure DoLine(AX1, AY1, AX2, AY2: Double);
   begin
     ADrawer.Line(MaybeRotate(AX1, AY1), MaybeRotate(AX2, AY2));
   end;
   function GetGraphPointYIndex(AIndex, AYIndex: Integer): Double;
   begin
     if AYIndex = 0 then
       Result := GetGraphPointY(AIndex)
     else
       Result := AxisToGraphY(Source[AIndex]^.YList[AYIndex - 1]);
   end;
   procedure DoRect(AX1, AY1, AX2, AY2: Double);
   var
     r: TRect;
   begin
     with ParentChart do begin
       r.TopLeft := MaybeRotate(AX1, AY1);
       r.BottomRight := MaybeRotate(AX2, AY2);
     end;
     ADrawer.FillRect(r.Left, r.Top, r.Right, r.Bottom);
     ADrawer.Rectangle(r);
   end;
var
   my: Cardinal;
   ext2: TDoubleRect;
   i: Integer;
   x, tw, yopen, yhigh, ylow, yclose: Double;
   p: TPen;
begin
   my := MaxIntValue([YIndexOpen, YIndexHigh, YIndexLow, YIndexClose]);
   if IsEmpty or (my >= Source.YCount) then exit;
   ext2 := ParentChart.CurrentExtent;
   ExpandRange(ext2.a.X, ext2.b.X, 1.0);
   ExpandRange(ext2.a.Y, ext2.b.Y, 1.0);
   PrepareGraphPoints(ext2, true);
   for i := FLoBound to FUpBound do begin
     x := GetGraphPointX(i);
     yopen := GetGraphPointYIndex(i, YIndexOpen);
     yhigh := GetGraphPointYIndex(i, YIndexHigh);
     ylow := GetGraphPointYIndex(i, YIndexLow);
     yclose := GetGraphPointYIndex(i, YIndexClose);
     tw := GetXRange(x, i) * PERCENT * TickWidth;
     if (DownLinePen.Color = clTAColor) or (yopen <= yclose) then
       p := LinePen
     else
       p := DownLinePen;
     ADrawer.BrushColor:= P.Color;
     // set border black
     ADrawer.SetPenParams(p.Style, clBlack);
     DoLine(x, yhigh, x, ylow);
     DoRect(x - tw, yopen, x + tw, yclose);
   end;
end;

I attached a screenshot of the result.

Leonardo M. Ram? http://leonardorame.blogspot.com -------------- next part -------------- A non-text attachment was scrubbed... Name: candlestick.jpg Type: image/jpeg Size: 49653 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140914/8349ce56/attachment-0001.jpg>

Reply by email

Werner Pamler

I'm not familiar with the multivalued series. But you chandlestick series looks much like the Box/Whisker series which is included in TAChart. Can you point out the difference?

Reply by email

Leonardo M. Ramé
to Werner Pamler

El 14/09/14 a las 11:46, Werner Pamler escibi?:

Show quoted text 5 lines
I'm not familiar with the multivalued series. But you chandlestick series looks much like the Box/Whisker series which is included in TAChart. Can you point out the difference? --

Mm, they look similar, but Box/Whisker, has a top and bottom horizontal lines and an horizontal line in the middle of the candle, maybe adding a couple of boolean properties like TopMark, BottomMark and MiddleMark to the Box/Whisker chart will let use it as a CandleStick chart.

Reply by email

Werner Pamler

The "TopMark" and "BottomMark", as you call it, can be removed by setting WhiskerWidth to 0, and the "MiddleMark" disappears with MedianPen.Style set to psClear. The "Up" and "Down" colors can be set by some logics when adding data points (there's the color parameter in the AddXXXX method...). The intrinsic problem that Box/Whisker expects ordered y values, while the Candle obviously does not ("open" can be greater or smaller then "close"), could be overcome by pre-sorting the data which should also take account of the Up/Down colors. Well, in total, these are a few lines of code, but relatively straightforward. Nevertheless, maybe too much for less code-centered guys?

Anyway, I'll try to add your contribution to a new CandleStick series. A few question before I begin: Your posting says that Candlestick plots do not have top and bottom marks, but the wikipedia reference that you gave does show them. What is true? What should be the default up and down colors?

Reply by email

Leonardo M. Ramé
to Werner Pamler

El 15/09/14 a las 15:49, Werner Pamler escibi?:

Show quoted text 16 lines
The "TopMark" and "BottomMark", as you call it, can be removed by setting WhiskerWidth to 0, and the "MiddleMark" disappears with MedianPen.Style set to psClear. The "Up" and "Down" colors can be set by some logics when adding data points (there's the color parameter in the AddXXXX method...). The intrinsic problem that Box/Whisker expects ordered y values, while the Candle obviously does not ("open" can be greater or smaller then "close"), could be overcome by pre-sorting the data which should also take account of the Up/Down colors. Well, in total, these are a few lines of code, but relatively straightforward. Nevertheless, maybe too much for less code-centered guys? Anyway, I'll try to add your contribution to a new CandleStick series. A few question before I begin: Your posting says that Candlestick plots do not have top and bottom marks, but the wikipedia reference that you gave does show them. What is true? What should be the default up and down colors?

Thanks. I'm used to see CandleStick charts in many Stock Trading software, and I never saw thos top and bottom marks. Default UP can be clLime and Down clRed.

P.S.: the next chart type is Heikin Ashi, a variation of CandleStick.

Leonardo M. Ram? Medical IT - Griensu S.A. Av. Col?n 636 - Piso 8 Of. A X5000EPT -- C?rdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email

Werner Pamler

In r46244 the TOpenHighLowCloseSeries has a new property "Mode": selection "mOHLC" does the standard painting as before while "mChandleStick" paints the candlesticks that you requested. Since your code was mostly a copy of existing code I decided to stick to the old series type, but just add the painting modes. There are also properties "CandleStickUpBrush", "CandleStickDownBrush" and "CandleStickLinePen" to control painting of the up and down bars and the border and shadow line. I also added a little demo "financial" to the "demo" folder of the TAChart installation which demonstrates both modes. It would be nice if you could have a look if everything is fine - I'm not an expert in financial charts.

I also fixed the bug which ignored the true minimum of the data - therefore, it is no longer necessary to calculate the minimum by yourself, just have a look at the code in the demo project. And, finally, I modified the AddXOHLC method such that it automatically initializes the size of the YCount of the chart source if the current size would not be enough.

Reply by email

Leonardo M. Ramé
to Werner Pamler

El 16/09/14 a las 16:09, Werner Pamler escibi?:

Show quoted text 19 lines
In r46244 the TOpenHighLowCloseSeries has a new property "Mode": selection "mOHLC" does the standard painting as before while "mChandleStick" paints the candlesticks that you requested. Since your code was mostly a copy of existing code I decided to stick to the old series type, but just add the painting modes. There are also properties "CandleStickUpBrush", "CandleStickDownBrush" and "CandleStickLinePen" to control painting of the up and down bars and the border and shadow line. I also added a little demo "financial" to the "demo" folder of the TAChart installation which demonstrates both modes. It would be nice if you could have a look if everything is fine - I'm not an expert in financial charts. I also fixed the bug which ignored the true minimum of the data - therefore, it is no longer necessary to calculate the minimum by yourself, just have a look at the code in the demo project. And, finally, I modified the AddXOHLC method such that it automatically initializes the size of the YCount of the chart source if the current size would not be enough.

Great!, I'll take a look later today.

Leonardo M. Ram? Medical IT - Griensu S.A. Av. Col?n 636 - Piso 8 Of. A X5000EPT -- C?rdoba Tel.: +54(351)4246924 +54(351)4247788 +54(351)4247979 int. 19 Cel.: +54 9 (011) 40871877

Reply by email