/// <summary> /// 获取日线数据 /// </summary> /// <returns></returns> public static List <SecurityData> GetSecurityDatas(String path) { List <SecurityData> datas = new List <SecurityData>(); String appPath = Application.StartupPath; String filePath = path; String content = File.ReadAllText(filePath, Encoding.Default); String[] strs = content.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); int strsSize = strs.Length; for (int i = 2; i < strs.Length - 1; i++) { String str = strs[i]; String[] subStrs = str.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); SecurityData securityData = new SecurityData(); DateTime dayDate = Convert.ToDateTime(subStrs[0]); securityData.date = (dayDate - new DateTime(1970, 1, 1)).TotalSeconds; securityData.open = Convert.ToDouble(subStrs[1]); securityData.high = Convert.ToDouble(subStrs[2]); securityData.low = Convert.ToDouble(subStrs[3]); securityData.close = Convert.ToDouble(subStrs[4]); securityData.volume = Convert.ToDouble(subStrs[5]); securityData.amount = Convert.ToDouble(subStrs[6]); if (securityData.close <= 0) { continue; } datas.Add(securityData); } return(datas); }
/// <summary> /// 复制数据 /// </summary> /// <param name="data">数据</param> public void copy(SecurityData data) { close = data.close; date = data.date; high = data.high; low = data.low; open = data.open; volume = data.volume; amount = data.amount; day = data.day; }
/// <summary> /// 根据代码获取新浪历史数据 /// </summary> /// <param name="code"></param> public static List <SecurityData> GetSinaHistoryDatasByStr(String code, int cycle) { String str = SecurityService.GetSinaHistoryDatasStrByCode(code, cycle); List <SecurityData> datas = JsonConvert.DeserializeObject <List <SecurityData> >(str); for (int i = 0; i < datas.Count; i++) { SecurityData securityData = datas[i]; securityData.date = ((TimeSpan)(Convert.ToDateTime(datas[i].day) - new DateTime(1970, 1, 1))).TotalSeconds; } return(datas); }
/// <summary> /// 更新数据到图像 /// </summary> /// <param name="obj"></param> public void UpdateDataToGraphMinute(List <SecurityData> list, SecurityData latestData, bool clear) { if (clear) { DataSource.Clear(); } System.Drawing.Color color = System.Drawing.Color.SkyBlue; BarShape barVolume = GetShape("成交量") as BarShape; for (int i = 0; i < list.Count; i++) { SecurityData data = list[i]; if (latestData != null) { if (data.date > latestData.date) { continue; } } bool isFirst = i == 0; double date = data.date; if (latestData == null) { DataSource.Set(date, COLUMN_VOLUME, double.NaN); DataSource.Set(date, COLUMN_CLOSE, double.NaN); } else { DataSource.Set(date, COLUMN_VOLUME, data.volume); DataSource.Set(date, COLUMN_CLOSE, data.close); } if (isFirst) { LastClose = data.open; if (latestData == null) { DataSource.Set(date, COLUMN_VOLUME, LastClose); DataSource.Set(date, COLUMN_CLOSE, LastClose); } } } int indicatorsSize = indicators.Count; for (int i = 0; i < indicatorsSize; i++) { indicators[i].OnCalculate(0); } RefreshGraph(); }
/// <summary> /// 更新数据到图像 /// </summary> /// <param name="obj"></param> public void UpdateDataToGraph(List <SecurityData> list, bool clear) { if (clear) { DataSource.Clear(); } BarShape barVolume = GetShape("成交量") as BarShape; CandleShape candleShape = GetShape("K线") as CandleShape; int startIndex = DataSource.RowsCount; if (startIndex < 0) { startIndex = 0; } for (int i = 0; i < list.Count; i++) { SecurityData data = list[i]; if (!double.IsNaN(data.close)) { DataSource.Set(data.date, COLUMN_VOLUME, data.volume); int index = DataSource.GetRowIndex(data.date); DataSource.Set2(index, COLUMN_OPEN, data.open); DataSource.Set2(index, COLUMN_HIGH, data.high); DataSource.Set2(index, COLUMN_LOW, data.low); DataSource.Set2(index, COLUMN_CLOSE, data.close); if (data.open > data.close) { DataSource.Set2(index, barVolume.StyleField, 0); DataSource.Set2(index, candleShape.StyleField, 0); DataSource.Set2(index, barVolume.ColorField, System.Drawing.Color.FromArgb(80, 255, 255).ToArgb()); } else { DataSource.Set2(index, barVolume.StyleField, 1); DataSource.Set2(index, candleShape.StyleField, 1); DataSource.Set2(index, barVolume.ColorField, System.Drawing.Color.FromArgb(255, 80, 80).ToArgb()); } } } int indicatorsSize = indicators.Count; for (int i = 0; i < indicatorsSize; i++) { indicators[i].OnCalculate(startIndex); } RefreshGraph(); }
/// <summary> /// 更新数据到图像 /// </summary> /// <param name="obj"></param> public void UpdateDataToGraphMinute(List <SecurityData> list, bool empty) { System.Drawing.Color color = System.Drawing.Color.SkyBlue; BarShape barVolume = GetShape("成交量") as BarShape; int endIndex = list.Count; if (!empty) { endIndex = minuteDatasPos; } for (int i = 0; i < endIndex; i++) { SecurityData data = list[i]; bool isFirst = i == 0; double date = data.date; if (empty) { DataSource.Set(date, COLUMN_VOLUME, double.NaN); DataSource.Set(date, COLUMN_CLOSE, double.NaN); } else { DataSource.Set(date, COLUMN_VOLUME, data.volume); DataSource.Set(date, COLUMN_CLOSE, data.close); } if (isFirst) { LastClose = data.open; if (empty) { DataSource.Set(date, COLUMN_VOLUME, LastClose); DataSource.Set(date, COLUMN_CLOSE, LastClose); } } } int indicatorsSize = indicators.Count; for (int i = 0; i < indicatorsSize; i++) { indicators[i].OnCalculate(0); } RefreshGraph(); }
/// <summary> /// 秒表事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void thisTimer_Tick(object sender, EventArgs e) { if (minuteMode) { if (minuteDatasPos < minuteDatas.Count) { minuteDatasPos++; UpdateDataToGraphMinute(minuteDatas, false); Invalidate(); } } else { SecurityLatestData newData = new SecurityLatestData(); SecurityService.GetLatestData(currentCode, ref newData); if (!newData.equal(lastData) && newData.m_volume > 0) { double close = newData.m_close; double dVolume = 0; if (lastData.m_code.Length > 0) { dVolume = newData.m_volume - lastData.m_volume; } SecurityData securityData = new SecurityData(); securityData.date = (double)((long)newData.m_date / (cycle * 60) * (cycle * 60)); if (cycle != 1440) { securityData.date += (cycle * 60); } securityData.close = close; if (DataSource.RowsCount > 0) { if (DataSource.GetXValue(DataSource.RowsCount - 1) == securityData.date) { if (securityData.close > DataSource.Get2(DataSource.RowsCount - 1, COLUMN_HIGH)) { securityData.high = close; } else { securityData.high = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_HIGH); } if (securityData.close < DataSource.Get2(DataSource.RowsCount - 1, COLUMN_LOW)) { securityData.low = close; } else { securityData.low = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_LOW); } securityData.open = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_OPEN); double oldVolume = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_VOLUME); oldVolume += dVolume; securityData.volume = oldVolume; } else { securityData.high = close; securityData.low = close; securityData.open = close; securityData.volume = dVolume; } } else { securityData.high = close; securityData.low = close; securityData.open = close; securityData.volume = dVolume; } List <SecurityData> datas = new List <SecurityData>(); datas.Add(securityData); UpdateDataToGraph(datas, false); datas.Clear(); lastData = newData; } } }