Showing
2 changed files
with
142 additions
and
87 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 | - | ||
3 | const options = { method: 'GET', headers: { Accept: 'application/json' } }; | 2 | const options = { method: 'GET', headers: { Accept: 'application/json' } }; |
4 | const express = require('express'); | 3 | const express = require('express'); |
5 | const app = express(); | 4 | const app = express(); |
6 | const { Coin } = require("./models/Coin"); | 5 | const { Coin } = require("./models/Coin"); |
7 | const { User } = require('./models/User'); | 6 | const { User } = require('./models/User'); |
8 | require("dotenv").config(); | 7 | require("dotenv").config(); |
9 | - | 8 | +const fs = require('fs'); |
9 | +const path = require('path'); | ||
10 | +const HTTPS = require('https'); | ||
11 | +const domain = "2019102152.osschatbot.ga" | ||
12 | +const sslport = 23023; | ||
13 | +const bodyParser = require('body-parser'); | ||
10 | const crypto = require('crypto'); | 14 | const crypto = require('crypto'); |
11 | const queryEncode = require('querystring').encode; | 15 | const queryEncode = require('querystring').encode; |
12 | const request = require('request-promise-native'); | 16 | const request = require('request-promise-native'); |
13 | -// const request = require('request') | ||
14 | const { v4 } = require("uuid") | 17 | const { v4 } = require("uuid") |
15 | const sign = require('jsonwebtoken').sign | 18 | const sign = require('jsonwebtoken').sign |
19 | +const access_key = process.env.access_key; | ||
20 | +const secret_key = process.env.secret_key; | ||
21 | +const server_url = "https://api.upbit.com" | ||
22 | + | ||
23 | + | ||
16 | var krw_balance = 60000; | 24 | var krw_balance = 60000; |
17 | var divided_money = krw_balance / 10; | 25 | var divided_money = krw_balance / 10; |
18 | 26 | ||
19 | var sort_info = new Array(); | 27 | var sort_info = new Array(); |
20 | const mongoose = require('mongoose'); | 28 | const mongoose = require('mongoose'); |
21 | const config = require('./config/key'); | 29 | const config = require('./config/key'); |
22 | -const { json } = require('express'); | ||
23 | -const e = require('express'); | ||
24 | 30 | ||
25 | const connect = mongoose.connect(config.mongoURI, { | 31 | const connect = mongoose.connect(config.mongoURI, { |
26 | useNewUrlParser: true, useUnifiedTopology: true | 32 | useNewUrlParser: true, useUnifiedTopology: true |
... | @@ -28,12 +34,11 @@ const connect = mongoose.connect(config.mongoURI, { | ... | @@ -28,12 +34,11 @@ const connect = mongoose.connect(config.mongoURI, { |
28 | .then(() => console.log('디비연결 성공')) | 34 | .then(() => console.log('디비연결 성공')) |
29 | .catch((err) => console.log(err)); | 35 | .catch((err) => console.log(err)); |
30 | 36 | ||
37 | +app.use(bodyParser.json()); | ||
38 | +var korean_name = new Object(); | ||
39 | + | ||
31 | 40 | ||
32 | 41 | ||
33 | -var korean_name = new Object(); | ||
34 | -const access_key = process.env.access_key; | ||
35 | -const secret_key = process.env.secret_key; | ||
36 | -const server_url = "https://api.upbit.com" | ||
37 | 42 | ||
38 | function get_asset(market) { | 43 | function get_asset(market) { |
39 | const payload = { | 44 | const payload = { |
... | @@ -51,15 +56,19 @@ function get_asset(market) { | ... | @@ -51,15 +56,19 @@ function get_asset(market) { |
51 | if (err) throw new Error(err) | 56 | if (err) throw new Error(err) |
52 | var empty = new Object(); | 57 | var empty = new Object(); |
53 | data = JSON.parse(body); | 58 | data = JSON.parse(body); |
54 | - data.filter(function (item) { | 59 | + if (market) { |
55 | - if (item.currency == market.split('-')[1]) { | 60 | + data.filter(function (item) { |
56 | - // resolve(item); | 61 | + if (item.currency == market.split('-')[1]) { |
57 | - empty.market = item; | 62 | + // resolve(item); |
58 | - } else if (item.currency == "KRW") { | 63 | + empty.market = item; |
59 | - empty.KRW = item | 64 | + } else if (item.currency == "KRW") { |
60 | - } | 65 | + empty.KRW = item |
61 | - resolve(empty); | 66 | + } |
62 | - }) | 67 | + resolve(empty); |
68 | + }) | ||
69 | + } else { | ||
70 | + resolve(data); | ||
71 | + } | ||
63 | }) | 72 | }) |
64 | }) | 73 | }) |
65 | return result(market); | 74 | return result(market); |
... | @@ -77,13 +86,13 @@ async function get_marketName() { | ... | @@ -77,13 +86,13 @@ async function get_marketName() { |
77 | }) | 86 | }) |
78 | return data; | 87 | return data; |
79 | } | 88 | } |
80 | -async function transaction_coin(coin_name, side, volume, price, ord_type, first = false) { | 89 | +async function transaction_coin(coin_name, side, volume, price, ord_type, first = true) { |
81 | - var volume=volume; | 90 | + var volume = volume; |
82 | - if(side=="ask"){ | 91 | + if (side == "ask") { |
83 | - await User.findOne({uid:1}).then((result)=>{ | 92 | + await User.findOne({ uid: 1 }).then((result) => { |
84 | console.log(result.volume); | 93 | console.log(result.volume); |
85 | - volume=String(result.volume); | 94 | + volume = String(result.volume); |
86 | - }).catch(err=>{console.log(err.error)}); | 95 | + }).catch(err => { console.log(err.error) }); |
87 | } | 96 | } |
88 | const body = { | 97 | const body = { |
89 | market: coin_name, | 98 | market: coin_name, |
... | @@ -111,21 +120,39 @@ async function transaction_coin(coin_name, side, volume, price, ord_type, first | ... | @@ -111,21 +120,39 @@ async function transaction_coin(coin_name, side, volume, price, ord_type, first |
111 | json: body | 120 | json: body |
112 | } | 121 | } |
113 | await request(options).then(async (result) => { | 122 | await request(options).then(async (result) => { |
114 | - const asset=(delay)=>new Promise((resolve)=>{ | 123 | + const asset = (delay) => new Promise((resolve) => { |
115 | - setTimeout(async ()=>{ | 124 | + setTimeout(async () => { |
116 | resolve(await get_asset(result.market)); | 125 | resolve(await get_asset(result.market)); |
117 | - },delay) | 126 | + }, delay) |
118 | }) | 127 | }) |
119 | - var my_asset=await asset(1000); | 128 | + var my_asset = await asset(1000); |
120 | - if(side=="bid"){ | 129 | + if (side == "bid") { |
121 | - const user = new User({ uid:1,krw_balance: my_asset.KRW.balance, market: coin_name, count: 1, avg_price: my_asset.market.avg_buy_price,volume:my_asset.market.balance }); | 130 | + //처음 매수일 때 |
122 | - await user.save().then(() => { isuser = true }) | 131 | + if (first) { |
123 | - }else if(side=="ask"){ | 132 | + const user = new User({ uid: 1, krw_balance: my_asset.KRW.balance, market: coin_name, count: 1, avg_buy_price: my_asset.market.avg_buy_price, volume: my_asset.market.balance }); |
124 | - User.deleteOne({uid:1},(err,res)=>{ | 133 | + await user.save().then(() => { isuser = true }) |
125 | - if(err){ | 134 | + //2회 이상 매수일 때 |
126 | - console.log(err); | 135 | + } else { |
127 | - } | 136 | + User.findOneAndUpdate({uid:1},{krw_balance:my_asset.KRW.balance,avg_buy_price: my_asset.market.avg_buy_price, volume: my_asset.market.balance,$inc: { count: 1 }},{new :true},(err,result)=>{ |
128 | - }); | 137 | + if(err){ |
138 | + console.log(err); | ||
139 | + }else{ | ||
140 | + console.log(result); | ||
141 | + } | ||
142 | + }) | ||
143 | + } | ||
144 | + } else if (side == "ask") { | ||
145 | + //1회 매수 후 매도일 때 | ||
146 | + if (first) { | ||
147 | + User.deleteOne({ uid: 1 }, (err, res) => { | ||
148 | + if (err) { | ||
149 | + console.log(err); | ||
150 | + } | ||
151 | + }); | ||
152 | + //분할 매수 분할 매도 중일 때 | ||
153 | + } else { | ||
154 | + | ||
155 | + } | ||
129 | } | 156 | } |
130 | }).catch((err) => { console.log(err.error) }) | 157 | }).catch((err) => { console.log(err.error) }) |
131 | } | 158 | } |
... | @@ -194,42 +221,66 @@ async function get_candle(minute, market) { | ... | @@ -194,42 +221,66 @@ async function get_candle(minute, market) { |
194 | .then(json => candle = json) | 221 | .then(json => candle = json) |
195 | return candle; | 222 | return candle; |
196 | } | 223 | } |
224 | +async function price_comparison(candle, avg_buy_price = 0) { | ||
225 | + var isbuying=true; | ||
226 | + if (avg_buy_price == 0) { | ||
227 | + await Coin.findOne({ name: candle[0].market }).then(async (result) => { | ||
228 | + //가격이 떨어졌을때 | ||
229 | + if (result.current_price > candle[0].trade_price) { | ||
230 | + Coin.findOneAndUpdate({ name: candle[0].market }, { current_price: candle[0].trade_price, $inc: { count: 1 } }, { new: true }, (err, result) => { | ||
231 | + if (err) { | ||
232 | + console.log(err); | ||
233 | + } else { | ||
234 | + console.log("***" + result.korean_name + "은(는)" + result.count * 5 + "분 동안 하락중"); | ||
235 | + if (result.count >= 3) { | ||
236 | + transaction_coin(result.name, "bid", null, divided_money, "price"); | ||
237 | + isbuying=false; | ||
238 | + } | ||
239 | + } | ||
240 | + }) | ||
241 | + } else if (result.current_price < candle[0].trade_price) { | ||
242 | + await Coin.findOneAndUpdate({ name: candle[0].market }, { current_price: candle[0].trade_price, count: 0 }, { new: true }).then(async (err, result) => { | ||
243 | + if (err) { | ||
244 | + console.log(err); | ||
245 | + } else { | ||
246 | + console.log(result.korean_name + "은(는)" + result.count * 5 + "분 동안 상승 혹은 정체중"); | ||
247 | + } | ||
248 | + }) | ||
249 | + } | ||
250 | + | ||
251 | + }) | ||
252 | + } else { | ||
253 | + if (avg_buy_price < candle[0].trade_price) { | ||
254 | + transaction_coin(candle[0].market, "bid", null, divided_money, "price", false) | ||
255 | + } else { | ||
256 | + transaction_coin(candle[0].market, "ask", "", null, "market", false); | ||
257 | + } | ||
258 | + } | ||
259 | + return isbuying; | ||
260 | +} | ||
197 | async function check_coin(t1) { | 261 | async function check_coin(t1) { |
198 | User.find().then(async (user_data) => { | 262 | User.find().then(async (user_data) => { |
263 | + //아직 매수한 코인이 없을 때 | ||
199 | if (user_data.length == 0) { | 264 | if (user_data.length == 0) { |
200 | - var isuser = false; | 265 | + var isbuying = false; |
201 | for (var i = 0; i < t1.length; i++) { | 266 | for (var i = 0; i < t1.length; i++) { |
202 | - if (!isuser) { | 267 | + if (isbuying) { |
203 | var candle = await get_candle(5, t1[i]); | 268 | var candle = await get_candle(5, t1[i]); |
204 | - await Coin.findOne({ name: candle[0].market }).then(async (result) => { | 269 | + isbuying=await price_comparison(candle); |
205 | - //가격이 떨어졌을때 | 270 | + console.log(isbuying); |
206 | - if (result.current_price > candle[0].trade_price) { | ||
207 | - Coin.findOneAndUpdate({ name: candle[0].market }, { current_price: candle[0].trade_price, $inc: { count: 1 } }, { new: true }, (err, result) => { | ||
208 | - if (err) { | ||
209 | - console.log(err); | ||
210 | - } else { | ||
211 | - console.log("***" + result.korean_name + "은(는)" + result.count * 5 + "분 동안 하락중"); | ||
212 | - if (result.count >= 3) { | ||
213 | - transaction_coin(result.name, "bid", null, divided_money, "price"); | ||
214 | - } | ||
215 | - } | ||
216 | - }) | ||
217 | - }//그대로 이거나 올랐을때 | ||
218 | - else { | ||
219 | - await Coin.findOneAndUpdate({ name: candle[0].market }, { current_price: candle[0].trade_price, count: 0 }, { new: true }).then(async (result) => { | ||
220 | - //특정 조건... | ||
221 | - const user = new User({ krw_balance: 100, market: "KRW-BTC", count: 1, avg_price: 100 }); | ||
222 | - await user.save().then(() => { isuser = true }) | ||
223 | - console.log(result.korean_name + "은(는)" + result.count * 5 + "분 동안 상승 혹은 정체중"); | ||
224 | - }) | ||
225 | - } | ||
226 | - }) | ||
227 | - } else { | ||
228 | - console.log("메롱"); | ||
229 | } | 271 | } |
230 | } | 272 | } |
273 | + //매수한 코인이 있을 때 | ||
231 | } else { | 274 | } else { |
232 | - console.log(user_data); | 275 | + //3회 이상 매수 했을시 15분봉으로 교체해서 가격 확인 |
276 | + //15분봉 처리해야함 | ||
277 | + if (user_data[0].count > 3) { | ||
278 | + var candle = await get_candle(15, user_data[0].market); | ||
279 | + await price_comparison(candle, user_data[0].avg_buy_price); | ||
280 | + } else { | ||
281 | + var candle = await get_candle(5, user_data[0].market); | ||
282 | + await price_comparison(candle, user_data[0].avg_buy_price); | ||
283 | + } | ||
233 | } | 284 | } |
234 | }).catch((err) => { | 285 | }).catch((err) => { |
235 | console.log(err); | 286 | console.log(err); |
... | @@ -237,20 +288,20 @@ async function check_coin(t1) { | ... | @@ -237,20 +288,20 @@ async function check_coin(t1) { |
237 | 288 | ||
238 | } | 289 | } |
239 | async function latest_repeat(t1) { | 290 | async function latest_repeat(t1) { |
240 | - await Coin.find().sort({ tid: 1 }).then(result => { | ||
241 | - for (var key in result) { | ||
242 | - t1.push(result[key].name) | ||
243 | - } | ||
244 | - }) | ||
245 | let check_time = setInterval(async () => { | 291 | let check_time = setInterval(async () => { |
246 | let today = new Date(); | 292 | let today = new Date(); |
247 | let minutes = today.getMinutes(); | 293 | let minutes = today.getMinutes(); |
248 | let seconds = today.getSeconds(); | 294 | let seconds = today.getSeconds(); |
249 | - if (seconds == 0) { | 295 | + // if (seconds == 0) { |
250 | - // if (minutes == 0 && seconds == 0) { | 296 | + if (minutes == 0 && seconds == 0) { |
251 | clearInterval(check_time); | 297 | clearInterval(check_time); |
252 | sort_info = (await sort_data()); | 298 | sort_info = (await sort_data()); |
253 | (await refresh_db()); | 299 | (await refresh_db()); |
300 | + Coin.find().sort({ tid: 1 }).then(result => { | ||
301 | + for (var key in result) { | ||
302 | + t1.push(result[key].name) | ||
303 | + } | ||
304 | + }) | ||
254 | console.log("현재 시간은 " + today.toLocaleTimeString()); | 305 | console.log("현재 시간은 " + today.toLocaleTimeString()); |
255 | var count = 0; | 306 | var count = 0; |
256 | let coin = setInterval(async () => { | 307 | let coin = setInterval(async () => { |
... | @@ -266,20 +317,24 @@ async function latest_repeat(t1) { | ... | @@ -266,20 +317,24 @@ async function latest_repeat(t1) { |
266 | (await refresh_db()); | 317 | (await refresh_db()); |
267 | console.log("db최신화"); | 318 | console.log("db최신화"); |
268 | } | 319 | } |
269 | - }, 600 * 5); | 320 | + }, 60000 * 5); |
270 | } | 321 | } |
271 | }, 1000); | 322 | }, 1000); |
272 | } | 323 | } |
273 | -app.listen(5000, async () => { | ||
274 | - console.log('server start') | ||
275 | - //coin 이름,가격,거래대금 저장 , DB 최신화 1시간마다 반복 | ||
276 | - //5분마다 현재 가격 가져와서 db랑 비교후 매수 매도 기준잡기 | ||
277 | - // var t1 = new Array(); | ||
278 | - // test_data = await (latest_repeat(t1)); | ||
279 | - //계좌 정보 db 최신화 | ||
280 | - // console.log(await get_asset()); | ||
281 | - //매도 | ||
282 | - await transaction_coin("KRW-BTC","ask","",null,"market"); | ||
283 | - //매수 | ||
284 | - // await transaction_coin("KRW-BTC", "bid", null, divided_money, "price"); | ||
285 | -}) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
324 | +try { | ||
325 | + const option = { | ||
326 | + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), | ||
327 | + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), | ||
328 | + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), | ||
329 | + }; | ||
330 | + | ||
331 | + HTTPS.createServer(option, app).listen(sslport, async () => { | ||
332 | + console.log(`[HTTPS] Server is started on port ${sslport}`); | ||
333 | + var t1 = new Array(); | ||
334 | + await (latest_repeat(t1)); | ||
335 | + // console.log(await get_asset()); | ||
336 | + }); | ||
337 | +} catch (error) { | ||
338 | + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); | ||
339 | + console.log(error); | ||
340 | +} | ... | ... |
-
Please register or login to post a comment