/// <summary> /// Download all available historical data for a given contract from TWS server /// </summary> /// <param name="c"></param> public static void GetHistoricalData(GooContract c) { // Historical data is retrieved using a state machine var e = new FSM_EventArgs.GooContract_With_Payload(c); c.FSM.DownloadHistoricalData.Start(e); }
// Received final packet of data for the duration requested in the current historical data request. private static void Ibclient_HistoricalDataEnd(messages.HistoricalDataEndMessage hDataEnd) { // Data request is now completed = delete GooContract c = GetDataRequestContract(hDataEnd.RequestId, true); var e = new FSM_EventArgs.GooContract_With_Payload(c); c.FSM.DownloadHistoricalData.FireEvent(FSM_DownloadHistoricalData.Events.HistoricalDataEnd, e); }
private void DataReceived(FSM_EventArgs.GooContract_With_Payload e) { OHLCQuote dataBar = e.Payload as OHLCQuote; if (e.Contract.HistoricalData.Data.ContainsKey(dataBar.Date) == false) { e.Contract.HistoricalData.Data.Add(dataBar.Date, dataBar); } }
// TWS message response to request for how much data is available. Returns timestamp of furthest out data. private static void Ibclient_HeadTimestamp(messages.HeadTimestampMessage headTimeStamp) { // Get contract. This request is finished, so don't need any more. GooContract c = GetDataRequestContract(headTimeStamp.ReqId, true); // Send event to the download FSM to tell it the head time stamp has been received from TWS. var e = new FSM_EventArgs.GooContract_With_Payload(c, headTimeStamp.HeadTimestamp); c.FSM.DownloadHistoricalData.FireEvent(FSM_DownloadHistoricalData.Events.HeadTimeStamp, e); }
// Received packet of historical data from TWS private static void Ibclient_HistoricalData(messages.HistoricalDataMessage hData) { // Data request is valid until HistoricalDataEnd is received. GooContract c = GetDataRequestContract(hData.RequestId, false); // Package the quote and send the data received event to the state machine. OHLCQuote quote = new OHLCQuote(hData.Date, hData.Open, hData.High, hData.Low, hData.Close); var e = new FSM_EventArgs.GooContract_With_Payload(c, quote); c.FSM.DownloadHistoricalData.FireEvent(FSM_DownloadHistoricalData.Events.HistoricalData, e); }
private void TimeStampReceived(FSM_EventArgs.GooContract_With_Payload e) { // Convert head time stamp to DateTime for processing convenience later on. var headTimeStampString = e.Payload as string; DateTime dt = DateTime.ParseExact(headTimeStampString, TWSInfo.TimeStampStringFormat, CultureInfo.InvariantCulture); e.Contract.HeadTimeStamp = dt; // We'll work backwards to the headtimestamp from current time e.Contract.HistRequestTimeStamp = DateTime.Now; // Transition to start historical download. Can reuse the event args as they'll be the same. FireEvent(FSM_DownloadHistoricalData.Events.StartDownload, e); }
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); } }
private void DownloadHistoricalData(FSM_EventArgs.GooContract_With_Payload e) { // Submit a request for 24 hours of 1-min data TWS.RequestHistoricalData(e.Contract, e.Contract.HistRequestTimeStamp, TWSInfo.HistoricalDataStepSizes.Day_1, TWSInfo.BarSizeSetting.Min_1); }
// Methods for individual states. Should be private or protected to hide them since they don't need to be called directly. // NOTE: NAMES NEED TO MATCH STATES EXACTLY OR EXCEPTIONS WILL BE GENERATED! #region State Methods private void GetHeadTimeStamp(FSM_EventArgs.GooContract_With_Payload e) { TWS.RequestHeadTimeStamp(e.Contract); }