-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
126 lines (85 loc) · 3.32 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var Env = require('./config/env.js');
var Gdax = require('gdax');
var async = require('async');
var mongoose = require('mongoose');
mongoose.connect(Env.MONGO_CONNECTION_STRING);
var Order = require('./models/order.js');
var winston = require('winston');
winston.add(winston.transports.File, { filename: 'app.log' });
winston.log('info', "Getting available balance.");
var authedClient = new Gdax.AuthenticatedClient(Env.ACCESS_KEY, Env.SECRET_KEY, Env.PASSPHRASE_KEY);
if(Env.DELETE_PREVIOUS){
// Clear all orders
Order.remove({}, function(err){
if(err){
winston.log('error', "Error removing orders: " + error);
process.exit(-1);
}
// Now create new orders
createBids();
});
} else {
// Create new orders without deleting old ones
createBids();
}
function createBids(){
// Subtract 1% for fees
var available = Env.AMOUNT_USD * 0.99;
winston.log('info', "Amount available in account: $" + available);
var amountPerOrder = available / Env.ORDER_COUNT;
winston.log('info', "Amount per buy order: " + amountPerOrder);
authedClient.getProducts(function(error, response, data){
winston.log('info', "Products: " + JSON.stringify(data));
authedClient.getProductTicker(function(error, response, data){
if (error || response.statusCode != 200) {
winston.log('info', "Error getting ticker: " + error);
winston.log('info', "Response: " + JSON.stringify(response));
return;
}
winston.log('info', JSON.stringify(data));
var currentPrice = Number(data.price);
winston.log('info', "Current market price: $" + currentPrice);
var startPrice = Math.floor( currentPrice );
winston.log('info', "Starting price: $" + startPrice);
var ordersToCreate = [ ];
for(var i = 0; i < Env.ORDER_COUNT ; i++){
var orderPrice = startPrice - ( i * Env.GAP_AMOUNT) ;
var size = amountPerOrder / orderPrice ;
var orderToCreate = {
size : "" + size.toFixed(2),
price : "" + orderPrice.toFixed(2),
side : "buy",
product_id : "BTC-USD"
};
ordersToCreate.push(orderToCreate);
}
async.eachSeries(ordersToCreate, function(order, callback){
authedClient.buy(order, function(error, response, data){
if (error || response.statusCode != 200) {
winston.log('info', "Error creating buy order: " + error);
winston.log('info', "Response: " + JSON.stringify(response));
callback("Failed to place order: " + JSON.stringify(order));
}
order.id = data.id;
Order(order).save(function(error){
if(error){
winston.log('error', "Failed to create order" + order.id + " with errro " + error);
callback(error);
return;
}
callback();
});
});
}, function(err){
if(err){
winston.log('info', 'Order creation failed: ' + err);
} else {
winston.log('info', "Finished creating orders");
winston.log('info', JSON.stringify(ordersToCreate[0]));
//listenForOrders(ordersToCreate);
process.exit();
}
});
});
});
}