{********************************************}
{ TeeChart OHLC Compression Function         }
{ Copyright (c) 2000-2020 by Steema Software }
{    All Rights Reserved                     }
{********************************************}
unit TeeCompressOHLC;
{$I TeeDefs.inc}

interface

Uses {$IFNDEF LINUX}
     Windows,
     {$ENDIF}
     Classes,

     Forms, Controls, StdCtrls, ComCtrls,
     TeEngine, OHLChart, TeeBaseFuncEdit, TeCanvas, TeeProcs, ExtCtrls;

{
  This unit contains a "TeeFunction" editor ( TCompressOHLCFunction class )

  The purpose of this function is to "compress" an OHLC series
  (for example a financial Candle series).

  ( OHLC stands for "Open High Low Close" in financial terms )

  To "compress" means to find out the OHLC of a date-time period from
  the original data.

  This function also works with non-OHLC source series.

}

type
  TCompressFuncEditor = class(TBaseFunctionEditor)
    CBDatePeriod: TComboFlat;
    RadioDate: TRadioButton;
    ENum: TEdit;
    RadioPoints: TRadioButton;
    UpDown1: TUpDown;
    RadioTime: TRadioButton;
    CBTimePeriod: TComboFlat;
    procedure CBDatePeriodChange(Sender: TObject);
    procedure RadioDateClick(Sender: TObject);
    procedure RadioPointsClick(Sender: TObject);
    procedure ENumChange(Sender: TObject);
    procedure ENumClick(Sender: TObject);
    procedure CBDatePeriodClick(Sender: TObject);
    procedure RadioTimeClick(Sender: TObject);
    procedure CBTimePeriodChange(Sender: TObject);
  private
    { Private declarations }
  protected
    procedure ApplyFormChanges; override;
    procedure SetFunction; override;
  public
    { Public declarations }
  end;

implementation

{$IFNDEF LCL}
{$R *.DFM}
{$ELSE}
{$R *.lfm}
{$ENDIF}

Uses
  {$IFDEF D6}
  DateUtils,
  {$ENDIF}
  SysUtils, Chart, TeeProCo, CandleCh;

// TCompressFuncEditor

procedure TCompressFuncEditor.CBDatePeriodChange(Sender: TObject);
begin
  RadioDate.Checked:=True;
  EnableApply;
end;

procedure TCompressFuncEditor.SetFunction;
begin
  inherited;

  CBDatePeriod.ItemIndex:=0;
  CBTimePeriod.ItemIndex:=0;

  with TCompressFunction(IFunction) do
  begin
    if Period=0 then
    begin
      if Ord(Compress)<Ord(ocSecond) then
      begin
        CBDatePeriod.ItemIndex:=Ord(Compress);
        RadioDate.Checked:=True;
      end
      else
      begin
        CBTimePeriod.ItemIndex:=Ord(Compress)-Ord(ocSecond);
        RadioTime.Checked:=True;
      end;
    end
    else
    begin
      UpDown1.Position:=Round(Period);
      RadioPoints.Checked:=True;
    end;
  end;
end;

procedure TCompressFuncEditor.ApplyFormChanges;
begin
  inherited;

  with TCompressFunction(IFunction) do
  begin
    BeginUpdate;
    try
      if RadioDate.Checked then
         Compress:=TCompressionPeriod(CBDatePeriod.ItemIndex)
      else
         Compress:=TCompressionPeriod(CBTimePeriod.ItemIndex+Ord(ocSecond));

      Period:=UpDown1.Position;
    finally
      EndUpdate;
    end;
  end;
end;

procedure TCompressFuncEditor.RadioDateClick(Sender: TObject);
begin
  UpDown1.Position:=0;
  CBDatePeriod.SetFocus;
  EnableApply;
end;

procedure TCompressFuncEditor.RadioPointsClick(Sender: TObject);
begin
  ENum.SetFocus;
  EnableApply;
end;

procedure TCompressFuncEditor.ENumChange(Sender: TObject);
begin
  EnableApply;
end;

procedure TCompressFuncEditor.ENumClick(Sender: TObject);
begin
  RadioPoints.Checked:=True;
  EnableApply;
end;

procedure TCompressFuncEditor.CBDatePeriodClick(Sender: TObject);
begin
  RadioDate.Checked:=True;
  EnableApply;
end;

procedure TCompressFuncEditor.RadioTimeClick(Sender: TObject);
begin
  UpDown1.Position:=0;
  CBTimePeriod.SetFocus;
  EnableApply;
end;

procedure TCompressFuncEditor.CBTimePeriodChange(Sender: TObject);
begin
  RadioTime.Checked:=True;
  EnableApply;
end;

initialization
  RegisterClass(TCompressFuncEditor);
end.
