/********************************* Provided By: eSignal (Copyright c eSignal), a division of Interactive Data Corporation. 2012. 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: Low-Frequency Trading, by Ron McEwan
Version: 1.00 02/08/2013
Formula Parameters: Default: SMA Length 253 Long Position Color lime Short Position Color red
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() { setStudyTitle("OneYear_CumAdvDecMA"); setIntervalsBackfill(true); setCursorLabelName("Cum AD Line",0); setCursorLabelName("MA",1); setDefaultBarFgColor(Color.blue, 0); setDefaultBarFgColor(Color.purple, 1); var x = 0; fpArray[x] = new FunctionParameter("gLength", FunctionParameter.NUMBER); with(fpArray[x++]) { setName("SMA Length"); setLowerLimit(1); setDefault(253); }
fpArray[x] = new FunctionParameter("gBuyColor", FunctionParameter.COLOR); with(fpArray[x++]) { setName("Long Position Color"); setDefault(Color.lime); } fpArray[x] = new FunctionParameter("gSellColor", FunctionParameter.COLOR); with(fpArray[x++]) { setName("Short Position Color"); setDefault(Color.red); }
}
var bInit = false; var bVersion = null;
var xCumAd = null; var xSMA = null;
function main(gLength,gBuyColor,gSellColor) { if (bVersion == null) bVersion = verify(); if (bVersion == false) return; if (!bInit) { xCumAd=efsInternal("getCumAd"); xSMA = sma(gLength,xCumAd); }
var nCumAd = xCumAd.getValue(0); var nSMA = xSMA.getValue(0);
if (nCumAd == null || nSMA == null) return; if (getCurrentBarIndex() != 0) { var bLStrategy = Strategy.isLong(); if (!bLStrategy) { if (nCumAd>nSMA) { Strategy.doLong("Enter Long", Strategy.CLOSE, Strategy.THISBAR); drawShapeRelative(0, BottomRow3,Shape.DIAMOND,null, gBuyColor, Shape.PRESET); } } else { if (nCumAd<nSMA) { Strategy.doSell("Close Long", Strategy.CLOSE, Strategy.THISBAR); drawShapeRelative(0, TopRow3,Shape.DIAMOND,null, gSellColor, Shape.PRESET); } } } return new Array(nCumAd,nSMA); }
var bCumAd = false;
var xAdv = null; var xDecl = null;
var cumAd = 0;
function getCumAd() { if (getBarState() == BARSTATE_ALLBARS) { bCumAd = false; }
if(!bCumAd) { xAdv = close(sym("$ADV,D")); xDecl = close(sym("$DECL,D")); cumAd = 0; bCumAd = true; }
var nAdv = xAdv.getValue(0); var nDecl = xDecl.getValue(0);
if (nAdv == null || nDecl == null || (nAdv+nDecl)==0) return; var pctChg = (nAdv-nDecl)/(nAdv+nDecl)*1000; var ret = ref(-1); cumAd = ret + pctChg; return cumAd; }
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; } |