Announcement

Collapse
No announcement yet.

FunctionParameter helper class

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • FunctionParameter helper class

    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.

    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));
    }
    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
    Attached Files

  • #2
    I for one found it very useful for the techniques (like understanding the use of "this" keyword) but I can't run/test it all without the "EMA.efsLib".
    I will comment the EMA... lines out to play with how the utility class was created but would like the EMA.efsLib if possible.

    Thanks,

    Wayne
    Last edited by waynecd; 10-17-2015, 05:42 PM.

    Comment

    Working...
    X