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 __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 test_extending_new_old(self): # Create a new account with some orders account1 = protoex.Account(1, "Test", protoex.StateEx.good | protoex.StateEx.happy, protoex.Balance(proto.Balance("USD", 1000.0), 123.456), protoex.Balance(proto.Balance("EUR", 100.0), 12.34)) account1.orders.append(protoex.Order(1, "EURUSD", protoex.OrderSide.buy, protoex.OrderType.market, 1.23456, 1000.0, 0.0, 0.0)) account1.orders.append(protoex.Order(2, "EURUSD", protoex.OrderSide.sell, protoex.OrderType.limit, 1.0, 100.0, 0.1, -0.1)) account1.orders.append(protoex.Order(3, "EURUSD", protoex.OrderSide.tell, protoex.OrderType.stoplimit, 1.5, 10.0, 1.1, -1.1)) # Serialize the account to the FBE stream writer = protoex.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, 316) # Deserialize the account from the FBE stream account2 = proto.Account() reader = proto.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(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.assertNotEqual(account2.orders[2].side, proto.OrderSide.buy) self.assertNotEqual(account2.orders[2].type, proto.OrderType.market) self.assertEqual(account2.orders[2].price, 1.5) self.assertEqual(account2.orders[2].volume, 10.0)
def main(): # Create a new account using FBE model account = proto.AccountModel(fbe.WriteBuffer()) model_begin = account.create_begin() account_begin = account.model.set_begin() account.model.uid.set(1) account.model.name.set("Test") account.model.state.set(proto.State.good) wallet_begin = account.model.wallet.set_begin() account.model.wallet.currency.set("USD") account.model.wallet.amount.set(1000.0) account.model.wallet.set_end(wallet_begin) account.model.set_end(account_begin) account.create_end(model_begin) assert account.verify() # Show the serialized FBE size print("FBE size: {}".format(account.buffer.size)) # Access the account using the FBE model access = proto.AccountModel(fbe.ReadBuffer()) access.attach_buffer(account.buffer) assert access.verify() account_begin = access.model.get_begin() uid = access.model.uid.get() name = access.model.name.get() state = access.model.state.get() wallet_begin = access.model.wallet.get_begin() wallet_currency = access.model.wallet.currency.get() wallet_amount = access.model.wallet.amount.get() access.model.wallet.get_end(wallet_begin) access.model.get_end(account_begin) # Show account content print() print("account.uid = {}".format(uid)) print("account.name = {}".format(name)) print("account.state = {}".format(state)) print("account.wallet.currency = {}".format(wallet_currency)) print("account.wallet.amount = {}".format(wallet_amount))
def test_create_and_access(self): # Create a new account using FBE model into the FBE stream writer = proto.AccountModel(fbe.WriteBuffer()) self.assertEqual(writer.model.fbe_offset, 4) model_begin = writer.create_begin() account_begin = writer.model.set_begin() writer.model.id.set(1) writer.model.name.set("Test") writer.model.state.set(proto.State.good) wallet_begin = writer.model.wallet.set_begin() writer.model.wallet.currency.set("USD") writer.model.wallet.amount.set(1000.0) writer.model.wallet.set_end(wallet_begin) asset_begin = writer.model.asset.set_begin(True) asset_wallet_begin = writer.model.asset.value.set_begin() writer.model.asset.value.currency.set("EUR") writer.model.asset.value.amount.set(100.0) writer.model.asset.set_end(asset_begin) writer.model.asset.value.set_end(asset_wallet_begin) order = writer.model.orders.resize(3) order_begin = order.set_begin() order.id.set(1) order.symbol.set("EURUSD") order.side.set(proto.OrderSide.buy) order.type.set(proto.OrderType.market) order.price.set(1.23456) order.volume.set(1000.0) order.set_end(order_begin) order.fbe_shift(order.fbe_size) order_begin = order.set_begin() order.id.set(2) order.symbol.set("EURUSD") order.side.set(proto.OrderSide.sell) order.type.set(proto.OrderType.limit) order.price.set(1.0) order.volume.set(100.0) order.set_end(order_begin) order.fbe_shift(order.fbe_size) order_begin = order.set_begin() order.id.set(3) order.symbol.set("EURUSD") order.side.set(proto.OrderSide.buy) order.type.set(proto.OrderType.stop) order.price.set(1.5) order.volume.set(10.0) order.set_end(order_begin) order.fbe_shift(order.fbe_size) writer.model.set_end(account_begin) serialized = writer.create_end(model_begin) 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) # Access the account model in the FBE stream reader = proto.AccountModel(fbe.ReadBuffer()) self.assertEqual(reader.model.fbe_offset, 4) reader.attach_buffer(writer.buffer) self.assertTrue(reader.verify()) account_begin = reader.model.get_begin() id = reader.model.id.get() self.assertEqual(id, 1) name = reader.model.name.get() self.assertEqual(name, "Test") state = reader.model.state.get() self.assertTrue(state.has_flags(proto.State.good)) wallet_begin = reader.model.wallet.get_begin() wallet_currency = reader.model.wallet.currency.get() self.assertEqual(wallet_currency, "USD") wallet_amount = reader.model.wallet.amount.get() self.assertEqual(wallet_amount, 1000.0) reader.model.wallet.get_end(wallet_begin) self.assertTrue(reader.model.asset.has_value) asset_begin = reader.model.asset.get_begin() asset_wallet_begin = reader.model.asset.value.get_begin() asset_wallet_currency = reader.model.asset.value.currency.get() self.assertEqual(asset_wallet_currency, "EUR") asset_wallet_amount = reader.model.asset.value.amount.get() self.assertEqual(asset_wallet_amount, 100.0) reader.model.asset.value.get_end(asset_wallet_begin) reader.model.asset.get_end(asset_begin) self.assertEqual(reader.model.orders.size, 3) o1 = reader.model.orders[0] order_begin = o1.get_begin() order_id = o1.id.get() self.assertEqual(order_id, 1) order_symbol = o1.symbol.get() self.assertEqual(order_symbol, "EURUSD") order_side = o1.side.get() self.assertEqual(order_side, proto.OrderSide.buy) order_type = o1.type.get() self.assertEqual(order_type, proto.OrderType.market) order_price = o1.price.get() self.assertEqual(order_price, 1.23456) order_volume = o1.volume.get() self.assertEqual(order_volume, 1000.0) o1.get_end(order_begin) o2 = reader.model.orders[1] order_begin = o2.get_begin() order_id = o2.id.get() self.assertEqual(order_id, 2) order_symbol = o2.symbol.get() self.assertEqual(order_symbol, "EURUSD") order_side = o2.side.get() self.assertEqual(order_side, proto.OrderSide.sell) order_type = o2.type.get() self.assertEqual(order_type, proto.OrderType.limit) order_price = o2.price.get() self.assertEqual(order_price, 1.0) order_volume = o2.volume.get() self.assertEqual(order_volume, 100.0) o1.get_end(order_begin) o3 = reader.model.orders[2] order_begin = o3.get_begin() order_id = o3.id.get() self.assertEqual(order_id, 3) order_symbol = o3.symbol.get() self.assertEqual(order_symbol, "EURUSD") order_side = o3.side.get() self.assertEqual(order_side, proto.OrderSide.buy) order_type = o3.type.get() self.assertEqual(order_type, proto.OrderType.stop) order_price = o3.price.get() self.assertEqual(order_price, 1.5) order_volume = o3.volume.get() self.assertEqual(order_volume, 10.0) o1.get_end(order_begin) reader.model.get_end(account_begin)
def test_serialization_enums(self): enums1 = enums.Enums() # Serialize enums to the FBE stream writer = enums.EnumsModel(fbe.WriteBuffer()) self.assertEqual(writer.model.fbe_offset, 4) serialized = writer.serialize(enums1) 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, 232) # Deserialize enums from the FBE stream enums2 = enums.Enums() reader = enums.EnumsModel(fbe.ReadBuffer()) self.assertEqual(reader.model.fbe_offset, 4) reader.attach_buffer(writer.buffer) self.assertTrue(reader.verify()) (enums2, deserialized) = reader.deserialize(enums2) self.assertEqual(deserialized, reader.buffer.size) reader.next(deserialized) self.assertEqual(reader.model.fbe_offset, (4 + reader.buffer.size)) self.assertEqual(enums2.byte0, enums.EnumByte.ENUM_VALUE_0) self.assertEqual(enums2.byte1, enums.EnumByte.ENUM_VALUE_1) self.assertEqual(enums2.byte2, enums.EnumByte.ENUM_VALUE_2) self.assertEqual(enums2.byte3, enums.EnumByte.ENUM_VALUE_3) self.assertEqual(enums2.byte4, enums.EnumByte.ENUM_VALUE_4) self.assertEqual(enums2.byte5, enums1.byte3) self.assertEqual(enums2.char0, enums.EnumChar.ENUM_VALUE_0) self.assertEqual(enums2.char1, enums.EnumChar.ENUM_VALUE_1) self.assertEqual(enums2.char2, enums.EnumChar.ENUM_VALUE_2) self.assertEqual(enums2.char3, enums.EnumChar.ENUM_VALUE_3) self.assertEqual(enums2.char4, enums.EnumChar.ENUM_VALUE_4) self.assertEqual(enums2.char5, enums1.char3) self.assertEqual(enums2.wchar0, enums.EnumWChar.ENUM_VALUE_0) self.assertEqual(enums2.wchar1, enums.EnumWChar.ENUM_VALUE_1) self.assertEqual(enums2.wchar2, enums.EnumWChar.ENUM_VALUE_2) self.assertEqual(enums2.wchar3, enums.EnumWChar.ENUM_VALUE_3) self.assertEqual(enums2.wchar4, enums.EnumWChar.ENUM_VALUE_4) self.assertEqual(enums2.wchar5, enums1.wchar3) self.assertEqual(enums2.int8b0, enums.EnumInt8.ENUM_VALUE_0) self.assertEqual(enums2.int8b1, enums.EnumInt8.ENUM_VALUE_1) self.assertEqual(enums2.int8b2, enums.EnumInt8.ENUM_VALUE_2) self.assertEqual(enums2.int8b3, enums.EnumInt8.ENUM_VALUE_3) self.assertEqual(enums2.int8b4, enums.EnumInt8.ENUM_VALUE_4) self.assertEqual(enums2.int8b5, enums1.int8b3) self.assertEqual(enums2.uint8b0, enums.EnumUInt8.ENUM_VALUE_0) self.assertEqual(enums2.uint8b1, enums.EnumUInt8.ENUM_VALUE_1) self.assertEqual(enums2.uint8b2, enums.EnumUInt8.ENUM_VALUE_2) self.assertEqual(enums2.uint8b3, enums.EnumUInt8.ENUM_VALUE_3) self.assertEqual(enums2.uint8b4, enums.EnumUInt8.ENUM_VALUE_4) self.assertEqual(enums2.uint8b5, enums1.uint8b3) self.assertEqual(enums2.int16b0, enums.EnumInt16.ENUM_VALUE_0) self.assertEqual(enums2.int16b1, enums.EnumInt16.ENUM_VALUE_1) self.assertEqual(enums2.int16b2, enums.EnumInt16.ENUM_VALUE_2) self.assertEqual(enums2.int16b3, enums.EnumInt16.ENUM_VALUE_3) self.assertEqual(enums2.int16b4, enums.EnumInt16.ENUM_VALUE_4) self.assertEqual(enums2.int16b5, enums1.int16b3) self.assertEqual(enums2.uint16b0, enums.EnumUInt16.ENUM_VALUE_0) self.assertEqual(enums2.uint16b1, enums.EnumUInt16.ENUM_VALUE_1) self.assertEqual(enums2.uint16b2, enums.EnumUInt16.ENUM_VALUE_2) self.assertEqual(enums2.uint16b3, enums.EnumUInt16.ENUM_VALUE_3) self.assertEqual(enums2.uint16b4, enums.EnumUInt16.ENUM_VALUE_4) self.assertEqual(enums2.uint16b5, enums1.uint16b3) self.assertEqual(enums2.int32b0, enums.EnumInt32.ENUM_VALUE_0) self.assertEqual(enums2.int32b1, enums.EnumInt32.ENUM_VALUE_1) self.assertEqual(enums2.int32b2, enums.EnumInt32.ENUM_VALUE_2) self.assertEqual(enums2.int32b3, enums.EnumInt32.ENUM_VALUE_3) self.assertEqual(enums2.int32b4, enums.EnumInt32.ENUM_VALUE_4) self.assertEqual(enums2.int32b5, enums1.int32b3) self.assertEqual(enums2.uint32b0, enums.EnumUInt32.ENUM_VALUE_0) self.assertEqual(enums2.uint32b1, enums.EnumUInt32.ENUM_VALUE_1) self.assertEqual(enums2.uint32b2, enums.EnumUInt32.ENUM_VALUE_2) self.assertEqual(enums2.uint32b3, enums.EnumUInt32.ENUM_VALUE_3) self.assertEqual(enums2.uint32b4, enums.EnumUInt32.ENUM_VALUE_4) self.assertEqual(enums2.uint32b5, enums1.uint32b3) self.assertEqual(enums2.int64b0, enums.EnumInt64.ENUM_VALUE_0) self.assertEqual(enums2.int64b1, enums.EnumInt64.ENUM_VALUE_1) self.assertEqual(enums2.int64b2, enums.EnumInt64.ENUM_VALUE_2) self.assertEqual(enums2.int64b3, enums.EnumInt64.ENUM_VALUE_3) self.assertEqual(enums2.int64b4, enums.EnumInt64.ENUM_VALUE_4) self.assertEqual(enums2.int64b5, enums1.int64b3) self.assertEqual(enums2.uint64b0, enums.EnumUInt64.ENUM_VALUE_0) self.assertEqual(enums2.uint64b1, enums.EnumUInt64.ENUM_VALUE_1) self.assertEqual(enums2.uint64b2, enums.EnumUInt64.ENUM_VALUE_2) self.assertEqual(enums2.uint64b3, enums.EnumUInt64.ENUM_VALUE_3) self.assertEqual(enums2.uint64b4, enums.EnumUInt64.ENUM_VALUE_4) self.assertEqual(enums2.uint64b5, enums1.uint64b3)