home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Src Code / STATCHAR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  8.2 KB  |  301 lines

  1. {*************************************}
  2. {    TeeChart  Functions              }
  3. { Copyright (c) 1996 by David Berneda }
  4. {    All Rights Reserved              }
  5. {*************************************}
  6. {$I teedefs.inc}
  7. unit StatChar;
  8.  
  9. interface
  10.  
  11. Uses Classes, TeEngine;
  12.  
  13. Type { Abstract MovingTeeFunction for oscillators }
  14.      TMovingTeeFunction=class(TTeeFunction)
  15.      public
  16.        Constructor Create(AOwner:TComponent); override;
  17.      published
  18.        property Period;
  19.      end;
  20.  
  21.      { Moving Average }
  22.      TMovingAverageFunction=class(TMovingTeeFunction)
  23.      private
  24.        FWeighted:Boolean;
  25.        Procedure SetWeighted(Value:Boolean);
  26.      public
  27.        Function Calculate( Series:TChartSeries;
  28.                            FirstIndex,LastIndex:Longint):Double; override;
  29.        property Weighted:Boolean read FWeighted write SetWeighted default False;
  30.      end;
  31.  
  32.      { RSI }
  33.      TRSIFunction = class(TMovingTeeFunction)
  34.      public
  35.        Function Calculate( Series:TChartSeries;
  36.                            FirstIndex,LastIndex:Longint):Double; override;
  37.      end;
  38.  
  39.      { Exponential Average }
  40.      TExpAverageFunction = class(TMovingTeeFunction)
  41.      private
  42.        FWeight:Double;
  43.      protected
  44.        Procedure SetWeight(Const Value:Double);
  45.      public
  46.        Constructor Create(AOwner:TComponent); override;
  47.        Function Calculate( Series:TChartSeries;
  48.                            FirstIndex,LastIndex:Longint):Double; override;
  49.      published
  50.        property Weight:Double read FWeight write SetWeight;
  51.      end;
  52.  
  53.      { Momemtum }
  54.      TMomentumFunction=class(TMovingTeeFunction)
  55.      public
  56.        Function Calculate( Series:TChartSeries;
  57.                            FirstIndex,LastIndex:Longint):Double; override;
  58.      end;
  59.  
  60.      { StdDeviation }
  61.      TStdDeviationFunction=class(TTeeFunction)
  62.      private
  63.        FComplete : Boolean;
  64.        ISum      : Double;
  65.        ISum2     : Double;
  66.        INumPoints: LongInt;
  67.        Procedure SetComplete(Value:Boolean);
  68.        Function CalculateDeviation:Double;
  69.        Procedure Accumulate(Const Value:Double);
  70.      public
  71.        Function Calculate(SourceSeries:TChartSeries; FirstIndex,LastIndex:Longint):Double; override;
  72.        Function CalculateMany(SourceSeriesList:TList; ValueIndex:Longint):Double;  override;
  73.      published
  74.        property Complete:Boolean read FComplete write SetComplete default False;
  75.      end;
  76.  
  77. implementation
  78.  
  79. Uses SysUtils,TeeProcs,TeeConst,TeeProCo,Chart,TeCanvas;
  80.  
  81. { MovingTeeFunction }
  82. Constructor TMovingTeeFunction.Create(AOwner:TComponent);
  83. begin
  84.   inherited Create(AOwner);
  85.   MovingFunction:=True;
  86.   InternalSetPeriod(1);
  87.   {$IFDEF TEETRIAL}
  88.   TeeTrial(ComponentState);
  89.   {$ENDIF}
  90. end;
  91.  
  92. { MovingAverage }
  93. Procedure TMovingAverageFunction.SetWeighted(Value:Boolean);
  94. begin
  95.   if FWeighted<>Value then
  96.   begin
  97.     FWeighted:=Value;
  98.     Recalculate;
  99.   end;
  100. end;
  101.  
  102. Function TMovingAverageFunction.Calculate( Series:TChartSeries;
  103.                                            FirstIndex,LastIndex:Longint):Double;
  104. var t         : Longint;
  105.     tmpSumX   : Double;
  106.     tmpYValue : Double;
  107.     tmpXValue : Double;
  108.     tmpVList  : TChartValueList;
  109. begin
  110.   result:=0;
  111.   tmpSumX:=0;
  112.   tmpVList:=ValueList(Series);
  113.   for t:=FirstIndex to LastIndex do
  114.   begin
  115.     tmpYValue:=tmpVList.Value[t];
  116.     if FWeighted then
  117.     Begin
  118.       tmpXValue:=Series.XValue[t];
  119.       result:=result+tmpYValue*tmpXValue;
  120.       tmpSumX:=tmpSumX+tmpXValue;
  121.     end
  122.     else result:=result+tmpYValue;
  123.   end;
  124.   if FWeighted then
  125.   begin
  126.     if tmpSumX<>0 then result:=result/tmpSumX else result:=0;
  127.   end
  128.   else result:=result/(LastIndex-FirstIndex+1);
  129. end;
  130.  
  131. { R.S.I. }
  132. Function TRSIFunction.Calculate( Series:TChartSeries;
  133.                                  FirstIndex,LastIndex:Longint):Double;
  134. var NumPoints : Longint;
  135.     t         : Longint;
  136.     tmpClose  : Double;
  137.     Ups       : Double;
  138.     Downs     : Double;
  139.     Opens     : TChartValueList;
  140.     Closes    : TChartValueList;
  141. Begin
  142.   With Series do
  143.   Begin
  144.     Opens :=GetYValueList('OPEN');  
  145.     Closes:=GetYValueList('CLOSE');
  146.     Ups:=0;
  147.     Downs:=0;
  148.     for t:=FirstIndex to LastIndex do
  149.     Begin
  150.       tmpClose:=Closes[t];
  151.       if Opens[t]>tmpClose then Downs:=Downs+tmpClose
  152.                            else Ups  :=Ups  +tmpClose;
  153.     end;
  154.     NumPoints:=(LastIndex-FirstIndex)+1;
  155.     Downs:=Downs/NumPoints;
  156.     Ups  :=Ups  /NumPoints;
  157.     if Downs<>0 then
  158.     Begin
  159.       result:=100.0 - ( 100.0 / ( 1.0+Abs(Ups/Downs) ) );
  160.       if result<0   then result:=0 else
  161.       if result>100 then result:=100;
  162.     end
  163.     else result:=0;
  164.   end;
  165. end;
  166.  
  167. { Exponential Average }
  168. Constructor TExpAverageFunction.Create(AOwner: TComponent);
  169. Begin
  170.   inherited Create(AOwner);
  171.   FWeight:=0.2;
  172.   {$IFDEF TEETRIAL}
  173.   TeeTrial(ComponentState);
  174.   {$ENDIF}
  175. End;
  176.  
  177. Procedure TExpAverageFunction.SetWeight(Const Value:Double);
  178. Begin
  179.   if (Value<0) or (Value>1) then
  180.      raise Exception.Create(TeeMsg_ExpAverageWeight);
  181.   if FWeight<>Value then
  182.   begin
  183.     FWeight:=Value;
  184.     Recalculate;
  185.   end;
  186. End;
  187.  
  188. Function TExpAverageFunction.Calculate( Series:TChartSeries;
  189.                                         FirstIndex,LastIndex:Longint):Double;
  190. Begin
  191.   With ValueList(Series) do
  192.   begin
  193.     result:=Value[LastIndex];
  194.     if LastIndex>0 then result:=Value[LastIndex-1]*(1.0-FWeight)+result*FWeight;
  195.   end;
  196. end;
  197.  
  198. { Momentum }
  199. Function TMomentumFunction.Calculate( Series:TChartSeries;
  200.                                       FirstIndex,LastIndex:Longint):Double;
  201. Begin
  202.   if FirstIndex=TeeAllValues then
  203.   begin
  204.     FirstIndex:=0;
  205.     LastIndex:=Series.Count-1;
  206.   end;
  207.   With ValueList(Series) do result:=Value[LastIndex]-Value[FirstIndex];
  208. End;
  209.  
  210. { StdDeviation }
  211. Function TStdDeviationFunction.CalculateDeviation:Double;
  212. Var Divisor:Double;
  213. begin
  214.   if Complete then Divisor:=Sqr(INumPoints)
  215.               else Divisor:=INumPoints*(INumPoints-1);
  216.   result:=Sqrt( ((INumPoints*ISum2) - Sqr(ISum)) / Divisor );
  217. end;
  218.  
  219. Procedure TStdDeviationFunction.Accumulate(Const Value:Double);
  220. begin
  221.   ISum:=ISum+Value;
  222.   ISum2:=ISum2+Sqr(Value);
  223. end;
  224.  
  225. Function TStdDeviationFunction.Calculate(SourceSeries:TChartSeries; FirstIndex,LastIndex:Longint):Double;
  226. var t:LongInt;
  227. begin
  228.   if FirstIndex=TeeAllValues then
  229.   begin
  230.     FirstIndex:=0;
  231.     INumPoints:=SourceSeries.Count;
  232.     LastIndex:=INumPoints-1;
  233.   end
  234.   else INumPoints:=LastIndex-FirstIndex+1;
  235.   if INumPoints>1 then
  236.   begin
  237.     ISum2:=0;
  238.     ISum:=0;
  239.     With ValueList(SourceSeries) do
  240.     for t:=FirstIndex to LastIndex do Accumulate(Value[t]);
  241.     result:=CalculateDeviation;
  242.   end
  243.   else result:=0;
  244. end;
  245.  
  246. Function TStdDeviationFunction.CalculateMany(SourceSeriesList:TList; ValueIndex:Longint):Double;
  247. var t:Integer;
  248. begin
  249.   if SourceSeriesList.Count>0 then
  250.   begin
  251.     INumPoints:=0;
  252.     ISum2:=0;
  253.     ISum:=0;
  254.     for t:=0 to SourceSeriesList.Count-1 do
  255.     begin
  256.       With ValueList(TChartSeries(SourceSeriesList[t])) do
  257.       if Count>ValueIndex then
  258.       begin
  259.         Accumulate(Value[ValueIndex]);
  260.         Inc(INumPoints);
  261.       end;
  262.     end;
  263.     if INumPoints>1 then result:=CalculateDeviation
  264.                     else result:=0;
  265.   end
  266.   else result:=0;
  267. end;
  268.  
  269. Procedure TStdDeviationFunction.SetComplete(Value:Boolean);
  270. begin
  271.   if FComplete<>Value then
  272.   begin
  273.     FComplete:=Value;
  274.     Recalculate;
  275.   end;
  276. end;
  277.  
  278. Procedure TeeStatsExitProc; far;
  279. begin
  280.   UnRegisterTeeFunctions([ TMovingAverageFunction,
  281.                            TRSIFunction,
  282.                            TExpAverageFunction,
  283.                            TMomentumFunction,
  284.                            TStdDeviationFunction ]);
  285. end;
  286.  
  287. initialization
  288.   RegisterTeeBasicFunction( TMovingAverageFunction, TeeMsg_FunctionMovingAverage );
  289.   RegisterTeeBasicFunction( TExpAverageFunction,    TeeMsg_FunctionExpAverage );
  290.   RegisterTeeBasicFunction( TRSIFunction,           TeeMsg_FunctionRSI );
  291.   RegisterTeeBasicFunction( TMomentumFunction,      TeeMsg_FunctionMomentum );
  292.   RegisterTeeBasicFunction( TStdDeviationFunction,  TeeMsg_FunctionStdDeviation );
  293. {$IFDEF D1}
  294.   AddExitProc(TeeStatsExitProc);
  295. {$ENDIF}
  296. {$IFNDEF D1}
  297. finalization
  298.   TeeStatsExitProc;
  299. {$ENDIF}
  300. end.
  301.