Skip to content

Instantly share code, notes, and snippets.

@bergquist
Created November 19, 2015 11:46
Show Gist options
  • Save bergquist/f4a2d8abf1ecc152f71c to your computer and use it in GitHub Desktop.
Save bergquist/f4a2d8abf1ecc152f71c to your computer and use it in GitHub Desktop.
danske-bank-trading
var inputs = [ 100, 99, 87, 77, 85, 99, 105, 110, 120, 110, 105, 100, 105, 100, 99, 95, 95, 95, 90, 89, 88, 80, 85, 87, 88, 89, 90, 95, 97, 100 ];
var history = []
var lastAction = ''
var shortSpan = 2;
var longSpan = 5;
inputs.forEach(function(el, position) {
history.push(el)
if (history.length < shortSpan) {
return 'no action'; //without 200 points its unsafe, trading is not gambling.
}
trade(history, el, position)
});
function trade(history, input, number) {
//console.log(history.length, ' ', input, ' ', number)
var shortSMA = getSMA(history, shortSpan);
var longSMA = getSMA(history, longSpan);
if (shortSMA > longSMA && (lastAction != 'buy')) {
log(shortSMA, longSMA, 'buy')
lastAction = 'buy'
} else if (shortSMA < longSMA && (lastAction != 'sell')) {
log(shortSMA, longSMA, 'sell')
lastAction = 'sell'
} else {
log(shortSMA, longSMA, 'no action')
}
}
function getSMA(history, length) {
if (history.length < longSpan) {
length = history.length;
}
var last200points = history.slice(length * -1);
return last200points.reduce(function(x, y) { return x + y; }, 0) / length
}
function log(longSMA, shortSMA, action) {
console.log('longSMA: \t%s\t shortSMA: \t%s\t %s', longSMA, shortSMA, action)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment