2019102152 김다빈

Add latest repeat function && Modify coinSchema

......@@ -15,6 +15,7 @@ const connect = mongoose.connect(config.mongoURI, {
.then(() => console.log('디비연결 성공'))
.catch((err) => console.log(err));
var korean_name = new Object();
async function get_marketName() {
var data = new Array();
......@@ -24,6 +25,7 @@ async function get_marketName() {
.then(json => {
for (i in json) {
data.push(json[i].market);
korean_name[json[i].market] = json[i].korean_name;
}
})
return data;
......@@ -57,6 +59,7 @@ async function save_coin(arr) {
const coin = new Coin({
tid: i + 1,
name: arr[i][0],
korean_name: korean_name[arr[i][0]],
acc_trade_price_24h: arr[i][1],
current_price: arr[i][2]
});
......@@ -69,6 +72,21 @@ async function save_coin(arr) {
}
return true;
}
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 get_candle(minute, market) {
const url = `https://api.upbit.com/v1/candles/minutes/${minute}?market=${market}&count=1`;
var candle = new Array();
......@@ -77,87 +95,92 @@ async function get_candle(minute, market) {
.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) => {
async function check_coin(t1) {
for (var i = 0; i < t1.length; i++) {
var candle = await get_candle(5, t1[i]);
await Coin.findOne({ name: candle[0].market }).then((result) => {
//가격이 떨어졌을때
if (result.current_price > candle[0].trade_price) {
Coin.findOneAndUpdate({ name: candle[0].market }, { current_price: candle[0].trade_price, $inc: { count: 1 } }, { new: true }, (err, result) => {
if (err) {
console.log(err);
} else {
console.log(result);
console.log("***" + result.korean_name + "은(는)" + result.count * 5 + "분 동안 하락중");
}
})
}//그대로 이거나 올랐을때
else {
Coin.findOneAndUpdate({ name: candle[0].market }, { current_price: candle[0].trade_price, count: 0 }, { new: true }, (err, result) => {
if (err) {
console.log(err);
} else {
console.log(result.korean_name + "은(는)" + result.count * 5 + "분 동안 상승 혹은 정체중");
}
})
}
save_coin(sort_info);
})
}
}
async function repeat_check(t1) {
await Coin.find().then(result => {
// async function repeat_check(t1) {
// await Coin.find().sort({ tid: 1 }).then(result => {
// for (var key in result) {
// t1.push(result[key].name)
// }
// })
// let check_time = setInterval(() => {
// let today = new Date();
// let minutes = today.getMinutes();
// let seconds = today.getSeconds();
// if (minutes % 5 == 0 && seconds == 0) {
// clearInterval(check_time);
// console.log("현재 시간은 " + today.toLocaleTimeString());
// check_coin(t1);
// setInterval(async () => {
// let today = new Date();
// console.log("현재 시간은 " + today.toLocaleTimeString());
// check_coin(t1);
// }, 60000 * 5);
// }
// }, 1000)
// }
async function latest_repeat(t1) {
await Coin.find().sort({ tid: 1 }).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);
let check_time = setInterval(async () => {
let today = new Date();
let minutes = today.getMinutes();
let seconds = today.getSeconds();
if (seconds == 0) {
// if (minutes == 0 && seconds == 0) {
clearInterval(check_time);
sort_info = (await sort_data());
(await refresh_db());
console.log("현재 시간은 " + today.toLocaleTimeString());
let coin=setInterval(async () => {
let today = new Date();
let minutes=today.getMinutes();
let seconds=today.getSeconds();
console.log("현재 시간은 " + today.toLocaleTimeString());
await (check_coin(t1));
if(minutes==0&&seconds==0){
sort_info = (await sort_data());
(await refresh_db());
console.log("db 최신화");
}
}, 60000*5);
}
}, 1000);
}
app.listen(5000, async () => {
console.log('server start')
//coin 이름,가격,거래대금 저장
sort_info = (await sort_data());
//DB 최신화
(await refresh_db());
//coin 이름,가격,거래대금 저장 , DB 최신화 1시간마다 반복
//5분마다 현재 가격 가져와서 db랑 비교후 매수 매도 기준잡기
var t1 = new Array();
test_data = await (repeat_check(t1));
test_data=await (latest_repeat(t1));
//반복
})
\ No newline at end of file
......
......@@ -8,6 +8,10 @@ const coinSchema=mongoose.Schema({
type:String,
required:true
},
korean_name:{
type:String,
required:true
},
acc_trade_price_24h:{
type:Number,
required:true
......@@ -16,9 +20,6 @@ const coinSchema=mongoose.Schema({
type:Number,
required:true
},
five_candle:{
type:Number
},
count:{
type:Number,
default:0
......