I've been trying to implement a function that I found online, but when I try to run it in the terminal, I keep getting this error:
/home/simone/gekko/strategies/high.js:10
sma: function(name, price, points)
^^^
SyntaxError: Unexpected identifier
I even attempted to change the first parameter within the function from "this[name]" but without success. I'm new to JavaScript and eager to understand where I am going wrong. Here is the code snippet:
// simple sma function
// params: name-of-array, price (of something), number of points (sma length)
// returns: the moving average price/value
sma: function(name, price, points)
{
// create array if not exist + initialize array
if( !this[name] )
{
let a = 0, b = [];
for (a; a < points; a++) { b[a] = 0; }
this[name] = b;
}
let arr = this[name],
len = arr.length;
arr[len] = price; // add new value to last position in array
arr.shift(); // remove oldest value from array (maintaining order)
this[name] = arr; // save changes
// calculate current average
let i = 0,
total = 0;
for( i; i < len; i++ ) { total += arr[i]; }
let avg = total / len;
return avg;
},
Here is the complete code:
var strat = {
init : function() {
}
//======================================================================================
// simple sma function
// params: name-of-array, price (of something), number of points (sma length)
// returns: the moving average price/value
sma: function(name, price, points)
{
// create array if not exist + generate array
if( !this[name] )
{
let a = 0, b = [];
for (a; a < points; a++) { b[a] = 0; }
this[name] = b;
}
let arr = this[name],
len = arr.length;
arr[len] = price; // add new value to last position in array
arr.shift(); // remove oldest value from array (maintaining order)
this[name] = arr; // save changes
// calculate current average
let i = 0,
total = 0;
for( i; i < len; i++ ) { total += arr[i]; }
let avg = total / len;
return avg;
},
};
//======================================================================================
strat.check = function(candle) {
let sma_high = this.sma('sma_high', this.candle.high, 10);
let sma_low = this.sma('sma_low', this.candle.low, 10);
// additional logic can go here, for example:
if( sma_high < sma_low ) this.advice('long')
else this.advice('short')
}
//======================================================================================
module.exports = strat;