コード例 #1
0
ファイル: TalkerHub.cs プロジェクト: nagyist/mkbiltek.trading
        //
        // *****************************************************************
        // ****                 Update Market Topics()                  ****
        // *****************************************************************
        /// <summary>
        /// In order to throttle market updates, this method is called periodically.
        /// It updates subscriptions for the market hub directly, and also searches
        /// thru each FillHub topics that are market dependent and updates the topics.
        /// </summary>
        private void UpdateMarketTopics()
        {
            // Update all markets
            Book mktBook = null;

            if (!m_MarketHub.TryEnterReadBook(out mktBook))
            {
                return;
            }



            // Update MarketHub topics
            // TODO: Introduce a Subscription type for market requests, bid/ask etc.

            //
            // Get all market dependent Fill topics.
            //
            Dictionary <InstrumentName, Dictionary <SubscriptionType, List <Topic> > > subscriptions;

            foreach (string hubName in m_FillHubs.Keys)
            {
                if (m_SubscriptionsByHubName.TryGetValue(hubName, out subscriptions))   // handle any direct market requests.
                {
                    foreach (InstrumentName instrumentName in subscriptions.Keys)
                    {
                        // Get last price for this instrument.
                        Dictionary <SubscriptionType, List <Topic> > subscriptionList;
                        List <Topic> topicList;
                        FillBookLifo fillBook;
                        int          instrumentID;
                        if (subscriptions.TryGetValue(instrumentName, out subscriptionList) &&
                            subscriptionList.TryGetValue(SubscriptionType.UnRealPnL, out topicList) &&
                            m_FillHubs[hubName].TryEnterReadBook(instrumentName, out fillBook))
                        {
                            if (m_MarketHub.TryLookupInstrumentID(instrumentName, out instrumentID))
                            {   // We have found topics for UnRealPnL.
                                double lastPrice = mktBook.Instruments[instrumentID].LastPrice;
                                string newValue  = fillBook.UnrealizedDollarGains(lastPrice).ToString();
                                foreach (Topic topic in topicList)
                                {
                                    if (!topic.PeekAtValue().Equals(newValue))
                                    {
                                        topic.SetValue(newValue);
                                    }
                                }
                            }
                            m_FillHubs[hubName].ExitReadBook(instrumentName);
                        }
                    } //next instrumentName
                }
            }         // next fillhub hubName

            // Exit
            m_MarketHub.ExitReadBook(mktBook);
        }//UpdateMarketTopics.
コード例 #2
0
        }//listBoxInstruments_SelectedIndexChanged()

        /// <summary>
        /// The market reading timer ticks and read the market books to get prices.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MarketReadingTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                System.Timers.ElapsedEventHandler marketReadingDelegate = new System.Timers.ElapsedEventHandler(MarketReadingTimer_Elapsed);
                this.Invoke(marketReadingDelegate, new object[] { sender, e });
            }
            else
            {
                string instrName;
                string bidPrice;
                string bidQty;
                string askPrice;
                string askQty;

                // Find the price for the future instrument that is in the long term.
                Book           aBook;
                string         selectedInstrument = listBoxInstruments.SelectedItem.ToString();
                InstrumentName selectedInstrumentName;
                if (!m_FutureInstruments.TryGetValue(selectedInstrument, out selectedInstrumentName))
                {
                    Log.NewEntry(LogLevel.Warning, "Can not find selected instrument.");
                    return;
                }
                instrName = selectedInstrumentName.FullName;
                if (m_MarketTTAPI.TryEnterReadBook(out aBook))
                {
                    int instrID = -1;
                    foreach (Misty.Lib.BookHubs.Market mkt in aBook.Instruments.Values)
                    {
                        if (instrName.Equals(mkt.Name.FullName))
                        {
                            instrID = mkt.ID;
                            break;
                        }
                    }
                    if (instrID >= 0)
                    {
                        instrName = aBook.Instruments[instrID].Name.FullName;
                        bidPrice  = aBook.Instruments[instrID].Price[0][0].ToString();
                        bidQty    = aBook.Instruments[instrID].Qty[0][0].ToString();
                        askPrice  = aBook.Instruments[instrID].Price[1][0].ToString();
                        askQty    = aBook.Instruments[instrID].Qty[1][0].ToString();

                        // Publish the prices to the GUI.
                        textBoxBidPrice.Text     = bidPrice;
                        textBoxBidQty.Text       = bidQty;
                        textBoxAskPrice.Text     = askPrice;
                        textBoxAskQty.Text       = askQty;
                        textBoxExpirySeries.Text = selectedInstrumentName.SeriesName;
                    }
                    m_MarketTTAPI.ExitReadBook(aBook);
                }

                // Get the market prices for the spread instrument components.
                // Record the prices into logs and console.
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Clear();
                Log.BeginEntry(LogLevel.Minor);
                foreach (InstrumentName instrumentName in m_InstrumentSubscriptions)
                {
                    instrName = instrumentName.FullName;
                    if (m_MarketTTAPI.TryEnterReadBook(out aBook))
                    {
                        int instrID = -1;
                        foreach (Misty.Lib.BookHubs.Market mkt in aBook.Instruments.Values)
                        {
                            if (instrName.Equals(mkt.Name.FullName))
                            {
                                instrID = mkt.ID;
                                break;
                            }
                        }
                        if (instrID >= 0)
                        {
                            instrName = aBook.Instruments[instrID].Name.FullName;
                            bidPrice  = aBook.Instruments[instrID].Price[0][0].ToString();
                            bidQty    = aBook.Instruments[instrID].Qty[0][0].ToString();
                            askPrice  = aBook.Instruments[instrID].Price[1][0].ToString();
                            askQty    = aBook.Instruments[instrID].Qty[1][0].ToString();

                            // Write the prices to logs and console.
                            stringBuilder.AppendFormat("{5}_{0}:bid->{1}@{2},ask->{3}@{4}\n", instrName, bidPrice, bidQty, askPrice, askQty, DateTime.Now);
                            Log.AppendEntry("{5}_{0}:bid->{1}@{2},ask->{3}@{4}\n", instrName, bidPrice, bidQty, askPrice, askQty, DateTime.Now);
                        }
                        m_MarketTTAPI.ExitReadBook(aBook);
                    }
                }
                Console.Write(stringBuilder.ToString());
                Log.EndEntry();
            }
        }