I seem to be getting in a terrible fix trying to write a really simple piece of code. I am trying to calculate the sum of a number of highs higher than the previous days highs over a lookback period set by the user. For each higher high I want to calculate the sum of the differences over the same period and divide by the number of higher highs - i.e. if we were looking back say 10 days and five of those days had highs higher than their previous day then I would sum all the five higher highs and divide by five. Then taking that figure I want to obtain a 3 period moving average of the figure produced and plot it.
I wrote some code - really a beginner so please excuse the simplicity and errors, but I am not sure the code is correctly accumulating teh differences, cannot seem to find an easy way of getting a moving average of the values and cant seem to get a plot out of it either. Have spent hours going through examples but they all seem to plot some variation of an existing built in study rather than a variable set by me. Please help.
Code
/*
* Set global variable for array for lookback period and
*/
var lbArray = new Array();
function preMain() {
setStudyTitle("Sell");
setCursorLabelName("Sell",0);
setDefaultBarFgColor(Color.magenta,0);
setPlotType(PLOTTYPE_LINE,0);
setDefaultBarThickness(1,0);
var vHighs = 0;
var i=0;
var cumHigh = 0;
var numLowerHighs = 0;
askForInput ("Lookback Period");
}
/*
* This function displays a dynamic stop based on the degree of trend assessed by
the number and size of cumulative higher highs or lower lows over the previous
periods.
*/
function main() {
var sSell;
var x=0;
lbArray[x] = new FunctionParameter("Lookback", FunctionParameter.NUMBER);
with(lbArray[x++]){
setLowerLimit(3);
setUpperLimit(100);
setDefault(10);
vHighs = getValue("High", 0, x);
if(vHighs == null)
return;
for(i = 0; i < x-1; i++) {
if(vHighs[i] >= vHighs[i+1]) {
cumHigh = cumhigh + (High(i+1) - High (i));
numHigherHighs = numHigherHighs + 1;
} else return;
{sSell = sma(3,cumHigh/numHigherHighs);}
//this is obviously wrong as the sma only plots price data as far as I can see
}
return new Array (sSell);
//have no idea how to get this to plot
}
}
I wrote some code - really a beginner so please excuse the simplicity and errors, but I am not sure the code is correctly accumulating teh differences, cannot seem to find an easy way of getting a moving average of the values and cant seem to get a plot out of it either. Have spent hours going through examples but they all seem to plot some variation of an existing built in study rather than a variable set by me. Please help.
Code
/*
* Set global variable for array for lookback period and
*/
var lbArray = new Array();
function preMain() {
setStudyTitle("Sell");
setCursorLabelName("Sell",0);
setDefaultBarFgColor(Color.magenta,0);
setPlotType(PLOTTYPE_LINE,0);
setDefaultBarThickness(1,0);
var vHighs = 0;
var i=0;
var cumHigh = 0;
var numLowerHighs = 0;
askForInput ("Lookback Period");
}
/*
* This function displays a dynamic stop based on the degree of trend assessed by
the number and size of cumulative higher highs or lower lows over the previous
periods.
*/
function main() {
var sSell;
var x=0;
lbArray[x] = new FunctionParameter("Lookback", FunctionParameter.NUMBER);
with(lbArray[x++]){
setLowerLimit(3);
setUpperLimit(100);
setDefault(10);
vHighs = getValue("High", 0, x);
if(vHighs == null)
return;
for(i = 0; i < x-1; i++) {
if(vHighs[i] >= vHighs[i+1]) {
cumHigh = cumhigh + (High(i+1) - High (i));
numHigherHighs = numHigherHighs + 1;
} else return;
{sSell = sma(3,cumHigh/numHigherHighs);}
//this is obviously wrong as the sma only plots price data as far as I can see
}
return new Array (sSell);
//have no idea how to get this to plot
}
}