private PriceDTO GetMarketInfo(int marketId)
        {
            IStreamingListener <PriceDTO> listener = null;
            PriceDTO marketInfo = null;

            try
            {
                listener = _streamingClient.BuildPricesListener(marketId);
                var gate = new AutoResetEvent(false);

                listener.MessageReceived += (o, s) =>
                {
                    marketInfo = s.Data;
                    gate.Set();
                };


                if (!gate.WaitOne(10000))
                {
                    throw new Exception("timed out waiting for market data");
                }
            }
            finally
            {
                _streamingClient.TearDownListener(listener);
            }

            return(marketInfo);
        }
        private void BuildClients()
        {
            Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating rpc client"));
            RpcClient = new Client(RPC_URI, "CI-WP7");
            RpcClient.BeginLogIn(USERNAME, PASSWORD, ar =>
            {
                var session = RpcClient.EndLogIn(ar);

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating listeners"));
                    StreamingClient = StreamingClientFactory.CreateStreamingClient(STREAM_URI, USERNAME, session.Session);
                    MarketPricesStream = StreamingClient.BuildPricesListener(new[] { MarketId });
                    MarketPricesStream.MessageReceived += OnMarketPricesStreamMessageReceived;
                    OrdersStream = StreamingClient.BuildOrdersListener();
                    OrdersStream.MessageReceived += OnOrdersStreamMessageReceived;

                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting account info"));
                    RpcClient.AccountInformation.BeginGetClientAndTradingAccount(ar2 =>
                    {
                        Account = RpcClient.AccountInformation.EndGetClientAndTradingAccount(ar2);

                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting market info"));
                        RpcClient.Market.BeginGetMarketInformation(MarketId.ToString(), ar3 =>
                        {
                            Market = RpcClient.Market.EndGetMarketInformation(ar3).MarketInformation;
                            Dispatcher.BeginInvoke(() => button1.IsEnabled = true);
                        }, null);
                    }, null);
                });
            }, null);
        }
示例#3
0
 private void BuildListener_CITYINDEXSTREAMING_DataAdapterSet()
 {
     LogToScreen("building listener on CITYINDEXSTREAMING data adapter");
     _priceListener = StreamingClient.BuildPricesListener(PRICEMARKETIDS);
     _priceListener.MessageReceived += PriceListenerMessageReceived;
     LogToScreen("listener on CITYINDEXSTREAMING built");
 }
示例#4
0
        private static Dictionary <int, List <PriceDTO> > ListenToPricesFor(IStreamingClient streamingClient,
                                                                            IEnumerable <int> markets, TimeSpan within, int minPricesToCollect)
        {
            var gates           = new Dictionary <int, ManualResetEvent>();
            var priceListeners  = new Dictionary <int, IStreamingListener>();
            var collectedPrices = new Dictionary <int, List <PriceDTO> >();

            foreach (var market in markets)
            {
                var gate = new ManualResetEvent(false);
                gates.Add(market, gate);
                var prices = new List <PriceDTO>();
                collectedPrices.Add(market, prices);

                var priceListener = streamingClient.BuildPricesListener(new[] { market });
                priceListener.MessageReceived += (s, e) =>
                {
                    prices.Add(e.Data);
                    if (prices.Count >= minPricesToCollect)
                    {
                        gate.Set();
                    }
                };

                priceListeners.Add(market, priceListener);
                priceListener.Start();
            }


            try
            {
                foreach (var market in markets)
                {
                    if (!gates[market].WaitOne(within))
                    {
                        Assert.Fail("Not enough prices were collected for {0} within {1}.  Required {2}.  Got {3}",
                                    market, within, minPricesToCollect, collectedPrices[market].Count);
                    }
                }
            }
            finally
            {
                foreach (var market in markets)
                {
                    priceListeners[market].Stop();
                }
            }

            return(collectedPrices);
        }
示例#5
0
        private void StartButtonClick(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(() => { listBox2.Items.Add("Starting"); });

            Dispatcher.BeginInvoke(() =>
            {
                listBox1.Items.Clear();
                listBox2.Items.Clear();
                listBox3.Items.Clear();

                StartButton.IsEnabled = false;
                StopButton.IsEnabled  = false;


                _backgroundRpcStop = false;
                _backgroundRpc     = new Thread(() =>
                {
                    while (_backgroundRpcStop == false)
                    {
                        new AutoResetEvent(false).WaitOne(100000);
                        var marketInfo = rpcClient.Market.GetMarketInformation("400535967");
                        Dispatcher.BeginInvoke(() => listBox3.Items.Add(marketInfo.MarketInformation.MarketId));
                    }
                    return;
                });
                _backgroundRpc.Start();
                new Thread(() =>
                {
                    Debug.WriteLine("building listener2");
                    _listener = _streamingClient.BuildPricesListener(400535967, 81136, 400509294, 400535971, 80902, 400509295, 400193864, 400525367, 80926, 400498641, 400193866, 91047, 400194551, 121766, 400172033, 139144);
                    _listener.MessageReceived += ListenerMessageReceived;
                    Debug.WriteLine("building listener");

                    _listener2 = _streamingClient.BuildDefaultPricesListener(2347);
                    _listener2.MessageReceived += Listener2MessageReceived;
                    Debug.WriteLine("listener started");
                    Debug.WriteLine("listener2 started");

                    Dispatcher.BeginInvoke(() =>
                    {
                        StartButton.IsEnabled = false;
                        StopButton.IsEnabled  = true;
                    });
                }).Start();
            });
        }
示例#6
0
        private void BuildClients()
        {
            //Hook up a logger for the CIAPI.CS libraries
            LogManager.CreateInnerLogger = (logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat)
                                           => new SimpleDebugAppender(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat);

            Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating rpc client"));
            RpcClient = new Client(RPC_URI, STREAM_URI, "CI-WP7");

            RpcClient.BeginLogIn(USERNAME, PASSWORD, ar =>
            {
                _logger.Info("ending login");
                var session = RpcClient.EndLogIn(ar);

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating listeners"));
                    StreamingClient    = RpcClient.CreateStreamingClient();
                    MarketPricesStream = StreamingClient.BuildPricesListener(new[] { MarketId });
                    MarketPricesStream.MessageReceived += OnMarketPricesStreamMessageReceived;
                    OrdersStream = StreamingClient.BuildOrdersListener();
                    OrdersStream.MessageReceived += OnOrdersStreamMessageReceived;

                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting account info"));
                    RpcClient.AccountInformation.BeginGetClientAndTradingAccount(ar2 =>
                    {
                        Account = RpcClient.AccountInformation.EndGetClientAndTradingAccount(ar2);

                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting market info"));
                        RpcClient.Market.BeginGetMarketInformation(MarketId.ToString(), ar3 =>
                        {
                            Market = RpcClient.Market.EndGetMarketInformation(ar3).MarketInformation;
                            Dispatcher.BeginInvoke(() => button1.IsEnabled = true);
                        }, null);
                    }, null);
                });
            }, null);
        }
示例#7
0
        private void BuildClients()
        {
            //Hook up a logger for the CIAPI.CS libraries
            LogManager.CreateInnerLogger = (logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat)
                         => new SimpleDebugAppender(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat);

            Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating rpc client"));
            RpcClient = new Client(RPC_URI, STREAM_URI, "CI-WP7");
           
            RpcClient.BeginLogIn(USERNAME, PASSWORD, ar =>
            {
                _logger .Info("ending login");
                var session = RpcClient.EndLogIn(ar);

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating listeners"));
                    StreamingClient = RpcClient.CreateStreamingClient();
                    MarketPricesStream = StreamingClient.BuildPricesListener(new[] { MarketId });
                    MarketPricesStream.MessageReceived += OnMarketPricesStreamMessageReceived;
                    OrdersStream = StreamingClient.BuildOrdersListener();
                    OrdersStream.MessageReceived += OnOrdersStreamMessageReceived;

                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting account info"));
                    RpcClient.AccountInformation.BeginGetClientAndTradingAccount(ar2 =>
                    {
                        Account = RpcClient.AccountInformation.EndGetClientAndTradingAccount(ar2);

                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting market info"));
                        RpcClient.Market.BeginGetMarketInformation(MarketId.ToString(), ar3 =>
                        {
                            Market = RpcClient.Market.EndGetMarketInformation(ar3).MarketInformation;
                            Dispatcher.BeginInvoke(() => button1.IsEnabled = true);
                        }, null);
                    }, null);
                });
            }, null);
        }
        static void Main(string[] args)
        {
            try
            {
                var adapter = new MyLoggerFactoryAdapter(null) { OnMessage = AddLogMessage };
                LogManager.Adapter = adapter;

                _client = new Client(RPC_URI);
                _client.LogIn(USERNAME, PASSWORD);

                _streamingClient = StreamingClientFactory.CreateStreamingClient(STREAMING_URI, USERNAME, _client.Session);
                _streamingClient.Connect();

                Console.WriteLine("Connected");

                var accountInfo = _client.AccountInformation.GetClientAndTradingAccount();
                var markets = _client.CFDMarkets.ListCfdMarkets("", "", accountInfo.ClientAccountId, -1).Markets;

                var marketIds = markets.Select(market => market.MarketId).ToArray();

                var listener = _streamingClient.BuildPricesListener(marketIds);
                listener.MessageReceived += OnMessageReceived;
                listener.Start();

                Console.ReadKey();

                _streamingClient.Disconnect();

                _client.LogOut();
                _client.Dispose();
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc);
                Trace.WriteLine(exc);
            }
        }
示例#9
0
        public void CanOrder()
        {
            var      gate                   = new AutoResetEvent(false);
            PriceDTO currentPrice           = null;
            OrderDTO newOrder               = null;
            var      orderHasBeenPlacedFlag = false;

            var marketInformation = _rpcClient.Market.GetMarketInformation(_CFDmarketId.ToString());
            var pricesListener    = _streamingClient.BuildPricesListener(_CFDmarketId);
            var ordersListener    = _streamingClient.BuildOrdersListener();

            try
            {
                ordersListener.MessageReceived += (s, e) =>
                {
                    newOrder = e.Data;
                    Console.WriteLine(
                        string.Format(
                            "New order has been recieved on Orders stream\r\n {0}",
                            e.Data.ToStringWithValues()));
                    gate.Set();
                };

                pricesListener.MessageReceived += (o, s) =>
                {
                    if (orderHasBeenPlacedFlag)
                    {
                        return;
                    }

                    currentPrice = s.Data;
                    var order = new NewStopLimitOrderRequestDTO
                    {
                        MarketId          = currentPrice.MarketId,
                        BidPrice          = currentPrice.Bid + 1,
                        OfferPrice        = currentPrice.Offer + 1,
                        AuditId           = currentPrice.AuditId,
                        Quantity          = marketInformation.MarketInformation.WebMinSize.GetValueOrDefault() + 1,
                        TradingAccountId  = _accounts.TradingAccounts[0].TradingAccountId,
                        Direction         = "buy",
                        Applicability     = "GTD",
                        ExpiryDateTimeUTC = DateTime.UtcNow + TimeSpan.FromDays(1)
                    };

                    var response = _rpcClient.TradesAndOrders.Order(order);
                    orderHasBeenPlacedFlag = true;
                    _rpcClient.MagicNumberResolver.ResolveMagicNumbers(response);
                    Assert.AreEqual("Accepted", response.Status_Resolved, string.Format("Error placing order: \r\n{0}", response.ToStringWithValues()));
                };

                if (!gate.WaitOne(TimeSpan.FromSeconds(15)))
                {
                    throw new Exception("timed out waiting for order notification");
                }

                Assert.IsNotNull(newOrder);
            }
            finally
            {
                _streamingClient.TearDownListener(pricesListener);
                _streamingClient.TearDownListener(ordersListener);
            }
        }