示例#1
0
        public void AsynchronousCancels()
        {
            var socket = new MockSessionSocket();
            var mockMarketUid = new Uid(317002);
            var mockContractUid = new Uid(608008);
            var mockOrder = new NewOrder {
                Type = OrderCreateType.Limit,
                Market = mockMarketUid,
                Contract = mockContractUid,
                Side = Side.Buy,
                Quantity = new Quantity(QuantityType.PayoffCurrency, 60000),
                Price = new Price(PriceType.PercentOdds, 5714)
            };

            socket.Expect(Payloads.Sequenced(Payloads.Login("mockuser", "mockpassword"), 1));
            socket.Next(Payloads.Sequenced(Payloads.LoginResponse("00000000-0000-0000-0000-000000658a8", 2), 1));
            socket.Expect(Payloads.Sequenced(Payloads.OrderCreate(mockOrder), 2));
            socket.Next(Payloads.Sequenced(Payloads.OrderAccepted(new Uid(82892989397900053), 2), 2));
            socket.Next(Payloads.Sequenced(Payloads.OrderCancelled(new Uid(82892989397900053)), 3));
            socket.Expect(Payloads.Sequenced(Payloads.Logout(), 3));
            socket.Next(Payloads.Sequenced(Payloads.LogoutConfirmation(), 4));

            var session = new SeqSession(socket, SessionSettings);
            IClientSettings mockSettings = new ClientSettings(SocketSettings, SessionSettings);

            using (var client = SmarketsClient.Create(mockSettings, session))
            {
                client.Login();
                var mockOrderResponse1 = client.CreateOrder(mockOrder);
                Assert.True(mockOrderResponse1.WaitOne(DataWait));
                Assert.NotNull(mockOrderResponse1.Data);
                client.Logout();
                Assert.Equal(mockOrderResponse1.Data.State.Status, OrderStatus.Cancelled);
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "main";
            if (args.Length != 4)
            {
                Console.WriteLine(
                    "Usage: IronSmarkets.ConsoleExample.exe " +
                    "<host> <port> <username> <password>");
                return;
            }

            Log.Info("Application start");
            string host = args[0];
            int port = int.Parse(args[1]);
            string username = args[2];
            string password = args[3];
            ISocketSettings sockSettings = new SocketSettings(
                host, host, port, true, ValidateServerCertificate);
            ISessionSettings sessSettings = new SessionSettings(
                username, password);
            Log.Info("Logging in...");
            IClientSettings settings = new ClientSettings(sockSettings, sessSettings);
            using (var client = SmarketsClient.Create(settings))
            {
                client.PayloadReceived += (sender, eargs) => Log.Info(
                    string.Format(
                        "Event fired for payload [{0}] {1} / {2}",
                        eargs.Sequence,
                        eargs.Payload.EtoPayload.Type,
                        eargs.Payload.Type));
                client.AddPayloadHandler(payload => true);
                client.Login();
                Log.Info("Connected");
                var acct = client.GetAccountState().Data;
                Log.Info(string.Format("Got account {0}", acct));
                Log.Debug("Calling client.Logout()");
                var logoutSeq = client.Logout();
                Log.Debug(string.Format("Logout seq was {0}", logoutSeq));
                Log.Info("Logout returned, cleaning up...");
            }

            Console.WriteLine("Press <Enter> to exit...");
            Console.ReadLine();
        }
示例#3
0
        public void HandleQuotes()
        {
            var socket = new MockSessionSocket();
            var mockMarketUid = new Uid(317002);
            var mockContractUid = new Uid(608008);
            var mockOrder = new NewOrder {
                Type = OrderCreateType.Limit,
                Market = mockMarketUid,
                Contract = mockContractUid,
                Side = Side.Buy,
                Quantity = new Quantity(QuantityType.PayoffCurrency, 60000),
                Price = new Price(PriceType.PercentOdds, 5714)
            };
            var builder = new EventQueryBuilder();
            builder.SetCategory("sport");
            builder.SetSport("football");
            builder.SetDateTime(new DateTime(2012,2,21));

            socket.Expect(Payloads.Sequenced(Payloads.Login("mockuser", "mockpassword"), 1));
            socket.Next(Payloads.Sequenced(Payloads.LoginResponse("00000000-0000-0000-0000-000000658a8", 2), 1));
            socket.Expect(Payloads.Sequenced(Payloads.EventsRequest(builder.GetResult()), 2));
            socket.Next(Payloads.Sequenced(Payloads.HttpFound("http://mock/api/events/sport/football/20120221/2.pb", 2), 2));
            socket.Expect(Payloads.Sequenced(Payloads.MarketSubscribe(new Uid(317002)), 3));
            socket.Next(Payloads.Sequenced(Payloads.MarketQuotes(new Uid(317002)), 3));
            socket.Expect(Payloads.Sequenced(Payloads.OrderCreate(mockOrder), 4));
            socket.Next(
                Payloads.Sequenced(
                    Payloads.ContractQuotes(
                        new Uid(608008),
                        new List<Seto.Quote> { new Seto.Quote { Price = 5714, Quantity = 60000 } },
                        Enumerable.Empty<Seto.Quote>()), 4));
            socket.Next(Payloads.Sequenced(Payloads.OrderAccepted(new Uid(82892989397900053), 4), 5));
            socket.Expect(Payloads.Sequenced(Payloads.Logout(), 5));
            socket.Next(Payloads.Sequenced(Payloads.LogoutConfirmation(), 6));

            var session = new SeqSession(socket, SessionSettings);
            IClientSettings mockSettings = new ClientSettings(SocketSettings, SessionSettings);

            var mockHttpHandler = new MockHttpFoundHandler<Seto.Events>();
            mockHttpHandler.AddDocument(MockUrls.Football20120221);
            using (var client = SmarketsClient.Create(mockSettings, session, mockHttpHandler))
            {
                mockHttpHandler.SetClient(client);
                client.Login();
                var mockEventUid = new Uid(247001);
                var mockMapResponse = client.GetEvents(builder.GetResult());
                Assert.True(mockMapResponse.WaitOne(DataWait));
                var mockMap = mockMapResponse.Data;
                Assert.True(mockMap.ContainsKey(mockEventUid));
                var mockMarket = client.MarketMap[mockMarketUid];
                var mockContract = client.ContractMap[mockContractUid];
                mockMarket.SubscribeQuotes(client);
                var marketUpdatedEvent = new ManualResetEvent(false);
                var contractUpdatedEvent = new ManualResetEvent(false);
                MarketQuotes mockQuotes = null;
                mockMarket.MarketQuotesUpdated += (sender, args) => {
                    mockQuotes = args.Quotes;
                    marketUpdatedEvent.Set();
                };
                mockContract.ContractQuotesUpdated += (sender, args) => contractUpdatedEvent.Set();
                var createResponse = client.CreateOrder(mockOrder);
                Assert.True(createResponse.WaitOne(DataWait));
                Assert.NotNull(createResponse.Data);
                Assert.True(marketUpdatedEvent.WaitOne(1000));
                Assert.Equal(mockQuotes.QuantityType, QuantityType.PayoffCurrency);
                Assert.Equal(mockQuotes.PriceType, PriceType.PercentOdds);
                Assert.Equal(mockQuotes.Uid, mockMarketUid);
                Assert.True(contractUpdatedEvent.WaitOne(1000));
                Assert.True(mockQuotes.ContractQuotes.ContainsKey(mockContractUid));
                var mockContractQuotes = mockQuotes.ContractQuotes[mockContractUid];
                Assert.Equal(mockContractQuotes.Bids.Count(), 1);
                var mockBid = mockContractQuotes.Bids.First();
                Assert.Equal<uint>(mockBid.Price.Raw, 5714);
                Assert.Equal<uint>(mockBid.Quantity.Raw, 60000);
                Assert.Equal(mockContractQuotes.Offers.Count(), 0);
                Assert.Equal(mockContractQuotes.Executions.Count(), 0);
                Assert.Equal(mockContractQuotes.QuantityType, QuantityType.PayoffCurrency);
                Assert.Equal(mockContractQuotes.PriceType, PriceType.PercentOdds);
                Assert.Equal(mockContractQuotes.Uid, mockContractUid);
                client.Logout();
            }
        }
示例#4
0
        public void ReceiverDeadlockTest()
        {
            var socket = new MockSessionSocket();
            socket.Expect(Payloads.Sequenced(Payloads.Login("mockuser", "mockpassword"), 1));
            socket.Next(Payloads.Sequenced(Payloads.LoginResponse("00000000-0000-0000-0000-000000658a8", 2), 1));
            socket.Expect(Payloads.Sequenced(Payloads.Ping(), 2));
            socket.Next(Payloads.Sequenced(Payloads.Pong(), 2));
            socket.Expect(Payloads.Sequenced(Payloads.Ping(), 3));

            var session = new SeqSession(socket, SessionSettings);
            IClientSettings mockSettings = new ClientSettings(SocketSettings, SessionSettings);
            ManualResetEvent waiter = new ManualResetEvent(false);
            using (var client = SmarketsClient.Create(mockSettings, session))
            {
                client.Login();
                client.AddPayloadHandler(
                    (payload) => {
                        Assert.Throws<ReceiverDeadlockException>(() => client.Ping());
                        waiter.Set();
                        return true;
                    });
                client.Ping();
                waiter.WaitOne();
            }
        }
示例#5
0
        public void PollingOrderCancelTest()
        {
            var socket = new MockSessionSocket();
            var mockMarketUid = new Uid(1);
            var mockContractUid = new Uid(1);
            var mockOrderUid = new Uid(1);
            var mockNewOrder = new NewOrder {
                Type = OrderCreateType.Limit,
                Market = mockMarketUid,
                Contract = mockContractUid,
                Side = Side.Buy,
                Quantity = new Quantity(QuantityType.PayoffCurrency, 10M),
                Price = new Price(PriceType.PercentOdds, 2000)
            };
            var mockOrder = new Order(
                mockNewOrder.Price,
                new OrderState(
                    mockOrderUid,
                    OrderCreateType.Limit,
                    OrderStatus.Live,
                    mockNewOrder.Quantity,
                    1337694040417086,
                    new Quantity(QuantityType.PayoffCurrency, 0)),
                mockNewOrder.Market,
                mockNewOrder.Contract,
                mockNewOrder.Side);

            socket.Expect(Payloads.Sequenced(Payloads.Login("mockuser", "mockpassword"), 1));
            socket.Next(Payloads.Sequenced(Payloads.LoginResponse("00000000-0000-0000-0000-00000000000", 2), 1));
            socket.Expect(Payloads.Sequenced(Payloads.OrderCreate(mockNewOrder), 2));
            socket.Next(Payloads.Sequenced(Payloads.OrderAccepted(mockOrderUid, 2), 2));
            socket.Expect(Payloads.Sequenced(Payloads.OrdersForAccountRequest(), 3));
            socket.Next(Payloads.Sequenced(Payloads.OrdersForAccount(new List<Order> { mockOrder }), 3));
            socket.Expect(Payloads.Sequenced(Payloads.OrderCancel(mockOrderUid), 4));
            socket.Next(Payloads.Sequenced(Payloads.OrderCancelled(mockOrderUid), 4));
            socket.Expect(Payloads.Sequenced(Payloads.Logout(), 5));
            socket.Next(Payloads.Sequenced(Payloads.LogoutConfirmation(), 5));

            var session = new SeqSession(socket, SessionSettings);
            IClientSettings mockSettings = new ClientSettings(SocketSettings, SessionSettings);

            using (var client = SmarketsClient.Create(mockSettings, session))
            {
                client.Login();
                var mockOrderResponse1 = client.CreateOrder(mockNewOrder);
                Assert.True(mockOrderResponse1.WaitOne());
                var original = mockOrderResponse1.Data;
                Assert.NotNull(original);
                var orders = client.GetOrders();
                orders.WaitOne();
                var newOrder = orders.Data[original.Uid];
                client.CancelOrder(newOrder);
                var i = 0;
                // Wait up to 500 ms
                while (i++ < 10 && newOrder.State.Status == OrderStatus.Live)
                {
                    Thread.Sleep(50);
                }
                client.Logout();
                Assert.Equal(OrderStatus.Cancelled, original.State.Status);
                Assert.Equal(OrderStatus.Cancelled, newOrder.State.Status);
            }
        }