Can anybody please help me to add a histogram to the built-in DiNapoli MACD indicator?
Thanks, Steven
Thanks, Steven
/***************************
Copyright © eSignal, a division of Interactive Data Corporation. 2003. All rights reserved.
This sample eSignal Formula Script (EFS) may be modified and saved under a new
filename; however, eSignal is no longer responsible for the functionality once modified.
eSignal reserves the right to modify and overwrite this EFS file with each new release.
******************************/
function preMain() {
setStudyTitle("DiNapoli MACD");
setDefaultBarFgColor(Color.red, 0); // MACD EMA
setDefaultBarFgColor(Color.green, 1); // Signal Line
setDefaultBarFgColor(Color.magenta, 2); // histogram
setCursorLabelName("DiNapoli MACD", 0);
setCursorLabelName("Signal Line", 1);
setCursorLabelName("Histogram", 2); // histogram
setPlotType(PLOTTYPE_HISTOGRAM ,2); // histogram
setDefaultBarThickness(3,2); // histogram
}
var ema1 = 0;
var ema2 = 0;
var ema1_prev = 0;
var ema2_prev = 0;
var macd1 = 0;
var macd1_prev = 0;
var macdema = 0;
var macdema_prev = 0;
var SLine = 0;
var sf1 = .213;
var sf2 = .108;
var sf3 = .199;
var length1 = 8;
var length2 = 18;
var length3 = 9;
var cntr = 0;
var i = 0;
function ema(nClose, nEma, nSF){
var val = 0;
if(nClose == null){
return;
}
val = (nClose - nEma) * nSF + nEma;
return val;
}
function main() {
var nState = getBarState();
cntr = cntr + 1;
var P1 = getValue("Close");
var P2 = getValue("Close", -1, 1);
if (cntr == 18) {
var sum = 0;
var price = getValue("Close", 0, -length1);
// get ema1;
if(price == null){
return;
} else {
for(i = 0; i < length1; i++) {
sum += price[i];
}
ema1 = sum / length1;
}
ema1_prev = ema1;
sum = 0;
price = getValue("Close", 0, -length2);
// get ema2;
if(price == null) {
return;
} else {
for(i = 0; i < length2; i++) {
sum += price[i];
}
ema2 = sum / length2;
}
ema2_prev = ema2;
} else if (cntr > 18) {
if (nState == BARSTATE_NEWBAR) {
ema1_prev = ema1;
ema2_prev = ema2;
macd1_prev = macd1;
macdema_prev = macdema;
}
ema1 = ema(P1, ema1_prev, sf1);
ema2 = ema(P1, ema2_prev, sf2);
macd1 = ema1 - ema2;
macdema = ema(macd1, macdema_prev, sf3);
SLine = (macd1 - macd1_prev) * sf3 + macd1_prev;
xDiff = SLine - macdema ; // histogram
addBand( 0, PS_SOLID, 1, Color.black ,"HorzLine1") ; // histogram
return new Array(macdema, SLine, xDiff); // histogram
} else {
return;
}
}
Comment