Skip to content

Commit

Permalink
Added TrendLines
Browse files Browse the repository at this point in the history
  • Loading branch information
VertigoRay committed Jul 26, 2017
1 parent ce2139c commit 423b2fc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 76 deletions.
57 changes: 18 additions & 39 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,12 @@ BTC-USD:
strategies:
StdDev:
trades_n: 1000
trend_lines:
buy_on_trend_long_up: true
buy_on_trend_short_up: true
trend_trade_long_ct: '2000'
trend_trade_short_ct: '100'
buy_amount: '0.03'
buy_trigger: '0.50'
cancel_after: min
sell_amount: '0.01'
sell_above_buy: '0.01'
stop_loss: '1'
spread_n: '3'
spread_v: '0.50'
TrendLines:
trendlines_n: 2
trend_1_buy_on_up: true
trend_1_trades_n: 1000
trend_2_buy_on_up: true
trend_2_trades_n: 100


ETH-USD:
Expand All @@ -88,19 +81,12 @@ ETH-USD:
strategies:
StdDev:
trades_n: 1000
trend_lines:
buy_on_trend_long_up: true
buy_on_trend_short_up: true
trend_trade_long_ct: '1000'
trend_trade_short_ct: '100'
buy_amount: '0.01'
buy_trigger: '0.01'
cancel_after: min
sell_amount: '0.01'
sell_above_buy: '0.03'
stop_loss: '1'
spread_n: '3'
spread_v: '0.15'
TrendLines:
trendlines_n: 2
trend_1_buy_on_up: true
trend_1_trades_n: 1000
trend_2_buy_on_up: true
trend_2_trades_n: 100

LTC-USD:
trade_enabled: true
Expand All @@ -120,16 +106,9 @@ LTC-USD:
strategies:
StdDev:
trades_n: 1000
trend_lines:
buy_on_trend_long_up: true
buy_on_trend_short_up: true
trend_trade_long_ct: '1000'
trend_trade_short_ct: '100'
buy_amount: '1.00'
buy_trigger: '0.00'
cancel_after: min
sell_amount: '1.00'
sell_above_buy: '0.02'
stop_loss: '1'
spread_n: '3'
spread_v: '0.01'
TrendLines:
trendlines_n: 2
trend_1_buy_on_up: true
trend_1_trades_n: 1000
trend_2_buy_on_up: true
trend_2_trades_n: 100
2 changes: 0 additions & 2 deletions strategies/stddev.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class StdDev {
}

generateDummyData(product_id, count) {
const uuidv4 = require('uuid/v4');

this.set({
product_id: product_id
});
Expand Down
85 changes: 50 additions & 35 deletions strategies/trendlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,45 @@ const settings = require('config');
const stats = require('stats-lite');



class TrendLines {
constructor() {
this.trade_prices = [];
this.trend_lines = [];
this.buy_on_up = [];
this.trades_n = [];
}

add(add) {
if (typeof add !== 'object')
if (typeof add !== 'object' && Object.prototype.toString.call(add) === '[object Object]')
throw new Error(`The parameter 'add' should be an object; not a ${typeof add}.`);

if (add.trade)
this.addTrades([add.trade]);

if (add.trades)
this.addTrades(add.trades);
}

addTrades(trades) {
for (let trade in trades) {
// console.log('add trade price:', parseFloat(trades[trade].price.toString()))
this.trade_prices.push(parseFloat(trades[trade].toString()));
for (let i=0; i < this.trend_lines.length; i++) {
this.trend_lines[i].push(parseFloat(trades[trade].toString()));

while (this.trade_prices.length > settings.get(`${this.product_id}.strategies.StdDev.trades_n`)) {
this.trade_prices.shift();
settings.get(`${this.product_id}.strategies.TrendLines.trendlines_n`)

while (this.trend_lines[i].length > this.trades_n[i] + 1)
this.trend_lines[i].shift();
}
}
}

generateDummyData(product_id, count) {
const uuidv4 = require('uuid/v4');
product_id = product_id || settings.get('general.product_ids')[Math.floor(Math.random() * settings.get('general.product_ids').length)];

this.set({
product_id: product_id
});

count = count || settings.get(`${this.product_id}.strategies.StdDev.trades_n`);
count = count || Math.max.apply(null, this.trades_n) + 1;

let dummy_data = [];
let starting_id = Math.ceil(Math.random() * 100);
Expand All @@ -51,9 +57,8 @@ class TrendLines {
starting_price = Math.random() * 100;
}

for (let i = 0; i < count; i++) {
for (let i = 0; i < count; i++)
dummy_data.push(parseFloat((Math.floor(Math.random() * 2) ? starting_price + Math.random() * 2 : starting_price + Math.random() * 2)).toFixed(2));
}

this.add({
trades: dummy_data
Expand All @@ -62,32 +67,32 @@ class TrendLines {

get() {
let strategy = {
stddev: stats.stdev(this.trade_prices),
mean: stats.mean(this.trade_prices),
last_trade_price: this.trade_prices[this.trade_prices.length - 1],
trades_n: this.trade_prices.length,
trendlines_n: settings.get(`${this.product_id}.strategies.TrendLines.trendlines_n`),
last_trade_price: this.trend_lines[0][this.trend_lines[0].length - 1],
}

strategy.diff_price_and_mean = strategy.last_trade_price - strategy.mean;
strategy.direction = (Math.abs(strategy.diff_price_and_mean) === strategy.diff_price_and_mean) ? 'Up' : 'Down';

// Only change directions if we exceed the stddev; positive or negative.
if (
(Math.abs(strategy.diff_price_and_mean) > strategy.stddev)
&& (Math.abs(strategy.diff_price_and_mean) === strategy.diff_price_and_mean)
) {
this.trending_up = true;
} else if (
(Math.abs(strategy.diff_price_and_mean) > strategy.stddev)
&& (Math.abs(strategy.diff_price_and_mean) !== strategy.diff_price_and_mean)
) {
this.trending_up = false; //literally: direction down
}
var is_trending_up = [];

for (let i=0; i < this.trend_lines.length; i++) {
let trend_line_id = i + 1;

strategy[`trend_${trend_line_id}_buy_on_up`] = this.buy_on_up[i];
strategy[`trend_${trend_line_id}_trades_n`] = this.trades_n[i];

strategy.is_trending_up = this.trending_up;
let real_prev_trend_line = this.trend_lines[i].slice(0, this.trend_lines[i].length - 1);
strategy[`trend_${trend_line_id}_prev_mean`] = stats.mean(real_prev_trend_line);

// Buy as long as the overall direction is up.
strategy.should_buy = this.trending_up;
let real_trend_line = this.trend_lines[i];
real_trend_line.shift();
strategy[`trend_${trend_line_id}_mean`] = stats.mean(real_trend_line);

let trending_up = strategy[`trend_${trend_line_id}_mean`] > strategy[`trend_${trend_line_id}_prev_mean`];
strategy[`trend_${trend_line_id}_trending_up`] = trending_up;
is_trending_up.push(trending_up);
}

strategy.is_trending_up = is_trending_up.reduce((a, b) => { return a && b; });
strategy.should_buy = strategy.is_trending_up;

return strategy;
}
Expand All @@ -106,10 +111,20 @@ class TrendLines {


this.product_id = set.product_id

for (let i=0; i < settings.get(`${this.product_id}.strategies.TrendLines.trendlines_n`); i++) {
this.trend_lines[i] = [];

let trend_line_id = i + 1;

this.buy_on_up[i] = settings.get(`${this.product_id}.strategies.TrendLines.trend_${trend_line_id}_buy_on_up`);
this.trades_n[i] = settings.get(`${this.product_id}.strategies.TrendLines.trend_${trend_line_id}_trades_n`);
}
}
}
}

module.exports = StdDev;
module.exports = TrendLines;

// const stddev = require('./strategies/stddev.js')
// const TrendLines = require('./strategies/trendlines.js');
// const trendlines = new TrendLines();

0 comments on commit 423b2fc

Please sign in to comment.