Showing
2 changed files
with
163 additions
and
54 deletions
1 | const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); | 1 | const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); |
2 | -const url1 = 'https://api.upbit.com/v1/market/all'; | ||
3 | 2 | ||
4 | const options = { method: 'GET', headers: { Accept: 'application/json' } }; | 3 | const options = { method: 'GET', headers: { Accept: 'application/json' } }; |
5 | const express = require('express'); | 4 | const express = require('express'); |
6 | const app = express(); | 5 | const app = express(); |
7 | const { Coin } = require("./models/Coin"); | 6 | const { Coin } = require("./models/Coin"); |
7 | +const {User}=require('./models/User'); | ||
8 | + | ||
9 | +require("dotenv").config(); | ||
10 | + | ||
11 | +const crypto=require('crypto'); | ||
12 | +const queryEncode=require('querystring').encode; | ||
13 | + | ||
14 | + | ||
15 | + | ||
16 | + | ||
17 | +const request = require('request') | ||
18 | +const {v4} = require("uuid") | ||
19 | +const sign = require('jsonwebtoken').sign | ||
20 | + | ||
8 | 21 | ||
9 | var sort_info = new Array(); | 22 | var sort_info = new Array(); |
10 | const mongoose = require('mongoose'); | 23 | const mongoose = require('mongoose'); |
11 | const config = require('./config/key'); | 24 | const config = require('./config/key'); |
25 | +const { json } = require('express'); | ||
26 | + | ||
12 | const connect = mongoose.connect(config.mongoURI, { | 27 | const connect = mongoose.connect(config.mongoURI, { |
13 | useNewUrlParser: true, useUnifiedTopology: true | 28 | useNewUrlParser: true, useUnifiedTopology: true |
14 | }) | 29 | }) |
... | @@ -16,22 +31,90 @@ const connect = mongoose.connect(config.mongoURI, { | ... | @@ -16,22 +31,90 @@ const connect = mongoose.connect(config.mongoURI, { |
16 | .catch((err) => console.log(err)); | 31 | .catch((err) => console.log(err)); |
17 | 32 | ||
18 | 33 | ||
34 | + | ||
35 | +var korean_name = new Object(); | ||
36 | +const access_key = process.env.access_key; | ||
37 | +const secret_key = process.env.secret_key; | ||
38 | +const server_url = "https://api.upbit.com" | ||
39 | + | ||
40 | +function get_asset(){ | ||
41 | + const payload = { | ||
42 | + access_key: access_key, | ||
43 | + nonce: v4(), | ||
44 | + } | ||
45 | + const token = sign(payload, secret_key) | ||
46 | + const options = { | ||
47 | + method: "GET", | ||
48 | + url: server_url + "/v1/accounts", | ||
49 | + headers: {Authorization: `Bearer ${token}`}, | ||
50 | + } | ||
51 | + return new Promise(resolve=>{ | ||
52 | + request(options,function(err,res,body){ | ||
53 | + if (err) throw new Error(err) | ||
54 | + // test=res.json(); | ||
55 | + data=JSON.parse(body); | ||
56 | + // console.log(data[0].currency) | ||
57 | + data.filter(function(item){ | ||
58 | + if(item.currency=="PLA"){ | ||
59 | + resolve(item); | ||
60 | + } | ||
61 | + }) | ||
62 | + }) | ||
63 | + }) | ||
64 | +} | ||
65 | + | ||
19 | async function get_marketName() { | 66 | async function get_marketName() { |
20 | var data = new Array(); | 67 | var data = new Array(); |
21 | //전체 암호화폐 리스트 불러오기 | 68 | //전체 암호화폐 리스트 불러오기 |
22 | - let response = await fetch(url1, options) | 69 | + let response = await fetch(`${server_url}/v1/market/all`, options) |
23 | .then(res => res.json()) | 70 | .then(res => res.json()) |
24 | .then(json => { | 71 | .then(json => { |
25 | for (i in json) { | 72 | for (i in json) { |
26 | data.push(json[i].market); | 73 | data.push(json[i].market); |
74 | + korean_name[json[i].market] = json[i].korean_name; | ||
27 | } | 75 | } |
28 | }) | 76 | }) |
29 | return data; | 77 | return data; |
30 | } | 78 | } |
79 | +async function transaction_coin(coin_name,side,volume,price,ord_type){ | ||
80 | + const body = { | ||
81 | + market: coin_name, | ||
82 | + side: side, | ||
83 | + volume: volume, | ||
84 | + price: price, | ||
85 | + ord_type: ord_type, | ||
86 | + } | ||
87 | + //시장가 매수인 경우 price를 얼마치 살건지 입력 | ||
88 | + //시장가 매도인경우 volume에 몇개를 팔건지 입력 | ||
89 | + const query = queryEncode(body) | ||
90 | + | ||
91 | + const hash = crypto.createHash('sha512') | ||
92 | + const queryHash = hash.update(query, 'utf-8').digest('hex') | ||
93 | + | ||
94 | + const payload = { | ||
95 | + access_key: access_key, | ||
96 | + nonce: v4(), | ||
97 | + query_hash: queryHash, | ||
98 | + query_hash_alg: 'SHA512', | ||
99 | + } | ||
100 | + | ||
101 | + const token = sign(payload, secret_key) | ||
102 | + | ||
103 | + const options = { | ||
104 | + method: "POST", | ||
105 | + url: server_url + "/v1/orders", | ||
106 | + headers: {Authorization: `Bearer ${token}`}, | ||
107 | + json: body | ||
108 | + } | ||
109 | + request(options, (error, response, body) => { | ||
110 | + if (error) throw new Error(error) | ||
111 | + console.log(body) | ||
112 | + }) | ||
113 | +} | ||
31 | async function get_marketInfo() { | 114 | async function get_marketInfo() { |
32 | //각 암호화폐 정보 조회 | 115 | //각 암호화폐 정보 조회 |
33 | var name_list = await get_marketName(); | 116 | var name_list = await get_marketName(); |
34 | - const url2 = `https://api.upbit.com/v1/ticker/?markets=${name_list}`; | 117 | + const url2 = `${server_url}/v1/ticker/?markets=${name_list}`; |
35 | var arr = new Array(); | 118 | var arr = new Array(); |
36 | let response2 = await fetch(url2, options) | 119 | let response2 = await fetch(url2, options) |
37 | .then(res => res.json()) | 120 | .then(res => res.json()) |
... | @@ -57,6 +140,7 @@ async function save_coin(arr) { | ... | @@ -57,6 +140,7 @@ async function save_coin(arr) { |
57 | const coin = new Coin({ | 140 | const coin = new Coin({ |
58 | tid: i + 1, | 141 | tid: i + 1, |
59 | name: arr[i][0], | 142 | name: arr[i][0], |
143 | + korean_name: korean_name[arr[i][0]], | ||
60 | acc_trade_price_24h: arr[i][1], | 144 | acc_trade_price_24h: arr[i][1], |
61 | current_price: arr[i][2] | 145 | current_price: arr[i][2] |
62 | }); | 146 | }); |
... | @@ -69,35 +153,6 @@ async function save_coin(arr) { | ... | @@ -69,35 +153,6 @@ async function save_coin(arr) { |
69 | } | 153 | } |
70 | return true; | 154 | return true; |
71 | } | 155 | } |
72 | -async function get_candle(minute, market) { | ||
73 | - const url = `https://api.upbit.com/v1/candles/minutes/${minute}?market=${market}&count=1`; | ||
74 | - var candle = new Array(); | ||
75 | - let response = await fetch(url, options) | ||
76 | - .then(res => res.json()) | ||
77 | - .then(json => candle = json) | ||
78 | - return candle; | ||
79 | -} | ||
80 | -app.get('/get_market', async (req, res) => { | ||
81 | - var name_list = (await get_marketName()); | ||
82 | - var market_info = (await get_marketInfo(name_list)); | ||
83 | - sort_info = (await sort_data(market_info)); | ||
84 | - res.json(await save_coin(sort_info)); | ||
85 | - | ||
86 | -}) | ||
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() { | 156 | async function refresh_db() { |
102 | Coin.find() | 157 | Coin.find() |
103 | .then(result => { | 158 | .then(result => { |
... | @@ -113,51 +168,86 @@ async function refresh_db() { | ... | @@ -113,51 +168,86 @@ async function refresh_db() { |
113 | save_coin(sort_info); | 168 | save_coin(sort_info); |
114 | }) | 169 | }) |
115 | } | 170 | } |
116 | -async function repeat_check(t1) { | 171 | +async function get_candle(minute, market) { |
117 | - await Coin.find().then(result => { | 172 | + const url = `https://api.upbit.com/v1/candles/minutes/${minute}?market=${market}&count=1`; |
118 | - for (var key in result) { | 173 | + var candle = new Array(); |
119 | - t1.push(result[key].name) | 174 | + let response = await fetch(url, options) |
120 | - } | 175 | + .then(res => res.json()) |
121 | - }) | 176 | + .then(json => candle = json) |
122 | - setTimeout(() => { | 177 | + return candle; |
123 | - setInterval(async () => { | 178 | +} |
179 | +async function check_coin(t1) { | ||
124 | for (var i = 0; i < t1.length; i++) { | 180 | 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]); | 181 | 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) => { | 182 | await Coin.findOne({ name: candle[0].market }).then((result) => { |
130 | - | ||
131 | //가격이 떨어졌을때 | 183 | //가격이 떨어졌을때 |
132 | if (result.current_price > candle[0].trade_price) { | 184 | 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) => { | 185 | + Coin.findOneAndUpdate({ name: candle[0].market }, { current_price: candle[0].trade_price, $inc: { count: 1 } }, { new: true }, (err, result) => { |
134 | if (err) { | 186 | if (err) { |
135 | console.log(err); | 187 | console.log(err); |
136 | } else { | 188 | } else { |
189 | + console.log("***" + result.korean_name + "은(는)" + result.count * 5 + "분 동안 하락중"); | ||
190 | + if(count>=3){ | ||
191 | + transaction_coin(result.name,"bid",null,"얼마치 살건지","price"); | ||
192 | + } | ||
137 | } | 193 | } |
138 | }) | 194 | }) |
139 | }//그대로 이거나 올랐을때 | 195 | }//그대로 이거나 올랐을때 |
140 | else { | 196 | else { |
141 | - Coin.updateOne({ name: candle[0].market }, { current_price: candle[0].trade_price, count: 0 }, (err, result) => { | 197 | + Coin.findOneAndUpdate({ name: candle[0].market }, { current_price: candle[0].trade_price, count: 0 }, { new: true }, (err, result) => { |
142 | if (err) { | 198 | if (err) { |
143 | console.log(err); | 199 | console.log(err); |
144 | } else { | 200 | } else { |
201 | + //특정 조건... | ||
202 | + transaction_coin(result.name,"ask","몇개를 팔건지",null,"market"); | ||
203 | + console.log(result.korean_name + "은(는)" + result.count * 5 + "분 동안 상승 혹은 정체중"); | ||
145 | } | 204 | } |
146 | }) | 205 | }) |
147 | 206 | ||
148 | } | 207 | } |
149 | }) | 208 | }) |
150 | } | 209 | } |
151 | - }, 600 * 5); | ||
152 | - }, 600 * 5); | ||
153 | } | 210 | } |
154 | -app.listen(5000, async () => { | 211 | +async function latest_repeat(t1) { |
155 | - console.log('server start') | 212 | + await Coin.find().sort({ tid: 1 }).then(result => { |
156 | - //coin 이름,가격,거래대금 저장 | 213 | + for (var key in result) { |
214 | + t1.push(result[key].name) | ||
215 | + } | ||
216 | + }) | ||
217 | + let check_time = setInterval(async () => { | ||
218 | + let today = new Date(); | ||
219 | + let minutes = today.getMinutes(); | ||
220 | + let seconds = today.getSeconds(); | ||
221 | + // if (seconds == 0) { | ||
222 | + if (minutes == 0 && seconds == 0) { | ||
223 | + clearInterval(check_time); | ||
157 | sort_info = (await sort_data()); | 224 | sort_info = (await sort_data()); |
158 | - //DB 최신화 | ||
159 | (await refresh_db()); | 225 | (await refresh_db()); |
226 | + console.log("현재 시간은 " + today.toLocaleTimeString()); | ||
227 | + var count=0; | ||
228 | + let coin=setInterval(async () => { | ||
229 | + let today = new Date(); | ||
230 | + let minutes=today.getMinutes(); | ||
231 | + let seconds=today.getSeconds(); | ||
232 | + console.log("현재 시간은 " + today.toLocaleTimeString()); | ||
233 | + await (check_coin(t1).then(count++)); | ||
234 | + //1시간마다 db 최신화... | ||
235 | + if(count==12){ | ||
236 | + count=0; | ||
237 | + sort_info=(await sort_data()); | ||
238 | + (await refresh_db()); | ||
239 | + console.log("db최신화"); | ||
240 | + } | ||
241 | + }, 60000*5); | ||
242 | + } | ||
243 | + }, 1000); | ||
244 | +} | ||
245 | +app.listen(5000, async () => { | ||
246 | + console.log('server start') | ||
247 | + //coin 이름,가격,거래대금 저장 , DB 최신화 1시간마다 반복 | ||
248 | + //5분마다 현재 가격 가져와서 db랑 비교후 매수 매도 기준잡기 | ||
160 | var t1 = new Array(); | 249 | var t1 = new Array(); |
161 | - test_data = await (repeat_check(t1)); | 250 | + test_data=await (latest_repeat(t1)); |
162 | - //반복 | 251 | + //계좌 정보 db 최신화 |
252 | + console.log(await get_asset()); | ||
163 | }) | 253 | }) |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
models/User.js
0 → 100644
1 | +const mongoose=require('mongoose'); | ||
2 | + | ||
3 | +const userSchema=mongoose.Schema({ | ||
4 | + krw_balance:{ | ||
5 | + type:Number, | ||
6 | + }, | ||
7 | + market:{ | ||
8 | + type:String, | ||
9 | + }, | ||
10 | + count:{ | ||
11 | + type:Number, | ||
12 | + }, | ||
13 | + avg_buy_price:{ | ||
14 | + type:Number | ||
15 | + } | ||
16 | +}) | ||
17 | + | ||
18 | +const User=mongoose.model("User",userSchema); | ||
19 | +module.exports={User}; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment