I've been porting a bunch of indicators from another platform to eSignal, and found many of them use the same or very similar input parameters. This meant there was a lot of duplicate, long-winded FunctionParameter code, so I decided to create a class to manage it, which I've attached.
To use it, you just create an instance of Params and then call addNumber(), addString(), addColor() or addBoolean() as required. Each function takes a name and title, plus an optional default value. All except addBoolean() also accepts an array of options (to create a drop-down list) as a fourth argument. In addition, I've created some predefined examples for common requirements, such as addLength(), addSource(), addSymbol() and addInterval(). Just for fun, I've given it a fluent interface, so you can chain them as show in the example below.
I've no idea how many people are interesting in this sort of thing, so please let me know if you think it's worth me sharing stuff like this in future
To use it, you just create an instance of Params and then call addNumber(), addString(), addColor() or addBoolean() as required. Each function takes a name and title, plus an optional default value. All except addBoolean() also accepts an array of options (to create a drop-down list) as a fourth argument. In addition, I've created some predefined examples for common requirements, such as addLength(), addSource(), addSymbol() and addInterval(). Just for fun, I've given it a fluent interface, so you can chain them as show in the example below.
Code:
// Load libraries var paramsLib = addLibrary("BionicTrader/Util/Params.efsLib"); var emaLib = addLibrary("BionicTrader/MA/Ema.efsLib"); // Initialise global variables var paramsObj; var pricesObj; var emaObj; // Configure study function preMain() { setStudyTitle("EMA"); setCursorLabelName("EMA"); setPriceStudy(true); setDefaultBarFgColor(Color.RGB(0, 148, 255)); setPlotType(PLOTTYPE_LINE); setDefaultBarThickness(1); askForInput(); // Setup user-defined input parameters paramsObj = new paramsLib.Params(); paramsObj.addLength(20).addSource().addSymbol().addInterval(); paramsObj.addNumber('offset', 'Offset', 0, null, 0); paramsObj.addBoolean('params', 'Show Parameters', false); } // Get indicator value function main(length, source, symbol, interval, offset, params) { length = length || paramsObj.getDefault('length'); source = source || paramsObj.getDefault('source'); symbol = symbol || paramsObj.getDefault('symbol'); interval = interval || paramsObj.getDefault('interval'); offset = offset || paramsObj.getDefault('offset'); if (params === undefined) params = paramsObj.getDefault('params'); // Instantiate library classes etc on first call if (emaObj === undefined) { pricesObj = paramsObj.getPrices(source, symbol, interval); emaObj = new emaLib.Ema(length); setShowTitleParameters(params); } // Return new value return emaObj.getNext(pricesObj.getValue(offset)); }
Comment