hmap,
Have you looked at Mark Conway and Aaron Behl's model for pair trading in their book "Professional Stock Trading"? If not, they set up a day-trading pair model incorporating correlation and volatility. If for nothing else, you can see how they programmed theirs. It is all fully disclosed easylanguage. In fact, I'll just post it below. Since TS can only give signals on Data1 you'll have to set up TWO separate charts. The first will be configured as follows:
Data1: 5 min bars of ABC
Data2: 5 min bars of XYZ
Data3: daily bars of ABC
Data4: daily bars of XYZ
In the second chart, swap ABC for XYZ like so:
Data1: 5 min bars of XYZ
Data2: 5 min bars of ABC
Data3: daily bars of XYZ
Data4: daily bars of ABC
Then apply this strategy to both charts. Here's the code. Good luck!:
{START HERE -------------------------------------------------------}
inputs: Price1(close of data3),
Price2(close of data4),
StandardDeviations(1.5),
Length(30),
{Position Sizing Parameters}
Equity(48000),
RiskModel(3),
RiskPercent(2.0),
RiskATR(1.0),
{Trade Logging}
LogTrades(False),
LogFile("Orders.txt");
vars:
N(0),
HV1(0.0),
HV2(0.0),
CV(0.0),
VolatilityBand(0.0),
VolatilityConstant(0.0523),
UpperBand(0.0),
LowerBand(0.0),
Spread(0.0);
If Date <> Date[1] Then Begin
N = AcmeGetShares(Equity,RiskModel,RiskPercent,RiskATR,100);
HV1 = acmevolatility(Length) of Data3;
HV2 = acmevolatility(Length) of Data4;
CV = Correlation(Price1,Price2,Length);
VolatilityBand = VolatilityConstant*(HV1+HV2)*(1-CV);
UpperBand = StandardDeviations*VolatilityBand;
LowerBand = StandardDeviations*(-VolatilityBand);
End;
Spread = (Close of Data1/Price1)-(Close of Data2/Price2);
If {time<1495 and time>0840 and} Spread crosses above LowerBand then
Buy ("PairLE") N Shares This Bar on Close;
If Spread crosses above 0 {or Time>1495} then
Sell ("PairLX +") This bar on Close
Else if Spread <= StandardDeviations*LowerBand Then
Sell ("PairLX -")this Bar on Close;
If {time<1495 and time>0840 and} Spread crosses below UpperBand then
SellShort ("PairSE") N Shares This Bar on Close;
If Spread crosses below 0 {or Time>1495} then
BuytoCover("PairSX +") This bar on Close
Else if Spread >= StandardDeviations*UpperBand Then
BuyToCover("PairSX -")this Bar on Close;
{Log Trades for Spreadsheet Export}
Condition1 = AcmeLogTrades(LogTrades,LogFile,"Pairs");
{END HERE -------------------------------------------------------}
Here is the indicator that goes along with it so you can visually see the spread:
{START HERE -------------------------------------------------------}
inputs: Price1(close of data3),Price2(close of data4),StandardDeviations(1.5),
Length(30);
vars: HV1(0.0),HV2(0.0),CV(0.0),VolatilityBand(0.0),VolatilityConstant(0.0523),
UpperBand(0.0),LowerBand(0.0),Spread(0.0);
HV1 = acmevolatility(Length) of Data3;
HV2 = acmevolatility(Length) of Data4;
CV = Correlation(Price1,Price2,Length);
VolatilityBand = VolatilityConstant*(HV1+HV2)*(1-CV);
UpperBand = StandardDeviations*VolatilityBand;
LowerBand = StandardDeviations*(-VolatilityBand);
Spread = (Close of Data1/Price1)-(Close of Data2/Price2);
Plot1(Spread[1],"Spread");
Plot2(0,"Zeroline");
Plot3(UpperBand,"UpperBand");
Plot4(LowerBand,"LowerBand");
Here are two functions you'll need (AcmeVolatility and AcmeGetShares):
{START HERE -------------------------------------------------------}
{AcmeVolatility}
inputs: length(numeric);
variables: DaysinYear(365), DaysInMonth(30),DaysInWeek(7),
TimeFactor(0.0);
AcmeVolatility = 0;
If C>0 and C[1]>0 then Begin
If DataCompression>=2 and DataCompression<5 Then Begin
If DataCompression = 2 Then {Daily}
TimeFactor = DaysInYear
Else if DataCompression = 3 Then {Weekly}
TimeFactor = DaysInYear/DaysInWeek
Else if DataCompression = 4 Then {Monthly}
TimeFactor = DaysInYear/DaysInMonth;
AcmeVolatility = StdDev(Log(c/C[1]),Length)*SquareRoot(TimeFactor);
End;
End;
{END HERE -------------------------------------------------------}
{START HERE -------------------------------------------------------}
{AcmeGetShares}
{AcmeGetShares: Calculate the number of shares based on risk model
RiskModel1 = 1, Equal Value Units Model
RiskModel2 = 2, Percent Risk Model
RiskModel3 = 3, Percent Volatility Model
RiskModel4 = 4, Fixed Number of Contracts}
Inputs:
Equity(Numeric),
RiskModel(Numeric),
RiskPercent(Numeric),
RiskUnits(Numeric);
Variables:
MinimumShares(100),
RiskShares(0),
ERP(0.0),
Length(20);
ERP = Equity*RiskPercent/100;
If RiskModel = 1 and Close>0 then
RiskShares = Maxlist(MinimumShares,100*IntPortion(Equity/(100*C)));
If Riskmodel = 2 and RiskUnits>0 then
RiskShares = Maxlist(MinimumShares,100*intPortion(ERP/(100*RiskUnits)));
If Riskmodel = 3 and Volatility(length)>0 then
RiskShares = Maxlist(MinimumShares,100*IntPortion(ERP/(100*Volatility(length))));
If Riskmodel = 4 then
RiskShares = RiskUnits;
AcmeGetShares = RiskShares;
{END HERE -------------------------------------------------------}