-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
224 lines (184 loc) · 8.39 KB
/
Copy pathapp.js
File metadata and controls
224 lines (184 loc) · 8.39 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var CronJob = require('cron').CronJob;
var async = require('async');
var moment = require('moment-timezone');
var trade = require('./myModules/trade');
var realTime = require('./myModules/realTime');
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
app.get('/data',function(req,res){
data.ltp = realTime.getLtp().ltp;
res.json(data);
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
const SELL_PERCENTAGE = 2;
const data = {
referenceValues: {},
orders: [],
executions: [],
logs: []
};
function setReferenceValues() {
const ltp = realTime.getLtp();
myLog('基準: ' + ltp.ltp);
data.referenceValues.ltp = ltp.ltp;
data.referenceValues.timestamp = moment(ltp.timestamp).tz("Asia/Tokyo").format("YYYY年M月D日 HH:mm ss杪");
}
const buyPercentages = [
0.89, 0.86, 0.84, 0.83, 0.82, 0.81, 0.8,
0.79, 0.78, 0.77, 0.76, 0.75, 0.74, 0.73, 0.72, 0.71, 0.7,
0.69, 0.68, 0.67, 0.66, 0.65, 0.64, 0.63, 0.62, 0.61, 0.6
];
function init() {
myLog('日本円残高とビットコイン残高を取得しています。');
trade.getAverageBitCoinBalance(function(err, averageBitCoinBalance, moneyBalance, bitCoinBalance, executions) {
if (err) {
console.log('3分後にもう一度アクセスを試してみます。');
setTimeout(init, 1000 * 60 * 3);
return;
}
data.moneyBalance = moneyBalance;
data.bitCoinBalance = bitCoinBalance;
data.averagebitCoinBalance = averageBitCoinBalance;
data.executions = executions;
myLog(`あなたの 日本円 の残高は ${moneyBalance}円 です。`);
myLog(`あなたの Bitcoin の残高は ${bitCoinBalance} Bitcoin です。`);
myLog('平均取得額: ' + averageBitCoinBalance + '円');
myLog('手数料を取得しています。');
trade.getTradingCommission(function(err, response, payload) {
if (err) {
console.log('3分後にもう一度アクセスを試してみます。');
setTimeout(init, 1000 * 60 * 3);
return;
}
data.tradingCommission = payload.commission_rate;
myLog(`手数料は ${payload.commission_rate}円です。`);
trade.cancelAll(function(){
myLog('全ての注文をキャンセルしました。');
setTimeout(function() {
setReferenceValues();
// 売り注文
var sellPrice = Math.round(data.averagebitCoinBalance * SELL_PERCENTAGE);
var canSellBTCSize = data.bitCoinBalance - data.bitCoinBalance * data.tradingCommission; // 手数料で引かれる
var sellBTCSize = Math.floor(canSellBTCSize * 1000) / 1000; // 取引可能額に変更
trade.sellOrder(sellPrice, sellBTCSize, function(err, response, payload) {
if (err) { myLog(err); }
if (payload.error_message) { myLog(payload.error_message); }
data.mostRecentrySellId = payload.child_order_acceptance_id;
myLog(sellPrice + '円で ' + sellBTCSize + ' BTC 売り注文を出しました ' + payload.child_order_acceptance_id);
});
// 買い注文
var buyBTCSize = 0;
var canBuyBTCPrice = data.moneyBalance;
// 買いループ開始
async.eachSeries(buyPercentages, function(item, next) {
var buyPrice = Math.floor(data.referenceValues.ltp * item); // 買い値段
buyBTCSize = (buyBTCSize * 1000 + 0.001 * 1000) / 1000; // 買う数量
var buyTotalPrice = (buyPrice * (buyBTCSize * 1000)) / 1000; // 買う合計額
if (canBuyBTCPrice < (0.001 * buyPrice + buyPrice * data.tradingCommission))
{
next(); // もう買えない時
} else {
if (buyTotalPrice > canBuyBTCPrice) { // 買える金額の限界を迎えた時
buyBTCSize = Math.floor(canBuyBTCPrice * 1000 / buyPrice) / 1000; // 買える量額調整
buyTotalPrice = (buyPrice * (buyBTCSize * 1000)) / 1000; // 買う合計額
}
canBuyBTCPrice -= (buyTotalPrice + buyTotalPrice * data.tradingCommission);
setTimeout(function() {
trade.buyOrder(buyPrice, buyBTCSize, function(err, response, payload) {
if (err) { myLog(err); }
if (payload.error_message) { myLog(payload.error_message); }
myLog(buyPrice + '円で ' + buyBTCSize + ' BTC 買い注文を出しました ' + payload.child_order_acceptance_id);
next();
});
}, 1000);
}
}, function(err){
//処理2
if (err) { myLog(err); }
myLog('!買い注文完了!');
});
}, 1000 * 60);
});
});
});
}
setTimeout(init, 3000);
function myLog(message) {
console.log(message);
data.logs.push(message);
if (data.logs.length > 1000) {data.logs.length = 1000;}
}
// 毎朝4時の cron
new CronJob('21 43 05 * * *', function() { // 毎日 5時43分21杪
myLog('朝になりました。基準値と注文を更新します。');
init();
}, null, true, 'Asia/Tokyo');
// 3分おきに実行するもの
setInterval(function () {
// 注文一覧の取得
trade.getOrders(function(err, response, payload) {
if (err) return;
data.orders = payload;
});
// 平均取得額
trade.getAverageBitCoinBalance(function(err, averageBitCoinBalance, moneyBalance, bitCoinBalance, executions) {
if (err) return;
data.moneyBalance = moneyBalance;
data.bitCoinBalance = bitCoinBalance;
data.executions = executions;
if (data.averagebitCoinBalance != averageBitCoinBalance) { // 平均取得額が変わっている
data.averagebitCoinBalance = averageBitCoinBalance; // 値更新
myLog('平均値が変わっている');
trade.cancelOrder(data.mostRecentrySellId, function(payload) { // 売り注文のキャンセル
myLog('平均値が変わっているので売り注文のキャンセル');
setTimeout(function() {
// 売り注文
myLog('売り更新の setTimeout 内部');
var sellPrice = Math.round(data.averagebitCoinBalance * SELL_PERCENTAGE);
var canSellBTCSize = data.bitCoinBalance - data.bitCoinBalance * data.tradingCommission; // 手数料で引かれる
var sellBTCSize = Math.floor(canSellBTCSize * 1000) / 1000; // 取引可能額に変更
trade.sellOrder(sellPrice, sellBTCSize, function(err, response, payload) {
if (err) { myLog(err); }
if (payload.error_message) { myLog(payload.error_message); }
data.mostRecentrySellId = payload.child_order_acceptance_id;
myLog(sellPrice + '円で ' + sellBTCSize + ' BTC 売り注文を出しました ' + payload.child_order_acceptance_id);
});
}, 1000 * 60);
});
}
});
}, 1000 * 60 * 3);