Can anyone suggest an easy way to get this coded quickly ?
Announcement
Collapse
No announcement yet.
2% band over 10day Simple MA ?
Collapse
X
-
sgarbs
The Formula Wizard. Attached is the efs created with the FW.
AlexAttached Files
-
PHP Code:Length = 10;
var avg = new MAStudy(Length, 0, "Close", MAStudy.SIMPLE);
function preMain() {
setPriceStudy(true);
setCursorLabelName("top", 0);
setCursorLabelName("sma", 1);
setCursorLabelName("bottom", 2);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarFgColor(Color.blue, 1);
setDefaultBarFgColor(Color.green, 2);
}
function main(Length, Percent) {
if (Length == null) Length = 10;
if (Percent == null) Percent = 2;
var sma = avg.getValue(MAStudy.MA);
var top = sma * (1 + Percent * .01);
var bottom = sma * (1 - Percent * .01);
return new Array(top, sma, bottom);
}
Attached Files
Comment
-
xygeek...
Thanks for helping with that. My question is: If i try to emulate this formula by using the efs wizard - i cant figure out where i would tell it to increase the the 10day ma by 1+2% ?
I only see how to place the 10 day MA , but not add a new 2% band ?
Can this only be done by actually writing the sript ?
Comment
-
xygeek
The way your formula is laid out does not allow for inputs to be passed on to the built-in MA Study. In fact you cannot change the length of the MA via Edit Studies.
You need to modify the formula as follows if you want to be able to use Edit Studies to change the MA Length
Alex
PHP Code:function preMain() {
setPriceStudy(true);
setCursorLabelName("top", 0);
setCursorLabelName("sma", 1);
setCursorLabelName("bottom", 2);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarFgColor(Color.blue, 1);
setDefaultBarFgColor(Color.green, 2);
}
function main(Length, Percent) {
if (Length == null) Length = 10;
if (Percent == null) Percent = 2;
var avg = new MAStudy(Length, 0, "Close", MAStudy.SIMPLE);
var sma = avg.getValue(MAStudy.MA);
var top = sma * (1 + Percent * .01);
var bottom = sma * (1 - Percent * .01);
return new Array(top, sma, bottom);
}
Comment
-
Might I suggest a few changes to Alex's code.
Make the var declaration for the builtin a local global by placing the statement outside of main(). Matt has suggested to me in the past that declaring it inside of main() could cause problems.
Also be sure to initialize it just once for any change in MA length. If it is initialized for every tick you might see crashes at times (I have seen this first hand).
So it would look like:
PHP Code:
var avg = null;
var nOldLen = 0;
function preMain() {
setPriceStudy(true);
setCursorLabelName("top", 0);
setCursorLabelName("sma", 1);
setCursorLabelName("bottom", 2);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarFgColor(Color.blue, 1);
setDefaultBarFgColor(Color.green, 2);
}
function main(Length, Percent) {
if (Length == null) Length = 10;
if (Percent == null) Percent = 2;
if (nOldLen == 0 || Length != nOldLen){
avg = new MAStudy(Length, 0, "Close", MAStudy.SIMPLE);
nOldLen = Length;
}
var sma = avg.getValue(MAStudy.MA);
var top = sma * (1 + Percent * .01);
var bottom = sma * (1 - Percent * .01);
return new Array(top, sma, bottom);
}
the current length is overkill for this specific example, as changing any of the values via edit study will force eSignal to interate all the bars. This means a simple test for:
if (avg == null)
Would be enough. But I coded it this way so that if people wanted to create an EFS that would allow the Length of the MA to modifed by the EFS dynamically, they would have a template.
GarthGarth
Comment
-
Thanks, that makes sense. I appreciate the precise syntax you use to refer to things such as local globals. It's helping me understand javascript.
However the input parameters are inaccessible from the edit study menu again. I like the concept of dynamically updating parameters so please let me know if you get it sorted out.
Comment
-
They are accessible (and even work - just tested) here. If you made ANY changes to the code as posted (including adding comments) either post the changed version here and I will look at it, or remove the changes and see if it works.
GGarth
Comment
-
Is it possible the lengthy comments made the inputs disappear? It works fine with the comments removed. Here’s the version that didn't work:
PHP Code:/* Might I suggest a few changes to Alex's code.
Make the var declaration for the builtin a local global by placing the statement outside of main().
Matt has suggested to me in the past that declaring it inside of main() could cause problems.
Also be sure to initialize it just once for any change in MA length.
If it is initialized for every tick you might see crashes at times (I have seen this first hand).
So it would look like: */
var avg = null;
var nOldLen = 0;
function preMain() {
setPriceStudy(true);
setCursorLabelName("top", 0);
setCursorLabelName("sma", 1);
setCursorLabelName("bottom", 2);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarFgColor(Color.blue, 1);
setDefaultBarFgColor(Color.green, 2);
}
function main(Length, Percent) {
if (Length == null) Length = 10;
if (Percent == null) Percent = 2;
if (nOldLen == 0 || Length != nOldLen){
avg = new MAStudy(Length, 0, "Close", MAStudy.SIMPLE);
nOldLen = Length;
}
var sma = avg.getValue(MAStudy.MA);
var top = sma * (1 + Percent * .01);
var bottom = sma * (1 - Percent * .01);
return new Array(top, sma, bottom);
}
/* I believe that keeping the old value of Length and comparing it to
the current length is overkill for this specific example,
as changing any of the values via edit study will force eSignal to interate all the bars.
This means a simple test for:
if (avg == null)
Would be enough. But I coded it this way so that if people wanted to create an EFS that would
allow the Length of the MA to modifed by the EFS dynamically, they would have a template.
Garth */
Attached Files
Comment
-
Is it possible the lengthy comments made the inputs disappear?
PHP Code:Make the var declaration for the builtin a local global by placing the statement outside of main().
GarthGarth
Comment
-
With both EFS applied to the same chart it's easy to see the difference via the edit study menu. The version without comments has inputs while the other one doesn't.
PHP Code:
var avg = null;
var nOldLen = 0;
function preMain() {
setPriceStudy(true);
setCursorLabelName("top", 0);
setCursorLabelName("sma", 1);
setCursorLabelName("bottom", 2);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarFgColor(Color.blue, 1);
setDefaultBarFgColor(Color.green, 2);
}
function main(Length, Percent) {
if (Length == null) Length = 10;
if (Percent == null) Percent = 2;
if (nOldLen == 0 || Length != nOldLen){
avg = new MAStudy(Length, 0, "Close", MAStudy.SIMPLE);
nOldLen = Length;
}
var sma = avg.getValue(MAStudy.MA);
var top = sma * (1 + Percent * .01);
var bottom = sma * (1 - Percent * .01);
return new Array(top, sma, bottom);
}
Attached Files
Comment
-
Hmm...there should have been two lines in the code section that need to be fixed, but for whatever reason only one pasted and now it will not let me edit my own post to fix it (again, for whatever reason).
Anyway these are the lines:
Make the var declaration for the builtin a local global by placing the statement outside of main().
Matt has suggested to me in the past that declaring it inside of main() could cause problems.Garth
Comment
Comment