示例#1
0
        private void 历史数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HistData HD = new HistData();

            Controls.Add(HD);
            HD.Dock = DockStyle.Fill;
            HD.BringToFront();
        }
示例#2
0
        void tws_HistoricalData(object sender, HistoricalDataEventArgs e)
        {
            // get history object
            if (!hist_list.ContainsKey(e.RequestId))
            {
                return;
            }
            HistData h = hist_list[e.RequestId];

            h.history.Add(e);
        }
示例#3
0
        public static void TickBulkCopy(string InstrumentName, DateTime Start)
        {
            DateTime          _start      = DateTime.Now.AddDays(-10);
            DateTime          UseThisDate = Start > _start ? _start : Start;
            AlsiDBDataContext dc          = new AlsiDBDataContext();
            var tickData = HistData.GetHistoricalTICK_FromWEB(_start, DateTime.UtcNow.AddHours(2), InstrumentName);

            DataTable RawTicks = new DataTable("TickData");

            RawTicks.Columns.Add("N", typeof(long));
            RawTicks.Columns.Add("Stamp", typeof(DateTime));
            RawTicks.Columns.Add("Price", typeof(int));

            foreach (var p in tickData)
            {
                RawTicks.Rows.Add(1, p.TimeStamp, p.Close);
            }
            dc.CleanTick();

            #region BulkCopy

            DataSet tickDataSet = new DataSet("AllTicks");
            tickDataSet.Tables.Add(RawTicks);
            SqlConnection myConnection = new SqlConnection(AlsiUtils.Data_Objects.GlobalObjects.CustomConnectionString);
            myConnection.Open();
            SqlBulkCopy bulkcopy = new SqlBulkCopy(myConnection);
            bulkcopy.DestinationTableName = "RawTick";
            bulkcopy.WriteToServer(RawTicks);

            Debug.WriteLine("Tick Bulk Copy Complete");

            RawTicks.Dispose();
            myConnection.Close();
            dc.CleanUp();

            #endregion
        }
示例#4
0
        // get stock/option historical prices 
        public ArrayList GetHistoricalData(string ticker, DateTime start, DateTime end)
        {
            // check cache
            string cache_key = ticker + "," + yahoo_exchange_suffix;
            if (hist_cache.ContainsKey(cache_key))
            {
                HistData h_cache = hist_cache[cache_key];
                if (h_cache.history.Count > 0 &&
                    ((History)h_cache.history[h_cache.history.Count - 1]).date <= start &&
                    h_cache.timestamp.Date == DateTime.Now.Date) return h_cache.history;
            }

            // force en-US culture
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US", false);

            double p_factor = 1.0;

            ArrayList list = new ArrayList();

            string em = (end.Month - 1).ToString();
            string ed = (end.Day).ToString();
            string ey = (end.Year).ToString();
            string sm = (start.Month - 1).ToString();
            string sd = (start.Day).ToString();
            string sy = (start.Year).ToString();

            // check for index
            bool is_index = ticker.StartsWith("^");

            // create url ticker
            string url_ticker;
            if (is_index || yahoo_exchange_suffix == "") url_ticker = ticker;
            else url_ticker = ticker + "." + yahoo_exchange_suffix;

            string page = cap.DownloadHtmlWebPage(@"http://ichart.yahoo.com/table.csv?s=" + url_ticker + @"&amp;d=" + em + @"&amp;e=" + ed + @"&amp;f=" + ey + @"&amp;g=d&amp;a=" + sm + @"&amp;b=" + sd + @"&amp;c=" + sy + @"&amp;ignore=.csv");

            string[] split1 = page.Split(new char[] { '\r', '\n' });

            for (int i = 1; i < split1.Length; i++)
            {
                History history = new History();
                history.stock = ticker;

                try
                {
                    string[] split2 = split1[i].Split(new char[] { ',' });
                    if (split2.Length < 6) continue;

                    history.date = DateTime.Parse(split2[0]);
                    history.price.open = double.Parse(split2[1]) * p_factor;
                    history.price.high = double.Parse(split2[2]) * p_factor;
                    history.price.low = double.Parse(split2[3]) * p_factor;
                    history.price.close = double.Parse(split2[4]) * p_factor;
                    history.price.close_adj = double.Parse(split2[6]) * p_factor;
                    history.volume.total = double.Parse(split2[5]);

                    list.Add(history);
                }
                catch { }
            }

            // update open values
            for (int i = 0; i < list.Count - 1; i++)
                ((History)list[i]).price.open = ((History)list[i + 1]).price.close;
            if (list.Count > 0)
                ((History)list[list.Count - 1]).price.open = ((History)list[list.Count - 1]).price.close;

            HistData h = new HistData();
            h.history = list;
            h.timestamp = DateTime.Now;

            // keep in cache
            if (hist_cache.ContainsKey(cache_key)) hist_cache[cache_key] = h;
            else
            {
                if (hist_cache.Count >= MAX_CACHE_SIZE)
                    hist_cache.Remove(hist_cache.Keys.GetEnumerator().Current);

                hist_cache.Add(cache_key, h);
            }

            return list;
        }
示例#5
0
        private void LoadData(string tableName)
        {
            var databaseManager = new DatabaseManager(PluginSettings.DatabaseSettings);

            string commandText = $@"--ReadiMon - Load Transaction {tableName}
SET NOCOUNT ON

SELECT Action, TenantId, EntityId, FieldId, Data FROM Hist_Data_{tableName}
WHERE TransactionId = @transactionId";

            try
            {
                using (SqlCommand command = databaseManager.CreateCommand(commandText))
                {
                    databaseManager.AddParameter(command, "@transactionId", TransactionId);

                    using (IDataReader reader = command.ExecuteReader( ))
                    {
                        var datas = new List <HistData>( );

                        while (reader.Read( ))
                        {
                            var data = new HistData(reader);

                            datas.Add(data);
                        }

                        switch (tableName)
                        {
                        case "Bit":
                            DataBit        = datas;
                            DataBitVisible = datas.Count > 0;

                            if (SelectedTabIndex < 0 && DataBitVisible)
                            {
                                SelectedTabIndex = 3;
                            }
                            break;

                        case "DateTime":
                            DataDateTime        = datas;
                            DataDateTimeVisible = datas.Count > 0;

                            if (SelectedTabIndex < 0 && DataDateTimeVisible)
                            {
                                SelectedTabIndex = 4;
                            }
                            break;

                        case "Decimal":
                            DataDecimal        = datas;
                            DataDecimalVisible = datas.Count > 0;

                            if (SelectedTabIndex < 0 && DataDecimalVisible)
                            {
                                SelectedTabIndex = 5;
                            }
                            break;

                        case "Guid":
                            DataGuid        = datas;
                            DataGuidVisible = datas.Count > 0;

                            if (SelectedTabIndex < 0 && DataGuidVisible)
                            {
                                SelectedTabIndex = 6;
                            }
                            break;

                        case "Int":
                            DataInt        = datas;
                            DataIntVisible = datas.Count > 0;

                            if (SelectedTabIndex < 0 && DataIntVisible)
                            {
                                SelectedTabIndex = 7;
                            }
                            break;

                        case "NVarChar":
                            DataNVarChar        = datas;
                            DataNVarCharVisible = datas.Count > 0;

                            if (SelectedTabIndex < 0 && DataNVarCharVisible)
                            {
                                SelectedTabIndex = 8;
                            }
                            break;

                        case "Xml":
                            DataXml        = datas;
                            DataXmlVisible = datas.Count > 0;

                            if (SelectedTabIndex < 0 && DataXmlVisible)
                            {
                                SelectedTabIndex = 9;
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                PluginSettings.EventLog.WriteException(exc);
            }
        }
示例#6
0
        public HistData RequestHistoricalData(string symbol, string exchange, SecurityType type, string currency, DateTime end_date, bool cache_ok)
        {
            // make sure we are connected
            if (!Connect)
            {
                Connect = true;
                if (!Connect)
                {
                    return(null);
                }
            }

            // check validity of end-date
            if (end_date < DateTime.Now.AddMonths(-6))
            {
                end_date = DateTime.Now.AddMonths(-6);
            }

            // check cache
            string cache_key = symbol + "," + exchange + "," + type.ToString() + "," + currency;

            if (hist_cache.ContainsKey(cache_key) && cache_ok)
            {
                HistData h_cache = hist_cache[cache_key];
                if (h_cache.history.Count > 0 &&
                    h_cache.history[h_cache.history.Count - 1].Date <= end_date &&
                    h_cache.timestamp.Date == DateTime.Now.Date)
                {
                    return(h_cache);
                }
            }

            // get id from id queue
            if (id_queue.Count == 0)
            {
                return(null);
            }
            int id = id_queue.Dequeue();

            // get tick object
            if (!hist_list.ContainsKey(id))
            {
                hist_list[id] = new HistData(id);
            }
            HistData h = hist_list[id];

            h.timestamp = DateTime.Now;

            // request underlying contract
            Contract contract = new Contract(symbol, exchange, type, currency);

            tws.RequestHistoricalData(id, contract, end_date, new TimeSpan(1, 0, 0, 0), BarSize.OneDay, HistoricalDataType.Midpoint, 1);

            // wait for result
            h.ready_event.WaitOne(new TimeSpan(0, 0, tws_quote_timeout * 2), false);

            // remove tick from list
            hist_list.Remove(id);

            // return id to id queue
            id_queue.Enqueue(id);

            // keep in cache
            if (hist_cache.ContainsKey(cache_key))
            {
                hist_cache[cache_key] = h;
            }
            else
            {
                if (hist_cache.Count >= MAX_CACHE_SIZE)
                {
                    hist_cache.Remove(hist_cache.Keys.GetEnumerator().Current);
                }

                hist_cache.Add(cache_key, h);
            }

            return(h);
        }