June

Add repeat taker function

...@@ -28,8 +28,9 @@ async function get_marketName() { ...@@ -28,8 +28,9 @@ async function get_marketName() {
28 }) 28 })
29 return data; 29 return data;
30 } 30 }
31 -async function get_marketInfo(name_list) { 31 +async function get_marketInfo() {
32 //각 암호화폐 정보 조회 32 //각 암호화폐 정보 조회
33 + var name_list = await get_marketName();
33 const url2 = `https://api.upbit.com/v1/ticker/?markets=${name_list}`; 34 const url2 = `https://api.upbit.com/v1/ticker/?markets=${name_list}`;
34 var arr = new Array(); 35 var arr = new Array();
35 let response2 = await fetch(url2, options) 36 let response2 = await fetch(url2, options)
...@@ -43,7 +44,8 @@ async function get_marketInfo(name_list) { ...@@ -43,7 +44,8 @@ async function get_marketInfo(name_list) {
43 }) 44 })
44 return arr 45 return arr
45 } 46 }
46 -async function sort_data(arr) { 47 +async function sort_data() {
48 + arr = await get_marketInfo();
47 arr.sort((a, b) => { 49 arr.sort((a, b) => {
48 return b[1] - a[1]; 50 return b[1] - a[1];
49 }) 51 })
...@@ -75,7 +77,6 @@ async function get_candle(minute, market) { ...@@ -75,7 +77,6 @@ async function get_candle(minute, market) {
75 .then(json => candle = json) 77 .then(json => candle = json)
76 return candle; 78 return candle;
77 } 79 }
78 -
79 app.get('/get_market', async (req, res) => { 80 app.get('/get_market', async (req, res) => {
80 var name_list = (await get_marketName()); 81 var name_list = (await get_marketName());
81 var market_info = (await get_marketInfo(name_list)); 82 var market_info = (await get_marketInfo(name_list));
...@@ -90,13 +91,73 @@ app.get('/get_candle', async (req, res) => { ...@@ -90,13 +91,73 @@ app.get('/get_candle', async (req, res) => {
90 get_candle(5, item.name) 91 get_candle(5, item.name)
91 .then(result => { 92 .then(result => {
92 Coin.findOneAndUpdate({ name: result[0].market }, { five_candle: result[0].trade_price }, { new: true }, (err, doc) => { 93 Coin.findOneAndUpdate({ name: result[0].market }, { five_candle: result[0].trade_price }, { new: true }, (err, doc) => {
93 - console.log(doc); 94 + // console.log(doc);
94 }) 95 })
95 }) 96 })
96 }) 97 })
97 }); 98 });
98 99
99 }) 100 })
100 -app.listen(5000, () => { 101 +async function refresh_db() {
101 - console.log('server') 102 + Coin.find()
103 + .then(result => {
104 + if (result.length !== 0) {
105 + Coin.deleteMany({ tid: { $gt: 0 } }, (err, result) => {
106 + if (err) {
107 + console.log(err);
108 + } else {
109 + console.log(result);
110 + }
111 + })
112 + }
113 + save_coin(sort_info);
114 + })
115 +}
116 +async function repeat_check(t1) {
117 + await Coin.find().then(result => {
118 + for (var key in result) {
119 + t1.push(result[key].name)
120 + }
121 + })
122 + setTimeout(() => {
123 + setInterval(async () => {
124 + for (var i = 0; i < t1.length; i++) {
125 + // console.log(t1.length);
126 + // console.log(t1[i]);
127 + var candle = await get_candle(5, t1[i]);
128 + // console.log(i+"번째 코인 가격 "+candle[0].trade_price);
129 + await Coin.findOne({ name: candle[0].market }).then((result) => {
130 +
131 + //가격이 떨어졌을때
132 + if (result.current_price > candle[0].trade_price) {
133 + Coin.updateOne({ name: candle[0].market }, { current_price: candle[0].trade_price, $inc: { count: 1 } }, (err, result) => {
134 + if (err) {
135 + console.log(err);
136 + } else {
137 + }
138 + })
139 + }//그대로 이거나 올랐을때
140 + else {
141 + Coin.updateOne({ name: candle[0].market }, { current_price: candle[0].trade_price, count: 0 }, (err, result) => {
142 + if (err) {
143 + console.log(err);
144 + } else {
145 + }
146 + })
147 +
148 + }
149 + })
150 + }
151 + }, 600 * 5);
152 + }, 600 * 5);
153 +}
154 +app.listen(5000, async () => {
155 + console.log('server start')
156 + //coin 이름,가격,거래대금 저장
157 + sort_info = (await sort_data());
158 + //DB 최신화
159 + (await refresh_db());
160 + var t1 = new Array();
161 + test_data = await (repeat_check(t1));
162 + //반복
102 }) 163 })
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -18,6 +18,10 @@ const coinSchema=mongoose.Schema({ ...@@ -18,6 +18,10 @@ const coinSchema=mongoose.Schema({
18 }, 18 },
19 five_candle:{ 19 five_candle:{
20 type:Number 20 type:Number
21 + },
22 + count:{
23 + type:Number,
24 + default:0
21 } 25 }
22 }) 26 })
23 27
......