unit Thread2; interface uses Classes, Basics,Sysutils, extctrls, StdCtrls; type Taverager = class(TThread) private FReader : TTradeReader; // object for reading trade records FReaderA : TTradeReader; // for access to the reader in the main thread FDayRecordA, // security A trade record for 1 day FDayRecordB : TDayRecord; // security to compare to A FMasterRecordA : TmasterListItem; // detailed information for security A, including relationships to others Fan : TEdit; // Show N ( security number ) to user Fap : TEdit; // Show number of trades today Fab : TEdit; // Show symbols for best matching trade patterns so far Fflip : TEdit; // Show symbols for best inverted matchs Ffromi, Ftoi : integer; // the actual range of records to search View : Timage; // put graph up here // minimum distance between compared patterns IndexRecordA : Longint; // security's position in the index IndexRecordB : Longint; protected procedure Execute; override; procedure ShowIt; procedure Updates; procedure GetAinfo; procedure DoCleanup(Sender: TObject); public started : boolean; Saving : boolean; SaveFile : TextFile; procedure SetSaveFile( Afilename : string ); published constructor CreateIt(filename : string; An : Tedit; Ap : Tedit; Abest : Tedit; Aflip : Tedit; AView : Timage; Areader : TTradeReader ); end; implementation { Taverager } constructor TAverager.CreateIt( filename : string; An : Tedit; Ap : Tedit; Abest : Tedit; Aflip : Tedit; AView : Timage; Areader : TTradeReader ); begin inherited Create( true ); priority:=tplower; FreeOnTerminate:=true; FReader:=TTradeReader.Create( filename ); FReaderA:= Areader; FDayRecordA:=TDayRecord.Create; FDayRecordB:=TDayRecord.Create; Fan:=An; Fap:=Ap; Fab:=Abest; Fflip:=Aflip; View:=AView; IndexRecordA:=0; Started:=false; Saving:=false; priority:=tphigher; OnTerminate:=DoCleanup; end; procedure Taverager.SetSaveFile( Afilename : string ); begin AssignFile( SaveFile, Afilename ); Rewrite( SaveFile ); Saving:=true; {remember to if Saving then CloseFile( SaveFile ); } end; procedure Taverager.DoCleanup(Sender: TObject); begin if Saving then CloseFile( SaveFile); end; procedure Taverager.ShowIt; var xs : single; ys : single; ind : integer; outs,outs2 : string; begin with FReader do begin FaN.text:=FloatToStrf( FMasterRecordA.similar[1].distance, ffgeneral,5,4); FaN.RePaint; outs:=''; outs2:=''; for ind:=1 to Nrelations do begin Outs:=Outs + FMasterRecordA.similar[ind].symbol; Outs2:=Outs2 + FMasterRecordA.different[ind].symbol; end; FaB.text:=outs; Fflip.text:=outs2; FaB.RePaint; Fflip.RePaint; FDayRecordB.ShowTradeGraph( View, xs, ys ); end; end; procedure Taverager.Updates; begin FaP.text:= IntToStr( IndexRecordB ); FaP.RePaint; end; procedure Taverager.GetAinfo; begin IndexRecordA:=FReaderA.IndexPos; FReaderA.FillDayRecord( FDayRecordA ); FMasterRecordA:=masterl.FindMaster( FDayRecordA.symbol ); FReaderA.GetDateRange( Ffromi, Ftoi ); IndexRecordB:= Ffromi; end; procedure Taverager.Execute; var Distance : double; DistanceN : double; MinDist : double; MinDistN : double; Comparison : TRealPair; ind : integer; begin While (terminated = false) do begin Started:=true; with FReader do begin Synchronize( GetAinfo ); MinDist:=1e20; MinDistN:=1e20; FMasterRecordA.similar[1].distance:=MinDist; while (terminated = false) and (NOT EoFile) and (IndexRecordA=FReaderA.IndexPos) and (IndexRecordB <=Ftoi) do begin if IndexRecordB<>IndexRecordA then begin FetchIndex( IndexRecordB ); FillDayRecord( FDayRecordB ); Comparison:=FDayRecordB.CompareTo( FDayRecordA ); Distance:= Comparison.High; DistanceN:=Comparison.Low; if (Distance < MinDist) and (Distance>1E-100) then begin MinDist:=Distance; for ind:=Nrelations downto 2 do begin // keep track of next closest, too FMasterRecordA.similar[ind]:=FMasterRecordA.similar[ind-1]; end; FMasterRecordA.similar[1].distance:=MinDist; FMasterRecordA.similar[1].symbol:=FDayRecordB.symbol; Synchronize( ShowIt ); end; if (DistanceN < MinDistN) and (DistanceN>1E-100) then begin MinDistN:=DistanceN; for ind:=Nrelations downto 2 do begin FMasterRecordA.different[ind]:=FMasterRecordA.different[ind-1]; end; FMasterRecordA.different[1].distance:=MinDistN; FMasterRecordA.different[1].symbol:=FDayRecordB.symbol; Synchronize( ShowIt ); if saving then writeln( SaveFile, trim(FDayRecordB.symbol),'-',trim(FDayRecordA.symbol),'/',round(MinDistN*2e4),','); end; if IndexRecordB mod 20 = 0 then Synchronize( Updates ); end;{if} inc(IndexRecordB); end;{while} if IndexRecordB > Ftoi then begin // report the results to a text file, then suspend until further notice if saving then begin for ind:=1 to Nrelations do begin with FMasterRecordA do begin writeln( SaveFile, trim(similar[ind].symbol),'-',trim(FDayRecordA.symbol),'/',round(similar[ind].distance*2e4),','); writeln( SaveFile, trim(different[ind].symbol),'-',trim(FDayRecordA.symbol),'/',round(different[ind].distance*2e4),','); end;{with} end;{for} end;{if} Suspend; end;{if} end;{with} end;{infinite while} If Saving then CloseFile( SaveFile ); end; end.