2007 Feb: Anticipating Moving Average Crossovers

ICE Data Services -

SMA_CrossPredictor.efs  

EFSLibrary - Discussion Board  

File Name: SMA_CrossPredictor.efs


Description:
This study is based on the February 2007 article, Anticipatin Moving Average Crossovers, by Dimitris Tsokakis.

 

Formula Parameters:

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

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

 

Download File:
SMA_CrossPredictor.efs



EFS Code:

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

Version 1.0  12/05/2006

Notes:
* Feb 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() {
    setStudyTitle("SMA Cross Predictor ");
    setShowTitleParameters(false); 
    setCursorLabelName("Close", 0);
    setCursorLabelName("Fast MA", 1);
    setCursorLabelName("Slow MA", 2);
    setCursorLabelName("TC", 3);
    
    setDefaultBarFgColor(Color.black, 0);
    setDefaultBarFgColor(Color.blue, 1);
    setDefaultBarFgColor(Color.red, 2);
    setDefaultBarFgColor(Color.magenta, 3);
    
    setDefaultBarThickness(2, 0);
    setDefaultBarThickness(1, 1);
    setDefaultBarThickness(1, 2);
    setDefaultBarThickness(2, 3);
    
    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 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 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;
    
    // MA-crossover
    if (nMA1_0 > nMA2_0 && nMA1_1 <= nMA2_1) {
        // cross up
        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
        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)) {
        drawShapeRelative(0, nTC_0, Shape.CIRCLE, null, Color.red, null, "TC"+nTagID);
    } else if (nTC_0 < nC && nTC_1 > close(-1)) {
        drawShapeRelative(0, nTC_0, Shape.CIRCLE, null, Color.green, null, "TC"+nTagID);
    } else {
        removeShape("TC"+nTagID);
    }
    
    return new Array(nC, nMA1_0.toFixed(4), nMA2_0.toFixed(4), xTC.getValue(0));
}

// 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;
}