volume=-p,
                           side='bid',
                           order_type='ioc')
    print(e.get_positions())


# bid, volume p, price 1 - 'I want to buy p units at price 1'
# ask, volume p, price 1 - 'I want to sell p units at price 1'

# print(e.get_trade_history(instrument_id))
#book = e.get_last_price_book(instrument_id)
#print(book.bids)
#print(book.asks)

# current positions
print(e.get_positions_and_cash())

processed_order_book = e.get_last_price_book(instrument_id)
print("bid | price | ask")
for level in processed_order_book:
    print(f"{level.bid_volume}|{level.price_level}|{level.ask_volume}")

# e.insert_order(instrument_id, price=71.70, volume=1, side='bid', order_type='limit')
"""
# buy something
e.insert_order(instrument_id, price=70, volume=1, side='bid', order_type='limit')

print(e.get_positions_and_cash())

time.sleep(1)
示例#2
0
class Bot:
    instruments = ["PHILIPS_A", "PHILIPS_B"]

    def __init__(self):
        self.e = Exchange()
        logging.info(self.e.connect())
        logging.info("Setup was successful.")

    def get_out_of_positions(self):
        # Get out of all positions you are currently holding, regardless of the loss involved. That means selling whatever
        # you are long, and buying-back whatever you are short. Be sure you know what you are doing when you use this logic.
        print(self.e.get_positions())
        for s, p in self.e.get_positions().items():
            if p > 0:
                self.e.insert_order(s,
                                    price=1,
                                    volume=p,
                                    side='ask',
                                    order_type='ioc')
            elif p < 0:
                self.e.insert_order(s,
                                    price=100000,
                                    volume=-p,
                                    side='bid',
                                    order_type='ioc')
        print(self.e.get_positions())

    # Logging functions

    def log_new_trade_ticks(self):
        logger.info("Polling new trade ticks")
        for i in self.instruments:
            tradeticks = self.e.poll_new_trade_ticks(i)
            for t in tradeticks:
                logger.info(
                    f"[{t.instrument_id}] price({t.price}), volume({t.volume}), aggressor_side({t.aggressor_side}), buyer({t.buyer}), seller({t.seller})"
                )

    def log_positions_cash(self):
        logger.info(self.e.get_positions_and_cash())

    def log_all_outstanding_orders(self):
        for i in self.instruments:
            logger.info(self.e.get_outstanding_orders(i))

    def wait_until_orders_complete(self):
        orders_outstanding = True
        while orders_outstanding:
            orders_outstanding = False
            for i in self.instruments:
                if len(self.e.get_outstanding_orders(i)) > 0:
                    orders_outstanding = True
            self.log_all_outstanding_orders()
            #time.sleep(0.1)

    def mainloop(self):
        while True:
            # check for trade differences
            # m1 ask < m2 bid
            #logger.info("Checking for discrepancies:")
            books = [self.e.get_last_price_book(x) for x in self.instruments]
            for m1, m2 in [(0, 1), (1, 0)]:
                m1_id = self.instruments[m1]
                m2_id = self.instruments[m2]
                try:
                    m1_ask = books[m1].asks[0]
                    m2_bid = books[m2].bids[0]
                    if m1_ask.price < m2_bid.price:
                        logger.info(
                            f"Can profit: buy {m1_id} at {m1_ask} and sell {m2_id} at {m2_bid}"
                        )
                        self.e.insert_order(m1_id,
                                            price=m1_ask.price,
                                            volume=1,
                                            side='bid',
                                            order_type='limit')
                        self.e.insert_order(m2_id,
                                            price=m2_bid.price,
                                            volume=1,
                                            side='ask',
                                            order_type='limit')
                        self.log_all_outstanding_orders()
                        self.wait_until_orders_complete()
                        self.log_positions_cash()
                except Exception as e:
                    print(logger.error(e))
                    continue
            time.sleep(1.0 / 25)
示例#3
0
    low_range_b = stats_b[1] - stats_b[2]

    book_a = e.get_last_price_book(philips_a)
    book_b = e.get_last_price_book(philips_b)

    if len(book_a.asks) != 0 and len(book_a.bids) != 0:
        current_ask_a = book_a.asks[0].price
        current_bid_a = book_a.bids[0].price
    if len(book_b.asks) != 0 and len(book_b.bids) != 0:
        current_ask_b = book_b.asks[0].price
        current_bid_b = book_b.bids[0].price
    #print(current_ask)

    average = average_b(2000)
    #print(average)
    positions = e.get_positions_and_cash()
    volume_left_a = positions[philips_a]['volume']
    volume_left_b = positions[philips_b]['volume']

    # if current_bid_a-np.mean(weighted_prices_a)>1.5:

    # SELLING A WHEN HIGH
    if current_bid_a > np.mean(weighted_prices_a) and volume_left_a > -200:
        price = current_bid_a
        volume = max(int(10 * (current_bid_a - stats_a[1]) / max_range_a), 1)
        buying_order = e.insert_order(philips_a,
                                      price=current_bid_a,
                                      volume=float(volume),
                                      side='ask',
                                      order_type='limit')
        print('Selling %s A at price %s ****** Expect to buy at %s' %