i have ROC value (column) in my watch list of stocks. how do i color the number (value-roc) green above 0 and red below 0.
Announcement
Collapse
No announcement yet.
add color to the value of a built in study on watch list
Collapse
X
-
-
tob
At this time you can accomplish this only through efs. In essence it is no different than what you would need to do to color a plot on a chart based on some conditions. See this thread for an example. There may also be other threads related to the topic so run a search and you may find more examples.
Alex
Originally posted by tob View Posti have ROC value (column) in my watch list of stocks. how do i color the number (value-roc) green above 0 and red below 0.
-
below is the ROC efs modified by me to get colors. lime if roc>0 and red if roc<0. but doesnt give any colors on the cell background. only roc value. hope you can llok at it.
function preMain() {
setStudyTitle("Rate of Change");
setCursorLabelName("ROC", 0);
var fp1 = new FunctionParameter("nInputLength", FunctionParameter.NUMBER);
fp1.setName("Length");
fp1.setLowerLimit(1);
fp1.setDefault(10);
}
var xRoc = null;
function main(nInputLength) {
if (xRoc == null) xRoc = efsInternal("calcROC", nInputLength);
return xRoc.getValue(0);
}
function calcROC(n) {
var vNow = close(0);
var vThen = close(-n);
if(vThen == null) return;
if(xRoc>0){
setBarBgColor(color.lime,0);
}
else if(xRoc<0){
setBarBgColor(color.red,0);
}
return ((vNow - vThen) / vThen) * 100;
}
Comment
-
tob
There are a few issues with the script you posted
1. color.lime and color.red should actually be Color.lime and Color.red
2. The conditions together with the associated statements to color the background should be in the main function and not in the calcROC function
3. Since you are initializing xRoc once only when the global variable is null the xRoc series is cached after its first execution. To retrieve its values (which you are using in the conditions) you need to use the getValue() method of the Series Object ie each instance of xRoc in your conditions should be xRoc.getValue(bar_index) eg
if(xRoc.getValue(0) > 0)
...//etc
I am not sure I understand why you are implementing the more complicated solution of calculating the ROC from scratch rather than using the readily available roc() function (see this article in the EFS KnowledgeBase for information and examples of the use of the function)
Having said all the above you can accomplish the task without the need any coding if you are using 12.5. Just add the ROC study to a Watch List and use the Conditional Formatting feature to color the text or background
Alex
Comment
Comment