02-11-2018, 11:12 PM
So i would do this:
1. You do got access to this inside check() in your strategy:
this.candle.high
this.candle.low
...etc...
So...
2. Write a simple func for sma to just run the calc inline and get the sma-values
Here's a simple sma-function:
Use inside the strategy you're creating.
var strat = {
// init
init: function(){
// your init stuff
},
sma: function(){
// the SMA-code...
},
check: function(){
// get sma-values for candle.high and candle.low
let sma_high = this.sma('sma_high', this.candle.high, 10);
let sma_low = this.sma('sma_low', this.candle.low, 10);
// the rest of your logic, simple sample:
if( sma_high < sma_low ) this.advice('long')
else this.advice('short')
}
}
Do note that this is untested, but you should get the gist of it.....
1. You do got access to this inside check() in your strategy:
this.candle.high
this.candle.low
...etc...
So...
2. Write a simple func for sma to just run the calc inline and get the sma-values
Here's a simple sma-function:
Code:
// simple sma function
// params: name-of-array, price (of something), number of points (sma lenght)
// returns: the moving average price/value
sma: function(name, price, points)
{
// create arr 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 to last in array
arr.shift(); // remove first (old) from array (keeping max order)
this[name] = arr; // set/save
// calculate current average
let i = 0,
total = 0;
for( i; i < len; i++ ) { total += arr[i]; }
let avg = total / len;
return avg;
},
Use inside the strategy you're creating.
var strat = {
// init
init: function(){
// your init stuff
},
sma: function(){
// the SMA-code...
},
check: function(){
// get sma-values for candle.high and candle.low
let sma_high = this.sma('sma_high', this.candle.high, 10);
let sma_low = this.sma('sma_low', this.candle.low, 10);
// the rest of your logic, simple sample:
if( sma_high < sma_low ) this.advice('long')
else this.advice('short')
}
}
Do note that this is untested, but you should get the gist of it.....