I'm trying to change the color of a study when certain conditions are met ie. an ema changes from down to up or up to down. I've attempted to follow the example for setBarFgColor. Historic bars are colored as expected but new bars do not change colors.
Following is the code as well as the attached file.
Thanks,
Rod
Following is the code as well as the attached file.
Code:
/* Study loads ema into the top position of an array at the begining of each bar. Elements 0, 1, & 2 are compared for 0 > 1 && 1 >2 or 0 < 1 or 1 < 2. When conditions are met the bar color is changed to blue for up or red for down. */ function preMain() { setPriceStudy(false); setStudyTitle("ema with color changes"); setCursorLabelName("ema with colors",0); } var nCtr = null; var nLen = null; var var1 = null; var aArray = null; function main() { if (nCtr == null) nCtr = 0; if (var1 == null) var1 = 0; if (nLen == null) nLen = 3; if (aArray == null) aArray = new Array(0, 0, 0); if (getBarState() == BARSTATE_NEWBAR) { var1 = ema(3, close()); nLen = aArray.unshift(var1); var2 = aArray.pop(); for (nCtr = 0; nCtr < aArray.length; nCtr++) { debugPrintln("[" + nCtr + "]:" + aArray[nCtr].toFixed(2) + "]"); } debugPrintln(); if ((aArray[0] > aArray[1]) && (aArray[1] > aArray[2])) { setBarFgColor(Color.blue,0); debugPrintln("Change to blue"); } else if ((aArray[0] < aArray[1]) && (aArray[1] < aArray[2])) { setBarFgColor(Color.red,0); debugPrintln("Change to red"); } } return(aArray[0]); }
Rod
Comment