示例#1
0
from optibook.synchronous_client import Exchange

import logging
logger = logging.getLogger('client')
logger.setLevel('ERROR')

print("Setup was successful.")

instrument_id = 'PHILIPS_A'

e = Exchange()
a = e.connect()

print("Exchange connected")

#print(e.is_connected())
#result = e.insert_order(instrument_id, price=1., volume=1, side='bid', order_type='limit')
#print(f"Order Id: {result}")
#print(e.is_connected())

orders = e.get_outstanding_orders(instrument_id)
for o in orders.values():
    print(o)
print(orders)

print("Orders checked")
示例#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
 B_best_bid = e.get_last_price_book(instrument_id2).bids[0].price
 B_best_ask = e.get_last_price_book(instrument_id2).asks[0].price
 
 # print(A_best_bid, A_best_ask, B_best_bid, B_best_ask)
 
 if A_best_bid > B_best_ask:
     A_best_bid_vol = e.get_last_price_book(instrument_id1).bids[0].volume
     B_best_ask_vol = e.get_last_price_book(instrument_id2).asks[0].volume
     
     volume = min(A_best_bid_vol, B_best_ask_vol)
     
     result = e.insert_order(instrument_id1, price = A_best_bid, volume=volume, side='bid', order_type='limit')
     result = e.insert_order(instrument_id2, price = B_best_ask, volume=volume, side='ask', order_type='limit')
     print(f"Order Id: {result}")
     
 if B_best_bid > A_best_ask:
     A_best_ask_vol = e.get_last_price_book(instrument_id1).asks[0].volume
     B_best_bid_vol = e.get_last_price_book(instrument_id2).bids[0].volume
     
     volume = min(A_best_ask_vol, B_best_bid_vol)
     
     result = e.insert_order(instrument_id2, price = B_best_bid, volume=volume, side='bid', order_type='limit')
     result = e.insert_order(instrument_id1, price = A_best_ask, volume=volume, side='ask', order_type='limit')
     print(f"Order Id: {result}")
 
 time.sleep(0.25)
 
 if len(e.get_outstanding_orders(instrument_id1)) != 0:
     e.delete_orders(instrument_id1)
 if len(e.get_outstanding_orders(instrument_id2)) != 0:
     e.delete_orders(instrument_id2)
    if (bestAsk is not None):
        e.insert_order(highLiq,
                       price=bestAsk + 0.1,
                       volume=5,
                       side='ask',
                       order_type='limit')
        time.sleep(get_lag() - 2)
        currentTradeId = e.insert_order(lowLiq,
                                        price=bestAsk,
                                        volume=5,
                                        side='bid',
                                        order_type='limit')
        print("Sell to A")
        count_trades = count_trades + 1
        #trades = e.poll_new_trades("PHILIPS_B")
        time.sleep(3)
        if (e.get_outstanding_orders("PHILIPS_B").values()):
            # While there are still outstanding orders
            e.insert_order(highLiq,
                           price=bestAsk,
                           volume=5,
                           side='bid',
                           order_type='limit')
            e.delete_order(currentTradeId, order_id=lowLiq)

        print("Buy from B")
    else:
        print("No best ask")
    # noSold = trades[0].volume

printTrades(e)
示例#5
0
    i += 1
    if (i == 100):
        trades = e.get_trade_history('PHILIPS_A')
        for t in trades:
            print(
                f"[TRADED {t.instrument_id}] price({t.price}), volume({t.volume}), side({t.side})"
            )
        trades = e.get_trade_history('PHILIPS_B')
        for t in trades:
            print(
                f"[TRADED {t.instrument_id}] price({t.price}), volume({t.volume}), side({t.side})"
            )
        printOutstanding()
        i = 0

outstanding = e.get_outstanding_orders("PHILIPS_A")
for o in outstanding.values():
    print(
        f"Outstanding order: order_id({o.order_id}), instrument_id({o.instrument_id}), price({o.price}), volume({o.volume}), side({o.side})"
    )
outstanding = e.get_outstanding_orders("PHILIPS_B")
for o in outstanding.values():
    print(
        f"Outstanding order: order_id({o.order_id}), instrument_id({o.instrument_id}), price({o.price}), volume({o.volume}), side({o.side})"
    )

trades = e.get_trade_history('PHILIPS_A')
for t in trades:
    print(
        f"[TRADED {t.instrument_id}] price({t.price}), volume({t.volume}), side({t.side})"
    )