I am using the DonchianStudy builtin to get highest high and lowest low of the past N bars (I assume this is the only way to do it). I want to pass in N (the lookback period) from a parameter to my indicator. Normally you initialize the builtin as a global variable passing in the paramters to the builtin. The main function is the one that gets my indicator parameters so how to I pass it to the builtin.
Announcement
Collapse
No announcement yet.
how to pass a study paramter to a builtin study?
Collapse
X
-
Hi,
Good question. There are good ways and bad ways of doing this.
The way I would recommend look like:
PHP Code:var study = null;
function PreMain() {
<stuff goes here>
}
function main(nLookback) {
if (study == null)
study = new DonchianStudy(nLookback);
function PreMain() {
<stuff goes here>
}
function main(nLookback) {
if (nLookback == null)
nLookback = 20; // Default
if (study == null)
study = new DonchianStudy(nLookback);
<stuff goes here>
}
If you would like you could even make it so the number can be dynamic:
var study = null;
var nMyLookBack = 20; // Default
var nMyLastLook = 20;
function PreMain() {
<stuff goes here>
}
function main(nLookback) {
if (nLookBack == null){
nMyLookBack = 20;
nMyLastLook = nMyLookBack;
}
if (study == null || nMyLastLook != nMyLookBack){
study = new DonchianStudy(nMyLookback);
nMyLastLook = nMyLookBack;
}
<stuff goes here>
if (nMyCycle > nMyLookBack)
nMyLookBack = nMyCycle;
}
Garth
Comment