I have expanded on my previous code to include a trend line that changes color depending on a few variables. I am tracking the high and low of the day with the global variables xHigh and xLow that reset each day on the first bar of the day. When the trend line moves up it turns green and if it ticks above the xHigh it turns blue. When it ticks down it turns purple and when it ticks below the xLow it turns red. I also put a warning level in to indicate if there has been a deep retracement without a new tick up or down.
The color changes work perfectly when I replay data however in real time them occasionally fail. Sometimes price will be ticking up above xHigh and the line will be blue then it will suddenly start to be green as price continues to tick above xHigh, then it will turn blue again. If I reload the indicator it will be all blue as it should be. I must have missed some subtle piece of code that would keep the color correct.
I have heavily commented the code.
Murray
The color changes work perfectly when I replay data however in real time them occasionally fail. Sometimes price will be ticking up above xHigh and the line will be blue then it will suddenly start to be green as price continues to tick above xHigh, then it will turn blue again. If I reload the indicator it will be all blue as it should be. I must have missed some subtle piece of code that would keep the color correct.
I have heavily commented the code.
Murray
PHP Code:
/*****************************************************************
Trend Band
Murray Mortlock
Dec 2015
******************************************************************/
var nTrendLine = null;
var nTrendLineLast = null;
var aRange = new Array();
var cTrendLineColor = null;
var cTrendLineColorLast = null;
var xHigh;
var xLow;
var nFlatCount = 0;
function preMain() {
setPriceStudy(true);
setStudyTitle("Trend Band");
setShowTitleParameters(false);
setPlotType(PLOTTYPE_INSTANTCOLORLINE, 0);
setCursorLabelName("TrendLine", 0);
setDefaultBarStyle(PS_SOLID,0);
setDefaultBarThickness(5,0);
var colShadowColor = new FunctionParameter("ShadowColor", FunctionParameter.COLOR);
colShadowColor.setName("Shadow Color")
colShadowColor.setDefault(Color.RGB(113,113,113));
var colBGColor = new FunctionParameter("NewHighColor", FunctionParameter.COLOR);
colBGColor.setName("New High")
colBGColor.setDefault(Color.RGB(0,0,255));
var colBGColor = new FunctionParameter("UptrendColor", FunctionParameter.COLOR);
colBGColor.setName("Uptrend")
colBGColor.setDefault(Color.RGB(0,255,0));
var colBGColor = new FunctionParameter("UptrendWarningColor", FunctionParameter.COLOR);
colBGColor.setName("Uptrend Warning")
colBGColor.setDefault(Color.RGB(255,255,0));
var colBGColor = new FunctionParameter("DowntrendWarningColor", FunctionParameter.COLOR);
colBGColor.setName("Downtrend Warning")
colBGColor.setDefault(Color.RGB(255,155,0));
var colBGColor = new FunctionParameter("DowntrendColor", FunctionParameter.COLOR);
colBGColor.setName("Downtrend")
colBGColor.setDefault(Color.RGB(255,0,255));
var colBGColor = new FunctionParameter("NewLowColor", FunctionParameter.COLOR);
colBGColor.setName("New Low")
colBGColor.setDefault(Color.RGB(255,0,0));
var x = 0;
aRange[x] = new FunctionParameter("ShadowWidth", FunctionParameter.NUMBER);
with(aRange[x++]) {
setLowerLimit(1);
setDefault(16);
}
var y = 0;
aRange[y] = new FunctionParameter("TrendTicks", FunctionParameter.NUMBER);
with(aRange[y++]) {
setLowerLimit(1);
setDefault(36);
}
var z = 0;
aRange[z] = new FunctionParameter("WarningTicks", FunctionParameter.NUMBER);
with(aRange[z++]) {
setLowerLimit(1);
setDefault(24);
}
}
function main(ShadowWidth,ShadowColor,TrendTicks,WarningTicks,NewHighColor,UptrendColor,UptrendWarningColor,DowntrendWarningColor,DowntrendColor,NewLowColor) {
// Set trend and warning levels
var nTrend = getMinTick() * TrendTicks;
var nWarning = getMinTick() * WarningTicks;
// Set trendline level for first bar
if(getCurrentBarCount()<2) {
nTrendLine=(high()+low())/2;
cTrendLineColor = Color.RGB(0,255,0);
}
// Set new bar parameters
if(getBarState()==BARSTATE_NEWBAR) {
nTrendLineLast=nTrendLine; // Capture value of trendline on last bar
cTrendLineColorLast = cTrendLineColor // Capture color of trendline on last bar
nFlatCount = nFlatCount +1; // Count number of bars without tick up or down
if (getDay() != getDay(-1)) { // Set high and low values at start of the day
xHigh = nTrendLineLast + nTrend;
xLow = nTrendLineLast - nTrend;
}
}
// Set trendline value
if (nTrendLineLast < (high()-nTrend))
nTrendLine = (high() - nTrend);
else if (nTrendLineLast > (low() + nTrend))
nTrendLine = (low() + nTrend);
// Set shadow width and color
var UpperBand = nTrendLine + ShadowWidth;
var LowerBand = nTrendLine - ShadowWidth;
setBarBgColor(ShadowColor,0,UpperBand,LowerBand);
// Fix TrendLine Level
if (nTrendLineLast < (high()-nTrend))
nTrendLine = (high() - nTrend);
else if (nTrendLineLast > (low() + nTrend))
nTrendLine = (low() + nTrend);
// Fix TrendLine Color - will be the same as it was on the previous bar if not changed by the process below
cTrendLineColor = cTrendLineColorLast; // Capture the current color in case no changes are to be made
// When trendline ticks up
if (nTrendLine > nTrendLineLast) { //If TrendLine is greater than previous bar TrendLine
nFlatCount = 0; //Set count of flat line bars to 0
if (high() > xHigh) { //if the high is higher than the high of the day
cTrendLineColor = NewHighColor; //then the color will be the new high color (blue)
xHigh = high(); // and the highest value of the day will be changed to the new high
}
else cTrendLineColor = UptrendColor; // if not the color will be the higher color (green)
}
// When trendline ticks down
else if (nTrendLine < nTrendLineLast) { //if the miband is less than the previous bar TrendLine
nFlatCount = 0; //set the count of the flat line bars to 0
if (low() < xLow) { //if the low is lower than the low of the day
cTrendLineColor = NewLowColor; //then the color will be the new low color (red)
xLow = low(); // and the lowest value of the day will be changed to the new low
}
else cTrendLineColor = DowntrendColor; // if not then the color will be the low color (purple)
}
//if the TrendLine has been the same volue for more than 8 bars
if (nFlatCount > 8) {
// if the high is greater than the nB7 value AND the color is red or purple
if (high() > (nTrendLine + nWarning) && (cTrendLineColor == NewLowColor || cTrendLineColor == DowntrendColor))
cTrendLineColor = DowntrendWarningColor; //then change the color to the warning color (orange)
// if the low is lower than the nB1 value AND the color is blue or green
else if (low() < (nTrendLine - nWarning) && (cTrendLineColor == NewHighColor || cTrendLineColor == UptrendColor))
cTrendLineColor = UptrendWarningColor; //then change the color to the warning color (yellow)
}
setBarFgColor(cTrendLineColor,0);
return nTrendLine;
}
Comment