File Name: Regression_Divergence.efs
Description:
Trading The Nikkei by Markos Katsanos
Formula Parameters:
Regression_Divergence.efs
Security 1: FXY
Security 2: SPY
Div Length: 50
RLength: 50
ROC Length: 3
Stoc Length: 25
MA&LR Length: 25
OB: 90
Correlation: 0.8
DivCrit: 75
Lag: 3
Stop Loss (%): 1.5
Exit Days: 11
Notes:
The related article is copyrighted material. If you are not a subscriber
of Stocks & Commodities, please visit www.traders.com.
Download File:
Regression_Divergence.efs
Regression_Divergence.efs
EFS Code:
Description:
Trading The Nikkei by Markos Katsanos
Formula Parameters:
Regression_Divergence.efs
Security 1: FXY
Security 2: SPY
Div Length: 50
RLength: 50
ROC Length: 3
Stoc Length: 25
MA&LR Length: 25
OB: 90
Correlation: 0.8
DivCrit: 75
Lag: 3
Stop Loss (%): 1.5
Exit Days: 11
Notes:
The related article is copyrighted material. If you are not a subscriber
of Stocks & Commodities, please visit www.traders.com.
Download File:
Regression_Divergence.efs
Regression_Divergence.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:
Trading The Nikkei by Markos Katsanos
Version: 1.00 05/12/2017
Formula Parameters: Default:
Security 1 FXY
Security 2 SPY
Div Length 50
RLength 50
ROC Length 3
Stoc Length 25
MA&LR Length 25
OB 90
Correlation 0.8
DivCrit 75
Lag 3
Stop Loss (%) 1.5
Exit Days 11
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(){
setPriceStudy(false);
setStudyTitle("RegressionDivergence");
setIntervalsBackfill(true);
var x=0;
fpArray[x] = new FunctionParameter("S1", FunctionParameter.STRING);
with (fpArray[x++]){
setName("Security 1");
addOption(" ");
addOption("FXY");
setDefault("FXY");
}
fpArray[x] = new FunctionParameter("S2", FunctionParameter.STRING);
with (fpArray[x++]){
setName("Security 2");
addOption(" ");
addOption("SPY");
setDefault("SPY");
}
fpArray[x] = new FunctionParameter("DLength", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Div Length");
setDefault(50);
setLowerLimit(1);
}
fpArray[x] = new FunctionParameter("RLength", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("RLength");
setDefault(50);
setLowerLimit(1);
}
fpArray[x] = new FunctionParameter("ROCLength", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("RoC Length");
setDefault(3);
setLowerLimit(1);
}
fpArray[x] = new FunctionParameter("SLength", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Stoc Length");
setDefault(25);
setLowerLimit(1);
}
fpArray[x] = new FunctionParameter("MLLength", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("MA&LR Length");
setDefault(25);
setLowerLimit(1);
}
fpArray[x] = new FunctionParameter("OB", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("OB");
setDefault(90);
setLowerLimit(1);
}
fpArray[x] = new FunctionParameter("Corr", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Correlation");
setDefault(0.8);
setLowerLimit(0.1);
}
fpArray[x] = new FunctionParameter("DivCrit", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("DivCrit");
setDefault(75);
setLowerLimit(1);
}
fpArray[x] = new FunctionParameter("Lag", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Lag");
setDefault(3);
setLowerLimit(1);
}
fpArray[x] = new FunctionParameter("StLoss", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Stop Loss (%)");
setDefault(1.5);
setLowerLimit(0.1);
}
fpArray[x] = new FunctionParameter("Exit", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Exit Days");
setDefault(11);
setLowerLimit(1);
}
}
var bInit = false;
var bVersion = null;
var xCloseSeries1 = null;
var xCloseSeries2 = null;
var xStochactic = null;
var xSMA = null;
var xLR = null;
var xRoCMain = null;
var xRoC1 = null;
var xRoC2 = null;
var xCR1 = null;
var xCR2 = null;
var xRegr = null;
var xIM = null;
var xClose = null;
var xHigh = null;
var xLow = null;
var DivShort = 0;
var OS = 0;
var nEntryBarN = 0;
function main(S1, S2, DLength, RLength, ROCLength, SLength, MLLength, OB, Corr, DivCrit, Lag, StLoss, Exit){
if (bVersion == null) bVersion = verify();
if (bVersion == false) return;
// if (getCurrentBarCount() <= (RLength + DLength + ROCLength)) return;
if (getBarState() == BARSTATE_ALLBARS){
DivShort = 0;
OS = 0;
nEntryBarN = 0;
bInit = false;
}
if (!bInit){
xClose = close();
xCloseSeries1 = close(sym(S1));
xCloseSeries2 = close(sym(S2));
xHigh= high();
xLow =low();
xStochactic = efsInternal("CustStochastic", xClose, xHigh, xLow, SLength);
xSMA = sma(MLLength, xClose);
xLR = efsInternal("LRSlopeM", MLLength, xClose);
xRoCMain = efsInternal("ROCM", xClose, ROCLength);
xRoC1 = efsInternal("ROCM", xCloseSeries1, ROCLength);
xRoC2 = efsInternal("ROCM", xCloseSeries2, ROCLength);
xCR1 = efsInternal("CorrelationMK", xRoCMain, xRoC1, RLength);
xCR2 = efsInternal("CorrelationMK", xRoCMain, xRoC2, RLength);
xRegr = efsInternal("Regression", xCR1, xRoCMain, xRoC1, RLength);
xIM = efsInternal("CalcIM", xRegr, DLength);
DivShort = 100 - DivCrit;
OS = 100 - OB;
addBand(DivCrit, PS_DASH, 1, Color.grey, 1);
addBand(DivShort, PS_DASH, 1, Color.grey, 2);
bInit = true;
}
if (getCurrentBarIndex != 0){
if (!Strategy.isInTrade()){
if (xIM.getValue(-Lag) > DivCrit && xIM.getValue(0) < xIM.getValue(-Lag)
&& xIM.getValue(0) > DivShort
&& xCR2.getValue(0) < Corr
&& (xLR.getValue(0) >= xLR.getValue(-1) || xClose.getValue(0) > xSMA.getValue(0))){
Strategy.doLong("DIVJPY", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
nEntryBarN = getCurrentBarCount();
}
else if (xIM.getValue(-Lag) < DivShort && xIM.getValue(0) > xIM.getValue(-Lag)
&& xIM.getValue(0) < DivCrit
&& xCR2.getValue(0) < Corr
&& (xLR.getValue(0) <= xLR.getValue(-1) || xClose.getValue(0) < xSMA.getValue(0))){
Strategy.doShort("NDIVJPY", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
nEntryBarN = getCurrentBarCount();
}
}
else {
var nBarsSinceEntry = getCurrentBarCount() - nEntryBarN;
if (nBarsSinceEntry > 3){
if (Strategy.isLong() && xStochactic.getValue(0) > OS && xClose.getValue(0) < xClose.getValue(-nBarsSinceEntry) * (1 - StLoss/100))
Strategy.doSell("STOP", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
else if (Strategy.isShort() && xStochactic.getValue(0) < OB && xClose.getValue(0) > xClose.getValue(-nBarsSinceEntry) * (1 + StLoss/100))
Strategy.doCover("INITIAL STOP", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
}
if (nBarsSinceEntry > 7 && Strategy.isInTrade()){
if (Strategy.isLong() && xClose.getValue(0) < hhv(nBarsSinceEntry, xClose, 0) * (1 - StLoss/100))
Strategy.doSell("TRAIL", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
else if (Strategy.isShort() && xClose.getValue(0) > llv(nBarsSinceEntry, xClose, 0) * (1 + StLoss/100))
Strategy.doCover("TRAIL COVER", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT);
}
if (nBarsSinceEntry >= Exit && Strategy.isInTrade()){
if (Strategy.isLong()) Strategy.doSell("TIME", Strategy.CLOSE, Strategy.THISBAR, Strategy.ALL);
else if (Strategy.isShort()) Strategy.doCover("TIMEX", Strategy.CLOSE, Strategy.THISBAR, Strategy.ALL);
}
if (!Strategy.isInTrade()) nEntryBarN = 0;
}
}
if (Strategy.isLong()) setBarBgColor(Color.RGB(0,60,0));
else if (Strategy.isShort()) setBarBgColor(Color.RGB(170,46,46));
return xIM.getValue(0);
}
var xSTDDev1 = null;
var xSTDDev2 = null;
var xAvgRoC1 = null;
var xAvgRoC2 = null;
var xInit = false;
function Regression(xCR1, xRoC1, xRoC2, Length){
if (!xInit){
xSTDDev1 = stdDev(Length, xRoC1);
xSTDDev2 = stdDev(Length, xRoC2);
xAvgRoC1 = sma(Length, xRoC1);
xAvgRoC2 = sma(Length, xRoC2);
xInit = true;
}
var b2 = xCR1.getValue(0) * xSTDDev1.getValue(0) / (xSTDDev2.getValue(0) + 0.001);
var a2 = xAvgRoC1.getValue(0) - b2 * xAvgRoC2.getValue(0);
return (b2 * xRoC2.getValue(0) + a2) - xRoC1.getValue(0);
}
function CustStochastic(xClose, xHigh, xLow, Length){
var nStoc = null;
nStoc = (sma(3, efsInternal("SeriesSub", xClose, llv(Length, xLow))) * 100) /
(sma(3, efsInternal("SeriesSub", hhv(Length, xHigh), llv(Length, xLow))));
return nStoc;
}
function CalcIM(xRegr, Length){
var IM = sma(2, efsInternal("SeriesSub", xRegr, llv(Length, xRegr))) * 100 /
(sma(2, efsInternal("SeriesSub", hhv(Length, xRegr), llv(Length, xRegr))) + 0.01);
return IM;
}
function SeriesSub(xSeries1, xSeries2){
return xSeries1.getValue(0) - xSeries2.getValue(0);
}
function LRSlopeM(Length, xClose){
var SL = 0;
var SumBars = Length * (Length - 1) * 0.5;
var SumSqrBars = (Length - 1) * Length * (2 * Length - 1) / 6;
var Sum1 = 0;
var SumY = 0;
var i = 0;
for (i = 0; i < Length; i++) {
Sum1 += i * xClose.getValue(-i);
SumY += xClose.getValue(-i);
}
var Sum2 = SumBars * SumY;
var Num1 = Length * Sum1 - Sum2;
var Num2 = SumBars * SumBars - Length * SumSqrBars;
SL = Num1 / Num2;
return SL * 100;
}
function ROCM(xSeries, Length){
if (xSeries.getValue(-Length) != null)
return (xSeries.getValue(0) / xSeries.getValue(-Length) - 1) * 100;
}
function CorrelationMK(xSeries1, xSeries2, Length){
var nQ1 = Sum(efsInternal("SeriesMP", xSeries1, xSeries2), Length) -
(Sum(xSeries1, Length) * Sum(xSeries2, Length) / Length);
var nQ2 = Sum(efsInternal("SeriesMP", xSeries2, xSeries2), Length) -
(Sum(xSeries2, Length) * Sum(xSeries2, Length) / Length);
var nQ3 = Sum(efsInternal("SeriesMP", xSeries1, xSeries1), Length) -
(Sum(xSeries1, Length) * Sum(xSeries1, Length) / Length);
if (nQ2 * nQ3 > 0) var nQ2Q3 = Math.sqrt(nQ2 * nQ3);
if (nQ2Q3 != 0){
var nR = nQ1 / nQ2Q3;
if (nR <= 1 && nR >= -1) return nR;
}
}
function Sum(xSeries, Length){
if (xSeries.getValue(-Length) != null){
var sum = 0;
for (var i = 0; i < Length; i++){
sum += xSeries.getValue(-i);
}
return sum;
}
}
function SeriesMP(xSeries1, xSeries2){
if (xSeries1.getValue(0) != null && xSeries2.getValue(0) != null)
return xSeries1.getValue(0) * xSeries2.getValue(0);
}
function verify(){
var b = false;
if (getBuildNumber() < 3742){
drawTextAbsolute(5, 35, "This study requires version 12.1 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;
}