Ejemplo n.º 1
0
def main():
    # Create a new account with some orders
    account = proto.Account(1, "Test", proto.State.good,
                            proto.Balance("USD", 1000.0),
                            proto.Balance("EUR", 100.0))
    account.orders.append(
        proto.Order(1, "EURUSD", proto.OrderSide.buy, proto.OrderType.market,
                    1.23456, 1000.0))
    account.orders.append(
        proto.Order(2, "EURUSD", proto.OrderSide.sell, proto.OrderType.limit,
                    1.0, 100.0))
    account.orders.append(
        proto.Order(3, "EURUSD", proto.OrderSide.buy, proto.OrderType.stop,
                    1.5, 10.0))

    # Serialize the account to the FBE stream
    writer = proto.AccountFinalModel(fbe.WriteBuffer())
    writer.serialize(account)
    assert writer.verify()

    # Show the serialized FBE size
    print("FBE size: {}".format(writer.buffer.size))

    # Deserialize the account from the FBE stream
    reader = proto.AccountFinalModel(fbe.ReadBuffer())
    reader.attach_buffer(writer.buffer)
    assert reader.verify()
    reader.deserialize(account)

    # Show account content
    print()
    print(account)
    def send_and_receive(index):
        sender = MySender()

        # Create and send a new order
        order = proto.Order(1, "EURUSD", proto.OrderSide.buy,
                            proto.OrderType.market, 1.23456, 1000.0)
        sender.send(order)

        # Create and send a new balance wallet
        balance = proto.Balance("USD", 1000.0)
        sender.send(balance)

        # Create and send a new account with some orders
        account = proto.Account(1, "Test", proto.State.good,
                                proto.Balance("USD", 1000.0),
                                proto.Balance("EUR", 100.0))
        account.orders.append(
            proto.Order(1, "EURUSD", proto.OrderSide.buy,
                        proto.OrderType.market, 1.23456, 1000.0))
        account.orders.append(
            proto.Order(2, "EURUSD", proto.OrderSide.sell,
                        proto.OrderType.limit, 1.0, 100.0))
        account.orders.append(
            proto.Order(3, "EURUSD", proto.OrderSide.buy, proto.OrderType.stop,
                        1.5, 10.0))
        sender.send(account)

        receiver = MyReceiver()

        # Receive data from the sender
        index %= sender.buffer.size
        receiver.receive(sender.buffer, 0, index)
        receiver.receive(sender.buffer, index, sender.buffer.size - index)
        return receiver.check()
Ejemplo n.º 3
0
def main():
    # Create a new account with some orders
    account = proto.Account(1, "Test", proto.State.good,
                            proto.Balance("USD", 1000.0),
                            proto.Balance("EUR", 100.0))
    account.orders.append(
        proto.Order(1, "EURUSD", proto.OrderSide.buy, proto.OrderType.market,
                    1.23456, 1000.0))
    account.orders.append(
        proto.Order(2, "EURUSD", proto.OrderSide.sell, proto.OrderType.limit,
                    1.0, 100.0))
    account.orders.append(
        proto.Order(3, "EURUSD", proto.OrderSide.buy, proto.OrderType.stop,
                    1.5, 10.0))

    # Serialize the account to the JSON string
    json = account.to_json()

    # Show the serialized JSON and its size
    print("JSON: {}".format(json))
    print("JSON size: {}".format(len(json)))

    # Deserialize the account from the JSON string
    account = proto.Account.from_json(json)

    # Show account content
    print()
    print(account)
Ejemplo n.º 4
0
    def __init__(self):
        # Create a new account with some orders
        self.account = proto.Account(1, "Test", proto.State.good,
                                     proto.Balance("USD", 1000.0),
                                     proto.Balance("EUR", 100.0))
        self.account.orders.append(
            proto.Order(1, "EURUSD", proto.OrderSide.buy,
                        proto.OrderType.market, 1.23456, 1000.0))
        self.account.orders.append(
            proto.Order(2, "EURUSD", proto.OrderSide.sell,
                        proto.OrderType.limit, 1.0, 100.0))
        self.account.orders.append(
            proto.Order(3, "EURUSD", proto.OrderSide.buy, proto.OrderType.stop,
                        1.5, 10.0))

        # Serialize the account to the FBE stream
        self.writer = proto.AccountFinalModel(fbe.WriteBuffer())
        self.writer.serialize(self.account)
        assert self.writer.verify()

        # Deserialize the account from the FBE stream
        self.reader = proto.AccountFinalModel(fbe.ReadBuffer())
        self.reader.attach_buffer(self.writer.buffer)
        assert self.reader.verify()
        self.reader.deserialize(self.account)
    def __init__(self):
        # Create a new account with some orders
        self.account = proto.Account(1, "Test", proto.State.good, proto.Balance("USD", 1000.0), proto.Balance("EUR", 100.0))
        self.account.orders.append(proto.Order(1, "EURUSD", proto.OrderSide.buy, proto.OrderType.market, 1.23456, 1000.0))
        self.account.orders.append(proto.Order(2, "EURUSD", proto.OrderSide.sell, proto.OrderType.limit, 1.0, 100.0))
        self.account.orders.append(proto.Order(3, "EURUSD", proto.OrderSide.buy, proto.OrderType.stop, 1.5, 10.0))

        # Serialize the account to the JSON string
        self.json = self.account.to_json()

        # Deserialize the account from the JSON string
        self.account = proto.Account.from_json(self.json)
Ejemplo n.º 6
0
    def test_clone(self):
        # Create a new account with some orders
        account1 = proto.Account(1, "Test", proto.State.good,
                                 proto.Balance("USD", 1000.0),
                                 proto.Balance("EUR", 100.0))
        account1.orders.append(
            proto.Order(1, "EURUSD", proto.OrderSide.buy,
                        proto.OrderType.market, 1.23456, 1000.0))
        account1.orders.append(
            proto.Order(2, "EURUSD", proto.OrderSide.sell,
                        proto.OrderType.limit, 1.0, 100.0))
        account1.orders.append(
            proto.Order(3, "EURUSD", proto.OrderSide.buy, proto.OrderType.stop,
                        1.5, 10.0))

        # Clone the account
        account2 = account1.clone()

        # Clear the source account
        # noinspection PyUnusedLocal
        account1 = proto.Account()

        self.assertEqual(account2.uid, 1)
        self.assertEqual(account2.name, "Test")
        self.assertTrue(account2.state.has_flags(proto.State.good))
        self.assertEqual(account2.wallet.currency, "USD")
        self.assertEqual(account2.wallet.amount, 1000.0)
        self.assertNotEqual(account2.asset, None)
        self.assertEqual(account2.asset.currency, "EUR")
        self.assertEqual(account2.asset.amount, 100.0)
        self.assertEqual(len(account2.orders), 3)
        self.assertEqual(account2.orders[0].uid, 1)
        self.assertEqual(account2.orders[0].symbol, "EURUSD")
        self.assertEqual(account2.orders[0].side, proto.OrderSide.buy)
        self.assertEqual(account2.orders[0].type, proto.OrderType.market)
        self.assertEqual(account2.orders[0].price, 1.23456)
        self.assertEqual(account2.orders[0].volume, 1000.0)
        self.assertEqual(account2.orders[1].uid, 2)
        self.assertEqual(account2.orders[1].symbol, "EURUSD")
        self.assertEqual(account2.orders[1].side, proto.OrderSide.sell)
        self.assertEqual(account2.orders[1].type, proto.OrderType.limit)
        self.assertEqual(account2.orders[1].price, 1.0)
        self.assertEqual(account2.orders[1].volume, 100.0)
        self.assertEqual(account2.orders[2].uid, 3)
        self.assertEqual(account2.orders[2].symbol, "EURUSD")
        self.assertEqual(account2.orders[2].side, proto.OrderSide.buy)
        self.assertEqual(account2.orders[2].type, proto.OrderType.stop)
        self.assertEqual(account2.orders[2].price, 1.5)
        self.assertEqual(account2.orders[2].volume, 10.0)
Ejemplo n.º 7
0
    def __init__(self):
        # Create a new account with some orders
        self.account = proto.Account(1, "Test", proto.State.good, proto.Balance("USD", 1000.0), proto.Balance("EUR", 100.0))
        self.account.orders.append(proto.Order(1, "EURUSD", proto.OrderSide.buy, proto.OrderType.market, 1.23456, 1000.0))
        self.account.orders.append(proto.Order(2, "EURUSD", proto.OrderSide.sell, proto.OrderType.limit, 1.0, 100.0))
        self.account.orders.append(proto.Order(3, "EURUSD", proto.OrderSide.buy, proto.OrderType.stop, 1.5, 10.0))

        self.sender1 = MySender1()
        self.sender1.send(self.account)

        self.sender2 = MySender2()
        self.sender2.send(self.account)

        self.receiver = MyReceiver()
        self.receiver.receive(self.sender2.buffer)
def main():
    sender = MySender()

    # Enable logging
    sender.logging = True

    # Create and send a new order
    order = proto.Order(1, "EURUSD", proto.OrderSide.buy,
                        proto.OrderType.market, 1.23456, 1000.0)
    sender.send(order)

    # Create and send a new balance wallet
    balance = proto.Balance("USD", 1000.0)
    sender.send(balance)

    # Create and send a new account with some orders
    account = proto.Account(1, "Test", proto.State.good,
                            proto.Balance("USD", 1000.0),
                            proto.Balance("EUR", 100.0))
    account.orders.append(
        proto.Order(1, "EURUSD", proto.OrderSide.buy, proto.OrderType.market,
                    1.23456, 1000.0))
    account.orders.append(
        proto.Order(2, "EURUSD", proto.OrderSide.sell, proto.OrderType.limit,
                    1.0, 100.0))
    account.orders.append(
        proto.Order(3, "EURUSD", proto.OrderSide.buy, proto.OrderType.stop,
                    1.5, 10.0))
    sender.send(account)

    receiver = MyReceiver()

    # Enable logging
    receiver.logging = True

    # Receive all data from the sender
    receiver.receive(sender.buffer)
    def test_extending_old_new(self):
        # Create a new account with some orders
        account1 = proto.Account(1, "Test", proto.State.good, proto.Balance("USD", 1000.0), proto.Balance("EUR", 100.0))
        account1.orders.append(proto.Order(1, "EURUSD", proto.OrderSide.buy, proto.OrderType.market, 1.23456, 1000.0))
        account1.orders.append(proto.Order(2, "EURUSD", proto.OrderSide.sell, proto.OrderType.limit, 1.0, 100.0))
        account1.orders.append(proto.Order(3, "EURUSD", proto.OrderSide.buy, proto.OrderType.stop, 1.5, 10.0))

        # Serialize the account to the FBE stream
        writer = proto.AccountModel(fbe.WriteBuffer())
        self.assertEqual(writer.model.fbe_offset, 4)
        serialized = writer.serialize(account1)
        self.assertEqual(serialized, writer.buffer.size)
        self.assertTrue(writer.verify())
        writer.next(serialized)
        self.assertEqual(writer.model.fbe_offset, (4 + writer.buffer.size))

        # Check the serialized FBE size
        self.assertEqual(writer.buffer.size, 252)

        # Deserialize the account from the FBE stream
        account2 = protoex.Account()
        reader = protoex.AccountModel(fbe.ReadBuffer())
        self.assertEqual(reader.model.fbe_offset, 4)
        reader.attach_buffer(writer.buffer)
        self.assertTrue(reader.verify())
        (account2, deserialized) = reader.deserialize(account2)
        self.assertEqual(deserialized, reader.buffer.size)
        reader.next(deserialized)
        self.assertEqual(reader.model.fbe_offset, (4 + reader.buffer.size))

        self.assertEqual(account2.uid, 1)
        self.assertEqual(account2.name, "Test")
        self.assertTrue(account2.state.has_flags(protoex.StateEx.good))
        self.assertEqual(account2.wallet.currency, "USD")
        self.assertEqual(account2.wallet.amount, 1000.0)
        self.assertEqual(account2.wallet.locked, 0.0)
        self.assertNotEqual(account2.asset, None)
        self.assertEqual(account2.asset.currency, "EUR")
        self.assertEqual(account2.asset.amount, 100.0)
        self.assertEqual(account2.asset.locked, 0.0)
        self.assertEqual(len(account2.orders), 3)
        self.assertEqual(account2.orders[0].uid, 1)
        self.assertEqual(account2.orders[0].symbol, "EURUSD")
        self.assertEqual(account2.orders[0].side, protoex.OrderSide.buy)
        self.assertEqual(account2.orders[0].type, protoex.OrderType.market)
        self.assertEqual(account2.orders[0].price, 1.23456)
        self.assertEqual(account2.orders[0].volume, 1000.0)
        self.assertEqual(account2.orders[0].tp, 10.0)
        self.assertEqual(account2.orders[0].sl, -10.0)
        self.assertEqual(account2.orders[1].uid, 2)
        self.assertEqual(account2.orders[1].symbol, "EURUSD")
        self.assertEqual(account2.orders[1].side, protoex.OrderSide.sell)
        self.assertEqual(account2.orders[1].type, protoex.OrderType.limit)
        self.assertEqual(account2.orders[1].price, 1.0)
        self.assertEqual(account2.orders[1].volume, 100.0)
        self.assertEqual(account2.orders[1].tp, 10.0)
        self.assertEqual(account2.orders[1].sl, -10.0)
        self.assertEqual(account2.orders[2].uid, 3)
        self.assertEqual(account2.orders[2].symbol, "EURUSD")
        self.assertEqual(account2.orders[2].side, protoex.OrderSide.buy)
        self.assertEqual(account2.orders[2].type, protoex.OrderType.stop)
        self.assertEqual(account2.orders[2].price, 1.5)
        self.assertEqual(account2.orders[2].volume, 10.0)
        self.assertEqual(account2.orders[2].tp, 10.0)
        self.assertEqual(account2.orders[2].sl, -10.0)