# Path: order.py
# class Order(object):
#     def __init__(self, price, volume, type=None, pair=None, orderID=None, timestamp=None):
#         """
#         markets are usually expressed in terms of BASE_ALT where you buy
#         and sell units of BASE with ALT.
#         price = price (in units alt) of 1 unit of base
#         volume = total volume of base desired
#         orderID = necessary for tracking & cancelling & ignoring already executed orders when backtesting
#         """
#         self.p = price
#         self.v = volume
#         self.type = type # buy or sell
#         self.pair = pair # market we are trading on
#         self.id = orderID
#         self.time = timestamp


import config
import utils
from order import Order


    def get_best_type1_roundtrip(self):
        """
        sell(AC), sell(CB)
        buy(AB)
        """
        A,B = self.pair # use A_B notation instead of base, alt
        b = self.broker
        alt = B
        tx = 1 - b.xchg.trading_fee
        asks_ab = b.get_orders((A,B), 'asks') # people who are selling A_B
        for C, spread in self.type1_spreads.items():
            if spread > config.PROFIT_THRESH[alt]:
                self.type1_roundtrips[C] = {}
                bids_ac = b.get_orders((A,C), 'bids') # people who are buying A_C
                bids_cb = b.get_orders((C,B), 'bids') # people who are buying C_B
                # minimum volume constraints
                min_ac = b.xchg.get_min_vol((A,C), bids_ac) # how much DOGE we have to sell in the first trade
                min_cb = b.xchg.get_min_vol((C,B), bids_cb) # how much LTC we have to sell in the second trade
                min_ab = b.xchg.get_min_vol((A,B), asks_ab) # how much DOGE we have to buy in the third trade
                # assuming value A << value C, first trade has to acquire enough LTC to perform the second trade
                bids_ac_clipped = b.xchg.get_clipped_alt_volume(bids_ac, min_ac/tx)
                # how much DOGE we sold to acquire 0.01 LTC
                v1 = utils.total_base_volume(bids_ac)
                # performing the second trade will net enough BTC to buy back at least 0.01 DOGE
                bids_cb_clipped = b.xchg.get_clipped_base_volume(bids_cb, min_ac)
                # buy back exactly how much DOGE we spent in the first place
                asks_ab_clipped = b.xchg.get_clipped_base_volume(asks_ab, v1/tx)
                # construct the orders

# Path: order.py
# class Order(object):
#     def __init__(self, price, volume, type=None, pair=None, orderID=None, timestamp=None):
#         """
#         markets are usually expressed in terms of BASE_ALT where you buy
#         and sell units of BASE with ALT.
#         price = price (in units alt) of 1 unit of base
#         volume = total volume of base desired
#         orderID = necessary for tracking & cancelling & ignoring already executed orders when backtesting
#         """
#         self.p = price
#         self.v = volume
#         self.type = type # buy or sell
#         self.pair = pair # market we are trading on
#         self.id = orderID
#         self.time = timestamp


# Path: TriangleProfitCalculator.py
import config
import utils
from order import Order



    def get_best_type1_roundtrip(self):
        """
        sell(AC), sell(CB)
        buy(AB)
        """
        A,B = self.pair # use A_B notation instead of base, alt
        b = self.broker
        alt = B
        tx = 1 - b.xchg.trading_fee
        asks_ab = b.get_orders((A,B), 'asks') # people who are selling A_B
        for C, spread in self.type1_spreads.items():
            if spread > config.PROFIT_THRESH[alt]:
                self.type1_roundtrips[C] = {}
                bids_ac = b.get_orders((A,C), 'bids') # people who are buying A_C
                bids_cb = b.get_orders((C,B), 'bids') # people who are buying C_B
                # minimum volume constraints
                min_ac = b.xchg.get_min_vol((A,C), bids_ac) # how much DOGE we have to sell in the first trade
                min_cb = b.xchg.get_min_vol((C,B), bids_cb) # how much LTC we have to sell in the second trade
                min_ab = b.xchg.get_min_vol((A,B), asks_ab) # how much DOGE we have to buy in the third trade
                # assuming value A << value C, first trade has to acquire enough LTC to perform the second trade
                bids_ac_clipped = b.xchg.get_clipped_alt_volume(bids_ac, min_ac/tx)
                # how much DOGE we sold to acquire 0.01 LTC
                v1 = utils.total_base_volume(bids_ac)
                # performing the second trade will net enough BTC to buy back at least 0.01 DOGE
                bids_cb_clipped = b.xchg.get_clipped_base_volume(bids_cb, min_ac)
                # buy back exactly how much DOGE we spent in the first place
                asks_ab_clipped = b.xchg.get_clipped_base_volume(asks_ab, v1/tx)
                # construct the orders

                o1 = Order(utils.lowest_price(bids_ac_clipped), utils.total_base_volume(bids_ac_clipped), type="sell", pair=(A,C))