def out_json(num):

	ticker = coin.ticker_api()
	with open(log_location + 'ticker_' + str(num) + '.json', 'w') as json_file:
		json.dump(ticker, json_file, ensure_ascii=True)
		json_file.close
	fileio.write_log(str(datetime.datetime.now()) + " get ticker_" + str(num) + "\n", 'calculate_log')
def get_minus(ticker1, ticker2):
	#get minuscoin

	list_minus = {
		"btc": ((float(ticker2["btc"]["last"]) - float(ticker1["btc"]["last"])) / float(ticker1["btc"]["last"])) * 100,
		"btg": ((float(ticker2["btg"]["last"]) - float(ticker1["btg"]["last"])) / float(ticker1["btg"]["last"])) * 100,
		"bch": ((float(ticker2["bch"]["last"]) - float(ticker1["bch"]["last"])) / float(ticker1["bch"]["last"])) * 100,
		"eth": ((float(ticker2["eth"]["last"]) - float(ticker1["eth"]["last"])) / float(ticker1["eth"]["last"])) * 100,
		"etc": ((float(ticker2["etc"]["last"]) - float(ticker1["etc"]["last"])) / float(ticker1["etc"]["last"])) * 100,
		"xrp": ((float(ticker2["xrp"]["last"]) - float(ticker1["xrp"]["last"])) / float(ticker1["xrp"]["last"])) * 100,
		"qtum": ((float(ticker2["qtum"]["last"]) - float(ticker1["qtum"]["last"])) / float(ticker1["qtum"]["last"])) * 100,
		"iota": ((float(ticker2["iota"]["last"]) - float(ticker1["iota"]["last"])) / float(ticker1["iota"]["last"])) * 100,
		"ltc": ((float(ticker2["ltc"]["last"]) - float(ticker1["ltc"]["last"])) / float(ticker1["ltc"]["last"])) * 100
	}
	
	list_minus_temp = {
		"btc": 1,
		"btg": 1,
		"bch": 1,
		"eth": 1,
		"etc": 1,
		"xrp": 1,
		"qtum": 1,
		"iota": 1,
		"ltc": 1,
	}
		
	
	if list_minus["btc"] > -0.7:
		del list_minus["btc"]
		list_minus_temp["btc"] = 0
	if list_minus["btg"] > -0.7:
		del list_minus["btg"]
		list_minus_temp["btg"] = 0
	if list_minus["bch"] > -0.7:
		del list_minus["bch"]
		list_minus_temp["bch"] = 0
	if list_minus["eth"] > -0.7:
		del list_minus["eth"]
		list_minus_temp["eth"] = 0
	if list_minus["etc"] > -0.7:
		del list_minus["etc"]
		list_minus_temp["etc"] = 0
	if list_minus["xrp"] > -0.7:
		del list_minus["xrp"]
		list_minus_temp["xrp"] = 0
	if list_minus["qtum"] > -0.7:
		del list_minus["qtum"]
		list_minus_temp["qtum"] = 0
	if list_minus["iota"] > -0.7:
		del list_minus["iota"]
		list_minus_temp["iota"] = 0
	if list_minus["ltc"] > -0.7:
		del list_minus["ltc"]
		list_minus_temp["ltc"] = 0
	
	log = (str(datetime.datetime.now()) + str(list_minus_temp) + '\n')
	fileio.write_log(log, 'calculate_log')
	return list_minus_temp
def trade(PAYLOAD, krw_money):

    order_info_pre = cryptotrade.orderbook_api(PAYLOAD["currency"])
    crypto_krw_pre = float(order_info_pre["ask"][1]["price"])
    krw_crypto_pre = float(order_info_pre["bid"][1]["price"])

    #process buy
    buy_crypto_price = source_func.round_4(krw_money / crypto_krw_pre)
    buy_crypto(PAYLOAD, crypto_krw_pre, buy_crypto_price)

    #process sell
    balance = cryptotrade.balance_api()
    cur_crypto_avail = float(balance[PAYLOAD["currency"]]["avail"])
    cur_crypto_total = float(balance[PAYLOAD["currency"]]["balance"])
    cur_crypto_avail = float(balance[PAYLOAD["currency"]]["avail"])
    cur_crypto_total = float(balance[PAYLOAD["currency"]]["balance"])

    count = 0
    while True:
        time.sleep(1)
        count = count + 1
        order_info = cryptotrade.orderbook_api(PAYLOAD["currency"])
        krw_crypto = float(order_info["bid"][1]["price"])
        crypto_krw = float(order_info["bid"][1]["price"])
        crypto_krw_value = krw_crypto * cur_crypto_avail

        if crypto_krw_value > krw_money * 1.0045:
            fileio.write_log(
                str(datetime.datetime.now()) + " reached 1.0045!\n",
                'calculate_log')
            time.sleep(1)
            cur_1 = order_info
            while True:
                time.sleep(180)
                cur_2 = cryptotrade.orderbook_api(PAYLOAD["currency"])
                cur_1_crypto_krw_value = float(
                    cur_1["bid"][1]["price"]) * cur_crypto_avail
                cur_2_crypto_krw_value = float(
                    cur_2["bid"][1]["price"]) * cur_crypto_avail

                if ((cur_1_crypto_krw_value - cur_2_crypto_krw_value) /
                        cur_1_crypto_krw_value) * 100 > 0.1:
                    sell_crypto(PAYLOAD, float(cur_2["bid"][1]["price"]),
                                cur_crypto_avail)
                    break

                cur_1 = cur_2
            break
def get_onecoin(coins):
	
	ticker = read_json(3)
	
	if len(coins) == 0:
		return 'NULL'
	else:
		numof_coin = len(coins)
		num = 0
		value_yesterday_per_today = { "start": 0}
		while(True):
			last = float(ticker[coins[(numof_coin-1) - num]]["last"])
			yesterday = (float(ticker[coins[(numof_coin-1) - num]]["yesterday_low"]) + float(ticker[coins[(numof_coin-1) - num]]["yesterday_high"])) / 2
		
			value_yesterday_per_today[coins[(numof_coin-1) - num]] = (last - yesterday) / yesterday * 100
			
			num = num + 1
			if num == numof_coin:
				break


		del value_yesterday_per_today["start"]	
		print coins
		print value_yesterday_per_today

		num = 0
		checker = value_yesterday_per_today[coins[(numof_coin-1) - num]]
		returner = coins[(numof_coin-1) - num]
		while(True):
			if value_yesterday_per_today[coins[(numof_coin-1) - num]] < checker:
				checker = value_yesterday_per_today[coins[(numof_coin-1) - num]]
				returner = coins[(numof_coin-1) - num]
				
			num = num + 1
			if num == numof_coin:
				break
		
		print returner
			
		log = (str(datetime.datetime.now()) + " catched coin is : " + str(returner) + '\n')
		fileio.write_log(log, 'calculate_log')
		fileio.write_log(log, 'main_log')
		return returner
def buy_crypto(payload, price, buy_crypto_price):
    #send buy order to server
    payload["price"] = price
    payload["qty"] = buy_crypto_price
    buy = cryptotrade.buy_api(payload)
    fileio.write_log((
        str(datetime.datetime.now()) +
        " buy order placed, error: %(err)s, coinname: %(coin)s, price: %(price)d, quantity: %(qty)f, total: %(total)d\n"
        % {
            'err': buy["errorCode"],
            'coin': payload["currency"],
            'price': payload["price"],
            'qty': payload["qty"],
            'total': payload["price"] * payload["qty"]
        }), 'main_log')
    while True:
        time.sleep(1)
        temp = cryptotrade.limit_order_api(payload["currency"])
        if temp["limitOrders"] == []:
            fileio.write_log((str(datetime.datetime.now()) +
                              ' buy order completed, buy finished\n'),
                             'main_log')
            break
def sell_crypto(payload, price, cur_crypto_avail):
    #send sell order to server
    payload["price"] = price
    payload["qty"] = source_func.round_4(cur_crypto_avail)
    sell = cryptotrade.sell_api(payload)
    fileio.write_log((
        str(datetime.datetime.now()) +
        " sell order placed, error: %(err)s, coinname: %(coin)s, price: %(price)d, quantity: %(qty)f, total: %(total)d\n"
        % {
            'err': sell["errorCode"],
            'coin': payload["currency"],
            'price': payload["price"],
            'qty': payload["qty"],
            'total': payload["price"] * payload["qty"]
        }), 'main_log')
    while True:
        time.sleep(1)
        temp = cryptotrade.limit_order_api(payload["currency"])
        if temp["limitOrders"] == []:
            fileio.write_log((str(datetime.datetime.now()) +
                              ' sell order completed, sell finished\n'),
                             'main_log')
            break
def break_trade(PAYLOAD, krw_money, balance):

    cur_crypto_avail = float(balance[PAYLOAD["currency"]]["avail"])
    cur_crypto_total = float(balance[PAYLOAD["currency"]]["balance"])
    cur_crypto_avail = float(balance[PAYLOAD["currency"]]["avail"])
    cur_crypto_total = float(balance[PAYLOAD["currency"]]["balance"])
    count = 0
    while True:
        time.sleep(1)
        count = count + 1
        order_info = cryptotrade.orderbook_api(PAYLOAD["currency"])
        krw_crypto = float(order_info["bid"][1]["price"])
        crypto_krw = float(order_info["bid"][1]["price"])
        crypto_krw_value = krw_crypto * cur_crypto_avail

        if crypto_krw_value > krw_money * 1.0045:
            fileio.write_log(
                str(datetime.datetime.now()) + " reached 1.0045!\n",
                'calculate_log')
            time.sleep(1)
            cur_1 = order_info
            while True:
                time.sleep(180)
                cur_2 = cryptotrade.orderbook_api(PAYLOAD["currency"])
                cur_1_crypto_krw_value = float(
                    cur_1["bid"][1]["price"]) * cur_crypto_avail
                cur_2_crypto_krw_value = float(
                    cur_2["bid"][1]["price"]) * cur_crypto_avail

                if ((cur_1_crypto_krw_value - cur_2_crypto_krw_value) /
                        cur_1_crypto_krw_value) * 100 > 0.1:
                    sell_crypto(PAYLOAD, float(cur_2["bid"][1]["price"]),
                                cur_crypto_avail)
                    break

                cur_1 = cur_2
            break
def evaluate(term1, term2):
	
	temp3 = {
		"btc": term1["btc"] + term2["btc"],
		"btg": term1["btg"] + term2["btg"],
		"bch": term1["bch"] + term2["bch"],
		"eth": term1["eth"] + term2["eth"],
		"etc": term1["etc"] + term2["etc"],
		"xrp": term1["xrp"] + term2["xrp"],
		"qtum": term1["qtum"] + term2["qtum"],
		"iota": term1["iota"] + term2["iota"],
		"ltc": term1["ltc"] + term2["ltc"],
	}
	
	if temp3["btc"] != 2:
		del temp3["btc"]
	if temp3["btg"] != 2:
		del temp3["btg"]
	if temp3["bch"] != 2:
		del temp3["bch"]
	if temp3["eth"] != 2:
		del temp3["eth"]
	if temp3["etc"] != 2:
		del temp3["etc"]
	if temp3["xrp"] != 2:
		del temp3["xrp"]
	if temp3["qtum"] != 2:
		del temp3["qtum"]
	if temp3["iota"] != 2:
		del temp3["iota"]
	if temp3["ltc"] != 2:
		del temp3["ltc"]
		
	list_return = sorted(temp3, key=lambda k : temp3[k])
	log = (str(datetime.datetime.now()) + " " + str(list_return) + '\n')
	fileio.write_log(log, 'calculate_log')
	return list_return
Exemple #9
0
PAYLOAD = {
    "access_token": ACCESS_TOKEN,
    "price": 10000,
    "qty": 0.1,
    "currency": "NULL",
}

if __name__ == "__main__":

    #get money
    krw_money = fileio.get_money()
    print(krw_money, type(krw_money))

    #start system, log
    fileio.write_log((str(datetime.datetime.now()) +
                      ' Starting trade, input money: %(money)d\n' % {
                          'money': krw_money
                      }), 'main_log')

    #get already available coin
    ticker = cryptotrade.ticker_api()
    balance_outro = cryptotrade.balance_api()
    balance_coininfo = get_coin.get_balance_avail_coin(balance_outro, ticker,
                                                       krw_money)
    balance_krw = float(balance_outro["krw"]["avail"])

    #if altcoin is detected _ break trade
    if balance_coininfo != "krw":
        PAYLOAD["currency"] = balance_coininfo
        fileio.write_log(
            str(datetime.datetime.now()) + ' avaliable coin: ' +
            balance_coininfo + ', target krw: ' + str(krw_money * 1.06) + '\n',