While using the script I found that if the timeArr or selected time0,time1,etc menu options aren't in chronological order it will plot incorrectly so I added a sort function to automatically sort the array from lowest time value to highest keeping the corresponding colors and line thicknesses.
Wayne
PHP Code:
var arrLen = null;
function preMain() {
var aFPArray = new Array();
setPriceStudy(true);
setStudyTitle("Vertical.q3 Time Plots");
var timeArr = [800,930,935,940,1030,1115,1600];
var lineClr = [Color.grey,Color.white,Color.aqua,Color.aqua,Color.darkgrey,Color.darkgrey,Color.navy];
var lineThick = [1,1,1,1,1,1,1];
arrLen = timeArr.length;
var x=0, y=0, i=0, j=0;
aFPArray[x] = new FunctionParameter("NumberOfLinesToDraw", FunctionParameter.NUMBER);
with(aFPArray[x++]){
setName("Draw The 1st ? Lines");//i.e., if NumberOfLinesToDraw==1 one line is drawn every time the 1st "time0" bar time is reached, if it==2 then two lines are drawn for "time0" and "time1", etc., NOTE: times and corresponding color/thickness will be automatically sorted in chronological order
setLowerLimit(1);
setUpperLimit(arrLen);
for (j = 0; j < timeArr.length; j++) {
addOption(j + 1);
}
setDefault(timeArr.length);
}
for(i = 0; i < timeArr.length; i++){
y="time"+i;
aFPArray[x] = new FunctionParameter(y, FunctionParameter.NUMBER);
with(aFPArray[x++]){
setLowerLimit(1);
setDefault(timeArr[i]);
}
y="lineClr"+i;
aFPArray[x] = new FunctionParameter(y, FunctionParameter.COLOR);
with(aFPArray[x++]){
setDefault(lineClr[i]);
}
y="lineThick"+i;
aFPArray[x] = new FunctionParameter(y, FunctionParameter.NUMBER);
with(aFPArray[x++]){
setLowerLimit(1);
for (j = 0; j < timeArr.length; j++) {
addOption(j+1);
}
setDefault(lineThick[i]);
}
}
aFPArray[x] = new FunctionParameter("TodayOnly", FunctionParameter.BOOLEAN);
with(aFPArray[x++]){
setDefault(false);
}
aFPArray[x] = new FunctionParameter("enableSubStudy", FunctionParameter.BOOLEAN);
with(aFPArray[x++]){
setDefault(false);
}
aFPArray[x] = new FunctionParameter("displayParam", FunctionParameter.BOOLEAN);
with(aFPArray[x++]){
setDefault(false);
}
}
var bInit=false;
var paramArr=[];
function main(NumberOfLinesToDraw,
time0,time1,time2,time3,time4,time5,time6,
lineClr0,lineClr1,lineClr2,lineClr3,lineClr4,lineClr5,lineClr6,
lineThick0,lineThick1,lineThick2,lineThick3,lineThick4,lineThick5,lineThick6,
TodayOnly,enableSubStudy,displayParam) {
if(isDWM()) return;
var drawIt = true;
if(TodayOnly && !is_Today()) drawIt = false;
if(!bInit){
setShowTitleParameters( displayParam );
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
var args = Array.prototype.slice.call(arguments);
var tempArr = [];
var i = 0, temp=null;
for(i = 0; i < 3; i += 1){
tempArr.push([]);
}
for(i = 0; i < arrLen; i += 1){
if(args[i+1] != null) tempArr[0][i] = args[i+1];
if(args[i+arrLen+1] != null) tempArr[1][i] = args[i+arrLen+1];
if(args[i+2*arrLen+1] != null) tempArr[2][i] = args[i+2*arrLen+1];
}
paramArr=sort2D_Array(tempArr)[0];
bInit=true;
}
if(drawIt) DrawTrigger(paramArr, NumberOfLinesToDraw);
return null;
}
var flag0 = 0, flag1 = 0, timeCtr=0;
function DrawTrigger(paramArr, NumberOfLinesToDraw){
var i = 0;
var barTm = (hour(0)*100)+minute(0);
if(flag1 == 1 && barTm > paramArr[0][timeCtr+1]) flag1 = 0;
if(flag0 == 0 && barTm >= paramArr[0][paramArr[0].length -1]){
if(NumberOfLinesToDraw == paramArr[0].length && getCurrentBarCount()>1) onAction0(paramArr[1][paramArr[1].length-1],paramArr[2][paramArr[2].length-1]);
flag0 = 1;
flag1 = 0;
}
if(flag1 == 0){
for(i = 0; i < paramArr[0].length; i += 1){
if((barTm >= paramArr[0][i] && barTm <paramArr[0][i+1])){
if(i <= NumberOfLinesToDraw-1) onAction0(paramArr[1][i],paramArr[2][i]);
flag0 = 0;
flag1 = 1;
timeCtr = i;
}
}
}
}
function onAction0(clr, thick) {
drawLineRelative(0, -999999999, 0, 999999999, PS_SOLID, thick, clr, "line"+rawtime(0));
}
function is_Today(){//"bCurrentDay" only works while the chart receives ticks so it's always false when Mkt is closed
var bCurrentDay = false;
var xTime = getValue("time");
var xDate = new Date();
var sTime = (xTime.getMonth()+1+"/"+xTime.getDate()+"/"+xTime.getFullYear());
var sToday = (xDate.getMonth()+1+"/"+xDate.getDate()+"/"+xDate.getFullYear());
if ( sTime == sToday ) {
bCurrentDay = true;
}
return bCurrentDay;
}
function sort2D_Array(arr){
var arrT = [];
for(i = 0; i < 3; i += 1){
arrT.push([]);
}
var idxCtr=0;
while (arr[0].length > 0){
var arrLen=arr[0].length;
var index = null;
var value = arr[0].min();
for (var j = 0; j < arrLen; j+=1) {
if (arr[0][j] == value) {
if(arr[0][j]!=null){
arrT[0][idxCtr]=arr[0][j];
arrT[1][idxCtr]=arr[1][j];
arrT[2][idxCtr]=arr[2][j];
idxCtr+=1;
}
removeElement(arr, j)
}
}
}
return [arrT,arr];
}
function removeElement(array, index){
if (index > -1) {
array[0].splice(index, 1);//removes the element at specified index
array[1].splice(index, 1);//removes the element at specified index
array[2].splice(index, 1);//removes the element at specified index
return true;
}
return false;
}
Comment