Hi,
I have written a study (the code is below) that basically plots two moving averages and offsets them backwards. One of the averages has envelopes as well. My issue is that I run this study on day bar charts with a time template of 260 bars. For some reason my average with the envelopes only shows for the first 12 bars and then stops plotting. I have tried a number of things and have been working on trying to solve this for about 2 days solid (I am new to efs programming and still learning) and have run into a dead end. I have no idea why the study will not work properly when there are no syntax errors. If someone could point our the errors or point me in a direction to resolving this I would greatly appreciate it.
Also, I have tried to post an error message via the return statement as commented out below but have not been able to get this to work.
var fnError = null;
if(getNumBars() < BarLength) { // check that BarLength number of bars has loaded in the chart
return fnError("Insufficient number of bars on chart.");
Thank you very much for any help you may be able to provide.
Regards,
Jane
function preMain() {
setPriceStudy(true); // study plots in price pane (main chart)
setStudyTitle("Centered MA w Bands");
setShowTitleParameters(false);
setShowCursorLabel(false); // supresses this study's label in cursor window
setCursorLabelName("MA upper", 0);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarThickness(1, 0);
setPlotType(PLOTTYPE_LINE, 0);
setShowCursorLabel(false, 0); //supresses this label in cursor window
setCursorLabelName("MA Centered", 1);
setDefaultBarFgColor(Color.red, 1);
setDefaultBarThickness(2, 1);
setPlotType(PLOTTYPE_LINE, 1);
setShowCursorLabel(false, 1); //supresses this label in cursor window
setCursorLabelName("MA lower", 2);
setDefaultBarFgColor(Color.red, 2);
setDefaultBarThickness(1, 2);
setPlotType(PLOTTYPE_LINE, 2);
setShowCursorLabel(false, 2); //supresses this label in cursor window
setCursorLabelName("MA long", 3);
setDefaultBarFgColor(Color.blue, 3);
setDefaultBarThickness(2, 3);
setPlotType(PLOTTYPE_LINE, 3);
setShowCursorLabel(false, 3); //supresses this label in cursor window
setComputeOnClose(); //force this script to only update on each new bar
var fp1 = new FunctionParameter("BarLength", FunctionParameter.NUMBER);
fp1.setName("BarLength");
fp1.setLowerLimit(1);
fp1.setDefault(260); // default number of bars
var fp2 = new FunctionParameter("MA_Length1", FunctionParameter.NUMBER);
fp2.setName("MA_Length1");
fp2.setLowerLimit(1);
fp2.setDefault(13); // default length for fast MA
var fp3 = new FunctionParameter("MA_Length2", FunctionParameter.NUMBER);
fp3.setName("MA_Length2");
fp3.setLowerLimit(1);
fp3.setDefault(27); // default length for slow MA
}
// Global Variables
var vMA1 = null;
var vMA2 = null;
var vBands = null;
var bInit = false;
function main(BarLength, MA_Length1, MA_Length2) {
var fnError = null;
if(getNumBars() < BarLength) { // check that BarLength number of bars has loaded in the chart
// return fnError("Insufficient number of bars on chart.");
drawTextPixel( 50, 50,"Insufficient number of bars on chart.", Color.red, null, Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM | Text.BOLD, "Name" ,10, -10 );
return;
}
if(!bInit) {
vBands = efsInternal("calcBands", BarLength, MA_Length1);
bInit = true;
}
var offset1 = Math.floor(MA_Length1*0.5);
var offset2 = Math.floor(MA_Length2*0.5);
if (vMA1 == null) vMA1 = sma(MA_Length1, hl2());
if (vMA2 == null) vMA2 = sma(MA_Length2, hl2());
/*
for(var j = 0; j <= dif; j++) {
var hMax = high(-j);
var lMax = low(-j);
if(hMax > highMax) highMax = hMax;
if(lMax < lowMax) lowMax = lMax;
}
*/
var MA_upper = offsetSeries(getSeries(vBands), -offset1);
var MA_center = offsetSeries(getSeries(vBands,1), -offset1);
var MA_lower = offsetSeries(getSeries(vBands,2), -offset1);
var MA_long = offsetSeries(getSeries(vMA2), -offset2);
return new Array(MA_upper, MA_center, MA_lower, MA_long);
}
// Global Variables
var vMA1 = null;
//== calcBands function calculates the value of the bands about the centered moving average
function calcBands(BarLength, MA_Length1) {
if (vMA1 == null) vMA1 = sma(MA_Length1, hl2());
var sumHi = 0;
var sumLo = 0;
var envSize = 0;
var medianPrice = 0;
var nUpper = null;
var nLower = null;
var offset1 = Math.floor(MA_Length1*0.5);
var dif = offset1-BarLength;
for(var i = dif; i < 1; i++) {
medianPrice = 0.5*(high(i)+low(i));
sumHi += Math.pow((high(i)-medianPrice), 2);
sumLo += Math.pow((medianPrice-low(i)), 2);
}
envSize = 4.5*Math.sqrt(Math.max(sumHi, sumLo)/(-dif));
var nMA = vMA1.getValue(0);
if(nMA == null) return;
nUpper = nMA+envSize;
nLower = nMA-envSize;
return new Array(nUpper,nMA,nLower);
}
I have written a study (the code is below) that basically plots two moving averages and offsets them backwards. One of the averages has envelopes as well. My issue is that I run this study on day bar charts with a time template of 260 bars. For some reason my average with the envelopes only shows for the first 12 bars and then stops plotting. I have tried a number of things and have been working on trying to solve this for about 2 days solid (I am new to efs programming and still learning) and have run into a dead end. I have no idea why the study will not work properly when there are no syntax errors. If someone could point our the errors or point me in a direction to resolving this I would greatly appreciate it.
Also, I have tried to post an error message via the return statement as commented out below but have not been able to get this to work.
var fnError = null;
if(getNumBars() < BarLength) { // check that BarLength number of bars has loaded in the chart
return fnError("Insufficient number of bars on chart.");
Thank you very much for any help you may be able to provide.
Regards,
Jane
function preMain() {
setPriceStudy(true); // study plots in price pane (main chart)
setStudyTitle("Centered MA w Bands");
setShowTitleParameters(false);
setShowCursorLabel(false); // supresses this study's label in cursor window
setCursorLabelName("MA upper", 0);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarThickness(1, 0);
setPlotType(PLOTTYPE_LINE, 0);
setShowCursorLabel(false, 0); //supresses this label in cursor window
setCursorLabelName("MA Centered", 1);
setDefaultBarFgColor(Color.red, 1);
setDefaultBarThickness(2, 1);
setPlotType(PLOTTYPE_LINE, 1);
setShowCursorLabel(false, 1); //supresses this label in cursor window
setCursorLabelName("MA lower", 2);
setDefaultBarFgColor(Color.red, 2);
setDefaultBarThickness(1, 2);
setPlotType(PLOTTYPE_LINE, 2);
setShowCursorLabel(false, 2); //supresses this label in cursor window
setCursorLabelName("MA long", 3);
setDefaultBarFgColor(Color.blue, 3);
setDefaultBarThickness(2, 3);
setPlotType(PLOTTYPE_LINE, 3);
setShowCursorLabel(false, 3); //supresses this label in cursor window
setComputeOnClose(); //force this script to only update on each new bar
var fp1 = new FunctionParameter("BarLength", FunctionParameter.NUMBER);
fp1.setName("BarLength");
fp1.setLowerLimit(1);
fp1.setDefault(260); // default number of bars
var fp2 = new FunctionParameter("MA_Length1", FunctionParameter.NUMBER);
fp2.setName("MA_Length1");
fp2.setLowerLimit(1);
fp2.setDefault(13); // default length for fast MA
var fp3 = new FunctionParameter("MA_Length2", FunctionParameter.NUMBER);
fp3.setName("MA_Length2");
fp3.setLowerLimit(1);
fp3.setDefault(27); // default length for slow MA
}
// Global Variables
var vMA1 = null;
var vMA2 = null;
var vBands = null;
var bInit = false;
function main(BarLength, MA_Length1, MA_Length2) {
var fnError = null;
if(getNumBars() < BarLength) { // check that BarLength number of bars has loaded in the chart
// return fnError("Insufficient number of bars on chart.");
drawTextPixel( 50, 50,"Insufficient number of bars on chart.", Color.red, null, Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM | Text.BOLD, "Name" ,10, -10 );
return;
}
if(!bInit) {
vBands = efsInternal("calcBands", BarLength, MA_Length1);
bInit = true;
}
var offset1 = Math.floor(MA_Length1*0.5);
var offset2 = Math.floor(MA_Length2*0.5);
if (vMA1 == null) vMA1 = sma(MA_Length1, hl2());
if (vMA2 == null) vMA2 = sma(MA_Length2, hl2());
/*
for(var j = 0; j <= dif; j++) {
var hMax = high(-j);
var lMax = low(-j);
if(hMax > highMax) highMax = hMax;
if(lMax < lowMax) lowMax = lMax;
}
*/
var MA_upper = offsetSeries(getSeries(vBands), -offset1);
var MA_center = offsetSeries(getSeries(vBands,1), -offset1);
var MA_lower = offsetSeries(getSeries(vBands,2), -offset1);
var MA_long = offsetSeries(getSeries(vMA2), -offset2);
return new Array(MA_upper, MA_center, MA_lower, MA_long);
}
// Global Variables
var vMA1 = null;
//== calcBands function calculates the value of the bands about the centered moving average
function calcBands(BarLength, MA_Length1) {
if (vMA1 == null) vMA1 = sma(MA_Length1, hl2());
var sumHi = 0;
var sumLo = 0;
var envSize = 0;
var medianPrice = 0;
var nUpper = null;
var nLower = null;
var offset1 = Math.floor(MA_Length1*0.5);
var dif = offset1-BarLength;
for(var i = dif; i < 1; i++) {
medianPrice = 0.5*(high(i)+low(i));
sumHi += Math.pow((high(i)-medianPrice), 2);
sumLo += Math.pow((medianPrice-low(i)), 2);
}
envSize = 4.5*Math.sqrt(Math.max(sumHi, sumLo)/(-dif));
var nMA = vMA1.getValue(0);
if(nMA == null) return;
nUpper = nMA+envSize;
nLower = nMA-envSize;
return new Array(nUpper,nMA,nLower);
}
Comment