I pieced together this script to return the sum of the differences (MA1-MA2). It works fine until I scroll the chart back in time; a bad first value for (MA1-MA2) is then given. If I reload the study after scrolling, the first value recomputes properly. What correction needs to be made so that the first value is correct without reloading the study everytime I scroll the chart back?
PHP Code:
//*********************************************************
Alexis C. Montenegro © December 2004
Use and/or modify this code freely. If you redistribute it
please include this and/or any other comment blocks and a
description of any changes you make.
**********************************************************/
var fpArray = new Array();
var a = 0;
var suma = 0;
function preMain() {
setStudyTitle("MAx2SumDif");
setCursorLabelName("SumDif",0);
setDefaultBarFgColor(Color.blue,0);
setPlotType(PLOTTYPE_FLATLINES,0);
setDefaultBarThickness(2,0);
askForInput();
setPriceStudy(false);
var x=0;
fpArray[x] = new FunctionParameter("Type1", FunctionParameter.STRING);
with(fpArray[x++]){
addOption("sma");
addOption("ema");
addOption("wma");
addOption("vwma");
setDefault("sma");
}
fpArray[x] = new FunctionParameter("Length1", FunctionParameter.NUMBER);
with(fpArray[x++]){
setLowerLimit(1);
setDefault(5);
}
fpArray[x] = new FunctionParameter("Source1", 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("Offset1", FunctionParameter.NUMBER);
with(fpArray[x++]){
setDefault(0);
}
fpArray[x] = new FunctionParameter("Type2", FunctionParameter.STRING);
with(fpArray[x++]){
addOption("sma");
addOption("ema");
addOption("wma");
addOption("vwma");
setDefault("sma");
}
fpArray[x] = new FunctionParameter("Length2", FunctionParameter.NUMBER);
with(fpArray[x++]){
setLowerLimit(1);
setDefault(390);
}
fpArray[x] = new FunctionParameter("Source2", 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("Offset2", FunctionParameter.NUMBER);
with(fpArray[x++]){
setDefault(0);
}
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("LineColor1", FunctionParameter.COLOR);
with(fpArray[x++]){
setName("DI+ Color");
setDefault(Color.black);
}
fpArray[x] = new FunctionParameter("LineColor2", FunctionParameter.COLOR);
with(fpArray[x++]){
setName("DI- Color");
setDefault(Color.red);
}
fpArray[x] = new FunctionParameter("Params", FunctionParameter.BOOLEAN);
with(fpArray[x++]){
setName("Show Parameters");
setDefault(false);
}
}
var bInit = false;
var xMA1 = null;
var xMA2 = null;
function main(Type1,Length1,Source1,Offset1,Type2,Length2,Source2,Offset2,Symbol,Interval,LineColor1,LineColor2,Params) {
if (getCurrentBarCount() < Length2) return;
var nState = getBarState();
if (nState == BARSTATE_NEWBAR) {
suma += a;
}
if(bInit == false){
if(Symbol == null) Symbol = getSymbol();
if(Interval == null) Interval = getInterval();
vSymbol = Symbol+","+Interval;
xMA1 = offsetSeries(eval(Type1)(Length1,eval(Source1)(sym(vSymbol))),Offset1);
xMA2 = offsetSeries(eval(Type2)(Length2,eval(Source2)(sym(vSymbol))),Offset2);
setDefaultBarFgColor(LineColor1,0);
setDefaultBarFgColor(LineColor2,1);
setShowTitleParameters(eval(Params));
bInit = true;
}
a = (xMA1.getValue(MAStudy.MA) - xMA2.getValue(MAStudy.MA)) / 1;
if(a == null) return;
return (suma + a);
}
Comment