Beispiel #1
0
        public void error(int id, int errorCode, string errorMsg)
        {
            if (id == -1 && Array.IndexOf(TwsRtdServerErrors.TwsServerErrors(), errorCode) >= 0)
            {
                m_connection.SetError(TwsRtdServerErrors.CANNOT_CONNECT_TO_TWS, errorMsg);
            }

            if (id != -1 && Array.IndexOf(TwsRtdServerErrors.TwsTickerErrors(), errorCode) >= 0)
            {
                TwsRtdServerMktDataRequest mktDataRequest = m_connection.GetMktDataRequest(id);
                if (mktDataRequest != null)
                {
                    mktDataRequest.SetError(errorCode, errorMsg);
                }

                switch (errorCode)
                {
                case 10167:
                {
                    SetAllLiveTopicsValues(id, "TwsRtdServer error: " + errorMsg);
                    break;
                }

                default:
                {
                    SetAllTopicsValues(id, "TwsRtdServer error: " + errorMsg);
                    break;
                }
                }
            }
        }
Beispiel #2
0
        void SetOptionComputationTopicsValues(int tickerId, int field, TwsRtdServerData.OptionComputationData value)
        {
            TwsRtdServerMktDataRequest mktDataRequest = m_connection.GetMktDataRequest(tickerId);
            string tickTypeStr = TwsRtdServerData.GetTickTypeStrByTickId(field);

            if (mktDataRequest != null && tickTypeStr != null)
            {
                switch (tickTypeStr)
                {
                // assigning implied vol, delta, opt price, pv dividend, gamma, vega, theta and und price
                case TwsRtdServerData.BID_OPTION_COMPUTATION:
                    GetTopicAndAddUpdate(TwsRtdServerData.BID_TICK_ATTRIB, mktDataRequest, value.getTickAttrib());
                    GetTopicAndAddUpdate(TwsRtdServerData.BID_IMPLIED_VOL, mktDataRequest, value.getImpliedVolatility());
                    GetTopicAndAddUpdate(TwsRtdServerData.BID_DELTA, mktDataRequest, value.getDelta());
                    GetTopicAndAddUpdate(TwsRtdServerData.BID_OPT_PRICE, mktDataRequest, value.getOptPrice());
                    GetTopicAndAddUpdate(TwsRtdServerData.BID_PV_DIVIDEND, mktDataRequest, value.getPvDividend());
                    GetTopicAndAddUpdate(TwsRtdServerData.BID_GAMMA, mktDataRequest, value.getGamma());
                    GetTopicAndAddUpdate(TwsRtdServerData.BID_VEGA, mktDataRequest, value.getVega());
                    GetTopicAndAddUpdate(TwsRtdServerData.BID_THETA, mktDataRequest, value.getTheta());
                    GetTopicAndAddUpdate(TwsRtdServerData.BID_UND_PRICE, mktDataRequest, value.getUndPrice());
                    break;

                case TwsRtdServerData.ASK_OPTION_COMPUTATION:
                    GetTopicAndAddUpdate(TwsRtdServerData.ASK_TICK_ATTRIB, mktDataRequest, value.getTickAttrib());
                    GetTopicAndAddUpdate(TwsRtdServerData.ASK_IMPLIED_VOL, mktDataRequest, value.getImpliedVolatility());
                    GetTopicAndAddUpdate(TwsRtdServerData.ASK_DELTA, mktDataRequest, value.getDelta());
                    GetTopicAndAddUpdate(TwsRtdServerData.ASK_OPT_PRICE, mktDataRequest, value.getOptPrice());
                    GetTopicAndAddUpdate(TwsRtdServerData.ASK_PV_DIVIDEND, mktDataRequest, value.getPvDividend());
                    GetTopicAndAddUpdate(TwsRtdServerData.ASK_GAMMA, mktDataRequest, value.getGamma());
                    GetTopicAndAddUpdate(TwsRtdServerData.ASK_VEGA, mktDataRequest, value.getVega());
                    GetTopicAndAddUpdate(TwsRtdServerData.ASK_THETA, mktDataRequest, value.getTheta());
                    GetTopicAndAddUpdate(TwsRtdServerData.ASK_UND_PRICE, mktDataRequest, value.getUndPrice());
                    break;

                case TwsRtdServerData.LAST_OPTION_COMPUTATION:
                    GetTopicAndAddUpdate(TwsRtdServerData.LAST_TICK_ATTRIB, mktDataRequest, value.getTickAttrib());
                    GetTopicAndAddUpdate(TwsRtdServerData.LAST_IMPLIED_VOL, mktDataRequest, value.getImpliedVolatility());
                    GetTopicAndAddUpdate(TwsRtdServerData.LAST_DELTA, mktDataRequest, value.getDelta());
                    GetTopicAndAddUpdate(TwsRtdServerData.LAST_OPT_PRICE, mktDataRequest, value.getOptPrice());
                    GetTopicAndAddUpdate(TwsRtdServerData.LAST_PV_DIVIDEND, mktDataRequest, value.getPvDividend());
                    GetTopicAndAddUpdate(TwsRtdServerData.LAST_GAMMA, mktDataRequest, value.getGamma());
                    GetTopicAndAddUpdate(TwsRtdServerData.LAST_VEGA, mktDataRequest, value.getVega());
                    GetTopicAndAddUpdate(TwsRtdServerData.LAST_THETA, mktDataRequest, value.getTheta());
                    GetTopicAndAddUpdate(TwsRtdServerData.LAST_UND_PRICE, mktDataRequest, value.getUndPrice());
                    break;

                case TwsRtdServerData.MODEL_OPTION_COMPUTATION:
                    GetTopicAndAddUpdate(TwsRtdServerData.MODEL_TICK_ATTRIB, mktDataRequest, value.getTickAttrib());
                    GetTopicAndAddUpdate(TwsRtdServerData.MODEL_IMPLIED_VOL, mktDataRequest, value.getImpliedVolatility());
                    GetTopicAndAddUpdate(TwsRtdServerData.MODEL_DELTA, mktDataRequest, value.getDelta());
                    GetTopicAndAddUpdate(TwsRtdServerData.MODEL_OPT_PRICE, mktDataRequest, value.getOptPrice());
                    GetTopicAndAddUpdate(TwsRtdServerData.MODEL_PV_DIVIDEND, mktDataRequest, value.getPvDividend());
                    GetTopicAndAddUpdate(TwsRtdServerData.MODEL_GAMMA, mktDataRequest, value.getGamma());
                    GetTopicAndAddUpdate(TwsRtdServerData.MODEL_VEGA, mktDataRequest, value.getVega());
                    GetTopicAndAddUpdate(TwsRtdServerData.MODEL_THETA, mktDataRequest, value.getTheta());
                    GetTopicAndAddUpdate(TwsRtdServerData.MODEL_UND_PRICE, mktDataRequest, value.getUndPrice());
                    break;
                }
            }
        }
Beispiel #3
0
        void SetAllLiveTopicsValues(int tickerId, string value)
        {
            TwsRtdServerMktDataRequest mktDataRequest = m_connection.GetMktDataRequest(tickerId);

            if (mktDataRequest != null)
            {
                m_server.AddUpdatedTopicIds(mktDataRequest.SetAllLiveTopicsValues(value));
            }
        }
Beispiel #4
0
        // method is called when there are new data to update
        public Array RefreshData(ref int topicCount)
        {

            object[,] data = null;

            try
            {
                TwsRtdServerConnection connection = null;
                TwsRtdServerMktDataRequest mktDataRequest = null;
                TwsRtdServerTopic topic = null;

                // in loop - update all topics from updatedTopicIds array
                int[] updatedTopicIds;
                lock (m_updatedTopicIds)
                {
                    updatedTopicIds = m_updatedTopicIds.ToArray();
                    m_updatedTopicIds.Clear();
                }

                topicCount = updatedTopicIds.Length;
                data = new object[2, topicCount];

                int n = 0;
                foreach(var topicId in updatedTopicIds)
                {
                    TwsRtdServerTopicIdMap twsRtdServerTopicIdMap = null;
                    if (m_topicIdMap.TryGetValue(topicId, out twsRtdServerTopicIdMap))
                    {
                        // get appropriate connection
                        if (m_connections.TryGetValue(twsRtdServerTopicIdMap.ConnectionStr(), out connection))
                        {
                            // get appropriate mktDataRequest
                            mktDataRequest = connection.GetMktDataRequest(twsRtdServerTopicIdMap.TwsReqId());
                            if (mktDataRequest != null)
                            {
                                // get appropriate topic
                                topic = mktDataRequest.GetTopic(twsRtdServerTopicIdMap.TopicStr());
                            }
                            if (topic != null)
                            {
                                // update data array with topic.Id and topic.Value
                                data[0, n] = topic.TopicId();
                                data[1, n] = topic.TopicValue();
                                n++;
                            }
                        }
                    }
                }
            }
            catch //(COMException comException)
            {
                // comException.Message = "The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))"
                // Console.WriteLine(comException.Message);
            }

            return data;
        }
Beispiel #5
0
        void SetTopicValue(int tickerId, int field, object value)
        {
            TwsRtdServerMktDataRequest mktDataRequest = m_connection.GetMktDataRequest(tickerId);
            string tickTypeStr = TwsRtdServerData.GetTickTypeStrByTickId(field);

            if (mktDataRequest != null && tickTypeStr != null)
            {
                GetTopicAndAddUpdate(tickTypeStr, mktDataRequest, value);
            }
        }
Beispiel #6
0
        public void rerouteMktDataReq(int reqId, int conId, string exchange)
        {
            TwsRtdServerMktDataRequest mktDataRequest = m_connection.GetMktDataRequest(reqId);
            String errorMsg = "Re-route market data request to conId:" + conId + " exchange:" + exchange;

            if (mktDataRequest != null)
            {
                mktDataRequest.SetError(TwsRtdServerErrors.REQUEST_MKT_DATA_ERROR, errorMsg);
            }
            SetAllTopicsValues(reqId, "TwsRtdServer error: " + errorMsg);
        }
Beispiel #7
0
        void GetTopicAndAddUpdate(string topicStr, TwsRtdServerMktDataRequest mktDataRequest, object value)
        {
            TwsRtdServerTopic topic = mktDataRequest.GetTopic(topicStr);

            if (topic != null)
            {
                // set topic's new value
                topic.TopicValue(value);

                m_server.AddUpdatedTopicId(topic.TopicId());  // add topic to updatedTopicIds array
            }

            // save latest value
            mktDataRequest.SetMktDataTickValue(topicStr, value);
        }
Beispiel #8
0
        // this method is called when topic is removed
        public void DisconnectData(int topicId)
        {
            try
            {
                // find appropriate connection, mktDataRequest and topic
                TwsRtdServerTopicIdMap twsRtdServerTopicIdMap = null;
                if (!m_topicIdMap.TryGetValue(topicId, out twsRtdServerTopicIdMap))
                {
                    return;
                }

                // get appropriate connection
                TwsRtdServerConnection connection = null;
                if (!m_connections.TryGetValue(twsRtdServerTopicIdMap.ConnectionStr(), out connection))
                {
                    return;
                }

                // get appropriate mktDataRequest
                TwsRtdServerMktDataRequest mktDataRequest = connection.GetMktDataRequest(twsRtdServerTopicIdMap.TwsReqId());
                if (mktDataRequest != null)
                {
                    // get appropriate topic
                    TwsRtdServerTopic topic = mktDataRequest.GetTopic(twsRtdServerTopicIdMap.TopicStr());
                    if (topic != null)
                    {
                        // remove topic
                        mktDataRequest.RemoveTopic(twsRtdServerTopicIdMap.TopicStr());
                    }

                    m_topicIdMap.Remove(topicId);
                    // remove topicId from updatedTopicIds
                    lock (m_updatedTopicIds)
                    {
                        m_updatedTopicIds.Remove(topicId);
                    }

                    // try to remove mktDataRequest
                    connection.RemoveMktDataRequest(twsRtdServerTopicIdMap.TwsReqId());
                }
            }
            catch
            {
                // error disconnecting data
            }
        }
Beispiel #9
0
        public TwsRtdServerMktDataRequest GetOrAddMktDataRequest(string mktDataRequestStr)
        {
            TwsRtdServerMktDataRequest mktDataRequest = null;

            int mktDataRequestId;

            // find mktdata request (TWS reqMktData) (to reuse existing mktdata request and not to create new one)
            if (mktDataRequestStr != null && (!m_mktDataRequestsStrToIdMapping.TryGetValue(mktDataRequestStr, out mktDataRequestId) ||
                                              !m_mktDataRequests.TryGetValue(mktDataRequestId, out mktDataRequest)))
            {
                // if marketdata request was not found, then create new one
                mktDataRequest = new TwsRtdServerMktDataRequest(mktDataRequestStr, m_twsReqIdNext, this);

                m_twsReqIdNext++;
            }

            return(mktDataRequest);
        }
        public void RemoveMktDataRequest(int twsReqId)
        {
            TwsRtdServerMktDataRequest mktDataRequest = null;

            if (m_mktDataRequests.TryGetValue(twsReqId, out mktDataRequest))
            {
                // if collection of topics is empty, then remove mktdatarequest
                if (mktDataRequest.TopicsCount() <= 0)
                {
                    // cancel TWS market data request
                    mktDataRequest.TwsRtdServerMktDataRequestCancel(twsReqId, this);

                    // remove mktDataRequest from collection using key==reqId
                    m_mktDataRequests.Remove(twsReqId);

                    // remove str->reqId mapping using key==str
                    m_mktDataRequestsStrToIdMapping.Remove(mktDataRequest.MktDataRequestStr());
                }
            }
        }
Beispiel #11
0
        // this method is called when new topic is requested
        public object ConnectData(int topicId, ref Array strings, ref bool newValues)
        {
            string connectionStr, mktDataRequestStr, topicStr;

            newValues = true;

            try
            {
                // parse input strings (connection, marketDataRequest and topic)
                connectionStr     = TwsRtdServerConnection.ParseConnectionStrings(strings);
                mktDataRequestStr = TwsRtdServerMktDataRequest.ParseMktDataRequestStrings(strings);
                topicStr          = TwsRtdServerTopic.ParseTopicStrings(strings);

                // check that connectioStr, mktDataRequestStr and topicStr is not null
                if (connectionStr == null)
                {
                    return("TwsRtdServer: Cannot parse connection strings");
                }
                if (mktDataRequestStr == null)
                {
                    return("TwsRtdServer: Cannot parse mktDataRequest strings");
                }
                if (topicStr == null)
                {
                    return("TwsRtdServer: Cannot parse topic strings");
                }

                TwsRtdServerConnection connection = null;
                // find connection from RTD server to TWS (to reuse existing connection and not to create new one)
                if (!m_connections.TryGetValue(connectionStr, out connection))
                {
                    // if connection is not found, then create new one and add it to collection
                    connection = new TwsRtdServerConnection(this, connectionStr);

                    // save connection
                    m_connections.Add(connectionStr, connection);
                }

                if (connection != null && connection.GetErrorCode() != -1)
                {
                    // error connecting to TWS
                    return("TwsRtdServer error: " + connection.GetErrorText());
                }

                TwsRtdServerMktDataRequest mktDataRequest = connection.GetOrAddMktDataRequest(mktDataRequestStr);
                string errorStr = null;

                if (mktDataRequest != null)
                {
                    // save topicId -> connection/mktDataRequest/topicStr map
                    m_topicIdMap.Add(topicId, new TwsRtdServerTopicIdMap(connectionStr, mktDataRequest.TwsReqId(), topicStr));
                    if (mktDataRequest.GetErrorCode() != -1 &&
                        mktDataRequest.GetErrorCode() != TwsRtdServerErrors.REQUESTED_MARKET_DATA_NOT_SUBSCRIBED)
                    {
                        // error creating market data request
                        return(errorStr = "TwsRtdServer error: " + mktDataRequest.GetErrorText());
                    }
                }
                else
                {
                    // error creating market data request
                    return(errorStr = "TwsRtdServer error: market data request creation error");
                }

                TwsRtdServerTopic topic = mktDataRequest.GetOrAddTopic(topicStr, topicId);
                if (topic == null)
                {
                    // error creating topic
                    return(errorStr = "TwsRtdServer error: topic creation error");
                }

                // check if topic is delayed type
                if (topic != null && Array.IndexOf(TwsRtdServerData.DelayedTopics(), topic.TopicStr()) < 0 &&
                    mktDataRequest.GetErrorCode() == TwsRtdServerErrors.REQUESTED_MARKET_DATA_NOT_SUBSCRIBED)
                {
                    errorStr = "TwsRtdServer error: " + mktDataRequest.GetErrorText();
                }

                return((topic != null && errorStr == null) ? topic.TopicValue() : errorStr);
            }
            catch
            {
                return("RTDServer: Error connecting data");
            }
        }