Пример #1
0
        /// <summary>
        /// Break connection to TWS. Can be called for both intentional and unintentional disconnections.
        /// </summary>
        public static void Disconnect()
        {
            ibclient.ClientSocket.Close();
            bool status = ConnectionStatusChanged();

            MessageLogger.LogMessage("TWS-Disconnected");
        }
Пример #2
0
        /// <summary>
        /// Connection made to TWS.
        /// </summary>
        public static void Connected()
        {
            bool status = ConnectionStatusChanged();

            // Always sync server time after connection. We'll do this behind the scenes unlike most TWS events as it just clutters the state machine
            ibclient.ClientSocket.reqCurrentTime();
            MessageLogger.LogMessage("TWS-Requesting server time");
        }
Пример #3
0
        // TWS response for a single instance of contractDetails for a given request
        private static void Ibclient_ContractDetails(messages.ContractDetailsMessage msg_cd)
        {
            ContractDetails cd          = msg_cd.ContractDetails;
            string          contractKey = GetContractKey(cd.Contract);

            // Keep the request active until end is signaled by ContractDetailsEnd event.
            GooContract c = GetDataRequestContract(msg_cd.RequestId, false);

            // TWS returns contracts in calendar order (front month first) so we preserve the order.
            c.TWSContractDetailsList.Add(cd);

            MessageLogger.LogMessage(String.Format("ContractDetails request {0}: {1}", msg_cd.RequestId.ToString(), contractKey));
        }
Пример #4
0
        // Order Status, execution, etc.
        #region Order Handling
        #endregion Order Handling

        #region Miscellaneous
        // TWS response with request for server time. Used to get local offset
        private static void Ibclient_CurrentTime(long time)
        {
            var twsTime = new DateTime(1970, 1, 1);

            twsTime = twsTime.AddSeconds(time).ToLocalTime();
            var localTime = DateTime.Now;

            ServerTimeOffset = twsTime - localTime;

            var msg = String.Format("Current TWS Server Time: {0}. Local: {1}, Difference {2}ms",
                                    twsTime.ToLongTimeString(), localTime.ToLongTimeString(), TWS.ServerTimeOffset.TotalMilliseconds.ToString());

            MessageLogger.LogMessage(msg);
        }
Пример #5
0
        private void DataRequestDone(FSM_EventArgs.GooContract_With_Payload e)
        {
            var c = e.Contract;

            // Skip to next day
            e.Contract.HistRequestTimeStamp = e.Contract.HistRequestTimeStamp.AddDays(-1);

            if (DateTime.Compare(c.HeadTimeStamp, c.HistRequestTimeStamp) < 0)
            {
                FireEvent(FSM_DownloadHistoricalData.Events.DownloadNextDay, e);

                string msg = String.Format("{0}: {1}", e.Contract.Symbol, e.Contract.HistRequestTimeStamp.ToString());
                MessageLogger.LogMessage(msg);
            }
            else
            {
                FireEvent(FSM_DownloadHistoricalData.Events.Finished);
            }
        }
Пример #6
0
        // TWS has replied with all available contract details for a given request Id
        private static void Ibclient_ContractDetailsEnd(int reqId)
        {
            // Details end means this request is done, so delete after it is accessed.
            GooContract c = GetDataRequestContract(reqId, true);

            // TODO: Active month is front month. Near expiration this may not be the best.
            c.TWSActiveContractDetails = c.TWSContractDetailsList[0];

            // Some details we want to break out for the UI
            c.Expiration = c.TWSActiveContractDetails.ContractMonth;
            c.Name       = c.TWSActiveContractDetails.LongName;
            c.Symbol     = c.TWSActiveContractDetails.MarketName;

            string contractKey = GetContractKey(c.TWSActiveContractDetails.Contract);

            // Notify outside world that a new contract has been created and pass along the contractkey and GooContract.
            OnNewContract?.Invoke(contractKey, c);

            MessageLogger.LogMessage(String.Format("ContractDetails request {0} completed", reqId.ToString()));
        }
Пример #7
0
        public MainWindow()
        {
            #region System Initialization
            InitializeComponent();

            // Top level data context is the viewmodel.
            main.DataContext = vm;

            // MessageLogger is static class, so need to assign a messages collection. Use the one from the Viewmodel as UI binding is already set.
            MessageLogger.messages = ViewModel.Messages;

            // System timer for clock
            DispatcherTimer sysClock = new DispatcherTimer();
            sysClock.Interval = TimeSpan.FromSeconds(1);
            sysClock.Tick    += SysClock_Tick;
            sysClock.Start();
            #endregion System Initialization

            // Start the TWS connectivity state machine.
            TWS.FSM.Connection.Start();
            MessageLogger.LogMessage("Starting connection state machine.");
        }
Пример #8
0
        // Error message handler. Handles both standard error codes as well as exceptions
        private static void Ibclient_Error(int id, int errorCode, string errorMsg, Exception exception)
        {
            // Bookeeping behind the scenes so we can debug later on
            #region TWS Error Logging
            // Default message just prints error codes
            var logErrorMsg = errorMsg;

            // All message info may only be within the exception
            if (errorMsg == null)
            {
                logErrorMsg = "";
            }

            // If an exception was generated, it will provide additional information.
            if (exception != null)
            {
                logErrorMsg = String.Format("{0}. EXCEPTION: {1}", logErrorMsg, exception.Message);
            }

            logErrorMsg = String.Format("ID={0},Error={1}:{2}", id.ToString(), errorCode.ToString(), logErrorMsg);

            // Exceptions logged
            MessageLogger.LogMessage(logErrorMsg);
            #endregion TWS Error Logging

            switch (errorCode)
            {
                #region 0=Connection Error
            case 0:
                TWS.FSM.Connection.FireEvent(FSM_TwsConnectivity.Events.TWS_Error_0);
                break;
                #endregion 0=Connection Error

            default:
                break;
            }
        }