File Name: PredictabilityOfEvent.efs, SimpleStocTrSystem.efs

Description:
Trading System Design: A Statistical Approach by John F. Ehlers and Ric Way

Formula Parameters:

PredictabilityOfEvent.efs
Stoc Length: 10
Offset Bars Length: 10
Threshold: 0.2

SimpleStocTrSystem.efs
Stoc Length: 8
Threshold: 0.3
Trade Length: 14
Percent Loss: 3.8
Entry Position Color: lime
Exit Position Color: red

Notes:
The related article is copyrighted material. If you are not a subscriber
of Stocks & Commodities, please visit www.traders.com.

Download File:
PredictabilityOfEvent.efs
SimpleStocTrSystem.efs

SimpleStocTrSystem.efs


EFS Code:

PredictabilityOfEvent.efs
PHP Code:
/*********************************
Provided By:  
    Interactive Data Corporation (Copyright © 2015) 
    All rights reserved. This sample eSignal Formula Script (EFS)
    is for educational purposes only. Interactive Data Corporation
    reserves the right to modify and overwrite this EFS file with 
    each new release. 

Description:        
    Trading System Design: A Statistical Approach by John F. Ehlers and Ric Way
    
Version:            1.00  01/15/2015

Formula Parameters:                     Default:
Stoc Length                             10
Offset Bars Length                      10
Threshold                               0.2

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(){
    
    
setStudyTitle("PredictabilityOfEvent");
    
setPriceStudy(false);
    
    var 
0;

    
fpArray[x] = new FunctionParameter("fpStocLength"FunctionParameter.NUMBER);
    
with(fpArray[x++]){
        
setName("Stoc Length");
        
setLowerLimit(1);
        
setDefault(10);
    }

    
fpArray[x] = new FunctionParameter("fpOffLength"FunctionParameter.NUMBER);
    
with(fpArray[x++]){
        
setName("Offset Bars Length");
        
setLowerLimit(1);    
        
setDefault(10);
    }

    
fpArray[x] = new FunctionParameter("fpThreshold"FunctionParameter.NUMBER);
    
with(fpArray[x++]){
        
setName("Threshold");
        
setLowerLimit(0);
        
setUpperLimit(1); 
        
setDefault(0.2);
    }
}

var 
bInit false;
var 
bVersion null;

var 
xClose null;
var 
xStoc null;

var 
nFuturePrice  null;

var 
aPredictBin = [];
var 
fStat = new File('Stat.csv');

function 
main(fpStocLengthfpOffLengthfpThreshold){
    
    if (
bVersion == nullbVersion verify();
    if (
bVersion == false) return;
        
    if (!
bInit){
        
xClose close();        
        
xStoc efsInternal("calc_Stoc"fpStocLengthxClose);
        
        
bInit true
    }

    if (
getBarState() == BARSTATE_ALLBARS){
        for (
0<= 100i++)
            
aPredictBin[i] = 0;
    }
    
    var 
nClose xClose.getValue(0);
    var 
nPastClose xClose.getValue(-(fpOffLength 1));
    
    if (
nPastClose == null)
        return;
    
    if (
crossUnder(xStocfpStocLengthfpOffLengthfpThreshold)){
        
        if (
getBarState() == BARSTATE_CURRENTBAR){
            for (
0<= 100i++)
                if (
nFuturePrice && nFuturePrice <= i)
                    
aPredictBin[i] = aPredictBin[i] - 1;
        }
        
        
nFuturePrice 100 * (nClose nPastClose) / nPastClose;
        
        if (
nFuturePrice < -10nFuturePrice = -10;
        if (
nFuturePrice 10nFuturePrice 10;
        
        
nFuturePrice * (nFuturePrice 10);
        
        for (
0<= 100i++)
            if (
nFuturePrice && nFuturePrice <= i)
                
aPredictBin[i] = aPredictBin[i] + 1;
    }
    
    var 
nCG 0;
    var 
nDenom 0;
            
    for (
0<= 100i++){
        
nCG nCG aPredictBin[i];
        
nDenom nDenom aPredictBin[i];
    }
    
    if (
nDenom == null)
       return;
    
    
nCG = (nCG nDenom 50) / 5;

    if (
isLastBarOnChart()){
        
fStat.open("wt");
        
fStat.writeln("Change in %, Change in Bins, Occurrences");
        
        for (
0<= 100i++)
            
fStat.writeln(0.2 10 '%, ' ', ' aPredictBin[i]);
        
        
fStat.close(); 
    }
    
    return 
nCG;  
}

var 
xHignest null;
var 
xLowest null;

function 
calc_Stoc(nLengthxSource){
    
   if (
getBarState() == BARSTATE_ALLBARS){
       
xHignest highest(nLengthxSource);
       
xLowest lowest(nLengthxSource);
   }
   
   var 
nSource xSource.getValue(0);
   var 
nHighest xHignest.getValue(0);
   var 
nLowest xLowest.getValue(0);

   if (
nSource == null || nHighest == null || nLowest == null || nHighest == nLowest)
        return;
  
   var 
nReturnValue = (nSource nLowest) / (nHighest nLowest);
   
   return 
nReturnValue;
}

function 
crossUnder(xStocnStocLengthnOffLengthnThreshold){
        
    var 
nReturnValue false;
    
    var 
nStoc xStoc.getValue(-(nOffLength 1));
    
    if (
nStoc == null)
        return;
       
    if (
nStoc nThreshold){
        for (var 
= -nOffLength>= -(getCurrentBarCount() - nStocLength); i--){
                
            var 
nPrevStoc xStoc.getValue(i);
            
            if (
nPrevStoc == null)
                return;
                
            if (
nPrevStoc != nThreshold){
                if (
nPrevStoc nThreshold)
                    
nReturnValue true
                break;
            }
        } 
    }

    return 
nReturnValue;
}

function 
verify(){
    
    var 
false;
    
    if (
getBuildNumber() < 779){
        
drawTextAbsolute(535"This study requires version 8.0 or later."
            
Color.whiteColor.blueText.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            
null13"error");
        
drawTextAbsolute(520"Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp"
            
Color.whiteColor.blueText.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            
null13"upgrade");
        return 
b;
    } 
    else{
        
true;
    }

    return 
b;

SimpleStocTrSystem.efs
PHP Code:
/*********************************
Provided By:  
    Interactive Data Corporation (Copyright © 2015) 
    All rights reserved. This sample eSignal Formula Script (EFS)
    is for educational purposes only. Interactive Data Corporation
    reserves the right to modify and overwrite this EFS file with 
    each new release. 

Description:        
    Trading System Design: A Statistical Approach by John F. Ehlers and Ric Way
    
Version:            1.00  01/12/2015

Formula Parameters:                     Default:
Stoc Length                             8
Threshold                               0.3
Trade Length                            14
Percent Loss                            3.8
Entry Position Color                    lime
Exit Position Color                     red

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(){
    
    
setStudyTitle("SimpleStocTrSystem");
    
setPriceStudy(true);
    
    var 
0;

    
fpArray[x] = new FunctionParameter("fpLength"FunctionParameter.NUMBER);
    
with(fpArray[x++]){
        
setName("Stoc Length");
        
setLowerLimit(1);
        
setDefault(8);
    }

    
fpArray[x] = new FunctionParameter("fpThreshold"FunctionParameter.NUMBER);
    
with(fpArray[x++]){
        
setName("Threshold");
        
setLowerLimit(0);
        
setUpperLimit(1); 
        
setDefault(0.3);
    }

    
fpArray[x] = new FunctionParameter("fpTradeLength"FunctionParameter.NUMBER);
    
with(fpArray[x++]){
        
setName("Trade Length");
        
setLowerLimit(0);    
        
setDefault(14);
    } 
    
    
fpArray[x] = new FunctionParameter("fpPctLoss"FunctionParameter.NUMBER);
    
with(fpArray[x++]){
        
setName("Percent Loss");    
        
setLowerLimit(0);
        
setUpperLimit(100); 
        
setDefault(3.8);
    }

    
fpArray[x] = new FunctionParameter("fpEntryColor"FunctionParameter.COLOR);
    
with(fpArray[x++]){
        
setName("Entry Position Color");    
        
setDefault(Color.lime);
    }

    
fpArray[x] = new FunctionParameter("fpExitColor"FunctionParameter.COLOR);
    
with(fpArray[x++]){
        
setName("Exit Position Color");    
        
setDefault(Color.red);
    }
}

var 
bInit false;
var 
bVersion null;

var 
xClose null;
var 
xOpen null;
var 
xLow null;

var 
xStoc null;

var 
nBarsSinceEntry null;
var 
nEntryPrice null;

var 
nLotSize null;

function 
main(fpLengthfpThresholdfpTradeLengthfpPctLossfpEntryColorfpExitColor){
    
    if (
bVersion == nullbVersion verify();
    if (
bVersion == false) return;
        
    if (!
bInit){
        
        
xClose close();
        
xOpen open();
        
xLow low();
        
        
xStoc efsInternal("calc_Stoc"fpLengthxClose);
        
        
nLotSize Strategy.getDefaultLotSize();
        
        
bInit true
    }
    
    if (
getCurrentBarIndex() != 0){
        
        if (!
Strategy.isInTrade() && crossUnder(xStocfpThresholdfpLength)){
            
            
nEntryPrice xOpen.getValue(1);
            
            
Strategy.doLong("Enter Long"Strategy.MARKETStrategy.NEXTBAR);
            
drawShapeRelative(1BelowBar1Shape.UPTRIANGLEnullfpEntryColorText.PRESET,
            
getCurrentBarIndex()+"Ent");
            
drawTextRelative(1BelowBar2"Enter Long"fpEntryColornullText.PRESET|Text.CENTER,
            
nullnullgetCurrentBarIndex()+"Ent_Label"); 
            
drawTextRelative(1BelowBar3nLotSize " @ " formatPriceNumber(nEntryPrice),
            
fpEntryColornullText.PRESET|Text.CENTERnullnullgetCurrentBarIndex()+"Ent_Size");
            
            
nBarsSinceEntry getCurrentBarCount() + 1;
            
            return;
        }
        
        if (
Strategy.isLong()){
            
            var 
bExit false;
            var 
sLabel "";
            
            var 
nCountEntry getCurrentBarCount() - nBarsSinceEntry;
            if (
nCountEntry >= fpTradeLength){
                
bExit true;
                
sLabel "Exit Length";
            }
             
            var 
nLow xLow.getValue(0);
            var 
nStopLevel nEntryPrice * (fpPctLoss 100); 
            if (
nLow nStopLevel){
                
bExit true;
                
sLabel "Exit Stop";
            }
                
            if (
bExit){
                var 
nExitPrice xOpen.getValue(1);
            
                
Strategy.doSell("Exit Long"Strategy.MARKETStrategy.NEXTBAR)
                
drawShapeRelative(1AboveBar1Shape.DOWNTRIANGLEnullfpExitColor,
                
Text.PRESETgetCurrentBarIndex()+"Ex");
                
drawTextRelative(1AboveBar2sLabelfpExitColornullText.PRESET|Text.CENTER,
                
nullnullgetCurrentBarIndex()+"Ex_Label"); 
                
drawTextRelative(1AboveBar3nLotSize " @ " formatPriceNumber(nExitPrice),
                
fpExitColornullText.PRESET|Text.CENTERnullnullgetCurrentBarIndex()+"Ex_Size");
            }    
        }
    }
}

var 
xHignest null;
var 
xLowest null;

function 
calc_Stoc(nLengthxSource){
    
   if (
getBarState() == BARSTATE_ALLBARS){
       
xHignest highest(nLengthxSource);
       
xLowest lowest(nLengthxSource);
   }
   
   var 
nSource xSource.getValue(0);
   var 
nHighest xHignest.getValue(0);
   var 
nLowest xLowest.getValue(0);

   if (
nSource == null || nHighest == null || nLowest == null)
        return;
    
   var 
nReturnValue = (nSource nLowest) / (nHighest nLowest);
   
   return 
nReturnValue;
}

function 
crossUnder(xStocnThresholdnStocLength){
        
    var 
nReturnValue false;
    
    var 
nStoc xStoc.getValue(0);
       
    if (
nStoc nThreshold){
        for (var 
= -1>= -(getCurrentBarCount() - nStocLength) ; i--){
                
            var 
nPrevStoc xStoc.getValue(i);
                
            if (
nPrevStoc != nThreshold){
                if (
nPrevStoc nThreshold)
                    
nReturnValue true
                break;
            }
        } 
    }
    return 
nReturnValue;
}

function 
verify(){
    
    var 
false;
    
    if (
getBuildNumber() < 779){
        
drawTextAbsolute(535"This study requires version 8.0 or later."
            
Color.whiteColor.blueText.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            
null13"error");
        
drawTextAbsolute(520"Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp"
            
Color.whiteColor.blueText.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            
null13"upgrade");
        return 
b;
    } 
    else{
        
true;
    }

    return 
b;