window.onload = () => {
//modal
const modal = document.querySelector(".modal");
const modalBackground = modal.querySelector(".modal_background");
function displayModal(){
modal.classList.toggle("hidden");
}
modalBackground.addEventListener("click", displayModal);
/*<<<<<<<<<<<<<<<<<<<< INIT >>>>>>>>>>>>>>>>>>>>>>>*/
const info_tag = document.getElementById('info');
const account_tag = document.getElementById('accounts');
const order_tag = document.getElementById('order');
const status_tag = document.getElementById('status');
const market_id_input = document.getElementById('market_id');
const side_select = document.getElementById('side');
const volumn_input = document.getElementById('volumn');
const price_input = document.getElementById('price');
const order_type_select = document.getElementById('order_type');
const identifier_input = document.getElementById('identifier');
const main_url = window.location.host;
let account_info;
let upbit_ticker_interval;
let k_noise = document.getElementById('k_noise_input');
const td_coin = new Object();
/*<<<<<<<<<<<<<<<<<< EXECUTION >>>>>>>>>>>>>>>>>>>>*/
document.getElementById('order_btn').onclick = upbit_order_execute;
document.getElementById('time').innerHTML = timerOn();
setInterval(() => {
document.getElementById('time').innerHTML = timerOn();
},1000);
upbit_accounts();
upbit_market_code();
/*<<<<<<<<<<<<<<<<<<< FUNCTION >>>>>>>>>>>>>>>>>>>>>>*/
/*---------------- common ------------------*/
//common fetch call
function call_fetch(t, ch, ref=null){
return fetch(`http://${main_url}/api`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
type: t,
channel: ch,
reference: ref,
}),
});
}
//timerOn
function timerOn(){
let now = new Date();
let h = now.getHours() < 10 ? `0${now.getHours()}` : now.getHours();
let m = now.getMinutes() < 10 ? `0${now.getMinutes()}` : now.getMinutes();
let s = now.getSeconds() < 10 ? `0${now.getSeconds()}` : now.getSeconds();
return `${h} : ${m} : ${s}`
}
//clear interval
function clear_interval(){
clearInterval(upbit_ticker_interval);
}
/*---------------- upbit ------------------*/
//accounts call
function upbit_accounts(){
return call_fetch('u','accounts')
.then((response) => response.json())
.then((result) => {
account_info = result;
let result_html = '';
for(let i=0; i<account_info.length; i++){
result_html += `<li>${account_info[i].currency} : ${Number(account_info[i].balance).toFixed(2)}</li>`;
}
document.getElementById('account_ul').innerHTML = result_html;
})
}
//market call
function upbit_market_code(){
return call_fetch('u','market_code')
.then((response) => response.json())
.then((result) => {
let market_text = '';
let result_html = '';
for(let i=0; i < result.length; i++){
if(result[i].market.substr(0,3) == 'KRW'){
market_text += i != result.length-1 ? `${result[i].market},` : result[i].market;
result_html += `<li id='${result[i].market}-list'><span id='${result[i].market}'>${result[i].market.substr(4,5)} : <span id='${result[i].market}-value'></span></span><label class="switch-button"> <input type="checkbox"/> <span class="onoff-switch"></span></label></li>`;
}
}
document.getElementById('info_ul').innerHTML = result_html;
upbit_ticker_interval = setInterval(() => {
upbit_ticker(market_text)
.then((result) => {
for(let i=0; i < result.length; i++){
td_coin[result[i].market] = result[i];
//console.log(document.getElementById(`${result[i].market}-list`));
document.getElementById(`${result[i].market}`).onclick = () => {
//upbit_orderable(ref)
//displayModal();
console.log(td_coin[result[i].market]);
day_check_candle(result[i].market,2)
.then((result) => {
console.log(k_noise.value);
/*
let td_candle = result[0];
let ye_candle = result[1];
let td_range = ye_candle.high_price-ye_candle.low_price;
let td_ask_price = td_range * k_noise + td_candle.opening_price;
td_coin[result[i].market][]
*/
/*
for(let i=0; i < result.length; i++){
//console.log(result[i].candle_acc_trade_volume);
candle_html += `<li>${result[i].candle_date_time_kst.substr(5, 5)} : ${result[i].low_price} >> ${result[i].high_price}</li>`;
}
document.getElementById('status_ul').innerHTML = candle_html;
*/
});
};
document.getElementById(`${result[i].market}-value`).innerHTML = result[i].trade_price.toFixed(2);
}
});
}, 200);
});
}
//orderable call
function upbit_orderable(ref){
const ref_obj = {
"market" : ref
}
return call_fetch('u','orderable', ref_obj)
.then((response) => response.json());
}
//ticker_call
function upbit_ticker(ref){
const ref_obj = {
"text" : ref
}
return call_fetch('u','price_ticker', ref_obj)
.then((response) => response.json());
}
//order_execute_call
function upbit_order_execute(ref){
console.log(market_id.value);
console.log(side.value);
console.log(volume.value);
console.log(price.value);
console.log(order_type.value);
/*
const ref_obj = ref;
return call_fetch('u','order_execute', ref_obj)
.then((response) => response.json());
*/
}
//check_candle
function min_check_candle(unit, market, count){
const ref_obj = {
"unit" : unit,
"market" : market,
"count" : count
}
return call_fetch('u','min_check_candle', ref_obj)
.then((response) => response.json());
}
function day_check_candle(market, count){
const ref_obj = {
"market" : market,
"count" : count
}
return call_fetch('u','day_check_candle', ref_obj)
.then((response) => response.json());
}
};