Пример #1
0
        public void CreateAndAccess()
        {
            // Create a new account using FBE model into the FBE stream
            var writer = new FBE.proto.AccountModel();

            Assert.That(writer.model.FBEOffset == 4);
            long modelBegin   = writer.CreateBegin();
            long accountBegin = writer.model.SetBegin();

            writer.model.uid.Set(1);
            writer.model.name.Set("Test");
            writer.model.state.Set(proto.State.good);
            long walletBegin = writer.model.wallet.SetBegin();

            writer.model.wallet.currency.Set("USD");
            writer.model.wallet.amount.Set(1000.0);
            writer.model.wallet.SetEnd(walletBegin);
            long assetBegin       = writer.model.asset.SetBegin(true);
            long assetWalletBegin = writer.model.asset.Value.SetBegin();

            writer.model.asset.Value.currency.Set("EUR");
            writer.model.asset.Value.amount.Set(100.0);
            writer.model.asset.SetEnd(assetBegin);
            writer.model.asset.Value.SetEnd(assetWalletBegin);
            var  order      = writer.model.orders.Resize(3);
            long orderBegin = order.SetBegin();

            order.uid.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.SetEnd(orderBegin);
            order.FBEShift(order.FBESize);
            orderBegin = order.SetBegin();
            order.uid.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.SetEnd(orderBegin);
            order.FBEShift(order.FBESize);
            orderBegin = order.SetBegin();
            order.uid.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.SetEnd(orderBegin);
            order.FBEShift(order.FBESize);
            writer.model.SetEnd(accountBegin);
            long serialized = writer.CreateEnd(modelBegin);

            Assert.That(serialized == writer.Buffer.Size);
            Assert.That(writer.Verify());
            writer.Next(serialized);
            Assert.That(writer.model.FBEOffset == (4 + writer.Buffer.Size));

            // Check the serialized FBE size
            Assert.That(writer.Buffer.Size == 252);

            // Access the account model in the FBE stream
            var reader = new FBE.proto.AccountModel();

            Assert.That(reader.model.FBEOffset == 4);
            reader.Attach(writer.Buffer);
            Assert.That(reader.Verify());

            accountBegin = reader.model.GetBegin();
            reader.model.uid.Get(out var id);
            Assert.That(id == 1);
            reader.model.name.Get(out var name);
            Assert.That(name == "Test");
            reader.model.state.Get(out var state);
            Assert.That(state.HasFlags(proto.State.good));

            walletBegin = reader.model.wallet.GetBegin();
            reader.model.wallet.currency.Get(out var walletCurrency);
            Assert.That(walletCurrency == "USD");
            reader.model.wallet.amount.Get(out var walletAmount);
            Assert.That(walletAmount == 1000.0);
            reader.model.wallet.GetEnd(walletBegin);

            Assert.That(reader.model.asset.HasValue);
            assetBegin       = reader.model.asset.GetBegin();
            assetWalletBegin = reader.model.asset.Value.GetBegin();
            reader.model.asset.Value.currency.Get(out var assetWalletCurrency);
            Assert.That(assetWalletCurrency == "EUR");
            reader.model.asset.Value.amount.Get(out var assetWalletAmount);
            Assert.That(assetWalletAmount == 100.0);
            reader.model.asset.Value.GetEnd(assetWalletBegin);
            reader.model.asset.GetEnd(assetBegin);

            Assert.That(reader.model.orders.Size == 3);

            var o1 = reader.model.orders[0];

            orderBegin = o1.GetBegin();
            o1.uid.Get(out var orderUId1);
            Assert.That(orderUId1 == 1);
            o1.symbol.Get(out var orderSymbol1);
            Assert.That(orderSymbol1 == "EURUSD");
            o1.side.Get(out var orderSide1);
            Assert.That(orderSide1 == proto.OrderSide.buy);
            o1.type.Get(out var orderType1);
            Assert.That(orderType1 == proto.OrderType.market);
            o1.price.Get(out var orderPrice1);
            Assert.That(orderPrice1 == 1.23456);
            o1.volume.Get(out var orderVolume1);
            Assert.That(orderVolume1 == 1000.0);
            o1.GetEnd(orderBegin);

            var o2 = reader.model.orders[1];

            orderBegin = o2.GetBegin();
            o2.uid.Get(out var orderUId2);
            Assert.That(orderUId2 == 2);
            o2.symbol.Get(out var orderSymbol2);
            Assert.That(orderSymbol2 == "EURUSD");
            o2.side.Get(out var orderSide2);
            Assert.That(orderSide2 == proto.OrderSide.sell);
            o2.type.Get(out var orderType2);
            Assert.That(orderType2 == proto.OrderType.limit);
            o2.price.Get(out var orderPrice2);
            Assert.That(orderPrice2 == 1.0);
            o2.volume.Get(out var orderVolume2);
            Assert.That(orderVolume2 == 100.0);
            o1.GetEnd(orderBegin);

            var o3 = reader.model.orders[2];

            orderBegin = o3.GetBegin();
            o3.uid.Get(out var orderUId3);
            Assert.That(orderUId3 == 3);
            o3.symbol.Get(out var orderSymbol3);
            Assert.That(orderSymbol3 == "EURUSD");
            o3.side.Get(out var orderSide3);
            Assert.That(orderSide3 == proto.OrderSide.buy);
            o3.type.Get(out var orderType3);
            Assert.That(orderType3 == proto.OrderType.stop);
            o3.price.Get(out var orderPrice3);
            Assert.That(orderPrice3 == 1.5);
            o3.volume.Get(out var orderVolume3);
            Assert.That(orderVolume3 == 10.0);
            o1.GetEnd(orderBegin);

            reader.model.GetEnd(accountBegin);
        }
Пример #2
0
        public void ExtendingOldNew()
        {
            // Create a new account with some orders
            var account1 = proto.Account.Default;

            account1.id              = 1;
            account1.name            = "Test";
            account1.state           = proto.State.good;
            account1.wallet.currency = "USD";
            account1.wallet.amount   = 1000.0;
            account1.asset           = new proto.Balance("EUR", 100.0);
            account1.orders.Add(new proto.Order(1, "EURUSD", proto.OrderSide.buy, proto.OrderType.market, 1.23456, 1000.0));
            account1.orders.Add(new proto.Order(2, "EURUSD", proto.OrderSide.sell, proto.OrderType.limit, 1.0, 100.0));
            account1.orders.Add(new proto.Order(3, "EURUSD", proto.OrderSide.buy, proto.OrderType.stop, 1.5, 10.0));

            // Serialize the account to the FBE stream
            var writer = new FBE.proto.AccountModel();

            Assert.True(writer.model.FBEOffset == 4);
            long serialized = writer.Serialize(account1);

            Assert.True(serialized == writer.Buffer.Size);
            Assert.True(writer.Verify());
            writer.Next(serialized);
            Assert.True(writer.model.FBEOffset == (4 + writer.Buffer.Size));

            // Check the serialized FBE size
            Assert.True(writer.Buffer.Size == 252);

            // Deserialize the account from the FBE stream
            var reader = new FBE.protoex.AccountModel();

            Assert.True(reader.model.FBEOffset == 4);
            reader.Attach(writer.Buffer);
            Assert.True(reader.Verify());
            long deserialized = reader.Deserialize(out var account2);

            Assert.True(deserialized == reader.Buffer.Size);
            reader.Next(deserialized);
            Assert.True(reader.model.FBEOffset == (4 + reader.Buffer.Size));

            Assert.True(account2.id == 1);
            Assert.True(account2.name == "Test");
            Assert.True(account2.state == protoex.StateEx.good);
            Assert.True(account2.wallet.parent.currency == "USD");
            Assert.True(account2.wallet.parent.amount == 1000.0);
            Assert.True(account2.wallet.locked == 0.0);
            Assert.True(account2.asset.HasValue);
            Assert.True(account2.asset.Value.parent.currency == "EUR");
            Assert.True(account2.asset.Value.parent.amount == 100.0);
            Assert.True(account2.asset.Value.locked == 0.0);
            Assert.True(account2.orders.Count == 3);
            Assert.True(account2.orders[0].id == 1);
            Assert.True(account2.orders[0].symbol == "EURUSD");
            Assert.True(account2.orders[0].side == protoex.OrderSide.buy);
            Assert.True(account2.orders[0].type == protoex.OrderType.market);
            Assert.True(account2.orders[0].price == 1.23456);
            Assert.True(account2.orders[0].volume == 1000.0);
            Assert.True(account2.orders[0].tp == 10.0);
            Assert.True(account2.orders[0].sl == -10.0);
            Assert.True(account2.orders[1].id == 2);
            Assert.True(account2.orders[1].symbol == "EURUSD");
            Assert.True(account2.orders[1].side == protoex.OrderSide.sell);
            Assert.True(account2.orders[1].type == protoex.OrderType.limit);
            Assert.True(account2.orders[1].price == 1.0);
            Assert.True(account2.orders[1].volume == 100.0);
            Assert.True(account2.orders[1].tp == 10.0);
            Assert.True(account2.orders[1].sl == -10.0);
            Assert.True(account2.orders[2].id == 3);
            Assert.True(account2.orders[2].symbol == "EURUSD");
            Assert.True(account2.orders[2].side == protoex.OrderSide.buy);
            Assert.True(account2.orders[2].type == protoex.OrderType.stop);
            Assert.True(account2.orders[2].price == 1.5);
            Assert.True(account2.orders[2].volume == 10.0);
            Assert.True(account2.orders[2].tp == 10.0);
            Assert.True(account2.orders[2].sl == -10.0);
        }