So in general it's more effiecient to return the values unless using a different interval than what is on the chart.
Now back to series questions.
This is a different script than previous.
I'm building a series xVT (using efsInternal) but getting some errors/problems.
1) I want to paint the bars different colors based on there value. and I'm using a global var sType which is being set in the UDF but is null when I try to access in main().
2) I tried painting the bars inside the UDF called by efsInternal but that didn't work either.
3) Also, sma(nLen, xVT) is producing NaN for values.
Would appreciate any guidance on these three issues.
Now back to series questions.
This is a different script than previous.
I'm building a series xVT (using efsInternal) but getting some errors/problems.
1) I want to paint the bars different colors based on there value. and I'm using a global var sType which is being set in the UDF but is null when I try to access in main().
2) I tried painting the bars inside the UDF called by efsInternal but that didn't work either.
3) Also, sma(nLen, xVT) is producing NaN for values.
Would appreciate any guidance on these three issues.
PHP Code:
/*************************************************
PJ's trend indicator
*************************************************/
debugClear();
function preMain() {
setPriceStudy(false);
setStudyTitle("Trendy2");
setCursorLabelName("Trendy", 0);
setDefaultBarFgColor(Color.blue, 0);
setPlotType(PLOTTYPE_HISTOGRAM,0);
setDefaultBarThickness(6,0);
setCursorLabelName("MA", 1);
setDefaultBarFgColor(Color.blue, 1);
addBand(0, PS_SOLID, 1, Color.RGB(128,128,128), "zl");
// setComputeOnClose();
}
var H,H1,L,L1,BL,BH,vSZ,vSum,ratio,prcnt,vG,vB,vUS,vLS;
var vT = null;
var vT1 = null;
var sType = null;
var rnd=100;
bInit=false;
var nLen=5;
function main() {
if (bInit == false) {
xO=open();
xH=high();
xL=low();
xC=close();
xVT = efsInternal("fGetVT",xO,xH,xL,xC);
xMA = sma(nLen, xVT);
bInit=true;
}
nVT = xVT.getValue(0);
nMA = xMA.getValue(0);
//debugPrint(sType+" "); // not working sType is null
//debugPrintln(nVT+" "+nMA); // nMA returning NaN
/*
if (sType=="inside" || sType=="outside") {
setBarFgColor(Color.grey, 0);
} else {
if (close(0)<open(0)) {
setBarFgColor(Color.red, 0);
}else {
setBarFgColor(Color.green, 0);
}
}
if (Math.abs(nVT)>.85 ) {
setBarBgColor(Color.yellow, 0);
}else{
setBarBgColor(Color.white, 0);
}
*/
return new Array(getSeries(xVT), getSeries(xMA));
}
function fGetVT(_xO, _xH, _xL, _xC) {
if ( _xO==null || _xH==null || _xL==null || _xC==null ) return;
vT1 = vT; // used only for inside bars
vG=0; // Gap size
vB=0; // Body size
vUS=0; // Upper shadow
vLS=0; // Lower shadow
H = Math.round(_xH.getValue(0)*rnd)/rnd;
H1 = Math.round(_xH.getValue(-1)*rnd)/rnd;
L = Math.round(_xL.getValue(0)*rnd)/rnd;
L1 = Math.round(_xL.getValue(-1)*rnd)/rnd;
BL=Math.min( _xC.getValue(0), _xO.getValue(0) );
BL = Math.round(BL*rnd)/rnd;
BH=Math.max( _xC.getValue(0), _xO.getValue(0) );
BH = Math.round(BH*rnd)/rnd;
if (H<=H1) {
if (L>=L1){
_sT = "inside";
} else { // lower hi and lower lo
_sT = "dntrend";
}
} else if (H>H1) {
if (L>=L1){ // higher hi & higher lo
_sT = "uptrend";
} else { // H>H1 & L<L1
_sT = "outside";
}
}
sType = _sT;
vSZ=(H-L);
ratio=vSZ/(H1-L1); // computes the ratio of currnet bar vs previous
switch (sType) {
case "inside" :
vT=vT1;
break;
case "uptrend" :
//ut GAP
if (L>H1) {
vG=L-H1;
} else {
vG=0;
}
//ut LOWER SHADOW
if (L<BL) {
// a gap up with a lower shadow (ie Hanging Man) could be a reversal sign
// but for Trendy purposes it's a higher hi so count everything above prev H
if (L>=H1) { // the whole bar is above prev H
vLS=BL-L;
} else { // only count the part > prev H
vLS=BL-H1;
}
}
//ut the BODY
if (BL>H1) {
vB=BH-BL;
} else { // only count the body > H1
vB=BH-H1;
}
//ut UPPER SHADOW
if (H>BH) {
// upper shadows are higher hi but could be a Shooting Star, Hanging Man or Doji, So,
// if trend is ending, for shadow > H1 only add 1/2 value and < H1 subtract 1/2 value
if (BH>=H1) {
vUS=(H-BH)/2; // bottom of shadow > prev H use +1/2 * shadow
} else {
vUS=(BH-H)/2; // bottom of shadow < prev H use -1/2 * shadow
}
}
// Compute Trendy for uptrend
vSum = (vG+vB+vUS+vLS);
prcnt = vSum/vSZ;
vT = ratio * prcnt;
break;
case "dntrend" :
// values should be negative
//dt GAP
if (H<L1) {
vG=H-L1;
} else {
vG=0;
}
//dt UPPER SHADOW
if (H>BH) {
// the upper shadow is "within" the downtrend, count everything < prev L
if (H<=L1) { // the whole bar is < prev L
vUS=BH-H;
} else { // only count the part < prev L
if (BH<L1) {
vUS=BH-L1;
} else {
vUS=0;
}
}
} else {
vUS=0;
}
//dt the BODY
if (BH<L1) { // get body size below prev L
vB=BL-BH;
} else { // only count the body < L1
if (BL<L1) {
vB=BL-L1;
} else {
vB=0; // none of the body is < prev L
}
}
//dt LOWER SHADOW
if (L<BL) {
// could be a Doji or a Hammer, if lower shadow below prev bar add -1/2 value
// and if within the the prev BAR add +1/2 value (to reduce negative amt)
if (BL<=L1) {
vLS=(L-BL)/2; // top of shadow < prev L add -1/2 * shadow
} else { // sType requires L<L1
vLS=(L1-L)/2; // top of shadow > prev L add +1/2 * shadow (part below)
}
} else{
vLS=0;
}
// Compute Trendy for downtrend
vSum = (vG+vB+vUS+vLS);
prcnt = vSum/vSZ;
vT = ratio * prcnt;
break;
case "outside" :
// The outside bar may be an engulfing pattern so set Trendy to match the direction
vT=0;
// should an outside bar be part of the trend?
break;
default :
// should never be here
debugPrintln("Trendy has bad data");
}
if (sType=="inside" || sType=="outside") {
setBarFgColor(Color.grey, 0);
} else {
if (_xC.getValue(0) < _xO.getValue(0)) {
setBarFgColor(Color.red, 0);
}else {
setBarFgColor(Color.green, 0);
}
}
if (Math.abs(vT)>.85 ) {
setBarBgColor(Color.yellow, 0);
}else{
setBarBgColor(Color.white, 0);
}
return (vT);
}
Comment