home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Delphi1_And_Delphi2 / EXAMPLES / EXTENDED / CANDLE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  7.8 KB  |  259 lines

  1. {*********************************************}
  2. { TeeChart Delphi Component Library           }
  3. { Stock Candle Series Type Demo               }
  4. { Copyright (c) 1995-1997 by David Berneda    }
  5. { All rights reserved                         }
  6. {*********************************************}
  7. unit Candle;
  8.  
  9. interface
  10.  
  11. uses
  12.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  13.   Forms, Dialogs, Chart, Series, ExtCtrls, StatChar, StdCtrls, OHLChart,
  14.   CandleCh, Teengine, Buttons, teeprocs, TeeComma;
  15.  
  16. type
  17.   TCandleForm = class(TForm)
  18.     Chart1: TChart;
  19.     Panel1: TPanel;
  20.     CheckBox1: TCheckBox;
  21.     Timer1: TTimer;
  22.     CandleSeries1: TCandleSeries;
  23.     RadioGroup1: TRadioGroup;
  24.     VolumeSeries1: TVolumeSeries;
  25.     Chart2: TChart;
  26.     CBShowOpens: TCheckBox;
  27.     CBShowCloses: TCheckBox;
  28.     CheckBox2: TCheckBox;
  29.     CheckBox3: TCheckBox;
  30.     CheckBox4: TCheckBox;
  31.     CheckBox5: TCheckBox;
  32.     RSISeries1: TLineSeries;
  33.     TeeFunction1: TRSIFunction;
  34.     MovingAverageSeries1: TLineSeries;
  35.     TeeFunction2: TMovingAverageFunction;
  36.     TeeCommander1: TTeeCommander;
  37.     BitBtn3: TBitBtn;
  38.     procedure FormCreate(Sender: TObject);
  39.     procedure CheckBox1Click(Sender: TObject);
  40.     procedure Timer1Timer(Sender: TObject);
  41.     procedure RadioGroup1Click(Sender: TObject);
  42.     procedure Chart2GetNextAxisLabel(Sender: TChartAxis;
  43.       LabelIndex: Longint; var LabelValue: Double; var Stop: Boolean);
  44.     procedure CBShowClosesClick(Sender: TObject);
  45.     procedure CBShowOpensClick(Sender: TObject);
  46.     procedure CheckBox2Click(Sender: TObject);
  47.     procedure CheckBox3Click(Sender: TObject);
  48.     procedure Chart1UndoZoom(Sender: TObject);
  49.     procedure Chart1Zoom(Sender: TObject);
  50.     procedure Chart1Scroll(Sender: TObject);
  51.     procedure CheckBox4Click(Sender: TObject);
  52.     procedure CheckBox5Click(Sender: TObject);
  53.     procedure Chart2DblClick(Sender: TObject);
  54.     procedure BitBtn3Click(Sender: TObject);
  55.   private
  56.     { Private declarations }
  57.   public
  58.     { Public declarations }
  59.     Procedure AddNewRandomCandle;
  60.   end;
  61.  
  62. implementation
  63.  
  64. {$R *.DFM}
  65.  
  66. Uses EditChar,CandlEdi; { <-- for Chart & Series Editing }
  67.  
  68. { This method adds a new Candle point to the Series }
  69. { Calculates random Open, High, Low and Close values }
  70. Procedure TCandleForm.AddNewRandomCandle;
  71. Var AOpen,AClose,AHigh,ALow:Double;
  72. Begin
  73.   With CandleSeries1 do
  74.   begin
  75.     if Count>0 then AOpen:=CloseValues.Last+Random(10)-5
  76.                else AOpen:=Random(10000);
  77.     GetRandomOHLC(AOpen,AClose,AHigh,ALow,10000);
  78.     { Add a new random OHLC point }
  79.     if Count>0 then AddOHLC(XValues.Last+1,AOpen,AHigh,ALow,AClose)
  80.                else AddOHLC(Date,AOpen,AHigh,ALow,AClose);
  81.     { Recalculate RSI and Moving Average }
  82.     RefreshSeries;
  83.   end;
  84. end;
  85.  
  86. procedure TCandleForm.FormCreate(Sender: TObject);
  87. var t:Longint;
  88. begin
  89. { This means RSI and moving average will NOT be recalculated when
  90.   deleting Candle points }
  91.   With RSISeries1 do RecalcOptions:=RecalcOptions-[rOnDelete];
  92.   With MovingAverageSeries1 do RecalcOptions:=RecalcOptions-[rOnDelete];
  93.  
  94.   With CandleSeries1 do
  95.   for t:=1 to NumSampleValues do { <-- Some random points }
  96.       AddNewRandomCandle;
  97.  
  98.   With VolumeSeries1 do
  99.   for t:=1 to NumSampleValues do { <-- Some random points }
  100.       AddXY(Date+t-1,100+Random(1000),'',clTeeColor);
  101.  
  102. { set volume series vertical axis maximum to ten times max volume value }
  103. { this will draw volume series on the chart bottom }
  104.   VolumeSeries1.GetVertAxis.SetMinMax(0,10000);
  105.  
  106.   { Date Time Axis support }
  107.   Chart1.BottomAxis.ExactDateTime:=True;
  108.   Chart1.BottomAxis.Increment:=DateTimeStep[dtOneMonth];
  109.   Chart1.BottomAxis.DateTimeFormat:='mmm/yy';
  110.  
  111.   { Date Time Axis support }
  112.   Chart2.BottomAxis.ExactDateTime:=True;
  113.   Chart2.BottomAxis.Increment:=DateTimeStep[dtOneMonth];
  114.   Chart2.BottomAxis.DateTimeFormat:='mm-yyyy';
  115. end;
  116.  
  117. procedure TCandleForm.CheckBox1Click(Sender: TObject);
  118. begin
  119.   Timer1.Enabled:=CheckBox1.Checked; { <-- on / off animation }
  120.   if Timer1.Enabled then
  121.   begin
  122.     Chart1.UndoZoom;
  123.     Chart2.UndoZoom;
  124.   end;
  125. end;
  126.  
  127. procedure TCandleForm.Timer1Timer(Sender: TObject);
  128. begin
  129.   Timer1.Enabled:=False;
  130.   With CandleSeries1 do
  131.   Begin
  132.     Delete(0); { <-- remove the first point }
  133.  
  134.     { If the MovingAverage RecalcOptions property contains the [rOnDelete]
  135.       recalc option, then MovingAverage will be cleared and recalculated.
  136.       If not, we should manually remove the first MovingAverage point.
  137.     }
  138.     if not (rOnDelete in MovingAverageSeries1.RecalcOptions) then
  139.        MovingAverageSeries1.Delete(0);
  140.     { Same as above, but with the RSI series }
  141.     if not (rOnDelete in RSISeries1.RecalcOptions) then
  142.        RSISeries1.Delete(0);
  143.     AddNewRandomCandle;
  144.  
  145.     if Random(10)<2 then
  146.        DownCloseColor:=ColorPalette[1+Random(MaxDefaultColors)];
  147.     if Random(10)<2 then
  148.        UpCloseColor:=ColorPalette[1+Random(MaxDefaultColors)];
  149.   end;
  150.   With VolumeSeries1 do
  151.   Begin
  152.     Delete(0);
  153.     AddXY(XValues.Last+1,100+Random(1000),'',clTeeColor);
  154.     RefreshSeries;
  155.   end;
  156.   Timer1.Enabled:=True;
  157. end;
  158.  
  159. procedure TCandleForm.RadioGroup1Click(Sender: TObject);
  160. begin { change the candle series style }
  161.   if RadioGroup1.ItemIndex=0 then
  162.      CandleSeries1.CandleStyle:=csCandleStick
  163.   else
  164.      CandleSeries1.CandleStyle:=csCandleBar;
  165.  
  166.   { Open and Close allowed only with Candle Bars }
  167.   CBShowOpens.Enabled:=RadioGroup1.ItemIndex=1;
  168.   CBShowCloses.Enabled:=RadioGroup1.ItemIndex=1;
  169. end;
  170.  
  171. procedure TCandleForm.Chart2GetNextAxisLabel(Sender: TChartAxis;
  172.   LabelIndex: Longint; var LabelValue: Double; var Stop: Boolean);
  173. begin
  174.   if Sender=Chart2.RightAxis then
  175.   Begin
  176.     Stop:=False;
  177.     { this demonstrates how to draw the 20% and 80% RSI axis labels only }
  178.     Case LabelIndex of
  179.       0: LabelValue:=20.0;
  180.       1: LabelValue:=80.0;
  181.     else  Stop:=True;
  182.     end;
  183.   end;
  184. end;
  185.  
  186. procedure TCandleForm.CBShowClosesClick(Sender: TObject);
  187. begin
  188.   CandleSeries1.ShowCloseTick:=CBShowCloses.Checked;
  189. end;
  190.  
  191. procedure TCandleForm.CBShowOpensClick(Sender: TObject);
  192. begin
  193.   CandleSeries1.ShowOpenTick:=CBShowOpens.Checked;
  194. end;
  195.  
  196. procedure TCandleForm.CheckBox2Click(Sender: TObject);
  197. begin
  198.   Chart1.View3d:=CheckBox2.Checked;
  199.   if Chart1.View3D then Chart1.BackColor:=clYellow
  200.                    else Chart1.BackColor:=clTeeColor;
  201. end;
  202.  
  203. procedure TCandleForm.CheckBox3Click(Sender: TObject);
  204. begin
  205.   Chart1.BottomAxis.ExactDateTime:=CheckBox3.Checked;
  206.   if CheckBox3.Checked then
  207.      Chart1.BottomAxis.DateTimeFormat:='mmm/yy'
  208.   else
  209.      Chart1.BottomAxis.DateTimeFormat:='dd/mm/yy';
  210.   Chart2.BottomAxis.ExactDateTime:=CheckBox3.Checked;
  211.   if CheckBox3.Checked then
  212.      Chart2.BottomAxis.DateTimeFormat:='mm-yyyy'
  213.   else
  214.      Chart2.BottomAxis.DateTimeFormat:='mmm-dd';
  215. end;
  216.  
  217. procedure TCandleForm.Chart1UndoZoom(Sender: TObject);
  218. begin
  219.   VolumeSeries1.GetVertAxis.SetMinMax(0,10000);
  220. end;
  221.  
  222. procedure TCandleForm.Chart1Zoom(Sender: TObject);
  223. begin
  224.   if not CheckBox5.Checked then
  225.     VolumeSeries1.GetVertAxis.SetMinMax(0,10000);
  226. end;
  227.  
  228. procedure TCandleForm.Chart1Scroll(Sender: TObject);
  229. begin
  230.   if not CheckBox5.Checked then
  231.      VolumeSeries1.GetVertAxis.SetMinMax(0,10000);
  232. end;
  233.  
  234. procedure TCandleForm.CheckBox4Click(Sender: TObject);
  235. begin
  236.   Chart1.BottomAxis.RoundFirstLabel:=CheckBox4.Checked;
  237.   Chart2.BottomAxis.RoundFirstLabel:=CheckBox4.Checked;
  238.   if not CheckBox4.Checked then CheckBox3.Checked:=False;
  239.   Chart1.Repaint;
  240.   Chart2.Repaint;
  241. end;
  242.  
  243. procedure TCandleForm.CheckBox5Click(Sender: TObject);
  244. begin
  245.   if not CheckBox5.Checked then VolumeSeries1.GetVertAxis.SetMinMax(0,10000);
  246. end;
  247.  
  248. procedure TCandleForm.Chart2DblClick(Sender: TObject);
  249. begin
  250.   EditChart(Self,Chart2);
  251. end;
  252.  
  253. procedure TCandleForm.BitBtn3Click(Sender: TObject);
  254. begin
  255.   Close;
  256. end;
  257.  
  258. end.
  259.