2009 Feb: Trading the Aussie, by Markos Katsanos

ICE Data Services -

BB_Divergence.efs  
EFSLibrary - Discussion Board  

File Name: BB_Divergence.efs

Description:
Trading The Aussie, by Markos Katsanos

Formula Parameters:

BB_Divergence.efs

  • Symbol: $XAU
  • Line Color: Green
  • Length: 30
  • Line Thickness: 2
  • Display Cursor Labels: True

BB_Divergence_System.efs

  • XAU: $XAU
  • CRB: CR A0
  • YBA: $IRX
  • EURJPY: EURJPY A0-FX
  • Length: 30

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

Download File:
BB_Divergence.efs
BB_Divergence_System.efs


EFS Code:

/*********************************
Provided By:  
    eSignal (Copyright c eSignal), a division of Interactive Data 
    Corporation. 2008. 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:        
    Trading The Aussie, by Markos Katsanos

Version:            1.0  12/05/2008

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

Formula Parameters:                     Default:
    Symbol                              $XAU
    Line Color                          Green
    Length                              30
    Line Thickness                      2
    Display Cursor Labels               True
**********************************/


var fpArray = new Array();
var bInit = false;
var bVersion = null;

function preMain() {

    setPriceStudy(false);
    setShowCursorLabel(false);
    setShowTitleParameters( false );
    
    setStudyTitle("BB Divergence");
    setCursorLabelName("BB Divergence", 0);

    setDefaultBarFgColor(Color.red, 0);
    setPlotType(PLOTTYPE_LINE, 0); 
    setDefaultBarThickness(2, 0);

    addBand(0, PS_SOLID, 1, Color.black);
   
    askForInput();
    var x=0;
    fpArray[x] = new FunctionParameter("LineColor", FunctionParameter.COLOR);
    with(fpArray[x++]){
        setName("Line Color");
        setDefault(Color.green);
    }    

    fpArray[x] = new FunctionParameter("ViewValue", FunctionParameter.BOOLEAN);
    with(fpArray[x++]){
        setName("Display Cursor Labels");
        setDefault(true);
    }    
    
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);		
        setUpperLimit(200);		        
        setDefault(30);
    }

    fpArray[x] = new FunctionParameter("Thickness", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Line Thickness");
        setLowerLimit(1);		
        setDefault(2);
    }
    
    fpArray[x] = new FunctionParameter("InSymbol", FunctionParameter.STRING);
    with(fpArray[x++]){
        setName("Symbol");
        setDefault("$XAU");
    }    
}

var xSEC2 = null; 
var xMovSeg2 = null;
var xStdevSeg2 = null;
var xMA = null;
var xStdev = null;

function main(InSymbol, Length, Thickness, LineColor, ViewValue) {
var nsec1BOL = 0;
var nsec2BOL = 0;
var nDIVERG = 0;

    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;   

    if ( bInit == false ) { 
        setDefaultBarFgColor(LineColor, 0);
        setDefaultBarThickness(Thickness, 0);
        setShowCursorLabel(ViewValue);       
        setStudyTitle("BB Divergence ("+InSymbol+")");
        xSEC2 = close(sym(InSymbol+","+getInterval()));  
        xMovSeg2 = sma(Length, xSEC2);
        xStdevSeg2 = efsInternal("StdDev", Length, xSEC2);
        xMA = sma(Length);
        xStdev = efsInternal("StdDev", Length, close());
        bInit = true; 
    } 

    if (getCurrentBarCount() < Length) return;

    nsec1BOL = 1 + ((close(0) - xMA.getValue(0) + 2 * xStdev.getValue(0)) / (4 * xStdev.getValue(0) + 0.0001)); 
    nsec2BOL = 1 + ((xSEC2.getValue(0) - xMovSeg2.getValue(0) + 2 * xStdevSeg2.getValue(0)) / (4 * xStdevSeg2.getValue(0) + 0.0001));

    nDIVERG = (nsec2BOL - nsec1BOL) / nsec1BOL * 100;
    
    if (nDIVERG != null)
        return nDIVERG; 
}

function StdDev(Period, DataArray) {
var sum = 0;
var avg = 0;
var res = 0;
    if (getCurrentBarCount() < Period) return;
    for (var barsBack = Period-1; barsBack >= 0; barsBack--) {
        sum += DataArray.getValue(-barsBack);
    }
    avg = sum / Period;
    sum = 0;
    for (var barsBack = Period - 1; barsBack >= 0; barsBack--) {
        sum += (DataArray.getValue(-barsBack) - avg) * (DataArray.getValue(-barsBack) - avg);
    }
    res = Math.sqrt(sum / Period);
    return res; 
}

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;
}
/*********************************
Provided By:  
    eSignal (Copyright c eSignal), a division of Interactive Data 
    Corporation. 2008. 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:        
    Trading The Aussie, by Markos Katsanos

Version:            1.0  12/11/2008

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

Formula Parameters:                     Default:
    XAU                                   $XAU
    CRB                                   CR A0  
    YBA                                   $IRX
    EURJPY                                EURJPY a0-fx
    Length                                30
**********************************/

var fpArray = new Array();
var bInit = false;
var bVersion = null;
var bBail = false;

function preMain() {
    setPriceStudy(true);
    setShowCursorLabel(false);
    setShowTitleParameters( false );
    setStudyTitle("BB Divergence");
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.grey);
   
    askForInput();
    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);		
        setUpperLimit(200);		        
        setDefault(30);
    }
    fpArray[x] = new FunctionParameter("XAU", FunctionParameter.STRING);
    with(fpArray[x++]){
        setName("XAU");
        setDefault("$XAU");
    }    
    fpArray[x] = new FunctionParameter("CRB", FunctionParameter.STRING);
    with(fpArray[x++]){
        setName("CRB");
        setDefault("CR A0");
    }    
    fpArray[x] = new FunctionParameter("YBA", FunctionParameter.STRING);
    with(fpArray[x++]){
        setName("YBA");
        setDefault("$IRX");
    }    
    fpArray[x] = new FunctionParameter("EURJPY", FunctionParameter.STRING);
    with(fpArray[x++]){
        setName("EURJPY");
        setDefault("EURJPY a0-fx");
    }    
}

var xCRB = null; 
var xYBA = null; 
var xXAU = null;
var xEURJPY = null;
var xMovEURJPY = null;
var xROCEURJPY = null;
var xMovXAU = null;
var xStdevXAU = null;
var xROCXAU = null;
var xMovCRB = null;
var xStdevCRB = null;
var xROCCRB = null;
var xMovYBA = null;
var xMA = null;
var xMA15 = null;
var xMA50 = null;
var xStdev = null;
var aDIV1 = new Array(4);
var nDIV1 = 0;
var xROC = null;
var xLLV = null;
var xHHV = null;
var xMACD = null;
var xMACD_MA = null;
var xMACDLL5 = null;
var xMACDLL50 = null;
var xMACDHH5 = null;
var xMACDHH50 = null;

function main(XAU, CRB, YBA, EURJPY, Length) {
var nState = getBarState();
var nsec1BOL = 0;
var nsec2BOL = 0;
var nsec3BOL = 0;
var nDIV2 = 0;
var nDIV3 = 0;

    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;   
    if ( bBail == true ) { 
        return; 
    } 

    if (isIntraday()) {
        setStudyTitle("Sorry. This script doesn't work on intraday charts.");
        bBail = true;
        return;
    }

    if ( bInit == false ) { 
        setStudyTitle("BB Divergence ("+XAU+", "+CRB+", "+YBA+" , "+EURJPY+")");
        xCRB = close(sym(CRB));  
        xYBA = close(sym(YBA));          
        xXAU = close(sym(XAU));          
        xEURJPY = close(sym(EURJPY));          
        xMovEURJPY = ema(40, xEURJPY);
        xROCEURJPY = roc(2, xEURJPY);
        xMA = sma(Length);
        xMA15 = sma(15);
        xMA50 = sma(50);        
        xStdev = efsInternal("StdDev", Length, close());
        xMovXAU = sma(Length, xXAU);
        xROCXAU = roc(1, xXAU);
        xStdevXAU = efsInternal("StdDev", Length, xXAU);
        xMovCRB = sma(Length, xCRB);
        xROCCRB = roc(1, xCRB);
        xStdevCRB = efsInternal("StdDev", Length, xCRB);
        xMovYBA = sma(40, xYBA);
        xROC = roc(2);
        xLLV = lowerDonchian(4);
        xHHV = upperDonchian(4);
        xMACD = macd( 12, 26, 9 ); 
        xMACD_MA = ema(9, xMACD);
        xMACDLL5 = lowerDonchian(5, xMACD);
        xMACDLL50 = lowerDonchian(50, xMACD);
        xMACDHH5 = upperDonchian(5, xMACD);
        xMACDHH50 = upperDonchian(50, xMACD);
        bInit = true; 
    } 

    if (getCurrentBarCount() < Length) return;
    if (getCurrentBarIndex() == 0) return;
    
    if (nState == BARSTATE_NEWBAR) {
        aDIV1.pop();
        aDIV1.unshift(nDIV1);
    }

    nsec1BOL = 1 + ((close(0) - xMA.getValue(0) + 2 * xStdev.getValue(0)) / (4 * xStdev.getValue(0) + 0.0001)); 
    nsec2BOL = 1 + ((xXAU.getValue(0) - xMovXAU.getValue(0) + 2 * xStdevXAU.getValue(0)) / (4 * xStdevXAU.getValue(0) + 0.0001));
    nsec3BOL = 1 + ((xCRB.getValue(0) - xMovCRB.getValue(0) + 2 * xStdevCRB.getValue(0)) / (4 * xStdevCRB.getValue(0) + 0.0001));
    nDIV2 = (nsec2BOL - nsec1BOL) / nsec1BOL * 100;
    nDIV3 = (nsec3BOL - nsec1BOL) / nsec1BOL * 100;
    nDIV1 = Math.max(nDIV2, nDIV3);
    aDIV1[0] = nDIV1;
    
    //Buy
    if  (((HHV(3, aDIV1) > 10 && 
            nDIV1 < aDIV1[1] &&
            xROC.getValue(0) > 0 && 
            close(0) > (1 + 0.7 / 100) * xLLV.getValue(0) &&
            xMovYBA.getValue(0) > xMovYBA.getValue(-1)
        ) || (
            nDIV1 > 40 && 
            nDIV2 + nDIV3 > 80 && 
            nDIV1 < aDIV1[1] &&
            close(0) > close(-1) && 
            close(0) > (1 +0.7 / 100) * xLLV.getValue(0)
        ) || (
            CROSSABOVE(xMA15, xMA50) && 
            xMovYBA.getValue(0) > xMovYBA.getValue(-1)
        )) && !Strategy.isLong() && !Strategy.isShort()) {
   		Strategy.doLong("Entry Long", Strategy.CLOSE , Strategy.THISBAR, Strategy.getDefaultLotSize());
   		drawShape(Shape.UPARROW, BelowBar1, Color.lime);
   		setPriceBarColor(Color.lime);
    }

    //Sell short
    if  (((LLV(3, aDIV1) < -10 &&
            nDIV1 > aDIV1[1] &&
            xROC.getValue(0) < 0 &&
            close(0) < (1 - 0.7 / 100) * xHHV.getValue(0) &&
            xMovYBA.getValue(0) < xMovYBA.getValue(-1)
        ) || (
            nDIV1 < -20 &&
            nDIV2 + nDIV3 < -40 &&
            nDIV1 > aDIV1[1] && 
            close(0) < close(-1) && 
            close(0) < (1 - 0.7 / 100) * xHHV.getValue(0)
        ) || (
            CROSSBELOW(xMA50, xMA15) &&
            xMovYBA.getValue(0) < xMovYBA.getValue(-1)
        )) && !Strategy.isShort() && !Strategy.isLong()) {
        Strategy.doShort("Entry Short", Strategy.CLOSE , Strategy.THISBAR, Strategy.getDefaultLotSize());
   		drawShape(Shape.DOWNARROW, AboveBar1, Color.red);
   		setPriceBarColor(Color.red);        
    }

    //Sell
    var nnDIV1 = Math.min(nDIV2, nDIV3);
    if  (((CROSSABOVE(xMACD_MA, xMACD) &&
            xMACDHH5.getValue(0) > xMACDHH50.getValue(0)
            ) || (
            nnDIV1 < -30 && 
            (
                xROCXAU.getValue(0) < - 0.5 ||
                xROCCRB.getValue(0) < - 0.5
            ) &&
            close(0) < (1 - 0.7 / 100) * xHHV.getValue(0)
            ) || ( 
                nnDIV1 < 0 && 
                xROCEURJPY.getValue(0) < -1 && 
                close(0) < (1 - 0.7 / 100) * xHHV.getValue(0) &&
                xMovEURJPY.getValue(0) < xMovEURJPY.getValue(-1)
            )
        ) && Strategy.isLong())  {
        Strategy.doSell("Exit Long", Strategy.CLOSE , Strategy.THISBAR, Strategy.getDefaultLotSize());
        drawText("X", AboveBar1, Color.magenta, Text.BOLD|Text.CENTER|Text.FRAME);
   		setPriceBarColor(Color.magenta);
    }   

    //Buy to cover
    if  (((CROSSBELOW(xMACD, xMACD_MA) &&
        xMACDLL5.getValue(0) < xMACDLL50.getValue(-5)
        ) || (
            nDIV1 > 30 &&
            (
                xROCXAU.getValue(0) > 0.5 ||
                xROCCRB.getValue(0) > 0.5
            ) && 
            close(0) > (1 + 0.7 / 100) * xHHV.getValue(0)
            )
        ) && Strategy.isShort())  {
        Strategy.doCover("Exit Short", Strategy.CLOSE , Strategy.THISBAR, Strategy.getDefaultLotSize()); 
        drawText("X", BelowBar1, Color.magenta, Text.BOLD|Text.CENTER|Text.FRAME);
   		setPriceBarColor(Color.magenta);
    }
    return; 
}

function StdDev(Period, DataArray) {
var sum = 0;
var avg = 0;
var res = 0;
    if (getCurrentBarCount() < Period) return;
    for (var barsBack = Period-1; barsBack >= 0; barsBack--) {
        sum += DataArray.getValue(-barsBack);
    }
    avg = sum / Period;
    sum = 0;
    for (var barsBack = Period - 1; barsBack >= 0; barsBack--) {
        sum += (DataArray.getValue(-barsBack) - avg) * (DataArray.getValue(-barsBack) - avg);
    }
    res = Math.sqrt(sum / Period);
    return res; 
}

function HHV(Period, aDataArray) {
var nRes = 0;
    nRes = aDataArray[0];
    for (var i = 0; i <= Period; i++) {
        if (nRes < aDataArray[i]) nRes = aDataArray[i];
    }
    return nRes;
}

function LLV(Period, aDataArray){
var nRes = 0;
    nRes = aDataArray[0];
    for (var i = 0; i <= Period; i++) {
        if (nRes > aDataArray[i]) nRes = aDataArray[i];
    }
    return nRes;
}

function CROSSABOVE(Series1, Series2) {
var bRes = false;
    if (Series1.getValue(0) < Series2.getValue(0) && Series1.getValue(-1) > Series2.getValue(-1)) bRes = true;    
    return bRes;
}

function CROSSBELOW(Series1, Series2) {
var bRes = false;
    if (Series1.getValue(0) > Series2.getValue(0) && Series1.getValue(-1) < Series2.getValue(-1)) bRes = true;
    return bRes;
}


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