Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Crypto
/
Crypto-auto-trading
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
2019102152 김다빈
2021-12-03 03:35:14 +0000
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
edd4ba78f682691b853d57525fba58266090d7e8
edd4ba78
1 parent
d490040b
Add buying algorithm
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
142 additions
and
87 deletions
README.md
index.js
README.md
View file @
edd4ba7
...
...
@@ -4,4 +4,4 @@
module.exports = {
mongoURI:{mongoURI}
}
.env 파일 생성 후 access_key와 secret_key 할당 필요
...
...
index.js
View file @
edd4ba7
const
fetch
=
(...
args
)
=>
import
(
'node-fetch'
).
then
(({
default
:
fetch
})
=>
fetch
(...
args
));
const
options
=
{
method
:
'GET'
,
headers
:
{
Accept
:
'application/json'
}
};
const
express
=
require
(
'express'
);
const
app
=
express
();
const
{
Coin
}
=
require
(
"./models/Coin"
);
const
{
User
}
=
require
(
'./models/User'
);
require
(
"dotenv"
).
config
();
const
fs
=
require
(
'fs'
);
const
path
=
require
(
'path'
);
const
HTTPS
=
require
(
'https'
);
const
domain
=
"2019102152.osschatbot.ga"
const
sslport
=
23023
;
const
bodyParser
=
require
(
'body-parser'
);
const
crypto
=
require
(
'crypto'
);
const
queryEncode
=
require
(
'querystring'
).
encode
;
const
request
=
require
(
'request-promise-native'
);
// const request = require('request')
const
{
v4
}
=
require
(
"uuid"
)
const
sign
=
require
(
'jsonwebtoken'
).
sign
const
access_key
=
process
.
env
.
access_key
;
const
secret_key
=
process
.
env
.
secret_key
;
const
server_url
=
"https://api.upbit.com"
var
krw_balance
=
60000
;
var
divided_money
=
krw_balance
/
10
;
var
sort_info
=
new
Array
();
const
mongoose
=
require
(
'mongoose'
);
const
config
=
require
(
'./config/key'
);
const
{
json
}
=
require
(
'express'
);
const
e
=
require
(
'express'
);
const
connect
=
mongoose
.
connect
(
config
.
mongoURI
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
...
...
@@ -28,12 +34,11 @@ const connect = mongoose.connect(config.mongoURI, {
.
then
(()
=>
console
.
log
(
'디비연결 성공'
))
.
catch
((
err
)
=>
console
.
log
(
err
));
app
.
use
(
bodyParser
.
json
());
var
korean_name
=
new
Object
();
var
korean_name
=
new
Object
();
const
access_key
=
process
.
env
.
access_key
;
const
secret_key
=
process
.
env
.
secret_key
;
const
server_url
=
"https://api.upbit.com"
function
get_asset
(
market
)
{
const
payload
=
{
...
...
@@ -51,15 +56,19 @@ function get_asset(market) {
if
(
err
)
throw
new
Error
(
err
)
var
empty
=
new
Object
();
data
=
JSON
.
parse
(
body
);
data
.
filter
(
function
(
item
)
{
if
(
item
.
currency
==
market
.
split
(
'-'
)[
1
])
{
// resolve(item);
empty
.
market
=
item
;
}
else
if
(
item
.
currency
==
"KRW"
)
{
empty
.
KRW
=
item
}
resolve
(
empty
);
})
if
(
market
)
{
data
.
filter
(
function
(
item
)
{
if
(
item
.
currency
==
market
.
split
(
'-'
)[
1
])
{
// resolve(item);
empty
.
market
=
item
;
}
else
if
(
item
.
currency
==
"KRW"
)
{
empty
.
KRW
=
item
}
resolve
(
empty
);
})
}
else
{
resolve
(
data
);
}
})
})
return
result
(
market
);
...
...
@@ -77,13 +86,13 @@ async function get_marketName() {
})
return
data
;
}
async
function
transaction_coin
(
coin_name
,
side
,
volume
,
price
,
ord_type
,
first
=
fals
e
)
{
var
volume
=
volume
;
if
(
side
==
"ask"
)
{
await
User
.
findOne
({
uid
:
1
}).
then
((
result
)
=>
{
async
function
transaction_coin
(
coin_name
,
side
,
volume
,
price
,
ord_type
,
first
=
tru
e
)
{
var
volume
=
volume
;
if
(
side
==
"ask"
)
{
await
User
.
findOne
({
uid
:
1
}).
then
((
result
)
=>
{
console
.
log
(
result
.
volume
);
volume
=
String
(
result
.
volume
);
}).
catch
(
err
=>
{
console
.
log
(
err
.
error
)
});
volume
=
String
(
result
.
volume
);
}).
catch
(
err
=>
{
console
.
log
(
err
.
error
)
});
}
const
body
=
{
market
:
coin_name
,
...
...
@@ -111,21 +120,39 @@ async function transaction_coin(coin_name, side, volume, price, ord_type, first
json
:
body
}
await
request
(
options
).
then
(
async
(
result
)
=>
{
const
asset
=
(
delay
)
=>
new
Promise
((
resolve
)
=>
{
setTimeout
(
async
()
=>
{
const
asset
=
(
delay
)
=>
new
Promise
((
resolve
)
=>
{
setTimeout
(
async
()
=>
{
resolve
(
await
get_asset
(
result
.
market
));
},
delay
)
},
delay
)
})
var
my_asset
=
await
asset
(
1000
);
if
(
side
==
"bid"
){
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
});
await
user
.
save
().
then
(()
=>
{
isuser
=
true
})
}
else
if
(
side
==
"ask"
){
User
.
deleteOne
({
uid
:
1
},(
err
,
res
)
=>
{
if
(
err
){
console
.
log
(
err
);
}
});
var
my_asset
=
await
asset
(
1000
);
if
(
side
==
"bid"
)
{
//처음 매수일 때
if
(
first
)
{
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
});
await
user
.
save
().
then
(()
=>
{
isuser
=
true
})
//2회 이상 매수일 때
}
else
{
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
)
=>
{
if
(
err
){
console
.
log
(
err
);
}
else
{
console
.
log
(
result
);
}
})
}
}
else
if
(
side
==
"ask"
)
{
//1회 매수 후 매도일 때
if
(
first
)
{
User
.
deleteOne
({
uid
:
1
},
(
err
,
res
)
=>
{
if
(
err
)
{
console
.
log
(
err
);
}
});
//분할 매수 분할 매도 중일 때
}
else
{
}
}
}).
catch
((
err
)
=>
{
console
.
log
(
err
.
error
)
})
}
...
...
@@ -194,42 +221,66 @@ async function get_candle(minute, market) {
.
then
(
json
=>
candle
=
json
)
return
candle
;
}
async
function
price_comparison
(
candle
,
avg_buy_price
=
0
)
{
var
isbuying
=
true
;
if
(
avg_buy_price
==
0
)
{
await
Coin
.
findOne
({
name
:
candle
[
0
].
market
}).
then
(
async
(
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
.
korean_name
+
"은(는)"
+
result
.
count
*
5
+
"분 동안 하락중"
);
if
(
result
.
count
>=
3
)
{
transaction_coin
(
result
.
name
,
"bid"
,
null
,
divided_money
,
"price"
);
isbuying
=
false
;
}
}
})
}
else
if
(
result
.
current_price
<
candle
[
0
].
trade_price
)
{
await
Coin
.
findOneAndUpdate
({
name
:
candle
[
0
].
market
},
{
current_price
:
candle
[
0
].
trade_price
,
count
:
0
},
{
new
:
true
}).
then
(
async
(
err
,
result
)
=>
{
if
(
err
)
{
console
.
log
(
err
);
}
else
{
console
.
log
(
result
.
korean_name
+
"은(는)"
+
result
.
count
*
5
+
"분 동안 상승 혹은 정체중"
);
}
})
}
})
}
else
{
if
(
avg_buy_price
<
candle
[
0
].
trade_price
)
{
transaction_coin
(
candle
[
0
].
market
,
"bid"
,
null
,
divided_money
,
"price"
,
false
)
}
else
{
transaction_coin
(
candle
[
0
].
market
,
"ask"
,
""
,
null
,
"market"
,
false
);
}
}
return
isbuying
;
}
async
function
check_coin
(
t1
)
{
User
.
find
().
then
(
async
(
user_data
)
=>
{
//아직 매수한 코인이 없을 때
if
(
user_data
.
length
==
0
)
{
var
is
user
=
false
;
var
is
buying
=
false
;
for
(
var
i
=
0
;
i
<
t1
.
length
;
i
++
)
{
if
(
!
isuser
)
{
if
(
isbuying
)
{
var
candle
=
await
get_candle
(
5
,
t1
[
i
]);
await
Coin
.
findOne
({
name
:
candle
[
0
].
market
}).
then
(
async
(
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
.
korean_name
+
"은(는)"
+
result
.
count
*
5
+
"분 동안 하락중"
);
if
(
result
.
count
>=
3
)
{
transaction_coin
(
result
.
name
,
"bid"
,
null
,
divided_money
,
"price"
);
}
}
})
}
//그대로 이거나 올랐을때
else
{
await
Coin
.
findOneAndUpdate
({
name
:
candle
[
0
].
market
},
{
current_price
:
candle
[
0
].
trade_price
,
count
:
0
},
{
new
:
true
}).
then
(
async
(
result
)
=>
{
//특정 조건...
const
user
=
new
User
({
krw_balance
:
100
,
market
:
"KRW-BTC"
,
count
:
1
,
avg_price
:
100
});
await
user
.
save
().
then
(()
=>
{
isuser
=
true
})
console
.
log
(
result
.
korean_name
+
"은(는)"
+
result
.
count
*
5
+
"분 동안 상승 혹은 정체중"
);
})
}
})
}
else
{
console
.
log
(
"메롱"
);
isbuying
=
await
price_comparison
(
candle
);
console
.
log
(
isbuying
);
}
}
//매수한 코인이 있을 때
}
else
{
console
.
log
(
user_data
);
//3회 이상 매수 했을시 15분봉으로 교체해서 가격 확인
//15분봉 처리해야함
if
(
user_data
[
0
].
count
>
3
)
{
var
candle
=
await
get_candle
(
15
,
user_data
[
0
].
market
);
await
price_comparison
(
candle
,
user_data
[
0
].
avg_buy_price
);
}
else
{
var
candle
=
await
get_candle
(
5
,
user_data
[
0
].
market
);
await
price_comparison
(
candle
,
user_data
[
0
].
avg_buy_price
);
}
}
}).
catch
((
err
)
=>
{
console
.
log
(
err
);
...
...
@@ -237,20 +288,20 @@ async function check_coin(t1) {
}
async
function
latest_repeat
(
t1
)
{
await
Coin
.
find
().
sort
({
tid
:
1
}).
then
(
result
=>
{
for
(
var
key
in
result
)
{
t1
.
push
(
result
[
key
].
name
)
}
})
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) {
//
if (seconds == 0) {
if
(
minutes
==
0
&&
seconds
==
0
)
{
clearInterval
(
check_time
);
sort_info
=
(
await
sort_data
());
(
await
refresh_db
());
Coin
.
find
().
sort
({
tid
:
1
}).
then
(
result
=>
{
for
(
var
key
in
result
)
{
t1
.
push
(
result
[
key
].
name
)
}
})
console
.
log
(
"현재 시간은 "
+
today
.
toLocaleTimeString
());
var
count
=
0
;
let
coin
=
setInterval
(
async
()
=>
{
...
...
@@ -266,20 +317,24 @@ async function latest_repeat(t1) {
(
await
refresh_db
());
console
.
log
(
"db최신화"
);
}
},
600
*
5
);
},
600
00
*
5
);
}
},
1000
);
}
app
.
listen
(
5000
,
async
()
=>
{
console
.
log
(
'server start'
)
//coin 이름,가격,거래대금 저장 , DB 최신화 1시간마다 반복
//5분마다 현재 가격 가져와서 db랑 비교후 매수 매도 기준잡기
// var t1 = new Array();
// test_data = await (latest_repeat(t1));
//계좌 정보 db 최신화
// console.log(await get_asset());
//매도
await
transaction_coin
(
"KRW-BTC"
,
"ask"
,
""
,
null
,
"market"
);
//매수
// await transaction_coin("KRW-BTC", "bid", null, divided_money, "price");
})
\ No newline at end of file
try
{
const
option
=
{
ca
:
fs
.
readFileSync
(
'/etc/letsencrypt/live/'
+
domain
+
'/fullchain.pem'
),
key
:
fs
.
readFileSync
(
path
.
resolve
(
process
.
cwd
(),
'/etc/letsencrypt/live/'
+
domain
+
'/privkey.pem'
),
'utf8'
).
toString
(),
cert
:
fs
.
readFileSync
(
path
.
resolve
(
process
.
cwd
(),
'/etc/letsencrypt/live/'
+
domain
+
'/cert.pem'
),
'utf8'
).
toString
(),
};
HTTPS
.
createServer
(
option
,
app
).
listen
(
sslport
,
async
()
=>
{
console
.
log
(
`[HTTPS] Server is started on port
${
sslport
}
`
);
var
t1
=
new
Array
();
await
(
latest_repeat
(
t1
));
// console.log(await get_asset());
});
}
catch
(
error
)
{
console
.
log
(
'[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'
);
console
.
log
(
error
);
}
...
...
Please
register
or
login
to post a comment