def handle_underlying_orders(u_order):
    if isinstance(u_order, LimitPriceOrder) or isinstance(
            u_order, MarketPriceOrder):
        sorted_asks = sorted(Marketplace.get_instance().asks)
        sorted_bids = sorted(Marketplace.get_instance().bids)

        # handle lpo or mpo
        handle_lpo(u_order, sorted_asks, sorted_bids) \
            if isinstance(u_order, LimitPriceOrder) \
            else handle_mpo(u_order, sorted_asks, sorted_bids)
def handle_mpo(u_order, asks, bids):
    if u_order.action == 'bid':
        if len(asks) == 0:
            print(
                'No sellers on the marketplace, marketplace price non existent'
            )
        else:
            matching_order = Marketplace.get_instance().asks.pop(0)
            print(f'Bought {matching_order}')
    if u_order.action == 'ask':
        if len(bids) == 0:
            print(
                'No buyers on the marketplace, marketplace price non existent')
        else:
            matching_order = Marketplace.get_instance().bids.pop()
            print(f'Bought {matching_order}')
示例#3
0
def simulate(handle_func):
    m = Marketplace.get_instance()

    plt.style.use('fivethirtyeight')
    index = count()

    def animate(i):
        handle_func(generate())

        bins = [x for x in range(0, 400, 5)]

        bids = sorted(order_to_int(Marketplace.get_instance().bids))
        asks = sorted(order_to_int(Marketplace.get_instance().asks))

        plt.cla()

        plt.hist(bids, bins=bins, edgecolor='orange', label='BIDS')
        plt.hist(asks, bins=bins, edgecolor='yellow', label='ASKS')

        plt.legend()
        plt.title('Marketplace info')

        plt.xlabel('Order prices')
        plt.ylabel('Number of orders')

    ani = FuncAnimation(plt.gcf(), animate, interval=2000)

    plt.tight_layout()
    plt.show()
    def __init__(self):
        # set the default state and start the default function
        self.state = states.Idle()
        # init the marketplace
        print('Initializing the marketplace instance')
        self.market = Marketplace()

        idle(self)
示例#5
0
def make_plot():
    plt.style.use('seaborn')
    m = Marketplace.get_instance()
    # TODO: have bins represent the data from the marketplace better
    bins = [x for x in range(0, 400, 5)]

    bids = sorted(order_to_int(Marketplace.get_instance().bids))
    asks = sorted(order_to_int(Marketplace.get_instance().asks))

    bids_t = order_to_timestamp(sorted(m.bids, key=lambda x: x.timestamp))
    asks_t = order_to_timestamp(sorted(m.asks, key=lambda x: x.timestamp))

    fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, ncols=1)

    ax1.hist(bids, bins=bins, edgecolor='orange', label='BIDS', log=True)
    ax1.hist(asks, bins=bins, edgecolor='yellow', label='ASKS', log=True)

    ax2.plot(bids_t,
             order_to_int(m.bids),
             color='#444444',
             linestyle='--',
             label='BIDS')
    ax3.plot(asks_t,
             order_to_int(m.asks),
             color='#888444',
             linestyle='--',
             label='ASKS')

    ax1.legend()
    plt.title('Marketplace info')

    ax1.set_xlabel('Order prices')
    ax1.set_ylabel('Number of orders')

    ax2.legend()
    ax2.set_xlabel('BIDS')
    ax2.set_ylabel('Timestamp')

    ax3.legend()
    ax3.set_xlabel('ASKS')
    ax3.set_ylabel('Timestamp')

    plt.tight_layout()

    plt.show()
示例#6
0
    def animate(i):
        handle_func(generate())

        bins = [x for x in range(0, 400, 5)]

        bids = sorted(order_to_int(Marketplace.get_instance().bids))
        asks = sorted(order_to_int(Marketplace.get_instance().asks))

        plt.cla()

        plt.hist(bids, bins=bins, edgecolor='orange', label='BIDS')
        plt.hist(asks, bins=bins, edgecolor='yellow', label='ASKS')

        plt.legend()
        plt.title('Marketplace info')

        plt.xlabel('Order prices')
        plt.ylabel('Number of orders')
def load():
    # order_type,order_action,limit_price,timestamp,quantity

    path = 'C:/Users/aleks/PycharmProjects/marketplace/utils/marketplace_orders.csv'

    data = pd.read_csv(path)
    order_action = data['order_action']
    limit_price = data['limit_price']
    timestamp = data['timestamp']
    quantity = data['quantity']

    for i in range(0, len(data)):
        limit_price_order = LimitPriceOrder(order_action[i], timestamp[i],
                                            quantity[i], limit_price[i])
        if order_action[i] == 'ask':
            Marketplace.get_instance().asks.append(limit_price_order)
        else:
            Marketplace.get_instance().bids.append(limit_price_order)

    # sort loaded data
    # for easier plotting down the line

    # pre-sort the ask and bids
    # self.market.get_instance().asks = sorted(self.market.get_instance().asks)
    # self.market.get_instance().bids = sorted(self.market.get_instance().bids)

    print('BIDS')
    for item in Marketplace.get_instance().bids:
        print(item)

    print('ASKS')
    for item in Marketplace.get_instance().asks:
        print(item)
    print(f'Lowest ask: {Marketplace.get_instance().lowest_ask()}')
    print(f'Highest bid: {Marketplace.get_instance().highest_bid()}')
def save():
    # don't do this
    path = 'C:/Users/aleks/PycharmProjects/marketplace/utils/marketplace_orders.csv'
    fieldnames = [
        'order_type', 'order_action', 'limit_price', 'timestamp', 'quantity'
    ]

    # get the newly added order to the csv
    # importing os module? never heard of it
    with open(path, 'w') as csv_file:
        # lpo,ask,113,2020-08-24 13:49:18.692822,1
        csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        csv_writer.writeheader()

    write_items(
        Marketplace.get_instance().bids,
        'C:/Users/aleks/PycharmProjects/marketplace/utils/marketplace_orders.csv',
        fieldnames)
    write_items(
        Marketplace.get_instance().asks,
        'C:/Users/aleks/PycharmProjects/marketplace/utils/marketplace_orders.csv',
        fieldnames)
def handle_lpo(u_order, asks, bids):
    # could these two blocks have been written just once? -probably
    # should I have done so? -yeah
    if u_order.action == 'bid':
        for item in asks:
            if u_order.quantity == 0:
                return
            if item.limit_price <= u_order.limit_price:
                # item.quantity < order quantity
                if item.quantity < u_order.quantity:
                    u_order.quantity -= item.quantity
                    print(f'Bought {item.quantity} of {item}')
                    Marketplace.get_instance().asks.remove(item)
                # item.quantity >= order quantity
                else:
                    item.quantity -= u_order.quantity
                    print(f'Bought {u_order.quantity} of {item}')
                    if item.quantity == 0:
                        Marketplace.get_instance().asks.remove(item)
                    return
        print(f'Added {u_order}')
        Marketplace.get_instance().bids.append(u_order)

    if u_order.action == 'ask':
        for item in bids:
            if u_order.quantity == 0:
                return
            if item.limit_price >= u_order.limit_price:
                if item.quantity < u_order.quantity:
                    u_order.quantity -= item.quantity
                    print(f'Sold {item.quantity} of {item}')
                    Marketplace.get_instance().bids.remove(item)
                else:
                    item.quantity -= u_order.quantity
                    print(f'Sold {u_order.quantity} of {item}')
                    if item.quantity == 0:
                        Marketplace.get_instance().bids.remove(item)
                    return
        print(f'Added {u_order}')
        Marketplace.get_instance().asks.append(u_order)