2007 Mar: Moving Average Crossovers (Part II)

ICE Data Services -

Probable_SMACross.efs, SMA_StochCross.efs  
EFSLibrary - Discussion Board  

File Name:

  • Probable_SMACross.efs
  • SMA_StochCross.efs

Description:

These studies are based on the March 2007 article, Moving Average Crossovers (Part II), by Dimitris Tsokakis.

Formula Parameters:

Probable_SMACross.efs

  • Fast MA Periods: 20
  • Slow MA Periods: 30

SMA_StochCross.efs

  • Fast MA Periods: 20
  • Slow MA Periods: 30
  • %K Length: 14
  • %K Smoothing: 1
  • %D Length: 3

Notes:
In real time, the Probable_SMACross.efs will print the messages as outlined in the side bar of the article to the formula output window. The SMA_StochCross.efs will draw a green/red circle on the chart to indicate the cross of the TC and green/red arrows for the MA crosses. The related article is copyrighted material. If you are not a subscriber of Stocks & Commodities, please visit www.traders.com.

Download File:
Probable_SMACross.efs
SMA_StochCross.efs



EFS Code:

Probable_SMACross.efs

/***************************************
Provided By : eSignal (c) Copyright 2006
Description:  Moving Average Crossovers Part II
              by Dimitris Tsokakis

Version 1.0  1/08/2007

Notes:
* March 2007 Issue of Stocks and Commodities Magazine
* Study requires version 8.0 or later.


Formula Parameters:                     Default:
Fast MA Periods                         20
Slow MA Periods                         30
*****************************************************************/

function preMain() {
    setPriceStudy(true);
    setStudyTitle("Probable SMA Cross ");
    setShowTitleParameters(false); 
    setCursorLabelName("Fast MA", 0);
    setCursorLabelName("Slow MA", 1);
    setCursorLabelName("TC", 2);
    
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarFgColor(Color.red, 1);
    setDefaultBarFgColor(Color.magenta, 2);
    
    setDefaultBarThickness(1, 0);
    setDefaultBarThickness(1, 1);
    setDefaultBarThickness(2, 2);
    
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.khaki);
    setDefaultFont("Arial", 11);

    var fp1 = new FunctionParameter("MAlen1", FunctionParameter.NUMBER);
        fp1.setName("Fast MA Periods");
        fp1.setLowerLimit(2);
        fp1.setDefault(20);
    var fp2 = new FunctionParameter("MAlen2", FunctionParameter.NUMBER);
        fp2.setName("Slow MA Periods");
        fp2.setLowerLimit(2);
        fp2.setDefault(30);
}

// Global Variables
var bVersion    = null;    // Version flag
var bInit       = false;   // Initialization flag
var nBars       = 0;
var aPrint      = new Array(6);
for (var i = 0; i < 6; i++) {
    aPrint[i] = true;
}


var xMA1 = null;        // fast MA
var xMA2 = null;        // slow MA
var xTC  = null;        // Tomorrow's close


function main(MAlen1, MAlen2) {
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    

    //Initialization
    if (bInit == false) {
        xMA1 = sma(MAlen1);  // fast MA
        xMA2 = sma(MAlen2);  // slow MA
        xTC = efsInternal("calcTC", MAlen1, MAlen2);
        bInit = true;
    }

    var nState      = getBarState();
    var bDscP       = false;
    var bAscP       = false;
    var bExpectMA   = false;
    var bConfirmed  = false;
    var bCrossUp    = false;
    var bCrossDn    = false;
    var bFilter     = false;
    var nUR         = null;
    var nLR         = null;
    var nUcoeff     = null;
    var nLcoeff     = null;
    var bExpect     = false;
    
    var nC          = close(0);
    var nTagID      = rawtime(0);    
    var nMA1_0      = xMA1.getValue(0);
    var nMA1_1      = xMA1.getValue(-1);
    var nMA2_0      = xMA2.getValue(0);
    var nMA2_1      = xMA2.getValue(-1);
    var nTC_0       = xTC.getValue(0);
    var nTC_1       = xTC.getValue(-1);
    if (nMA1_1 == null || nMA2_1 == null || nTC_1 == null) return;
    
    if (nState == BARSTATE_NEWBAR) nBars++;
    
    // MA-crossover
    if (nMA1_0 > nMA2_0 && nMA1_1 <= nMA2_1) {
        // cross up
        bConfirmed = true;
        bCrossUp = true;
        nBars = 1;
        drawText("MA", AboveBar2, Color.green, Text.BOLD|Text.FRAME|Text.CENTER, "MA"+nTagID);
        drawShape(Shape.UPARROW, AboveBar1, Color.green, nTagID);
    } else if (nMA1_0 < nMA2_0 && nMA1_1 >= nMA2_1) {
        // cross down
        bConfirmed = true;
        bCrossDn = true;
        nBars = 1;
        drawText("MA", BelowBar2, Color.red, Text.BOLD|Text.FRAME|Text.CENTER, "MA"+nTagID);
        drawShape(Shape.DOWNARROW, BelowBar1, Color.red, nTagID);
    } else {
        removeShape(nTagID);
        removeText("MA"+nTagID);
    }
    
    // TC cross
    if (nTC_0 > nC && nTC_1 < close(-1)) {
        // cross up
        bAscP = true;
        setPriceBarColor(Color.black);
    } else if (nTC_0 < nC && nTC_1 > close(-1)) {
        // cross down
        bDscP = true;
        setPriceBarColor(Color.black);
    } else {
        removeShape("TC"+nTagID);
    }
    
    if (bAscP || bDscP) bExpectMA = true;

    nUR         = 2 * upperDonchian(800, roc(1), 0);
    nLR         = 2 * lowerDonchian(800, roc(1), 0);
    if (nUR == null || nLR == null) return;
    
    nUcoeff      = 1 + nUR/100;
    nLcoeff      = 1 + nLR/100;
    
    bFilter = (nTC_0 < (nLcoeff * nC)) || (nTC_0 > (nUcoeff * nC));
    bExpect = !bFilter;
    
    if (getCurrentBarIndex() >= -1 && nState == BARSTATE_NEWBAR) {
        for (var i = 0; i < 6; i++) {
            aPrint[i] = false;
        }
    }
    
    if (bExpect && !bExpectMA && !bConfirmed && nBars > 3 && nTC_0 < nC) {
        if (aPrint[0] == false) {
            debugPrintln("Expect a bearish MAcross soon.");
            aPrint[0] = true;
        }
        setPriceBarColor(Color.black);
    }
    if (bExpect && !bExpectMA && !bConfirmed && nBars > 3 && nTC_0 > nC) {
        if (aPrint[1] == false) {
            debugPrintln("Expect a bullish MAcross soon.");
            aPrint[1] = true;
        }
        setPriceBarColor(Color.black);
    }
    if (bDscP) {
        if (aPrint[2] == false) {
            debugPrintln("EXPECT a bearish MAcross on next bar.");
            aPrint[2] = true;
        }
    }
    if (bAscP) {
        if (aPrint[3] == false) {
            debugPrintln("EXPECT a bullish MAcross on next bar.");
            aPrint[3] = true;
        }
    }
    if (bCrossUp) {
        if (aPrint[4] == false) {
            debugPrintln("Bullish MAcross.");
            aPrint[4] = true;
        }
    }        
    if (bCrossDn) {
        if (aPrint[5] == false) {
            debugPrintln("Bearish MAcross.");
            aPrint[5] = true;
        }
    }
    
    return new Array(nMA1_0, nMA2_0, nTC_0.toFixed(4));
}

// TC Globals
var bInitTC = false;
var xTCMA1 = null;
var xTCMA2 = null;

function calcTC(Pfast, Kslow) {
    // k > p
    //TC = Pfast*(Kslow-1) * MA(Kslow-1) - Kslow*(Pfast-1) * MA(Pfast-1)  /  Kslow-Pfast;
    
    if (bInitTC == false) {
        xTCMA1 = sma((Pfast-1));
        xTCMA2 = sma((Kslow-1))
        bInitTC = true;
    }
    
    var nTC = null;
    var nMAfast = xTCMA1.getValue(0);
    var nMAslow = xTCMA2.getValue(0);
    if (nMAslow == null || nMAfast == null) return;
    
    if (Kslow-Pfast == 0) {
        nTC = ( (Pfast*(Kslow-1) * nMAslow) - (Kslow*(Pfast-1) * nMAfast) ) /  1;
    } else {
        nTC = ( (Pfast*(Kslow-1) * nMAslow) - (Kslow*(Pfast-1) * nMAfast) ) /  (Kslow-Pfast);
    }
    
    return nTC;
}

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

SMA_StochCross.efs

/***************************************
Provided By : eSignal (c) Copyright 2006
Description:  Moving Average Crossovers Part II 
              by Dimitris Tsokakis

Version 1.0  1/08/2007

Notes:
* March 2007 Issue of Stocks and Commodities Magazine
* Study requires version 8.0 or later.


Formula Parameters:                     Default:
Fast MA Periods                         20
Slow MA Periods                         30
%K Length                               14
%K Smoothing                            1
%D Length                               3
*****************************************************************/

function preMain() {
    setStudyTitle("SMA of Stoch Cross Predictor ");
    setShowTitleParameters(false); 
    setCursorLabelName("StochD", 0);
    setCursorLabelName("Fast MA", 1);
    setCursorLabelName("Slow MA", 2);
    setCursorLabelName("TC", 3);
    
    setDefaultBarFgColor(Color.khaki, 0);
    setDefaultBarFgColor(Color.black, 1);
    setDefaultBarFgColor(Color.red, 2);
    setDefaultBarFgColor(Color.magenta, 3);
    
    setDefaultBarThickness(2, 0);
    setDefaultBarThickness(1, 1);
    setDefaultBarThickness(1, 2);
    setDefaultBarThickness(2, 3);
    
    setDefaultFont("Arial", 11);
    setStudyMax(115);
    setStudyMin(-15);

    var fp1 = new FunctionParameter("MAlen1", FunctionParameter.NUMBER);
        fp1.setName("Fast MA Periods");
        fp1.setLowerLimit(2);
        fp1.setDefault(20);
    var fp2 = new FunctionParameter("MAlen2", FunctionParameter.NUMBER);
        fp2.setName("Slow MA Periods");
        fp2.setLowerLimit(2);
        fp2.setDefault(30);
    var fp3 = new FunctionParameter("nK", FunctionParameter.NUMBER);
        fp3.setName("%K Length");
        fp3.setLowerLimit(1);
        fp3.setDefault(14);
    var fp4 = new FunctionParameter("nKSmooth", FunctionParameter.NUMBER);
        fp4.setName("%K Smoothing");
        fp4.setLowerLimit(1);
        fp4.setDefault(1);
    var fp5 = new FunctionParameter("nD", FunctionParameter.NUMBER);
        fp5.setName("%D Length");
        fp5.setLowerLimit(1);
        fp5.setDefault(3);
}

// Global Variables
var bVersion  = null;    // Version flag
var bInit     = false;   // Initialization flag

var xStochD = null;
var xMA1 = null;        // fast MA
var xMA2 = null;        // slow MA
var xTC  = null;        // Tomorrow's close


function main(MAlen1, MAlen2, nK, nKSmooth, nD) {
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    

    //Initialization
    if (bInit == false) {
        xStochD = stochD(nK, nKSmooth, nD);
        xMA1 = sma(MAlen1, xStochD);  // fast MA
        xMA2 = sma(MAlen2, xStochD);  // slow MA
        xTC = efsInternal("calcTC", MAlen1, MAlen2, xStochD);
        bInit = true;
    }

    var nC1    = xStochD.getValue(0);
    var nC1_1  = xStochD.getValue(-1);
    var nTagID = rawtime(0);    
    var nMA1_0 = xMA1.getValue(0);
    var nMA1_1 = xMA1.getValue(-1);
    var nMA2_0 = xMA2.getValue(0);
    var nMA2_1 = xMA2.getValue(-1);
    var nTC_0 = xTC.getValue(0);
    var nTC_1 = xTC.getValue(-1);
    if (nMA1_1 == null || nMA2_1 == null || nTC_1 == null) return;
    
    // MA-crossover
    if (nMA1_0 > nMA2_0 && nMA1_1 <= nMA2_1) {
        // cross up
        drawText("MA", TopRow1, Color.green, Text.BOLD|Text.FRAME|Text.CENTER, "MA"+nTagID);
        drawShapeRelative(0, nC1, Shape.UPARROW, null, Color.green, 
            Shape.TOP, nTagID);
    } else if (nMA1_0 < nMA2_0 && nMA1_1 >= nMA2_1) {
        // cross down
        drawText("MA", BottomRow1, Color.red, Text.BOLD|Text.FRAME|Text.CENTER, "MA"+nTagID);
        drawShapeRelative(0, nC1, Shape.DOWNARROW, null, Color.red, 
            Shape.BOTTOM, nTagID);
    } else {
        removeShape(nTagID);
        removeText("MA"+nTagID);
    }
    
    // TC cross
    if (nTC_0 > nC1 && nTC_1 <= nC1_1) {
        drawShapeRelative(0, Math.min(nTC_0, 110), Shape.CIRCLE, null, Color.red, null, "TC"+nTagID);
    } else if (nTC_0 < nC1 && nTC_1 >= nC1_1) {
        drawShapeRelative(0, Math.max(nTC_0, -10), Shape.CIRCLE, null, Color.green, null, "TC"+nTagID);
    } else {
        removeShape("TC"+nTagID);
    }
    
    return new Array(nC1, nMA1_0, nMA2_0, xTC.getValue(0));
}

// TC Globals
var bInitTC = false;
var xTCMA1 = null;
var xTCMA2 = null;

function calcTC(Pfast, Kslow, xStD) {
    // k > p
    //TC = Pfast*(Kslow-1) * MA(Kslow-1) - Kslow*(Pfast-1) * MA(Pfast-1)  /  Kslow-Pfast;
    
    if (bInitTC == false) {
        xTCMA1 = sma((Pfast-1), xStD);
        xTCMA2 = sma((Kslow-1), xStD)
        bInitTC = true;
    }
    
    var nTC = null;
    var nMAfast = xTCMA1.getValue(0);
    var nMAslow = xTCMA2.getValue(0);
    if (nMAslow == null || nMAfast == null) return;
    
    if (Kslow-Pfast == 0) {
        nTC = ( (Pfast*(Kslow-1) * nMAslow) - (Kslow*(Pfast-1) * nMAfast) ) /  1;
    } else {
        nTC = ( (Pfast*(Kslow-1) * nMAslow) - (Kslow*(Pfast-1) * nMAfast) ) /  (Kslow-Pfast);
    }
    
    return nTC;
}

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