I have an efs which computes the Hull Moving Average. Now i want to take the moving average as the input and plot the bollinger bands for the Moving average. I modified the HullMA efs and added a bit of my code. But the result is not as intended, please help me. The code in green is the addition done by me.
//-------------------------------------
var fpArray = new Array();
function preMain() {
setPriceStudy(true);
setStudyTitle("HullMABBs");
setCursorLabelName("HMABBs", 0);
//setDefaultBarStyle(PS_SOLID, 0);
//setDefaultBarFgColor(Color.blue, 0);
//setPlotType(PLOTTYPE_LINE, 0);
//setDefaultBarThickness(1, 0);
var x=0;
fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
with(fpArray[x++]){
setLowerLimit(1);
setDefault(10);
}
fpArray[x] = new FunctionParameter("Source", FunctionParameter.STRING);
with(fpArray[x++]){
addOption("open");
addOption("high");
addOption("low");
addOption("close");
addOption("hl2");
addOption("hlc3");
addOption("ohlc4");
setDefault("close");
}
fpArray[x] = new FunctionParameter("Symbol", FunctionParameter.STRING);
with(fpArray[x++]){
setDefault();
}
fpArray[x] = new FunctionParameter("Interval", FunctionParameter.STRING);
with(fpArray[x++]){
setDefault();
}
fpArray[x] = new FunctionParameter("Offset", FunctionParameter.NUMBER);
with(fpArray[x++]){
setDefault(0);
}
fpArray[x] = new FunctionParameter("Params", FunctionParameter.BOOLEAN);
with(fpArray[x++]){
setName("Show Parameters");
setDefault(false);
}
fpArray[x] = new FunctionParameter("K", FunctionParameter.NUMBER);
with(fpArray[x++]){
setLowerLimit(1);
setDefault(2);
}
}
var bInit = false;
var vSymbol = null;
var vSource = null;
var xHMA = null;
function main(Length,Source,Symbol,Interval,Offset,Params,K ) {
var avg = 0;
var sigma = 0;
var uplevel=0;
var bottomlevel=0;
if(getCurrentBarCount()<Length*2) return;
if(bInit == false){
if(Symbol == null) Symbol = getSymbol();
if(Interval == null) Interval = getInterval();
vSymbol = Symbol+","+Interval;
vSource = eval(Source);
xHMA = getSeries(wma(Math.round(Math.sqrt(Length)),efsInt ernal("calcHMA",Length,Offset,vSource(sym(vSymbol) ))));
setShowTitleParameters(eval(Params));
bInit = true;
avg = 0;
sigma = 0;
for(var i=0 ; i < Length; i++){
avg = avg+xHMA.getValue(0-i);
}
avg = avg/Length;
for(var i=0;i<Length ; i++){
sigma = sigma+(avg-xHMA.getValue(0-i))^2;
}
sigma = Math.sqrt(sigma/Length);
uplevel = xHMA.getValue(0)+K*sigma;
bottomlevel = xHMA.getValue(0)-K*sigma;
}
return new Array(uplevel, xHMA, bottomlevel);
}
var xAvg1 = null;
var xAvg2 = null;
function calcHMA(length,offset,source){
if(xAvg1==null) xAvg1 = offsetSeries(wma(length/2,source),offset);
if(xAvg2==null) xAvg2 = offsetSeries(wma(length,source),offset);
var Avg1 = xAvg1.getValue(0);
var Avg2 = xAvg2.getValue(0);
if(Avg1 == null || Avg2 == null) return;
return (2*Avg1)-Avg2;
}
//------------------------------------
//-------------------------------------
var fpArray = new Array();
function preMain() {
setPriceStudy(true);
setStudyTitle("HullMABBs");
setCursorLabelName("HMABBs", 0);
//setDefaultBarStyle(PS_SOLID, 0);
//setDefaultBarFgColor(Color.blue, 0);
//setPlotType(PLOTTYPE_LINE, 0);
//setDefaultBarThickness(1, 0);
var x=0;
fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
with(fpArray[x++]){
setLowerLimit(1);
setDefault(10);
}
fpArray[x] = new FunctionParameter("Source", FunctionParameter.STRING);
with(fpArray[x++]){
addOption("open");
addOption("high");
addOption("low");
addOption("close");
addOption("hl2");
addOption("hlc3");
addOption("ohlc4");
setDefault("close");
}
fpArray[x] = new FunctionParameter("Symbol", FunctionParameter.STRING);
with(fpArray[x++]){
setDefault();
}
fpArray[x] = new FunctionParameter("Interval", FunctionParameter.STRING);
with(fpArray[x++]){
setDefault();
}
fpArray[x] = new FunctionParameter("Offset", FunctionParameter.NUMBER);
with(fpArray[x++]){
setDefault(0);
}
fpArray[x] = new FunctionParameter("Params", FunctionParameter.BOOLEAN);
with(fpArray[x++]){
setName("Show Parameters");
setDefault(false);
}
fpArray[x] = new FunctionParameter("K", FunctionParameter.NUMBER);
with(fpArray[x++]){
setLowerLimit(1);
setDefault(2);
}
}
var bInit = false;
var vSymbol = null;
var vSource = null;
var xHMA = null;
function main(Length,Source,Symbol,Interval,Offset,Params,K ) {
var avg = 0;
var sigma = 0;
var uplevel=0;
var bottomlevel=0;
if(getCurrentBarCount()<Length*2) return;
if(bInit == false){
if(Symbol == null) Symbol = getSymbol();
if(Interval == null) Interval = getInterval();
vSymbol = Symbol+","+Interval;
vSource = eval(Source);
xHMA = getSeries(wma(Math.round(Math.sqrt(Length)),efsInt ernal("calcHMA",Length,Offset,vSource(sym(vSymbol) ))));
setShowTitleParameters(eval(Params));
bInit = true;
avg = 0;
sigma = 0;
for(var i=0 ; i < Length; i++){
avg = avg+xHMA.getValue(0-i);
}
avg = avg/Length;
for(var i=0;i<Length ; i++){
sigma = sigma+(avg-xHMA.getValue(0-i))^2;
}
sigma = Math.sqrt(sigma/Length);
uplevel = xHMA.getValue(0)+K*sigma;
bottomlevel = xHMA.getValue(0)-K*sigma;
}
return new Array(uplevel, xHMA, bottomlevel);
}
var xAvg1 = null;
var xAvg2 = null;
function calcHMA(length,offset,source){
if(xAvg1==null) xAvg1 = offsetSeries(wma(length/2,source),offset);
if(xAvg2==null) xAvg2 = offsetSeries(wma(length,source),offset);
var Avg1 = xAvg1.getValue(0);
var Avg2 = xAvg2.getValue(0);
if(Avg1 == null || Avg2 == null) return;
return (2*Avg1)-Avg2;
}
//------------------------------------
Comment