2019102152 김다빈

Add latest repeat function && Modify coinSchema

...@@ -15,6 +15,7 @@ const connect = mongoose.connect(config.mongoURI, { ...@@ -15,6 +15,7 @@ const connect = mongoose.connect(config.mongoURI, {
15 .then(() => console.log('디비연결 성공')) 15 .then(() => console.log('디비연결 성공'))
16 .catch((err) => console.log(err)); 16 .catch((err) => console.log(err));
17 17
18 +var korean_name = new Object();
18 19
19 async function get_marketName() { 20 async function get_marketName() {
20 var data = new Array(); 21 var data = new Array();
...@@ -24,6 +25,7 @@ async function get_marketName() { ...@@ -24,6 +25,7 @@ async function get_marketName() {
24 .then(json => { 25 .then(json => {
25 for (i in json) { 26 for (i in json) {
26 data.push(json[i].market); 27 data.push(json[i].market);
28 + korean_name[json[i].market] = json[i].korean_name;
27 } 29 }
28 }) 30 })
29 return data; 31 return data;
...@@ -57,6 +59,7 @@ async function save_coin(arr) { ...@@ -57,6 +59,7 @@ async function save_coin(arr) {
57 const coin = new Coin({ 59 const coin = new Coin({
58 tid: i + 1, 60 tid: i + 1,
59 name: arr[i][0], 61 name: arr[i][0],
62 + korean_name: korean_name[arr[i][0]],
60 acc_trade_price_24h: arr[i][1], 63 acc_trade_price_24h: arr[i][1],
61 current_price: arr[i][2] 64 current_price: arr[i][2]
62 }); 65 });
...@@ -69,6 +72,21 @@ async function save_coin(arr) { ...@@ -69,6 +72,21 @@ async function save_coin(arr) {
69 } 72 }
70 return true; 73 return true;
71 } 74 }
75 +async function refresh_db() {
76 + Coin.find()
77 + .then(result => {
78 + if (result.length !== 0) {
79 + Coin.deleteMany({ tid: { $gt: 0 } }, (err, result) => {
80 + if (err) {
81 + console.log(err);
82 + } else {
83 + console.log(result);
84 + }
85 + })
86 + }
87 + save_coin(sort_info);
88 + })
89 +}
72 async function get_candle(minute, market) { 90 async function get_candle(minute, market) {
73 const url = `https://api.upbit.com/v1/candles/minutes/${minute}?market=${market}&count=1`; 91 const url = `https://api.upbit.com/v1/candles/minutes/${minute}?market=${market}&count=1`;
74 var candle = new Array(); 92 var candle = new Array();
...@@ -77,87 +95,92 @@ async function get_candle(minute, market) { ...@@ -77,87 +95,92 @@ async function get_candle(minute, market) {
77 .then(json => candle = json) 95 .then(json => candle = json)
78 return candle; 96 return candle;
79 } 97 }
80 -app.get('/get_market', async (req, res) => { 98 +async function check_coin(t1) {
81 - var name_list = (await get_marketName()); 99 + for (var i = 0; i < t1.length; i++) {
82 - var market_info = (await get_marketInfo(name_list)); 100 + var candle = await get_candle(5, t1[i]);
83 - sort_info = (await sort_data(market_info)); 101 + await Coin.findOne({ name: candle[0].market }).then((result) => {
84 - res.json(await save_coin(sort_info)); 102 + //가격이 떨어졌을때
85 - 103 + if (result.current_price > candle[0].trade_price) {
86 -}) 104 + Coin.findOneAndUpdate({ name: candle[0].market }, { current_price: candle[0].trade_price, $inc: { count: 1 } }, { new: true }, (err, result) => {
87 -app.get('/get_candle', async (req, res) => {
88 - Coin.find()
89 - .then(result => {
90 - result.forEach((item) => {
91 - get_candle(5, item.name)
92 - .then(result => {
93 - Coin.findOneAndUpdate({ name: result[0].market }, { five_candle: result[0].trade_price }, { new: true }, (err, doc) => {
94 - // console.log(doc);
95 - })
96 - })
97 - })
98 - });
99 -
100 -})
101 -async function refresh_db() {
102 - Coin.find()
103 - .then(result => {
104 - if (result.length !== 0) {
105 - Coin.deleteMany({ tid: { $gt: 0 } }, (err, result) => {
106 if (err) { 105 if (err) {
107 console.log(err); 106 console.log(err);
108 } else { 107 } else {
109 - console.log(result); 108 + console.log("***" + result.korean_name + "은(는)" + result.count * 5 + "분 동안 하락중");
109 + }
110 + })
111 + }//그대로 이거나 올랐을때
112 + else {
113 + Coin.findOneAndUpdate({ name: candle[0].market }, { current_price: candle[0].trade_price, count: 0 }, { new: true }, (err, result) => {
114 + if (err) {
115 + console.log(err);
116 + } else {
117 + console.log(result.korean_name + "은(는)" + result.count * 5 + "분 동안 상승 혹은 정체중");
110 } 118 }
111 }) 119 })
120 +
112 } 121 }
113 - save_coin(sort_info);
114 }) 122 })
123 + }
115 } 124 }
116 -async function repeat_check(t1) { 125 +// async function repeat_check(t1) {
117 - await Coin.find().then(result => { 126 +// await Coin.find().sort({ tid: 1 }).then(result => {
127 +// for (var key in result) {
128 +// t1.push(result[key].name)
129 +// }
130 +// })
131 +// let check_time = setInterval(() => {
132 +// let today = new Date();
133 +// let minutes = today.getMinutes();
134 +// let seconds = today.getSeconds();
135 +// if (minutes % 5 == 0 && seconds == 0) {
136 +// clearInterval(check_time);
137 +// console.log("현재 시간은 " + today.toLocaleTimeString());
138 +// check_coin(t1);
139 +// setInterval(async () => {
140 +// let today = new Date();
141 +// console.log("현재 시간은 " + today.toLocaleTimeString());
142 +// check_coin(t1);
143 +// }, 60000 * 5);
144 +// }
145 +// }, 1000)
146 +
147 +// }
148 +async function latest_repeat(t1) {
149 + await Coin.find().sort({ tid: 1 }).then(result => {
118 for (var key in result) { 150 for (var key in result) {
119 t1.push(result[key].name) 151 t1.push(result[key].name)
120 } 152 }
121 }) 153 })
122 - setTimeout(() => { 154 + let check_time = setInterval(async () => {
123 - setInterval(async () => { 155 + let today = new Date();
124 - for (var i = 0; i < t1.length; i++) { 156 + let minutes = today.getMinutes();
125 - // console.log(t1.length); 157 + let seconds = today.getSeconds();
126 - // console.log(t1[i]); 158 + if (seconds == 0) {
127 - var candle = await get_candle(5, t1[i]); 159 + // if (minutes == 0 && seconds == 0) {
128 - // console.log(i+"번째 코인 가격 "+candle[0].trade_price); 160 + clearInterval(check_time);
129 - await Coin.findOne({ name: candle[0].market }).then((result) => { 161 + sort_info = (await sort_data());
130 - 162 + (await refresh_db());
131 - //가격이 떨어졌을때 163 + console.log("현재 시간은 " + today.toLocaleTimeString());
132 - if (result.current_price > candle[0].trade_price) { 164 + let coin=setInterval(async () => {
133 - Coin.updateOne({ name: candle[0].market }, { current_price: candle[0].trade_price, $inc: { count: 1 } }, (err, result) => { 165 + let today = new Date();
134 - if (err) { 166 + let minutes=today.getMinutes();
135 - console.log(err); 167 + let seconds=today.getSeconds();
136 - } else { 168 + console.log("현재 시간은 " + today.toLocaleTimeString());
137 - } 169 + await (check_coin(t1));
138 - }) 170 + if(minutes==0&&seconds==0){
139 - }//그대로 이거나 올랐을때 171 + sort_info = (await sort_data());
140 - else { 172 + (await refresh_db());
141 - Coin.updateOne({ name: candle[0].market }, { current_price: candle[0].trade_price, count: 0 }, (err, result) => { 173 + console.log("db 최신화");
142 - if (err) { 174 + }
143 - console.log(err); 175 + }, 60000*5);
144 - } else { 176 + }
145 - } 177 + }, 1000);
146 - })
147 -
148 - }
149 - })
150 - }
151 - }, 600 * 5);
152 - }, 600 * 5);
153 } 178 }
154 app.listen(5000, async () => { 179 app.listen(5000, async () => {
155 console.log('server start') 180 console.log('server start')
156 - //coin 이름,가격,거래대금 저장 181 + //coin 이름,가격,거래대금 저장 , DB 최신화 1시간마다 반복
157 - sort_info = (await sort_data()); 182 + //5분마다 현재 가격 가져와서 db랑 비교후 매수 매도 기준잡기
158 - //DB 최신화
159 - (await refresh_db());
160 var t1 = new Array(); 183 var t1 = new Array();
161 - test_data = await (repeat_check(t1)); 184 + test_data=await (latest_repeat(t1));
162 //반복 185 //반복
163 }) 186 })
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -8,6 +8,10 @@ const coinSchema=mongoose.Schema({ ...@@ -8,6 +8,10 @@ const coinSchema=mongoose.Schema({
8 type:String, 8 type:String,
9 required:true 9 required:true
10 }, 10 },
11 + korean_name:{
12 + type:String,
13 + required:true
14 + },
11 acc_trade_price_24h:{ 15 acc_trade_price_24h:{
12 type:Number, 16 type:Number,
13 required:true 17 required:true
...@@ -16,9 +20,6 @@ const coinSchema=mongoose.Schema({ ...@@ -16,9 +20,6 @@ const coinSchema=mongoose.Schema({
16 type:Number, 20 type:Number,
17 required:true 21 required:true
18 }, 22 },
19 - five_candle:{
20 - type:Number
21 - },
22 count:{ 23 count:{
23 type:Number, 24 type:Number,
24 default:0 25 default:0
......