Announcement

Collapse
No announcement yet.

2016 Dec: Pair Trading With A Twist by Domenico D'Errico

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 2016 Dec: Pair Trading With A Twist by Domenico D'Errico

    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:
    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 
    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 (Leg2FastMASlowMABgCColor){
        if (
    bVersion == nullbVersion 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(sSpread0);

            
    xFastSMA sma(FastMAxRatioClose);
            
    xSlowSMA sma(SlowMAxRatioClose);

            
    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 != nullsetBarBgColor(upColor,0);
            if (
    sMRStrategy != nullsetBarBgColor(upColor,1);
            if (
    sTBStrategy != nullsetBarBgColor(upColor,2);

            return [
    sTFStrategysMRStrategysTBStrategy];
        }
        else {
            if ((
    BgCColor == "Trend Following" && sTFStrategy != null) ||
                (
    BgCColor == "Mean Reverting" && sMRStrategy != null) ||
                (
    BgCColor == "Trend Pullback" && sTBStrategy != null))
                
    setBarBgColor(upColor,0);

            return [
    nRatioClosenFastSMAnSlowSMA];
        }
    }

    function 
    verify(){
        var 
    false;
        if (
    getBuildNumber() < 2455){

            
    drawTextAbsolute(535"This study requires version 11.3 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;

Working...
X