index.js
5.27 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
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const url1 = 'https://api.upbit.com/v1/market/all';
const options = { method: 'GET', headers: { Accept: 'application/json' } };
const express = require('express');
const app = express();
const { Coin } = require("./models/Coin");
var sort_info = new Array();
const mongoose = require('mongoose');
const config = require('./config/key');
const connect = mongoose.connect(config.mongoURI, {
useNewUrlParser: true, useUnifiedTopology: true
})
.then(() => console.log('디비연결 성공'))
.catch((err) => console.log(err));
async function get_marketName() {
var data = new Array();
//전체 암호화폐 리스트 불러오기
let response = await fetch(url1, options)
.then(res => res.json())
.then(json => {
for (i in json) {
data.push(json[i].market);
}
})
return data;
}
async function get_marketInfo() {
//각 암호화폐 정보 조회
var name_list = await get_marketName();
const url2 = `https://api.upbit.com/v1/ticker/?markets=${name_list}`;
var arr = new Array();
let response2 = await fetch(url2, options)
.then(res => res.json())
.then(json => {
for (i in json) {
if (json[i].acc_trade_price_24h > 100000000000) {
arr.push([json[i].market, json[i].acc_trade_price_24h, json[i].trade_price]);
}
}
})
return arr
}
async function sort_data() {
arr = await get_marketInfo();
arr.sort((a, b) => {
return b[1] - a[1];
})
return arr;
}
async function save_coin(arr) {
for (var i = 0; i < 10; i++) {
if (arr[i]) {
const coin = new Coin({
tid: i + 1,
name: arr[i][0],
acc_trade_price_24h: arr[i][1],
current_price: arr[i][2]
});
await coin.save((err) => {
if (err) {
console.log(err)
}
})
}
}
return true;
}
async function get_candle(minute, market) {
const url = `https://api.upbit.com/v1/candles/minutes/${minute}?market=${market}&count=1`;
var candle = new Array();
let response = await fetch(url, options)
.then(res => res.json())
.then(json => candle = json)
return candle;
}
app.get('/get_market', async (req, res) => {
var name_list = (await get_marketName());
var market_info = (await get_marketInfo(name_list));
sort_info = (await sort_data(market_info));
res.json(await save_coin(sort_info));
})
app.get('/get_candle', async (req, res) => {
Coin.find()
.then(result => {
result.forEach((item) => {
get_candle(5, item.name)
.then(result => {
Coin.findOneAndUpdate({ name: result[0].market }, { five_candle: result[0].trade_price }, { new: true }, (err, doc) => {
// console.log(doc);
})
})
})
});
})
async function refresh_db() {
Coin.find()
.then(result => {
if (result.length !== 0) {
Coin.deleteMany({ tid: { $gt: 0 } }, (err, result) => {
if (err) {
console.log(err);
} else {
console.log(result);
}
})
}
save_coin(sort_info);
})
}
async function repeat_check(t1) {
await Coin.find().then(result => {
for (var key in result) {
t1.push(result[key].name)
}
})
setTimeout(() => {
setInterval(async () => {
for (var i = 0; i < t1.length; i++) {
// console.log(t1.length);
// console.log(t1[i]);
var candle = await get_candle(5, t1[i]);
// console.log(i+"번째 코인 가격 "+candle[0].trade_price);
await Coin.findOne({ name: candle[0].market }).then((result) => {
//가격이 떨어졌을때
if (result.current_price > candle[0].trade_price) {
Coin.updateOne({ name: candle[0].market }, { current_price: candle[0].trade_price, $inc: { count: 1 } }, (err, result) => {
if (err) {
console.log(err);
} else {
}
})
}//그대로 이거나 올랐을때
else {
Coin.updateOne({ name: candle[0].market }, { current_price: candle[0].trade_price, count: 0 }, (err, result) => {
if (err) {
console.log(err);
} else {
}
})
}
})
}
}, 600 * 5);
}, 600 * 5);
}
app.listen(5000, async () => {
console.log('server start')
//coin 이름,가격,거래대금 저장
sort_info = (await sort_data());
//DB 최신화
(await refresh_db());
var t1 = new Array();
test_data = await (repeat_check(t1));
//반복
})