home *** CD-ROM | disk | FTP | other *** search
- {*************************************}
- { TeeChart OHLC Compression Function }
- { Copyright (c) 2000 by David Berneda }
- { All Rights Reserved }
- {*************************************}
- {$I teedefs.inc}
- unit TeeCompressOHLC;
-
- interface
-
- Uses Classes, TeEngine, OHLChart;
-
- type
- TOHLCCompression=(ocDay,ocWeek,ocMonth,ocBiMonth,ocQuarter,ocYear);
-
- TOHLCCompressGetDate=procedure(Sender:TTeeFunction; Source:TOHLCSeries;
- ValueIndex:Integer; Var Date:TDateTime) of object;
-
- TCompressOHLCFunction=class(TTeeFunction)
- private
- FCompress: TOHLCCompression;
- FOnGetDate: TOHLCCompressGetDate;
- procedure SetCompress(const Value: TOHLCCompression);
- public
- Constructor Create(AOwner: TComponent); override;
- procedure AddPoints(Source: TChartSeries); override;
- Procedure CompressSeries(OHLC,DestOHLC:TOHLCSeries;
- Volume,DestVolume:TChartSeries);
- published
- property Compress:TOHLCCompression read FCompress write SetCompress default ocWeek;
- property OnGetDate:TOHLCCompressGetDate read FOnGetDate write FOnGetDate;
- end;
-
- implementation
-
- Uses SysUtils;
-
- { TCompressOHLCFunction }
- constructor TCompressOHLCFunction.Create(AOwner: TComponent);
- begin
- inherited;
- CanUsePeriod:=False;
- FCompress:=ocWeek;
- end;
-
- procedure TCompressOHLCFunction.AddPoints(Source: TChartSeries);
- begin
- CompressSeries(Source as TOHLCSeries,ParentSeries as TOHLCSeries,nil,nil);
- end;
-
- Procedure TCompressOHLCFunction.CompressSeries(OHLC,DestOHLC:TOHLCSeries;
- Volume,DestVolume:TChartSeries);
- var t : Integer;
- tmpDay : Integer;
- OldDay : Integer;
- tmp : Integer;
- Year : Word;
- Month : Word;
- Day : Word;
- DoIt : Boolean;
- tmpDate : TDateTime;
- begin
- DestOHLC.Clear;
- if Assigned(DestVolume) then DestVolume.Clear;
- OldDay:=0;
- for t:=0 to OHLC.Count-1 do
- begin
- tmpDate:=OHLC.DateValues[t];
- if Assigned(FOnGetDate) then FOnGetDate(Self,OHLC,t,tmpDate);
- DecodeDate(tmpDate,Year,Month,Day);
-
- Case Compress of
- ocDay: tmpDay:=Trunc(tmpDate);
- ocWeek: begin
- tmpDay:=DayOfWeek(tmpDate)-1;
- if tmpDay=0 then tmpDay:=7;
- end;
- ocMonth: tmpDay:=Month;
- ocBiMonth: tmpDay:=(Month-1) div 2;
- ocQuarter: tmpDay:=(Month-1) div 3;
- else tmpDay:=Year;
- end;
-
- if Compress=ocWeek then DoIt:=tmpDay<OldDay else DoIt:=tmpDay<>OldDay;
-
- if (t=0) or DoIt then
- begin
- With OHLC do
- begin
- tmp:=DestOHLC.AddOHLC(DateValues[t],
- OpenValues[t],HighValues[t],LowValues[t],CloseValues[t]);
- DestOHLC.XLabel[tmp]:=OHLC.XLabel[t];
- if Assigned(DestVolume) then
- DestVolume.AddXY(DateValues[t],Volume.YValues[t]);
- end;
- end
- else
- begin
- tmp:=DestOHLC.Count-1;
- DestOHLC.CloseValues.Value[tmp]:=OHLC.CloseValues.Value[t];
- DestOHLC.DateValues.Value[tmp] :=OHLC.DateValues.Value[t];
- if OHLC.HighValues[t]>DestOHLC.HighValues[tmp] then
- DestOHLC.HighValues[tmp]:=OHLC.HighValues[t];
- if OHLC.LowValues[t]<DestOHLC.LowValues[tmp] then
- DestOHLC.LowValues[tmp]:=OHLC.LowValues[t];
- DestOHLC.XLabel[tmp]:=OHLC.XLabel[t];
- if Assigned(DestVolume) then
- begin
- With DestVolume.YValues do
- Value[tmp]:=Value[tmp]+Volume.YValues.Value[t];
- DestVolume.XValues.Value[tmp] :=Volume.XValues.Value[t];
- end;
- end;
- OldDay:=tmpDay;
- end;
- end;
-
- procedure TCompressOHLCFunction.SetCompress(const Value: TOHLCCompression);
- begin
- if FCompress<>Value then
- begin
- FCompress:=Value;
- ReCalculate;
- end;
- end;
-
- end.
-