{*********************************************}
{  TeeChart Pro Charting Library              }
{  Kagi Financial Series                      }
{  Copyright (c) 2007-2020 by Marjan Slatinek }
{  All Rights Reserved                        }
{*********************************************}
unit TeeKagiSeriesEditor;
{$I TeeDefs.inc}

// The Kagi financial chart.
//
// Kagi charts, at first glance, look like swing charts. Like swing charts, they have no time axis
// and are made up of a series of vertical lines, however in the case of kagi charts, the vertical
// lines are based solely on the action of closing prices, not a bar's high and low prices. Another
// difference is that the thickness of a kagi chart line changes when closing prices penetrate the
// previous column top or bottom.
//
// Construction of Kagi Chart:
//
// Choose a reversal amount - absolute or relative value.
// Note the first closing price in your data. This is called the BasePrice.
// If the closing price of Day 2 is greater than the closing price of Day 1
//    (the base price), use UpSwing stype to draw vertical line upwards from
//    the closing price of Day 1 to the closing price of Day 2.
// If the closing price of Day 2 is less than the closing price of Day 1,
//    use DownPen style to draw vertical line downwards from the closing price
//    of Day 1 to the closing price of Day 2.
// If the closing price of Day 2 is equal to the closing price of Day 1,
//    do nothing. Instead, wait until the end of Day 3, and compare this price
//    with the base price.
// If the closing price continues to rise (or fall), we keep moving the kagi
//    chart up (or down) to the close of each day, regardless of how much or
//    how little it moves.
// If the closing price moves in the opposite direction by less than a
//    predetermined number of cents, the reversal amount, we ignore the small
//    move and do nothing to our chart.
// If the kagi line has been moving upwards, and the closing price has fallen
//    by more than the reversal amount, we draw a short horizontal line,
//    called the 'inflection line', then a new line downwards, to the lower
//    close of that day.
// If the kagi line has been moving downwards, and the closing price has risen
//    by more than the reversal amount, we draw an inflection line, then a new
//    line upwards, to the higher close of that day.
//
// Vertical drawing pen style is defined by the following rules:
//
// If a DownSwing line is extended beyond the previous swing high,
//    pen style is change to UpSwing penbeyond that point.
//
// An UpSwing pen changes to DownSwing pen when the UpSwing line moves below
//    the previous swing low.

interface

uses
  {$IFNDEF LINUX}
  Windows, Messages,
  {$ENDIF}
  SysUtils, Classes,

  Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls,

  TeCanvas, TeEngine, TeePenDlg, TeeKagiSeries;

type
  TKagiSeriesEditor = class(TForm)
    PageControl1: TPageControl;
    TabUp: TTabSheet;
    TabBuy: TTabSheet;
    TabSell: TTabSheet;
    TabDown: TTabSheet;
    procedure FormShow(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
  private
    { Private declarations }
    Kagi : TKagiSeries;
  public
    { Public declarations }
  end;

implementation

uses
  Math, TeeProCo, Chart, TeePoEdi, TeeProcs;

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

{ TKagiSeriesEditor }
procedure TKagiSeriesEditor.FormShow(Sender: TObject);
begin
  Kagi:=TKagiSeries(Tag);

  if Assigned(Kagi) then
  with Kagi do
  begin
    TSeriesPointerEditor.InsertForm(BuySymbol,TabBuy);
    TSeriesPointerEditor.InsertForm(SellSymbol,TabSell);

    PageControl1Change(Self);
  end;
end;

procedure TKagiSeriesEditor.PageControl1Change(Sender: TObject);
begin
  if (PageControl1.ActivePage=TabUp) and (TabUp.ControlCount=0) then
     TPenDialog.InsertForm(Kagi.UpSwing,TabUp)
  else
  if (PageControl1.ActivePage=TabDown) and (TabDown.ControlCount=0) then
     TPenDialog.InsertForm(Kagi.DownSwing,TabDown);
end;

initialization
  RegisterClass(TKagiSeriesEditor);
end.

