[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Funky Analog Code



PureBytes Links

Trading Reference Links

Hi, All:

Here's something I put together today (not without a few minor
headaches.)

Its purpose was to identify analogs of current data with a particular
period from the past.

It returns the correlation between the current moving window of data and
the period specified by you.

I was disappointed on initial inspection (using ShowMe's to highlight
areas of high correlation). Bummer. So much for knocking off the Holy
Grail in a few hours' work. (Note that I haven't yet exported the values
to a spreadsheet to verify that the code is returning the correct
values.)

At any rate, thought I'd throw it out there to what is clearly a
ravenous, code-addicted group of traders.

Any suggestions for improvements or alternative methodologies are
definitely welcome, even if they relate more to programming practice
than to the code's underlying concept.


Cheers,

CAB VINTON
cvinton@xxxxxxxxxxxxxx



{F_Analogs
Returns the correlation of the present period (length = Window bars)
with a past period of your choosing, ending on EndDate (length = Window
bars)
Date format is YYMMDD
What : Close, RateofChange, Momentum, whatever
}

Inputs: What(NumericSeries), EndDate(Numeric), Window(Numeric) ;
Arrays: Ideal[200](0) ;
Vars: TSum(0), TAve(0), TSD(0), NowAve(0), NowSD(0), TSumSqr(0),
CorrDev(0), Denom(0), BarN(0) ;

TSum = 0 ; TSumSqr = 0 ; CorrDev = 0 ;

{ Fill the Ideal array with values: Ideal[0] is the most recent datum }
If Date = EndDate then begin
	BarN = Currentbar ;
	For Value1 = 0 to Window-1 begin
		Ideal[Value1] = What[Value1] ;
	End ;

{ Find the average of the Ideal array's values }
	For Value11 = 0 to Window-1 begin
		TSum = TSum + Ideal[Value11] ;
	End ;
	TAve = TSum/ Window ;

	{ Find the Std Dev of the Ideal array's values }
	For Value21 = 0 to Window-1 begin
		TSumSqr = TSumSqr + (Ideal[Value21]-TAve)*(Ideal[Value21]-TAve) ;
	End ;
	If Window-1 <> 0 then TSD = SquareRoot(TSumSqr/ (Window-1)) ;

End ;


{ Wait until fresh period arrives & start measuring the correlations of
the current period w/ the Ideal period }
If Currentbar >= BarN + Window then begin
	NowAve = Average(What, Window) ;
	NowSD = StdDev(What, Window) ;

	For Value31 = 0 to Window-1 begin
		CorrDev = CorrDev + (Ideal[Value31] - TAve) * (What[Value31] - NowAve)
;
	End ;

	Denom = (Window-1) * NowSD * TSD ;

	If Denom <> 0 then F_Analogs = CorrDev/ Denom
	Else F_Analogs = 0 ;
End
Else F_Analogs = 0 ;