END;
END;
// end of “find the best correlated symbol” code
// ****************************************************************************
// ############################################################################
Stock1 := GetSymbol; // the symbol of the Price Series that is currently being
operated on
Stock2 := CurrentSymbol; // the symbol with the best correlation
// ############################################################################
// compute series
SetPrimarySeries( Stock1);
Close1 := #Close;
SetPrimarySeries( Stock2);
Close2 := #Close;
RestorePrimarySeries;
ActualRatioSeries := DivideSeries( Close1, Close2);
ActualRatioSMASeries := SMASeries( ActualRatioSeries, MAPeriod);
DeltaSeries := SubtractSeries( ActualRatioSeries, ActualRatioSMASeries);
DeltaSMASeries := SMASeries( DeltaSeries, MAPeriod);
DeltaDifferenceSeries := SubtractSeries( DeltaSeries, DeltaSMASeries);
DeltaNormSeries := DivideSeries( DeltaDifferenceSeries, StdDevSeries( DeltaSeries,
MAPeriod));
// correlation coefficient
Value := Correlation( Close1, Close2, 0, BarCount - 1);
// graphics
HideVolume;
EnableNotes(false);
RatioPane := CreatePane( 100, True, True);
PlotSeries( ActualRatioSeries, RatioPane, #Navy, #Thick);
PlotSeries( ActualRatioSMASeries, RatioPane, #Blue, #Thick);
DrawLabel(‘Ratio ‘ + Stock1 + ‘/’ + Stock2 + ‘ and its ‘
+ IntToStr(MAPeriod) + ‘ Period Moving Average’ , RatioPane);
StDevPane := CreatePane( 100, True, True);
PlotSeries( DeltaNormSeries, StDevPane, #Red, #ThickHist);
DrawHorzLine( Up_Threshold, StDevPane, #Black, #Thick);
DrawHorzLine( Down_Threshold, StDevPane, #Black, #Thick);
DrawHorzLine( Exit_Down_Threshold, StDevPane, #Blue, #Thick);
DrawHorzLine( Exit_Up_Threshold, StDevPane, #Blue, #Thick);
DrawLabel(‘no of standard deviations’, StDevPane);
TextPane := CreatePane( 40, False, False);
DrawText( ‘Correlation: ‘ + FormatFloat(‘0.####’, Value), TextPane, 10, 10, #Black,
18 );
// procedures
How to Play the QQQ-SPY Spread Using Unilateral Pairs Trading 45
4199_P-02.qxd 12/30/03 3:15 PM Page 45