Ejemplo n.º 1
0
    def test_order_book_market_order(self):
        ob = OrderBook(_INSTRUMENT)

        _seed(ob, _INSTRUMENT)

        assert ob.topOfBook()[Side.BUY] == [5.0, 1.0]
        assert ob.topOfBook()[Side.SELL] == [5.5, 1.0]

        data = Order(
            volume=100.0,
            price=0.0,
            side=Order.Sides.SELL,
            order_type=Order.Types.MARKET,
            instrument=_INSTRUMENT,
        )
        ob.add(data)

        print(ob)
        print(ob.topOfBook())
        assert ob.topOfBook() == {Side.BUY: [0, 0], Side.SELL: [5.5, 1.0]}
        print(ob.levels(3))
        assert ob.levels(3) == {
            Side.BUY: [[0, 0], [0, 0], [0, 0]],
            Side.SELL: [[5.5, 1.0], [6.0, 1.0], [6.5, 1.0]],
        }
Ejemplo n.º 2
0
 def setup(self):
     self.ob = OrderBook(_INSTRUMENT)
     _seed(self.ob, _INSTRUMENT)
     assert self.ob.topOfBook() == {
         Side.BUY: [5.0, 1.0],
         Side.SELL: [5.5, 1.0]
     }
Ejemplo n.º 3
0
    def test_all_or_none_maker(self):
        self.ob = OrderBook(_INSTRUMENT)
        _seed(self.ob, _INSTRUMENT, Order.Flags.ALL_OR_NONE)
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(volume=0.5,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [4.5, 1.0],
            Side.SELL: [5.0, 0.5]
        }

        data = Order(volume=1.5,
                     price=4.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [3.5, 1.0],
            Side.SELL: [4.0, 0.5]
        }
Ejemplo n.º 4
0
    def test_order_book_clearing_order(self):
        ob = OrderBook(_INSTRUMENT)

        _seed(ob, _INSTRUMENT)

        assert ob.topOfBook()[Side.BUY] == [5.0, 1.0]
        assert ob.topOfBook()[Side.SELL] == [5.5, 1.0]

        data = Order(volume=100.0,
                     price=0.0,
                     side=Order.Sides.SELL,
                     instrument=_INSTRUMENT)
        ob.add(data)

        print(ob)
        print(ob.topOfBook())
        assert ob.topOfBook() == {Side.BUY: [0.0, 0.0], Side.SELL: [0.0, 90.0]}
        print(ob.levels(3))
        assert ob.levels(3) == {Side.BUY: [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], Side.SELL: [[0.0, 90.0], [5.5, 1.0], [6.0, 1.0]]}
Ejemplo n.º 5
0
    def test_fill_or_kill_maker(self):
        self.ob = OrderBook(_INSTRUMENT)
        _seed(self.ob, _INSTRUMENT, Order.Flags.FILL_OR_KILL)
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=0.5,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [4.5, 1.0],
            Side.SELL: [5.0, 0.5]
        }

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=1.5,
                     price=4.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [3.5, 1.0],
            Side.SELL: [4.0, 0.5]
        }
Ejemplo n.º 6
0
    def test_immediate_or_cancel_maker(self):
        self.ob = OrderBook(_INSTRUMENT)
        _seed(self.ob, _INSTRUMENT, Order.Flags.IMMEDIATE_OR_CANCEL)
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(volume=0.5,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     instrument=_INSTRUMENT)
        data.id = "1"
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [4.5, 1.0],
            Side.SELL: [5.5, 1.0]
        }
Ejemplo n.º 7
0
class TestOrderFlagsTaker:
    def setup(self):
        self.ob = OrderBook(_INSTRUMENT)
        _seed(self.ob, _INSTRUMENT)
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

    def test_fill_or_kill_market(self):
        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=2.0,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.MARKET,
                     flag=Order.Flags.FILL_OR_KILL,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=2.0,
                     price=4.5,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.MARKET,
                     flag=Order.Flags.FILL_OR_KILL,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [4.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

    def test_fill_or_kill_taker_limit(self):
        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=2.0,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     flag=Order.Flags.FILL_OR_KILL,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=2.0,
                     price=4.5,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     flag=Order.Flags.FILL_OR_KILL,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [4.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

    def test_all_or_none_market(self):
        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=1.5,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.MARKET,
                     flag=Order.Flags.ALL_OR_NONE,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=0.5,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.MARKET,
                     flag=Order.Flags.ALL_OR_NONE,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 0.5],
            Side.SELL: [5.5, 1.0]
        }

    def test_all_or_none_taker_limit(self):
        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=1.5,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     flag=Order.Flags.ALL_OR_NONE,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=0.5,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     flag=Order.Flags.ALL_OR_NONE,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 0.5],
            Side.SELL: [5.5, 1.0]
        }

    def test_immediate_or_cancel_market(self):
        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=2.0,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.MARKET,
                     flag=Order.Flags.IMMEDIATE_OR_CANCEL,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [4.5, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=2.0,
                     price=4.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.MARKET,
                     flag=Order.Flags.IMMEDIATE_OR_CANCEL,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [3.5, 1.0],
            Side.SELL: [5.5, 1.0]
        }

    def test_immediate_or_cancel_taker_limit(self):
        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=2.0,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     flag=Order.Flags.IMMEDIATE_OR_CANCEL,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [4.5, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=2.0,
                     price=4.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     flag=Order.Flags.IMMEDIATE_OR_CANCEL,
                     instrument=_INSTRUMENT)
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [3.5, 1.0],
            Side.SELL: [5.5, 1.0]
        }
Ejemplo n.º 8
0
    def test_stop_market(self):
        ob = OrderBook(_INSTRUMENT)

        _seed(ob, _INSTRUMENT)

        assert ob.topOfBook()[Side.BUY] == [5.0, 1.0]
        assert ob.topOfBook()[Side.SELL] == [5.5, 1.0]

        print(ob)
        assert ob.topOfBook() == {Side.BUY: [5.0, 1.0], Side.SELL: [5.5, 1.0]}

        data = Order(volume=0.0,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.STOP,
                     stop_target=Order(volume=0.5,
                                       price=4.5,
                                       side=Order.Sides.SELL,
                                       instrument=_INSTRUMENT,
                                       order_type=Order.Types.LIMIT),
                     instrument=_INSTRUMENT)
        print(ob)
        ob.add(data)

        print(ob.topOfBook())
        assert ob.topOfBook() == {Side.BUY: [5.0, 1.0], Side.SELL: [5.5, 1.0]}

        data = Order(volume=0.5,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     instrument=_INSTRUMENT)
        print(ob)
        ob.add(data)

        print(ob.topOfBook())
        assert ob.topOfBook() == {Side.BUY: [4.5, 1.0], Side.SELL: [5.5, 1.0]}
Ejemplo n.º 9
0
class TestOrderFlagsMaker:
    def test_fill_or_kill_maker(self):
        self.ob = OrderBook(_INSTRUMENT)
        _seed(self.ob, _INSTRUMENT, Order.Flags.FILL_OR_KILL)
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(
            volume=0.5,
            price=5.0,
            side=Order.Sides.SELL,
            order_type=Order.Types.LIMIT,
            instrument=_INSTRUMENT,
            id="1",
        )
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [4.5, 1.0],
            Side.SELL: [5.0, 0.5]
        }

        data = Order(
            volume=1.5,
            price=4.0,
            side=Order.Sides.SELL,
            order_type=Order.Types.LIMIT,
            instrument=_INSTRUMENT,
            id="1",
        )
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [3.5, 1.0],
            Side.SELL: [4.0, 0.5]
        }

    def test_all_or_none_maker(self):
        self.ob = OrderBook(_INSTRUMENT)
        _seed(self.ob, _INSTRUMENT, Order.Flags.ALL_OR_NONE)
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(
            volume=0.5,
            price=5.0,
            side=Order.Sides.SELL,
            order_type=Order.Types.LIMIT,
            instrument=_INSTRUMENT,
            id="1",
        )
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [4.5, 1.0],
            Side.SELL: [5.0, 0.5]
        }

        data = Order(
            volume=1.5,
            price=4.0,
            side=Order.Sides.SELL,
            order_type=Order.Types.LIMIT,
            instrument=_INSTRUMENT,
            id="1",
        )
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [3.5, 1.0],
            Side.SELL: [4.0, 0.5]
        }

    def test_immediate_or_cancel_maker(self):
        self.ob = OrderBook(_INSTRUMENT)
        _seed(self.ob, _INSTRUMENT, Order.Flags.IMMEDIATE_OR_CANCEL)
        assert self.ob.topOfBook() == {
            Side.BUY: [5.0, 1.0],
            Side.SELL: [5.5, 1.0]
        }

        data = Order(
            volume=0.5,
            price=5.0,
            side=Order.Sides.SELL,
            order_type=Order.Types.LIMIT,
            instrument=_INSTRUMENT,
            id="1",
        )
        print(self.ob)
        self.ob.add(data)

        print(self.ob.topOfBook())
        assert self.ob.topOfBook() == {
            Side.BUY: [4.5, 1.0],
            Side.SELL: [5.5, 1.0]
        }
Ejemplo n.º 10
0
 def test_order_book_init(self):
     ob = OrderBook(_INSTRUMENT)
     print(ob.topOfBook())
     assert ob.topOfBook()[Side.BUY] == [0, 0]
     assert ob.topOfBook()[Side.SELL] == [float('inf'), 0]
Ejemplo n.º 11
0
    def test_order_book_run(self):
        ob = OrderBook(_INSTRUMENT)
        _seed(ob, _INSTRUMENT)

        assert ob.topOfBook()[Side.BUY] == [5.0, 1.0]
        assert ob.topOfBook()[Side.SELL] == [5.5, 1.0]

        data = Order(volume=5.0,
                     price=4.5,
                     side=Order.Sides.SELL,
                     instrument=_INSTRUMENT)
        ob.add(data)

        print(ob)
        assert ob.topOfBook() == {Side.BUY: [4.0, 1.0], Side.SELL: [4.5, 3.0]}
        print(ob.levels(3))
        assert ob.levels(3) == {Side.BUY: [[4.0, 1.0], [3.5, 1.0], [3.0, 1.0]], Side.SELL: [[4.5, 3.0], [5.5, 1.0], [6.0, 1.0]]}

        data = Order(volume=4.0,
                     price=5.5,
                     side=Order.Sides.BUY,
                     instrument=_INSTRUMENT)
        ob.add(data)

        print(ob)
        assert ob.topOfBook() == {Side.BUY: [4.0, 1.0], Side.SELL: [6.0, 1.0]}
        print(ob.levels(3))
        assert ob.levels(3) == {Side.BUY: [[4.0, 1.0], [3.5, 1.0], [3.0, 1.0]], Side.SELL: [[6.0, 1.0], [6.5, 1.0], [7.0, 1.0]]}
Ejemplo n.º 12
0
#                         0.0))
#     print(orderbook)
#     start = round(start + increment, 2)

# print(orderbook)

# Event(EventType.OPEN, o)

from datetime import datetime
from aat.config import Side
from aat.core import Instrument, OrderBook, Order
from aat.tests.core.order_book.helpers import _seed

_INSTRUMENT = Instrument('TE.ST')

ob = OrderBook(_INSTRUMENT)

_seed(ob, _INSTRUMENT)

assert ob.topOfBook()[Side.BUY] == [5.0, 1.0]
assert ob.topOfBook()[Side.SELL] == [5.5, 1.0]

print(ob)
assert ob.topOfBook() == {Side.BUY: [5.0, 1.0], Side.SELL: [5.5, 1.0]}

data = Order(id=1,
             timestamp=datetime.now(),
             volume=0.0,
             price=5.0,
             side=Order.Sides.SELL,
             order_type=Order.Types.STOP,
Ejemplo n.º 13
0
    def test_stop_limit(self):
        ob = OrderBook(_INSTRUMENT)

        _seed(ob, _INSTRUMENT)

        assert ob.topOfBook()[Side.BUY] == [5.0, 1.0]
        assert ob.topOfBook()[Side.SELL] == [5.5, 1.0]

        print(ob)
        assert ob.topOfBook() == {Side.BUY: [5.0, 1.0], Side.SELL: [5.5, 1.0]}

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=0.0,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.STOP,
                     stop_target=Order(id=1,
                                       timestamp=datetime.now(),
                                       volume=1.0,
                                       price=4.0,
                                       side=Order.Sides.SELL,
                                       order_type=Order.Types.MARKET,
                                       instrument=_INSTRUMENT),
                     instrument=_INSTRUMENT)
        print(ob)
        ob.add(data)

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=0.0,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.STOP,
                     stop_target=Order(id=1,
                                       timestamp=datetime.now(),
                                       volume=0.5,
                                       price=5.0,
                                       side=Order.Sides.SELL,
                                       instrument=_INSTRUMENT),
                     instrument=_INSTRUMENT)
        print(ob)
        ob.add(data)

        print(ob.topOfBook())
        assert ob.topOfBook() == {Side.BUY: [5.0, 1.0], Side.SELL: [5.5, 1.0]}

        data = Order(id=1,
                     timestamp=datetime.now(),
                     volume=0.5,
                     price=5.0,
                     side=Order.Sides.SELL,
                     order_type=Order.Types.LIMIT,
                     instrument=_INSTRUMENT)
        print(ob)
        ob.add(data)

        print(ob.topOfBook())
        assert ob.topOfBook() == {Side.BUY: [4.5, 0.5], Side.SELL: [5.0, 0.5]}