2016 Nov: Does Fully Automated Trading Software Work? by James Breen

ICE Data Services -

 

Trend_Following.efs  

EFSLibrary - Discussion Board  

 

File Name: Trend_Following.efs

Description:
Does Fully Automated Trading Software Work? by James Breen

 

Formula Parameters:
Trend_Following.efs

  • MALength: 10
  • EntryTrigger%: 3
  • ExitTrigger%: 4
  • InitialStopLoss%: 2
  • TrailingStopTrigger%: 6
  • TrailingStop%: 2

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

 

Download File:
Trend_Following.efs

Trend_Following.efs


EFS 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:        
    Does Fully Automated Trading Software Work? by James Breen

Version:            1.00  09/06/2016

Formula Parameters:                     Default:
MA Length                               10
Entry Trigger %                         3
Exit Trigger %                          4
Initial Stop Loss %                     2
Trailing Stop Trigger %                 6
Trailing Stop %                         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(){
    setPriceStudy(true);
    setStudyTitle("MA");
    
    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);
        setUpperLimit(1000);  
        setDefault(10);
        setName("MA Length");
    }

    fpArray[x] = new FunctionParameter("EntTrigger", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.1);
        setUpperLimit(100);
        setDefault(3);
        setName("Entry Trigger %");
    }

    fpArray[x] = new FunctionParameter("ExtTrigger", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.1);
        setUpperLimit(100);
        setDefault(4);
        setName("Exit Trigger %");
    }
    
    fpArray[x] = new FunctionParameter("InStopLoss", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.1);
        setUpperLimit(100);
        setDefault(2);
        setName("Initial Stop Loss %");
    }

    fpArray[x] = new FunctionParameter("TrlStopTrigger", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.1);
        setUpperLimit(100);
        setDefault(6);
        setName("Trailing Stop Trigger %");
    }

    fpArray[x] = new FunctionParameter("TrlStop", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0.1);
        setUpperLimit(100);
        setDefault(2);
        setName("Trailing Stop %");
    }

    fpArray[x] = new FunctionParameter("MaxLoss", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(0);
        setDefault(500);
        setName("Max Daily Loss $");
    }
}

var bInit = false;
var bVersion = null;
var xSMA = null;
var xClose = null;
var xHigh = null;
var xLow = null;
var nInv = null;
var nShares = 0;
var nEntryPrice = 0;
var nTrlStop = null;
var nHighestHigh = null;
var nLowestLow = null;
var nCumProfit = 0;
var bMaxLost = false;

function main(Length, EntTrigger, ExtTrigger, InStopLoss, TrlStopTrigger, TrlStop, MaxLoss){
    
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;
    
    if (getCurrentBarCount() <= Length) return;
    
    if (getBarState() == BARSTATE_ALLBARS){
        xSMA = null;
        xClose = null;
        xHigh = null;
        xLow = null;
        nInv = null;
        nShares = 0;
        nEntryPrice = 0;
        nTrlStop = null;
        nHighestHigh = null;
        nLowestLow = null;
        nCumProfit = 0;
        bMaxLost = false;
        bInit = false;
    }
    
    if (!bInit){
        xClose = close();
        xHigh = high();
        xLow = low();
        xSMA = sma(Length, xClose);
        nHighestHigh = xHigh.getValue(0);
        nLowestLow = xLow.getValue(0);
        bInit = true;
    }

    var nSMA = xSMA.getValue(0);
    var nClose = xClose.getValue(0);
    var nHigh = xHigh.getValue(0);
    var nLow = xLow.getValue(0);
    var nTradeProfit = 0;

    if (day(0) != day(-1) && !Strategy.isInTrade()){ 
        bMaxLost = false;
        nCumProfit = 0;
    }

    if (getCurrentBarIndex() != 0){
        if (Strategy.isLong())
            nTradeProfit = nClose * nShares - nInv;
        else if (Strategy.isShort())
            nTradeProfit = nInv - nClose * nShares;
        

        if ((nCumProfit + nTradeProfit) <= -MaxLoss){
            if (Strategy.isLong())
                Strategy.doSell("Loss Limit exceeded", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
            else 
                Strategy.doCover("Loss Limit exceeded", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
            nCumProfit = 0;
            bMaxLost = true;
        }
        
        if (nShares == 0)
            nShares = Strategy.getDefaultLotSize();
        
        if (Strategy.isLong()){
            if (nTrlStop == null && (nHigh * nShares) >= (nEntryPrice * nShares * (1 + TrlStopTrigger/100))){
                nTrlStop = nHigh * (1 - TrlStop/100);
                nHighestHigh = nHigh;
            }
            
            if (nTrlStop != null && nHigh > nHighestHigh){
                nHighestHigh = nHigh;
                nTrlStop = nHighestHigh * (1 - TrlStop/100);
            }

            if (nClose < (nSMA * (1 - ExtTrigger/100)))
                Strategy.doSell("Exit Long", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
            else if (nClose * nShares < (nInv * (1 - InStopLoss/100)))
                Strategy.doSell("Stop Loss Long", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
            else if (nTrlStop != null && nClose < nTrlStop)
                Strategy.doSell("Trl Stop Long", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
            
            if (!Strategy.isLong()){
                nCumProfit += nClose * nShares - nInv;
                nTrlStop = null;
            }
        

        }
        else if (Strategy.isShort()){
                if (nTrlStop == null && (nLow * nShares) <= (nEntryPrice * nShares * (1 - TrlStopTrigger/100))){
                nTrlStop = nLow * (1 + TrlStop/100);
                nLowestLow = nLow;
            }
            
            if (nTrlStop != null && nLow < nLowestLow){
                nLowestLow = nLow;
                nTrlStop = nLowestLow * (1 + TrlStop/100);
            }
           if (nClose > (nSMA * (1 + ExtTrigger/100))) 
                Strategy.doCover("Exit Short", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
           else if (nClose * nShares > (nInv * (1 + InStopLoss/100))){
                Strategy.doCover("Stop Loss Short", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
           }
           else if (nTrlStop != null && nClose > nTrlStop)
                Strategy.doCover("Trl Stop Short", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
           
           if (!Strategy.isShort()){
            nCumProfit += nInv - nClose * nShares;
               nTrlStop = null;
           }

       }
        
        if (!Strategy.isInTrade() && !bMaxLost){
            nTrlStop = null;
            if (nClose > (nSMA * (1 + EntTrigger/100))){
                Strategy.doLong("Long", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
            }
            else if (nClose < (nSMA * (1 - EntTrigger/100))){
                Strategy.doShort("Short", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
            }
        
            if (Strategy.isInTrade()){
                nEntryPrice = nClose;
                nInv = nShares * nEntryPrice;
            }
        }
        
        if (Strategy.isLong()) setBarBgColor(Color.RGB(0,60,0));
        else if (Strategy.isShort()) setBarBgColor(Color.RGB(170,46,46))
        
    }

    if (nSMA != null) return  nSMA;
}

function verify(){
    var b = false;
    if (getBuildNumber() < 779){
        
        drawTextAbsolute(5, 35, "This study requires version 10.6 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;
}