I am trying to write a formula using a study from traders tips in technical analysis of S&C and one of the library studies from eS. But I'm getting no output. And the debugprintln doesn't seem to work either.
Announcement
Collapse
No announcement yet.
How can I get help on a formula I'm writing?
Collapse
X
-
here is the code
it is an ema of the infinte impulse response.i don't think it is even computing anything.
/************************************************** **************************************************
Copyright © eSignal, a division of Interactive Data Corporation. 2002. All rights reserved.
This sample eSignal Formula Script (EFS) may be modified and saved under a new
filename; however, eSignal is no longer responsible for the functionality once modified.
eSignal reserves the right to modify and overwrite this EFS file with each new release.
************************************************** ************************************************** */
var DV = new Array();
function preMain() {
setPriceStudy(true);
setCursorLabelName("ema of IIR");
setStudyTitle("EMA of Infinite Impulse Response");
setDefaultBarFgColor(Color.blue);
setDefaultBarThickness(2);
setComputeOnClose(true);
}
var dLastMA = 0.0;
var dThisMA = 0.0;
var dPercent = 0.0;
var dCount = 0;
var bPrimed = false;
var cur = 0.0;
var prev = 0.0;
function IIR(n) {
var vPrice = getValue("close",n,n-5);
var vRef = prev;
if(vPrice == null) {
return;
}
if(vRef == null) {
prev = vPrice[0];
return prev;
} else {
cur = 0.2 * (2 * vPrice[1] - vPrice[4]) + 0.8 * vRef;
prev = cur;
return cur;
}
}
function main(nLength) {
if(nLength == null)
nLength = 10;
var nBarState = getBarState();
var i;
var dSum = 0.0;
var dValue = new Array(nLength);
if(nBarState == BARSTATE_ALLBARS) {
// reset!
dPercent = (2.0 / (nLength + 1.0));
dLastMA = 0.0;
dThisMA = 0.0;
dCount = 0;
}
if(nBarState == BARSTATE_NEWBAR) {
dLastMA = dThisMA;
}
dThisMA = dLastMA;
if(bPrimed == false) {
for(i = -nLength; i <= 0; i++) {
vDV = DV.getValue(IIR(i),i);
dValue[i] = vDV;
}
if(dValue == null)
return;
for(i = 0; i < nLength; i++) {
dSum += dValue[i];
}
dLastMA = dSum / nLength;
dThisMA = dLastMA;
bPrimed = true;
} else {
dValue = DV.getValue(IIR(0));
if(dValue == null)
return;
dThisMA = (dValue - dLastMA) * dPercent + dLastMA;
}
return dThisMA;
}Last edited by Roderick; 06-08-2004, 03:39 PM.
Comment
-
Hello Roderick,
I was able to isolate the problems with your modified code. In regards to the debugPrintln() statement, what was probably happening was that you had placed the statement in your code after the offending line of code that was ending the formula execution. It was just not being called. My advise for debugging code in situations like this is to start with a debugPrintln() statement as the first line of code in main() to ensure that the main() function is being called. Then systematically move the debugPrintln() statement further down in the order of process until you isolate the problem code. I've left all the debug code I used to isolate the problems in your code for reference.
PHP Code:var DV = new Array();
function preMain() {
setPriceStudy(true);
setCursorLabelName("ema of IIR");
setStudyTitle("EMA of Infinite Impulse Response");
setDefaultBarFgColor(Color.blue);
setDefaultBarThickness(2);
setComputeOnClose(true);
}
var dLastMA = 0.0;
var dThisMA = 0.0;
var dPercent = 0.0;
var dCount = 0;
var bPrimed = false;
var cur = 0.0;
var prev = 0.0;
function IIR(n) {
//debugPrintln("IIR function called; n = " + n);
var vPrice = close(n, n-5);
var vRef = prev;
if(vPrice == null) {
return;
}
if(vRef == null) {
prev = vPrice[0];
//debugPrintln("vRef is null: prev = " + prev);
return prev;
} else {
cur = 0.2 * (2 * vPrice[1] - vPrice[4]) + 0.8 * vRef;
prev = cur;
//debugPrintln("vRef is Not null: cur = " + cur);
return cur;
}
}
function main(nLength) {
if(nLength == null) nLength = 10;
var nBarState = getBarState();
var i;
var dSum = 0.0;
var dValue = new Array(nLength);
var vDV;
if(nBarState == BARSTATE_ALLBARS) {
// reset!
dPercent = (2.0 / (nLength + 1.0));
dLastMA = 0.0;
dThisMA = 0.0;
dCount = 0;
}
if(nBarState == BARSTATE_NEWBAR) {
dLastMA = dThisMA;
}
dThisMA = dLastMA;
// ok to this line
//debugPrintln(getCurrentBarIndex())
if(bPrimed == false) {
for(i = -nLength + 1; i <= 0; i++){
//vDV = DV.getValue(IIR(i),i);
// never got here, above line ends formula execution
vDV = IIR(i);
//debugPrintln("vDV = " + vDV);
//dValue[i] = vDV;
// above line is trying to set values to negative array elements
dValue[-i] = vDV;
//debugPrintln("dValue[" + -i + "] = " + dValue[-i]);
//debugPrintln("dValue array = " + dValue);
if(dValue == null) return;
}
for(i = 0; i < nLength; i++) {
dSum += dValue[i];
}
//debugPrintln("Primed");
dLastMA = dSum / nLength;
//debugPrintln("dSum = " + dSum);
//debugPrintln("dLastMA = " + dLastMA); // returns NaN
dThisMA = dLastMA;
bPrimed = true;
} else {
//dValue = getValue(IIR());
// above line is improper usage
dValue = IIR(-nLength);
if(dValue == null) return;
dThisMA = (dValue - dLastMA) * dPercent + dLastMA;
//debugPrintln(dLastMA);
}
return dThisMA;
}
vDV = DV.getValue(IIR(i),i);
This line of code was killing the execution of the formula without generating any errors. This is a rare occurrence. To set the variable vDV to the returned result of the IIR() function, you just need to do the following.
vDV = IIR(i);
Then next problem was the calculation of dSum, which was NaN (not a number), in the priming routine. When passing values to array elements you need to reference positive array elements, otherwise the result won't be stored. So dValue[i] = vDV was changed to dValue[-i] = vDV. i in this case was already a negative value so referencing -i changes it back to a positive value. Now the for loop to add up the values in the dValue array will be a valid number so that dLastMA will become a valid number. It was also NaN as a result of dSum being NaN.
After fixing the priming routine, there is one more issue with the follow line of code.
dValue = getValue(IIR());
getValue is an EFS function, but passing a function call as a parameter is not valid. Again, to set a variable to the result of a function call is done like this.
dValue = IIR(-nLength);
Your function IIR(n) has a required parameter, n, which was not specified. Something needs to be passed for n or you get an error on close(n, n-5). Not sure what should be passed here, so you may need to change this. For now it's passing -nLength (i.e. -10).
Now the formula is calculating a valid number and returning it to your chart. I'm not sure if it's the correct calculation you're expecting, but you should be able to verify that. Hope you find this helpful. Let me know if you have further questions.
emaOfIIR_Roderick.efs
Jason K.
Project Manager
eSignal - an Interactive Data company
EFS KnowledgeBase
JavaScript for EFS Video Series
EFS Beginner Tutorial Series
EFS Glossary
Custom EFS Development Policy
New User Orientation
Comment
Comment