/// <summary>
        /// Shuts down the TT API
        /// </summary>
        public void Dispose()
        {
            lock (m_lock)
            {
                if (!m_disposed)
                {
                    // Unattached callbacks and dispose of all subscriptions
                    if (m_ts != null)
                    {
                        m_ts.Update -= m_ts_Update;
                        m_ts.Dispose();
                        m_ts = null;
                    }
                    if (m_req != null)
                    {
                        m_req.Update -= m_req_Update;
                        m_req.Dispose();
                        m_req = null;
                    }

                    // Begin shutdown the TT API
                    TTAPI.ShutdownCompleted += new EventHandler(TTAPI_ShutdownCompleted);
                    TTAPI.Shutdown();

                    m_disposed = true;
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Clean up TTAPI objects
 /// </summary>
 public void Dispose()
 {
     lock (m_lock)
     {
         if (!m_disposed)
         {
             // Detach callbacks and dispose of all subscriptions
             if (m_req != null)
             {
                 m_req.Dispose();
                 m_req = null;
             }
             if (m_tsSub != null)
             {
                 m_tsSub.Dispose();
             }
             m_tsSub = null;
         }
         // Shutdown the Dispatcher
         if (m_disp != null)
         {
             m_disp.BeginInvokeShutdown();
             m_disp = null;
         }
         m_disposed = true;
     }
 }
예제 #3
0
        /// <summary>
        /// Event notification for instrument lookup
        /// </summary>
        public void req_Update(object sender, InstrumentLookupSubscriptionEventArgs e)
        {
            if (e.Instrument != null && e.Error == null)
            {
                cout("Subscribed to Instrument: {0}", e.Instrument.Key);

                _instruments[GetIid(e.Instrument)].FoundInstrument(e.Instrument);

                // Market Depth subscription (or just Inside Market if you change the PriceSubscriptionSettings)
                var prcSub = new PriceSubscription(e.Instrument, Dispatcher.Current);
                //psSub.Settings = new PriceSubscriptionSettings(PriceSubscriptionType.InsideMarket);
                prcSub.Settings       = new PriceSubscriptionSettings(PriceSubscriptionType.MarketDepth);
                prcSub.FieldsUpdated += new FieldsUpdatedEventHandler(m_prc_FieldsUpdated);
                m_lprc.Add(prcSub);
                prcSub.Start();

                // Time & Sales subscription
                var tsSub = new TimeAndSalesSubscription(e.Instrument, Dispatcher.Current);
                tsSub.Update += new EventHandler <TimeAndSalesEventArgs>(tsSub_Update);
                m_lts.Add(tsSub);
                tsSub.Start();

                // Fills subscription
                var filSub = new FillsSubscription(m_TTAPI.Session, Dispatcher.Current);
                filSub.FillAdded        += new EventHandler <FillAddedEventArgs>(m_fil_FillAdded);
                filSub.FillAmended      += new EventHandler <FillAmendedEventArgs>(m_fil_FillAmended);
                filSub.FillBookDownload += new EventHandler <FillBookDownloadEventArgs>(m_fil_FillBookDownload);
                filSub.FillDeleted      += new EventHandler <FillDeletedEventArgs>(m_fil_FillDeleted);
                filSub.FillListEnd      += new EventHandler <FillListEventArgs>(m_fil_FillListEnd);
                filSub.FillListStart    += new EventHandler <FillListEventArgs>(m_fil_FillListStart);
                m_lfil.Add(filSub);
                filSub.Start();

                // Trade Subscription (to listen for order / fill events only for orders submitted through it)
                var trdSub = new InstrumentTradeSubscription(m_TTAPI.Session, Dispatcher.Current, e.Instrument, true, true, false, false);
                trdSub.OrderUpdated     += new EventHandler <OrderUpdatedEventArgs>(m_trd_OrderUpdated);
                trdSub.OrderAdded       += new EventHandler <OrderAddedEventArgs>(m_trd_OrderAdded);
                trdSub.OrderDeleted     += new EventHandler <OrderDeletedEventArgs>(m_trd_OrderDeleted);
                trdSub.OrderFilled      += new EventHandler <OrderFilledEventArgs>(m_trd_OrderFilled);
                trdSub.OrderRejected    += new EventHandler <OrderRejectedEventArgs>(m_trd_OrderRejected);
                m_dtrd[e.Instrument.Key] = trdSub;
                trdSub.Start();
            }
            else if (e.IsFinal)
            {
                // Instrument was not found and TT API has given up looking for it
                cout("Cannot find instrument, and giving up: {0}", e.Error.Message);
                Dispose();
            }
            else
            {
                cout("Cannot find instrument: {0}", e.Error.Message);
            }
        }
예제 #4
0
 public void req_Update(object sender, InstrumentLookupSubscriptionEventArgs e)
 {
     if (e.Instrument != null && e.Error == null)
     {
         ps = new PriceSubscription(e.Instrument, Dispatcher.Current)
         {
             Settings = new PriceSubscriptionSettings(PriceSubscriptionType.InsideMarket)
         }; ps.FieldsUpdated += new FieldsUpdatedEventHandler(ps_FieldsUpdated); ps.Start();
         tsSub      = new TimeAndSalesSubscription(e.Instrument, Dispatcher.Current); tsSub.Update += new EventHandler <TimeAndSalesEventArgs>(tsSub_Update); tsSub.Start();
         Instrument = e.Instrument; SetUp();
     }
 }
예제 #5
0
 /// <summary>
 /// Event notification for instrument lookup
 /// </summary>
 public void req_Update(object sender, InstrumentLookupSubscriptionEventArgs e)
 {
     if (e.Instrument != null && e.Error == null)
     {
         // Start a Time & Sales subscription
         TimeAndSalesSubscription tsSub = new TimeAndSalesSubscription(e.Instrument, Dispatcher.Current);
         tsSub.Update += new EventHandler <TimeAndSalesEventArgs>(tsSub_Update);
         m_ltsSub.Add(tsSub);
         tsSub.Start();
     }
     else if (e.IsFinal)
     {
         // Instrument was not found and TT API has given up looking for it
         Console.WriteLine("Cannot find instrument: " + e.Error.Message);
         Dispose();
     }
 }
        /// <summary>
        /// Dispose of all the TT API objects and shutdown the TT API
        /// </summary>
        public void shutdownTTAPI()
        {
            if (!m_shutdownInProcess)
            {
                // Dispose of all request objects
                if (m_timeAndSalesSubscription != null)
                {
                    m_timeAndSalesSubscription.Update -= timeAndSalesSubscription_Update;
                    m_timeAndSalesSubscription.Dispose();
                    m_timeAndSalesSubscription = null;
                }

                TTAPI.ShutdownCompleted += new EventHandler(TTAPI_ShutdownCompleted);
                TTAPI.Shutdown();
            }

            // only run shutdown once
            m_shutdownInProcess = true;
        }
        /// <summary>
        /// Event notification for instrument lookup
        /// </summary>
        void m_req_Update(object sender, InstrumentLookupSubscriptionEventArgs e)
        {
            if (e.Instrument != null && e.Error == null)
            {
                // Instrument was found
                Console.WriteLine("Found: {0}", e.Instrument.Name);

                // Subscribe for Time & Sales Data
                m_ts         = new TimeAndSalesSubscription(e.Instrument, Dispatcher.Current);
                m_ts.Update += new EventHandler <TimeAndSalesEventArgs>(m_ts_Update);
                m_ts.Start();
            }
            else if (e.IsFinal)
            {
                // Instrument was not found and TT API has given up looking for it
                Console.WriteLine("Cannot find instrument: {0}", e.Error.Message);
                Dispose();
            }
        }
예제 #8
0
        TTInstrument processInstrumentFound(Instrument instrument)
        {
            Dispatcher dispatcher = Dispatcher.Current;
            //Dispatcher dispatcher = _dispatcher;

            PriceSubscription priceSub = new PriceSubscription(instrument, dispatcher);

            if (SubscribeMarketDepth == true)
            {
                priceSub.Settings = new PriceSubscriptionSettings(PriceSubscriptionType.MarketDepth);
            }
            else
            {
                priceSub.Settings = new PriceSubscriptionSettings(PriceSubscriptionType.InsideMarket);
            }
            priceSub.FieldsUpdated += new FieldsUpdatedEventHandler(priceSub_FieldsUpdated);
            priceSub.Start();

            InstrumentTradeSubscription its = new InstrumentTradeSubscription(_apiSession, dispatcher, instrument);

            its.EnablePNL          = true;
            its.ProfitLossChanged += new EventHandler <ProfitLossChangedEventArgs>(its_ProfitLossChanged);
            its.Start();

            if (SubscribeTimeAndSales == true)
            {
                TimeAndSalesSubscription tsSub = new TimeAndSalesSubscription(instrument, dispatcher);
                tsSub.Update += new EventHandler <TimeAndSalesEventArgs>(tsSub_Update);
                tsSub.Start();
            }

            TTInstrument tti = NewTTInstrument(instrument);

            tti.TradeSubscription = its;
            its.Tag = tti;

            tti.OrderRoute = OrderRoute.GetOrderRoute(tti, instrument.Product.Market.Name);

            return(tti);
        }
        /// <summary>
        /// Instrument request completed event.
        /// </summary>
        void instrRequest_Completed(object sender, InstrumentLookupSubscriptionEventArgs e)
        {
            if (e.IsFinal && e.Instrument != null)
            {
                try
                {
                    UpdateStatusBar("Instrument Found.");
                    Console.WriteLine(String.Format("TT API FindInstrument {0}", e.Instrument.Name));

                    // Grab the contract specifications and output to the user
                    this.txtExchange.Text    = e.Instrument.Product.Market.Name;
                    this.txtProduct.Text     = e.Instrument.Product.Name;
                    this.txtProductType.Text = e.Instrument.Product.Type.ToString();
                    this.txtContract.Text    = e.Instrument.GetFormattedName(InstrumentNameFormat.User);

                    if (m_timeAndSalesSubscription != null)
                    {
                        m_timeAndSalesSubscription.Dispose();
                        m_timeAndSalesSubscription = null;
                    }

                    // subscribe for time and sales updates
                    m_timeAndSalesSubscription         = new TimeAndSalesSubscription(e.Instrument, Dispatcher.Current);
                    m_timeAndSalesSubscription.Update += new EventHandler <TimeAndSalesEventArgs>(timeAndSalesSubscription_Update);
                    m_timeAndSalesSubscription.Start();
                }
                catch (Exception err)
                {
                    Console.WriteLine(String.Format("TT API FindInstrument Exception: {0}", err.Message));
                }
            }
            else if (e.IsFinal)
            {
                Console.WriteLine(String.Format("TT API FindInstrument Instrument Not Found: {0}", e.Error));
            }
            else
            {
                Console.WriteLine(String.Format("TT API FindInstrument Instrument Not Found: (Still Searching) {0}", e.Error));
            }
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Event notification for instrument lookup. </summary>
        /// <param name="sender">   Source of the event. </param>
        /// <param name="e">        Instrument lookup subscription event information. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        void m_instrLookupRequest_OnData(object sender, InstrumentLookupEventArgs e)
        {
            if (e.Event == ProductDataEvent.Found)
            {
                // Instrument was found
                Instrument instrument = e.InstrumentLookup.Instrument;
                Console.WriteLine("Found: {0}", instrument);

                // Subscribe for Time & Sales Data
                m_tsSubscription         = new TimeAndSalesSubscription(instrument, tt_net_sdk.Dispatcher.Current);
                m_tsSubscription.Update += m_tsSubscription_Update;
                m_tsSubscription.Start();
            }
            else if (e.Event == ProductDataEvent.NotAllowed)
            {
                Console.WriteLine("Not Allowed : Please check your Token access");
            }
            else
            {
                // Instrument was not found and TT API has given up looking for it
                Console.WriteLine("Cannot find instrument: {0}", e.Message);
            }
        }
 /// <summary>
 /// Event notification for instrument lookup
 /// </summary>
 public void req_Update(object sender, InstrumentLookupSubscriptionEventArgs e)
 {
     if (e.Instrument != null && e.Error == null)
     {
         // Start a Time & Sales subscription
         TimeAndSalesSubscription tsSub = new TimeAndSalesSubscription(e.Instrument, Dispatcher.Current);
         tsSub.Update += new EventHandler<TimeAndSalesEventArgs>(tsSub_Update);
         m_ltsSub.Add(tsSub);
         tsSub.Start();
     }
     else if (e.IsFinal)
     {
         // Instrument was not found and TT API has given up looking for it
         Console.WriteLine("Cannot find instrument: " + e.Error.Message);
         Dispose();
     }
 }
        /// <summary>
        /// Shuts down the TT API
        /// </summary>
        public void Dispose()
        {
            lock (m_lock)
            {
                if (!m_disposed)
                {
                    // Unattached callbacks and dispose of all subscriptions
                    if (m_ts != null)
                    {
                        m_ts.Update -= m_ts_Update;
                        m_ts.Dispose();
                        m_ts = null;
                    }
                    if (m_req != null)
                    {
                        m_req.Update -= m_req_Update;
                        m_req.Dispose();
                        m_req = null;
                    }

                    // Begin shutdown the TT API
                    TTAPI.ShutdownCompleted += new EventHandler(TTAPI_ShutdownCompleted);
                    TTAPI.Shutdown();

                    m_disposed = true;
                }
            }
        }
        /// <summary>
        /// Event notification for instrument lookup
        /// </summary>
        void m_req_Update(object sender, InstrumentLookupSubscriptionEventArgs e)
        {
            if (e.Instrument != null && e.Error == null)
            {
                // Instrument was found
                Console.WriteLine("Found: {0}", e.Instrument.Name);

                // Subscribe for Time & Sales Data
                m_ts = new TimeAndSalesSubscription(e.Instrument, Dispatcher.Current);
                m_ts.Update += new EventHandler<TimeAndSalesEventArgs>(m_ts_Update);
                m_ts.Start();
            }
            else if (e.IsFinal)
            {
                // Instrument was not found and TT API has given up looking for it
                Console.WriteLine("Cannot find instrument: {0}", e.Error.Message);
                Dispose();
            }
        }
        /// <summary>
        /// Instrument request completed event.
        /// </summary>
        void instrRequest_Completed(object sender, InstrumentLookupSubscriptionEventArgs e)
        {
            if (e.IsFinal && e.Instrument != null)
            {
                try
                {
                    UpdateStatusBar("Instrument Found.");
                    Console.WriteLine(String.Format("TT API FindInstrument {0}", e.Instrument.Name));

                    // Grab the contract specifications and output to the user
                    this.txtExchange.Text = e.Instrument.Product.Market.Name;
                    this.txtProduct.Text = e.Instrument.Product.Name;
                    this.txtProductType.Text = e.Instrument.Product.Type.ToString();
                    this.txtContract.Text = e.Instrument.GetFormattedName(InstrumentNameFormat.User);

                    if (m_TimeAndSalesSubscription != null)
                    {
                        m_TimeAndSalesSubscription.Dispose();
                        m_TimeAndSalesSubscription = null;
                    }

                    // subscribe for time and sales updates
                    m_TimeAndSalesSubscription = new TimeAndSalesSubscription(e.Instrument, Dispatcher.Current);
                    m_TimeAndSalesSubscription.Update += new EventHandler<TimeAndSalesEventArgs>(timeAndSalesSubscription_Update);
                    m_TimeAndSalesSubscription.Start();
                }
                catch (Exception err)
                {
                    Console.WriteLine(String.Format("TT API FindInstrument Exception: {0}", err.Message));
                }
            }
            else if (e.IsFinal)
            {
                Console.WriteLine(String.Format("TT API FindInstrument Instrument Not Found: {0}", e.Error));
            }
            else
            {
                Console.WriteLine(String.Format("TT API FindInstrument Instrument Not Found: (Still Searching) {0}", e.Error));
            }
        }
        /// <summary>
        /// Dispose of all the TT API objects and shutdown the TT API 
        /// </summary>
        public void shutdownTTAPI()
        {
            if (!m_shutdownInProcess)
            {
                // Dispose of all request objects
                if (m_timeAndSalesSubscription != null)
                {
                    m_timeAndSalesSubscription.Update -= timeAndSalesSubscription_Update;
                    m_timeAndSalesSubscription.Dispose();
                    m_timeAndSalesSubscription = null;
                }

                TTAPI.ShutdownCompleted += new EventHandler(TTAPI_ShutdownCompleted);
                TTAPI.Shutdown();
            }

            // only run shutdown once
            m_shutdownInProcess = true;
        }
예제 #16
0
        }//Job
        // *****************************************************************
        // ****             Job Processing Methods                      ****
        // *****************************************************************
        /// <summary>
        /// Called by the thread's dispatcher after an outside thread has pushed a new Job.
        /// </summary>
        private void ProcessAJob()
        {
            if (m_isDisposing)
            {
                return;
            }
            Job aJob;

            while (m_InQueue.TryDequeue(out aJob))              // NET 4.5 concurrent queue. No locking needed.
            {
                m_WorkQueue.Enqueue(aJob);                      // push onto my private queue.
            }
            //
            // Process the jobs now.
            //
            while (m_WorkQueue.Count > 0)
            {
                Job job = m_WorkQueue.Dequeue();
                if (job.Product != null)
                {     //
                    if (string.IsNullOrEmpty(job.SeriesName))
                    { // User wants all instruments assoc with this product.
                        //
                        // Process Instrument Catalog requests
                        //
                        InstrumentCatalogSubscription instrumentSub = null;
                        if (!m_InstrumentCatalogs.TryGetValue(job.Product.Key, out instrumentSub))
                        {   // Failed to find a subscription.  Create a new one!
                            instrumentSub = new InstrumentCatalogSubscription(job.Product, m_Dispatcher);
                            instrumentSub.InstrumentsUpdated += InstrumentCatalog_InstrumentsUpdated;
                            instrumentSub.Start();                                                  // submit the request.
                            m_InstrumentCatalogs.Add(job.Product.Key, instrumentSub);               // store the catalog object
                            Log.NewEntry(LogLevel.Minor, "{0}: Subscribing to instr catalog for {1}.", this.Name, job.Product.Name);
                        }
                    }
                    else
                    {   // User requested Instrument info using the ProductKey and a series Name only. (Not instr Key).
                        //InstrumentLookupSubscription lookup = null;
                        Log.NewEntry(LogLevel.Major, "{0}: InstrumentLookup {1} {2}.", this.Name, job.Product, job.SeriesName);
                        InstrumentLookupSubscription subscriber = new InstrumentLookupSubscription(m_TTServices.session, m_Dispatcher, job.Product.Key, job.SeriesName);
                        subscriber.Update += new EventHandler <InstrumentLookupSubscriptionEventArgs>(InstrumentLookup_InstrumentUpdated);
                        //m_InstrumentLookupsUnknown.Add(job.InstrumentKey, subscriber);
                        subscriber.Start();
                    }
                }
                else if (job.InstrumentKey != null && job.Settings == null && !job.IsTimeAndSales)
                {   //
                    // Process an Instrument information request
                    //
                    InstrumentLookupSubscription lookup = null;
                    if (!m_InstrumentLookups.TryGetValue(job.InstrumentKey, out lookup))
                    {
                        Log.NewEntry(LogLevel.Major, "{0}: InstrumentLookup {1}.", this.Name, job.InstrumentKey);
                        InstrumentLookupSubscription subscriber = new InstrumentLookupSubscription(m_TTServices.session, m_Dispatcher, job.InstrumentKey);
                        subscriber.Update += new EventHandler <InstrumentLookupSubscriptionEventArgs>(InstrumentLookup_InstrumentUpdated);
                        m_InstrumentLookups.Add(job.InstrumentKey, subscriber);
                        subscriber.Start();
                    }
                    else
                    {
                        Log.NewEntry(LogLevel.Major, "{0}: InstrumentLookup {1} already submitted.", this.Name, job.InstrumentKey);
                    }
                }
                else if (job.InstrumentKey != null)
                {   //
                    // Subscribe to an instrument price
                    //
                    Instrument instrument = null;
                    InstrumentCatalogSubscription catalog       = null;                         // First, find instrument catalog for this instr.
                    InstrumentLookupSubscription  instrumentSub = null;                         // or find a specific instrument subscription.
                    //InstrumentDetails instrumentDetails = null;
                    //UVProd.InstrumentName instrumentName ;
                    if (m_InstrumentLookups.TryGetValue(job.InstrumentKey, out instrumentSub))
                    {
                        instrument = instrumentSub.Instrument;
                    }
                    else if (m_InstrumentCatalogs.TryGetValue(job.InstrumentKey.ProductKey, out catalog))
                    {
                        catalog.Instruments.TryGetValue(job.InstrumentKey, out instrument);
                    }
                    //else if (m_KeyToInstruments.TryGetValue(job.InstrumentKey, out instrumentName) && m_InstrumentDetails.TryGetValue(instrumentName, out instrumentDetails))
                    //{
                    //    m_InstrumentLookups.TryGetValue(job.InstrumentKey, out instrumentSub)
                    //}
                    else
                    {
                        Log.NewEntry(LogLevel.Minor, "{0}: I failed to find instrument key {1}.", this.Name, job.InstrumentKey.ToString());
                        return;
                    }
                    if (instrument != null)
                    {
                        if (!job.IsTimeAndSales)
                        { // this is a market data subscription request  - Subscribe or update pre-existing subscription.
                            PriceSubscription priceSub = null;
                            if (!m_PriceSubscriptions.TryGetValue(instrument.Key, out priceSub))
                            {   // Can't find a subscription, so create one.
                                Log.NewEntry(LogLevel.Major, "{0}: Creating new subscription for {1} with settings {2}.", this.Name, instrument.Name, job.Settings.PriceData);
                                priceSub = new PriceSubscription(instrument, Dispatcher.Current);
                                m_PriceSubscriptions.Add(instrument.Key, priceSub);                                      // add to our list of subscription objects.
                                if (!m_InstrKeyToVolume.ContainsKey(instrument.Key))
                                {
                                    m_InstrKeyToVolume.Add(instrument.Key, new int[4]);                                 // create a new array for volume aggregations
                                }
                                priceSub.FieldsUpdated += new FieldsUpdatedEventHandler(PriceSubscription_Updated);     // attach my handler to it.
                                priceSub.Settings       = job.Settings;
                                priceSub.Start();
                            }
                            else
                            {
                                Log.NewEntry(LogLevel.Major, "{0}: Found old subscription for {1}.  Overwriting settings {2}.", this.Name, instrument.Name, job.Settings.ToString());
                                priceSub.Settings = job.Settings;
                                priceSub.Start();
                            }
                        }
                        else
                        { // this is a time and sales data request
                            TimeAndSalesSubscription timeAndSalesSub = null;
                            if (!m_TimeAndSalesSubscriptions.TryGetValue(instrument.Key, out timeAndSalesSub))
                            {   // Can't find a subscription, so create one.
                                Log.NewEntry(LogLevel.Major, "{0}: Creating new time and sales subscription for {1}", this.Name, instrument.Name);
                                timeAndSalesSub = new TimeAndSalesSubscription(instrument, Dispatcher.Current);
                                m_TimeAndSalesSubscriptions.Add(instrument.Key, timeAndSalesSub);                       // add to our list of subscription objects.
                                if (!m_InstrKeyToVolume.ContainsKey(instrument.Key))
                                {
                                    m_InstrKeyToVolume.Add(instrument.Key, new int[4]);                                               // create a new array for volume aggregations
                                }
                                timeAndSalesSub.Update += new EventHandler <TimeAndSalesEventArgs>(TimeAndSalesSubscription_Updated); // attach my handler to it.
                                timeAndSalesSub.Start();
                            }
                            else
                            {
                                Log.NewEntry(LogLevel.Major, "{0}: Found existing time and sales subscription for {1}.", this.Name, instrument.Name, job.Settings.ToString());
                            }
                        }
                    }
                }
            } //wend Job in WorkQueue.
        }     //ProcessAJob()