File Name: Pair_Trading.efs
Description:
Pair Trading With A Twist by Domenico D'Errico
Formula Parameters:
Pair_Trading.efs
Correlating symbol: SPY
Fast MA: 4
Slow MA: 40
BG Color Condition: Trend Following
Notes:
The related article is copyrighted material. If you are not a subscriber
of Stocks & Commodities, please visit www.traders.com.
Download File:
Pair_Trading.efs
Pair_Trading.efs
EFS Code:
Description:
Pair Trading With A Twist by Domenico D'Errico
Formula Parameters:
Pair_Trading.efs
Correlating symbol: SPY
Fast MA: 4
Slow MA: 40
BG Color Condition: Trend Following
Notes:
The related article is copyrighted material. If you are not a subscriber
of Stocks & Commodities, please visit www.traders.com.
Download File:
Pair_Trading.efs
Pair_Trading.efs
EFS Code:
PHP Code:
/*********************************
Provided By:
eSignal (Copyright c eSignal), a division of Interactive Data
Corporation. 2016. All rights reserved. This sample eSignal
Formula Script (EFS) is for educational purposes only and may be
modified and saved under a new file name. eSignal is not responsible
for the functionality once modified. eSignal reserves the right
to modify and overwrite this EFS file with each new release.
Description:
Pair Trading With A Twist by Domenico D'Errico
Version: 1.00 10/10/2016
Formula Parameters: Default:
Correlating symbol SPY
Fast MA 4
Slow MA 40
BG Color Condition Trend Following
Notes:
The related article is copyrighted material. If you are not a subscriber
of Stocks & Commodities, please visit www.traders.com.
**********************************/
var fpArray = new Array();
function preMain(){
if (isWatchList()){
setCursorLabelName("Trend Following",0);
setCursorLabelName("Mean Rev",1);
setCursorLabelName("Trend&Pullback",2);
setDefaultBarFgColor(Color.white,0);
setDefaultBarFgColor(Color.white,1);
setDefaultBarFgColor(Color.white,2);
}
else {
setDefaultPrecision(4);
setCursorLabelName("Fast MA", 1);
setCursorLabelName("Slow MA", 2);
setDefaultBarFgColor(Color.RGB(255,106,0), 1);
setDefaultBarFgColor(Color.RGB(255,0,110), 2);
setDefaultBarThickness(3,0);
}
setIntervalsBackfill(true);
var x = 0;
fpArray[x] = new FunctionParameter("Leg2", FunctionParameter.STRING);
with (fpArray[x++]){
setName("Correlating symbol");
addOption(" ");
addOption("SPY");
setDefault("SPY");
}
fpArray[x] = new FunctionParameter("FastMA", FunctionParameter.NUMBER);
with (fpArray[x++]){
setName("Fast MA");
setLowerLimit(1);
setDefault(4);
}
fpArray[x] = new FunctionParameter("SlowMA", FunctionParameter.NUMBER);
with (fpArray[x++]){
setName("Slow MA");
setLowerLimit(1);
setDefault(40);
}
fpArray[x] = new FunctionParameter("BgCColor", FunctionParameter.STRING);
with (fpArray[x++]){
setName("BG Color Condition");
addOption("Trend Following");
addOption("Mean Reverting");
addOption("Trend Pullback");
setDefault("Trend Following");
}
}
var bVersion = null;
var bInit = false;
var xFastSMA = null;
var xRatioClose = null;
var xSlowSMA = null;
var sTFStrategy = null;
var sMRStrategy = null;
var sTBStrategy = null;
var upColor = null;
var sTBStrategy_1 = null;
function main (Leg2, FastMA, SlowMA, BgCColor){
if (bVersion == null) bVersion = verify();
if (bVersion == false) return;
if (getBarState() == BARSTATE_ALLBARS)
bInit = false;
if (getCurrentBarCount() <= SlowMA) return;
if (!bInit){
sTFStrategy = null;
sMRStrategy = null;
sTBStrategy = null;
upColor = Color.RGB(0,60,0);
var sSpread = getSymbol() + " / " + Leg2;
xRatioClose = close(sym(sSpread));
if (!isWatchList())
setCursorLabelName(sSpread, 0);
xFastSMA = sma(FastMA, xRatioClose);
xSlowSMA = sma(SlowMA, xRatioClose);
bInit = true;
}
if(getBarState() == BARSTATE_NEWBAR)
sTBStrategy_1 = sTBStrategy;
var nFastSMA = xFastSMA.getValue(0);
var nSlowSMA = xSlowSMA.getValue(0);
var nRatioClose = xRatioClose.getValue(0);
if (nFastSMA > nSlowSMA)
sTFStrategy = "UP";
else if (nFastSMA < nSlowSMA)
sTFStrategy = null;
if (nRatioClose < nFastSMA)
sMRStrategy = "Mean Rev UP";
else if (nRatioClose > nFastSMA)
sMRStrategy = null;
if (sTBStrategy_1 == null){
if (nFastSMA > nSlowSMA && nRatioClose < nFastSMA)
sTBStrategy = "UP";
else sTBStrategy = sTBStrategy_1
}
if (sTBStrategy_1 != null){
if (nFastSMA < nSlowSMA && nRatioClose > nFastSMA)
sTBStrategy = null;
else sTBStrategy = sTBStrategy_1;
}
if (isWatchList()){
if (sTFStrategy != null) setBarBgColor(upColor,0);
if (sMRStrategy != null) setBarBgColor(upColor,1);
if (sTBStrategy != null) setBarBgColor(upColor,2);
return [sTFStrategy, sMRStrategy, sTBStrategy];
}
else {
if ((BgCColor == "Trend Following" && sTFStrategy != null) ||
(BgCColor == "Mean Reverting" && sMRStrategy != null) ||
(BgCColor == "Trend Pullback" && sTBStrategy != null))
setBarBgColor(upColor,0);
return [nRatioClose, nFastSMA, nSlowSMA];
}
}
function verify(){
var b = false;
if (getBuildNumber() < 2455){
drawTextAbsolute(5, 35, "This study requires version 11.3 or later.",
Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
null, 13, "error");
drawTextAbsolute(5, 20, "Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp",
Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
null, 13, "upgrade");
return b;
}
else
b = true;
return b;
}