# coding: utf-8

from bigone import BigOneDog
from common import gen_logger

import logging
import time
import json


def strategy_eth_big_bnc_eth(dog):
    """
    正向：买BIG/ETH -> 卖BIG/BNC -> 买ETH/BNC
    反向：卖ETH/BNC -> 买BIG/BNC -> 卖BIG/ETH
    :param dog: implemention of BigOneDog
    :return: 正向收益率，反向收益率
    """
    big_eth_data = dog.get_order_book('BIG-ETH')
    big_bnc_data = dog.get_order_book('BIG-BNC')
    eth_bnc_data = dog.get_order_book('ETH-BNC')

    print('BIG-ETH')
    print('卖一', big_eth_data['asks'][0]['price'], big_eth_data['asks'][0]['amount'])
    print('买一', big_eth_data['bids'][0]['price'], big_eth_data['bids'][0]['amount'])
    print('BIG-BNC')
    print('卖一', big_bnc_data['asks'][0]['price'], big_bnc_data['asks'][0]['amount'])
    print('买一', big_bnc_data['bids'][0]['price'], big_bnc_data['bids'][0]['amount'])
    print('ETH-BNC')
    print('卖一', eth_bnc_data['asks'][0]['price'], eth_bnc_data['asks'][0]['amount'])
    print('买一', eth_bnc_data['bids'][0]['price'], eth_bnc_data['bids'][0]['amount'])

    # positive transaction
    pos_anc = 0.999*0.999*0.999*\
              ((1 / (float(big_eth_data['asks'][0]['price'])))
              * float(big_bnc_data['bids'][0]['price']) )
    pos_anc = pos_anc / float(eth_bnc_data['asks'][0]['price']) - 1

    # negative transaction
    neg_anc = 0.999 * 0.999 * 0.999 * \
          (float(eth_bnc_data['bids'][0]['price'])
           / float(big_bnc_data['asks'][0]['price'])
           * float(big_eth_data['asks'][0]['price']))
    neg_anc = neg_anc / 1 - 1

    flag = False

    amt = 2.0
    if float(big_eth_data['asks'][0]['amount']) >= amt:
        if float(big_bnc_data['bids'][0]['amount']) >= amt:
            if float(eth_bnc_data['asks'][0]['amount']) >= amt * float(big_eth_data['asks'][0]['price']):
                flag = True

    msg = "预期本次[正向套利:买BIG/ETH -> 卖BIG/BNC -> 买ETH/BNC]利润:"
    if pos_anc < 0.01:
        result = "利润空间小于1%, 放弃本次套利 0"
        logger.info("{0} {1:.2f}%, {2}".format(msg,pos_anc*100,result))
    else:
        result = "利润空间大于1%"
        if flag is False:
            result = "{},{}".format(result,"量不足, 放弃本次套利 0")
            logger.info("{0} {1:.2f}%, {2}".format(msg,pos_anc*100,result))
        else:
            result = "{},{}".format(result,"执行本次套利 1")
            logger.info("{0} {1:.2f}%, {2}".format(msg,pos_anc*100,result))

            print("{}  {}  {}  {}".format('BIG-ETH','BID', big_eth_data['asks'][0]['price'], str(amt)))
            print("{}  {}  {}  {}".format('BIG-BNC','ASK', big_bnc_data['bids'][0]['price'], str(amt)))
            print("{}  {}  {}  {}".format('ETH-BNC','BID', eth_bnc_data['asks'][0]['price'],
                             str(amt * float(big_eth_data['asks'][0]['price']))))
            # dog.create_order('BIG-ETH','ASK', big_eth_data['asks'][0]['price'], '2.0')
            # dog.create_order('BIG-BNC','BID', big_bnc_data['bids'][0]['price'], '2.0')
            # dog.create_order('ETH-BNC','ASK', eth_bnc_data['asks'][0]['price'],
            #                  str(2.0 * float(big_eth_data['asks'][0]['price'])))


            return True
    if neg_anc < 0.01:
        result = "利润空间小于1%, 放弃本次套利 0"
    else:
        result = "利润空间大于1%, 执行本次套利 1"

        logger.info("预期本次[反向套利:卖ETH/BNC -> 买BIG/BNC -> 卖BIG/ETH]利润: {0:.2f}%, {1}".format(neg_anc*100,result))

    return False



    # return pos_anc, neg_anc


def strategy_eth_bnc(dog):
    eth_bnc_data = dog.get_order_book('ETH-BNC')
    print('ETH-BNC')
    print('卖一', eth_bnc_data['asks'][0]['price'], eth_bnc_data['asks'][0]['amount'])
    print('买一', eth_bnc_data['bids'][0]['price'], eth_bnc_data['bids'][0]['amount'])
    anc = float(eth_bnc_data['asks'][0]['price']) / float(eth_bnc_data['bids'][0]['price']) - 1
    print(anc)
    if anc > 0.02:
        r = dog.create_order('ETH-BNC', 'BID', str(float(eth_bnc_data['bids'][0]['price'])+0.01), '0.01' )
        bid_order_id = r['order_id']

        r = dog.create_order('ETH-BNC', 'ASK', str(float(eth_bnc_data['asks'][0]['price'])-0.01), '0.01' )
        ask_order_id = r['order_id']

    return anc, anc

if __name__ == '__main__':
    gen_logger('bigonetest')
    logger = logging.getLogger("bigone")

    with open("PRIVATE_KEY.json",'r') as f:
        private_key = json.load(f)["key"]
    dog = BigOneDog(private_key)
    # strategy_eth_bnc(dog)
    # dog.get_orders("ETH-BNC",'10')
    # r = dog.get_order("b79ef031-c477-46f9-b452-7e97aa97435d")
    # print(r)
    # r = dog.get_orders('ETH-BNC','10')
    # print(r)

    while True:

        flag = strategy_eth_big_bnc_eth(dog)
        if flag is True:
            break
        else:
            print("休眠10秒")
            print("")
            time.sleep(10)
        # break
        # pos_anc, neg_anc = strategy_eth_bnc(dog)
        # if pos_anc < 0.01:
        #     result = "利润空间小于1%, 放弃本次套利 0"
        # else:
        #     result = "利润空间大于1%, 执行本次套利 1"
        #
        # logger.info("预期本次[正向套利:买BIG/ETH -> 卖BIG/BNC -> 买ETH/BNC]利润: {0:.2f}%, {1}".format(pos_anc*100,result))
        #
        # if neg_anc < 0.01:
        #     result = "利润空间小于1%, 放弃本次套利 0"
        # else:
        #     result = "利润空间大于1%, 执行本次套利 1"
        #
        # logger.info("预期本次[反向套利:卖ETH/BNC -> 买BIG/BNC -> 卖BIG/ETH]利润: {0:.2f}%, {1}".format(neg_anc*100,result))
        #
        # print("休眠10秒")
        # print("")
        # time.sleep(10)


[((108, 4, 108, 28), 'common.gen_logger', 'gen_logger', ({(108, 15, 108, 27): '"""bigonetest"""'}, {}), "('bigonetest')", False, 'from common import gen_logger\n'), ((109, 13, 109, 40), 'logging.getLogger', 'logging.getLogger', ({(109, 31, 109, 39): '"""bigone"""'}, {}), "('bigone')", False, 'import logging\n'), ((113, 10, 113, 32), 'bigone.BigOneDog', 'BigOneDog', ({(113, 20, 113, 31): 'private_key'}, {}), '(private_key)', False, 'from bigone import BigOneDog\n'), ((112, 22, 112, 34), 'json.load', 'json.load', ({(112, 32, 112, 33): 'f'}, {}), '(f)', False, 'import json\n'), ((129, 12, 129, 26), 'time.sleep', 'time.sleep', ({(129, 23, 129, 25): '(10)'}, {}), '(10)', False, 'import time\n')]