public static void MergeOneDay(string[] ss) { DbParam[] dps = new DbParam[] { new DbParam("@QuoteCode", DbType.String, null), }; for (int i = 0; i < ss.Length; i++) { if (ss[i] != "") { try { DataPacket dp = DataPacket.ParseEODData(ss[i]); dps[0].Value = dp.Symbol; DataRow dr = DB.GetFirstRow("select * from StockData where QuoteCode=?", dps); if (dr != null) { string QuoteCode = dr["QuoteCode"].ToString(); byte[] bs = DBDataManager.LoadHisDataFromFile(QuoteCode); bs = CommonDataProvider.MergeOneQuote(bs, dp); CommonDataProvider cdp = new CommonDataProvider(null); cdp.LoadBinary(bs); Utils.UpdateRealtime(QuoteCode, cdp); DBDataManager.SaveBinary(QuoteCode, bs); } } catch (Exception e) { Tools.Log("Merge Data:" + e.Message); } } } }
public static void ClearTimeOfHistoricalData(object Sender) { DataTable dt = DB.GetDataTable("select QuoteCode from stockdata"); DBDataManager ddm = new DBDataManager(); for (int j = 0; j < dt.Rows.Count; j++) { DataRow dr = dt.Rows[j]; string Code = dr["QuoteCode"].ToString(); try { CommonDataProvider cdp = (CommonDataProvider)ddm[Code]; cdp.TrimTime(); cdp.SaveBinary(DBDataManager.GetHisDataFile(Code)); Thread.Sleep(1); if ((j % 100) == 0) { Tools.Log("Clear time of historical data progress:" + j + "/" + dt.Rows.Count); } } catch (Exception e) { Tools.Log("Clear time of histoical data:" + Code + ";" + e); throw; } } }
private void BindChart() { FormulaChart fc = new FormulaChart(); FML.NATIVE.CustomFormula cf = new FML.NATIVE.CustomFormula("Custom"); cf.Add(tbName1.Text, TextToDoubleArray(tbLine1.Text)); double[] Date; double[] Data; GetDateAndData(tbLine2.Text, out Date, out Data); cf.Add(tbName2.Text, Date, Data); fc.AddArea("MAIN", 2); fc.AddArea(cf); fc.SetSkin(FormulaSkin.GetSkinByName(Config.DefaultSkin)); DBDataManager ddm = new DBDataManager(); fc.DataProvider = ddm[tbSymbol.Text]; FormulaBase fb = fc[0].Formulas[0]; fb.Name = fc.DataProvider.GetStringData("Code") + "(" + fc.DataProvider.GetStringData("Name") + ")"; fb.TextInvisible = false; iChart.ImageUrl = "../ImageFromCache.aspx?CacheId=" + fc.SaveToImageStream(600, 400, ImageFormat.Png, 0, 0); }
public static void UpdateRealtime(object Sender) { DataTable dt = DB.GetDataTable("select QuoteCode from stockdata"); DBDataManager ddm = new DBDataManager(); foreach (DataRow dr in dt.Rows) { string Code = dr["QuoteCode"].ToString(); IDataProvider idp = ddm[Code, 2]; Utils.UpdateRealtime(Code, idp); Thread.Sleep(1); } }
public override IDataProvider GetData(string Code, int Count) { CommonDataProvider cdp = (CommonDataProvider)ddm[Code, Count]; if (!cdp.HasData) { EasyStockChartDataFeed df = new EasyStockChartDataFeed(); try { byte[] bs = df.BinaryHistory(Code, true); if (bs != null) { cdp.LoadBinary(bs); DBDataManager.UpdateForNewSymbol(Code, cdp, Config.SaveInServerDataManager); } } catch { } } return(cdp); }
static public void DownloadYahooHistory(string Symbol, bool Force, bool AddRealtime) { string FileName = DBDataManager.GetHisDataFile(Symbol); if (Force || !File.Exists(FileName)) { DateTime d = DateTime.Now.AddDays(3); string URL = "http://table.finance.yahoo.com/table.csv?s={0}&d=" + (d.Month - 1) + "&e=" + d.Day + "&f=" + d.Year + "&g=d&a=6&b=30&c=" + Config.HistoricalDataYear + "&ignore=.csv"; string s = string.Format(URL, Symbol); byte[] bs = DownloadData(s); CommonDataProvider cdpn = new YahooDataManager().LoadYahooCSV(bs); if (AddRealtime) { cdpn.Merge(DataPacket.DownloadFromYahoo(Symbol)); } UpdateRealtime(Symbol, cdpn); if (File.Exists(FileName)) { File.Delete(FileName); } cdpn.SaveBinary(FileName); } }
/// <summary> /// Test days : FixData.TestDays /// When data increase or decline beyond +/-"FixData.Difference"% , reload data /// When data lost for "FixData.GapDays" days, reload data, still no data then delete /// When no data for latest "FixData.NoDataDays" days, reload data, still no data then delete /// /// Result saved in QuoteCode /// -1 : Stocks should be deleted /// -2 : Network error, Should redownload /// -3 : No need to fix again /// -4 : Fixed symbols /// </summary> /// <param name="Sender"></param> static public void FixData(object Sender) { Tools.Log("FixData started"); try { DataTable dtSkip = DB.GetDataTable("select QuoteCode from ScanedQuote where ConditionId=-3"); ArrayList alSkip = new ArrayList(); foreach (DataRow dr in dtSkip.Rows) { alSkip.Add(dr[0].ToString()); } DB.DoCommand("delete from ScanedQuote where ConditionId<0 and ConditionId<>-3"); DataTable dt = DB.GetDataTable("select QuoteCode from stockdata"); DBDataManager ddm = new DBDataManager(); FixedCount = 0; FixErrorCount = 0; TotalCount = dt.Rows.Count; for (int j = 0; j < dt.Rows.Count; j++) { NowIndex = j + 1; DataRow dr = dt.Rows[j]; string Symbol = dr["QuoteCode"].ToString(); try { if (TestGood(ddm, Symbol, false) != FixDataResult.OK) { if (alSkip.IndexOf(Symbol) >= 0) { if (TestGood(ddm, Symbol, true) == FixDataResult.OK) { continue; } else { DB.DoCommand("delete from ScanedQuote where ConditionId=-3 and QuoteCode='" + Symbol + "'"); } } for (int k = 0; k < 3; k++) { try { Utils.DownloadYahooHistory(Symbol, true, false); break; } catch (WebException) { if (k == 2) { throw; } } } FixDataResult fdr1 = TestGood(ddm, Symbol, false); FixDataResult fdr2 = TestGood(ddm, Symbol, true); if (fdr2 == FixDataResult.NoData) { InsertQuote(-1, Symbol); } else if (fdr1 == FixDataResult.Others && fdr2 == FixDataResult.OK) { InsertQuote(-3, Symbol); } else { InsertQuote(-4, Symbol); } FixedCount++; } } catch (Exception e) { if (e.Message.IndexOf("404") >= 0) { InsertQuote(-1, Symbol); } else { InsertQuote(-2, Symbol); } Tools.Log("FixData:" + Symbol + ";" + e.Message + ";" + e.GetType()); FixErrorCount++; } Thread.Sleep(1); } Tools.Log("FixData finished"); } finally { FixDataThread = null; } }
public ServiceDataManager() { ddm = new DBDataManager(); }
protected void Page_Load(object sender, System.EventArgs e) { int Height = 800; int Width = 600; Bitmap B; try { long Start = DateTime.Now.Ticks; ExchangeIntraday ei = null; DateTime StartTime = DateTime.Today.AddMonths(-6); DateTime EndTime = DateTime.Today; string Param = Request.QueryString["Span"]; if (Param != null && Param != "") { DataCycle dc; if (Param.Length > 2) { dc = DataCycle.Parse(Param); } else { dc = new DataCycle(DataCycleBase.MONTH, Tools.ToIntDef(Param, 6)); } StartTime = EndTime - dc; //StartTime = EndTime.AddMonths(-Tools.ToIntDef(Param,6)); } //Create financial chart instance FormulaChart fc = new FormulaChart(); string Main = Request.QueryString["Main"]; if (Main == null || Main == "") { fc.AddArea("MAIN", 3); } else { fc.AddArea(Main, 3); } string QuoteCode = Request.QueryString["Code"]; if (QuoteCode == null) { QuoteCode = ""; } DataManagerBase dmb; Param = Request.QueryString["Provider"]; if (Param == null) { Param = ""; } if (string.Compare(Param, "DB", true) == 0) { //Create DBDataManager , Get stock data from sql server. dmb = new DBDataManager(); ((DBDataManager)dmb).AutoYahooToDB = Request.QueryString["His"] != "0" && Config.AutoYahooToDB == 1; if (Request.QueryString["RT"] == "1") { ((DataManagerBase)dmb).DownloadRealTimeQuote = true; } } else if (Param == "" || string.Compare(Param, "Yahoo", true) == 0) { //Create YahooDataManager , Get stock data from yahoo. dmb = new YahooDataManager(); ((YahooDataManager)dmb).CacheRoot = HttpRuntime.AppDomainAppPath + "Cache\\"; if (Config.IncludeTodayQuote == 1) { ((DataManagerBase)dmb).DownloadRealTimeQuote = true; } } else { string ManagerName = "WebDemos." + Param + "DataManager"; Type t = Type.GetType(ManagerName); if (t == null) { throw new Exception(ManagerName + " not found!"); } dmb = (DataManagerBase)Activator.CreateInstance(t); if (dmb is IntraDataManagerBase) { ei = ExchangeIntraday.GetExchangeIntraday(Utils.GetExchange(QuoteCode)); ((CacheDataManagerBase)dmb).CacheTimeSpan = TimeSpan.FromMilliseconds(Config.IntradayCacheTime * 1000); //((CacheDataManagerBase)dmb).EnableFileCache = false; StartTime = ei.GetCurrentTradingDay(); EndTime = StartTime.AddSeconds(3600 * 24 - 1); QuoteCode = Utils.GetPart1(QuoteCode); } dmb.EndTime = Utils.ToDateDef(Request.QueryString["End"], EndTime); dmb.StartTime = Utils.ToDateDef(Request.QueryString["Start"], StartTime); } int MainIndex = 0; if (Config.SymbolCase == "Upper") { QuoteCode = QuoteCode.ToUpper(); } else if (Config.SymbolCase == "Lower") { QuoteCode = QuoteCode.ToLower(); } string[] QuoteCodes = QuoteCode.Split(','); string Over = ""; QuoteCode = QuoteCodes[0]; for (int i = 1; i < QuoteCodes.Length; i++) { if (Over != "") { Over += ";"; } Over += "Compare(" + QuoteCodes[i] + ")"; } Param = Request.QueryString["IND"]; string[] Inds = null; if (Param != null && Param != "") { Inds = Param.Split(';'); } //Set stock chart size Param = Request.QueryString["Size"]; try { if (Param != null && Param != "") { int i = Param.IndexOf('*'); if (i > 0) { Width = int.Parse(Param.Substring(0, i)); Height = int.Parse(Param.Substring(i + 1)); } else { Width = int.Parse(Param); Height = Width * 3 / 4; if (Inds != null && Inds.Length > 1) { Height += Height / 5 * (Inds.Length - 2); } Height += 36; } } } catch { } CommonDataProvider DataProvider = (CommonDataProvider)dmb[QuoteCode]; if (DataProvider == null) { throw new Exception(QuoteCode + " not found!"); } if (dmb is IntraDataManagerBase) { fc.FixedTime = object.Equals(Request.QueryString["Fix"], "1"); if (fc.FixedTime) { DataProvider.IntradayInfo = ei; } } if (DataProvider.Count == 0) { throw new Exception("No data found!"); } fc[MainIndex].Formulas[0].Name = QuoteCode; //Add MA lines to main stock view Param = Request.QueryString["MA"]; if (Param != null && Param != "") { foreach (string s in Param.Split(';')) { if (s != "") { fc[MainIndex].AddFormula("FML.MA(" + s + ")"); } } } //Add EMA lines to main stock view Param = Request.QueryString["EMA"]; if (Param != null && Param != "") { foreach (string s in Param.Split(';')) { if (s != "") { fc[MainIndex].AddFormula("FML.EMA(" + s + ")"); } } } //Add override lines to main stock view Param = Request.QueryString["OVER"]; if (Param != null && Param != "") { if (Over != "") { Param = Param + ";" + Over; } } else { Param = Over; } if (dmb is IntraDataManagerBase) { string s = DataProvider.GetStringData("LastPrice"); if (s != null && s != "") { double d = double.Parse(s); s = "DotLine(" + d.ToString("f2") + ")"; if (Param != null && Param != "") { Param += ";" + s; } else { Param = s; } } } if (Param != null && Param != "") { //foreach(string s in Param.Split(';')) string[] ss = Param.Split(';'); for (int i = 0; i < ss.Length; i++) { string s = ss[i].ToUpper(); if (s != "") { if (s == "AreaBB") { fc[MainIndex].InsertFormula(0, "FML." + s); } else { fc[MainIndex].AddFormula("FML." + s); } } } } //Add indicators to stock chart if (Inds != null) { foreach (string s in Inds) { if (s != "") { int k = s.IndexOf("{U}"); if (k > 0) { fc.InsertArea(MainIndex, "FML." + s.Substring(0, k)); MainIndex++; } else { fc.AddArea("FML." + s); } } } } //Apply build-in stock chart skin Param = Request.QueryString["Skin"]; if (Param == null || Param == "") { Param = Config.DefaultSkin; } FormulaSkin fs = FormulaSkin.GetSkinByName(Param); if (fs != null) { if (Config.YAxisFormat != "") { fs.AxisY.Format = Config.YAxisFormat; } if (Config.ShowXAxisInLastArea) { fs.ShowXAxisInLastArea = true; } fc.SetSkin(fs); } try { fc.LatestValueType = (LatestValueType)Enum.Parse(typeof(LatestValueType), Config.LatestValueType, true); } catch { } //Add compare line to other stocks Param = Request.QueryString["COMP"]; if (Param != null && Param != "") { foreach (string s in Param.Split(';', ',')) { if (s != "") { fc[MainIndex].AddFormula("COMPARE(" + s + ")"); } } fc[MainIndex].AxisY.ShowAsPercent = true; fc[MainIndex].AxisY.Format = "p1"; fc.LatestValueType = LatestValueType.None; } //Set stock chart time period fc.EndTime = Utils.ToDateDef(Request.QueryString["End"], EndTime); fc.StartTime = Utils.ToDateDef(Request.QueryString["Start"], StartTime); DataProvider.DataCycle = DataCycle.Parse(Request.QueryString["Cycle"]); //Set X-Axis format string XFormat = Request.QueryString["XFormat"]; if (XFormat != null && XFormat != "") { fc.AxisXFormat = XFormat; fc.AllXFormats = null; } //Set Y-Axis format string YFormat = Request.QueryString["YFormat"]; if (YFormat != null && YFormat != "") { fc.AxisYFormat = YFormat; } //Set X-Axis cycle string XCycle = Request.QueryString["XCycle"]; if (XCycle != null && XCycle != "") { fc.DataCycle = DataCycle.Parse(XCycle); } //Set render type : Bar , Candle or Line Param = Request.QueryString["Type"]; if (Param != null && Param != "") { StockRenderType srt; if (Param.Length == 1) { srt = (StockRenderType)int.Parse(Param); } else { srt = (StockRenderType)Enum.Parse(typeof(StockRenderType), Param, true); } fc[MainIndex].StockRenderType = srt; } //Set Scale type : Normal or Log Param = Request.QueryString["Scale"]; if (Param != null && Param != "") { ScaleType st; if (Param.Length == 1) { st = (ScaleType)int.Parse(Param); } else { st = (ScaleType)Enum.Parse(typeof(ScaleType), Param, true); } fc[MainIndex].AxisY.Scale = st; } //Bottom margin Param = Request.QueryString["BMargin"]; if (Param != null && Param != "") { fc[MainIndex].BottomMargin = Tools.ToDoubleDef(Param, 0); } //Bind stock data fc.DataProvider = DataProvider; for (int i = 1; i < fc[MainIndex].FormulaDataArray.Count; i++) { if (fc[MainIndex][i].ParentFormula.FormulaName.StartsWith("COMPARE")) { FormulaAxisY fay = fc[MainIndex].AddNewAxisY(AxisPos.Left); FormulaData fd = fc[MainIndex][i]; fd.Transform = Transform.Normal; fd.AxisYIndex = 1; break; } } fc[MainIndex].AxisY.MajorTick.ShowLine = Config.ShowMainAreaLineY; fc[MainIndex].AxisX.MajorTick.ShowLine = Config.ShowMainAreaLineX; //Set indicator line width Param = Request.QueryString["Width"]; if (Param != null && Param != "") { float LineWidth = float.Parse(Param); for (int i = 0; i < fc.Areas.Count; i++) { fc[i].LinePen.Width = LineWidth; } } fc[MainIndex].AxisY.AutoMultiply = false; if (Request.QueryString["SV"] == "0") { fc.ShowValueLabel = false; } fc.StickRenderType = Config.StickRenderType; Rectangle Rect = new Rectangle(0, 0, Width, Height); Param = Request.QueryString["Layout"]; if (Param == null || Param == "") { Param = "Default"; } string[] Layouts = Param.Split(';', ','); Layout L = Layout.ParseString(Config.Read(Layouts[0] + "Layout", "ChartRect=(0,0,0,0)"), Rect); //,DataProvider); for (int j = 1; j < Layouts.Length; j++) { L.Merge(Layout.ParseString(Config.Read(Layouts[j] + "Layout", "ChartRect=(0,0,0,0)"), Rect)); //,DataProvider)); } L.CompanyName = Config.CompanyName; L.URL = Config.URL; L.StartTick = Start; fc.Rect = L.ChartRect; B = fc.GetBitmap(Width, Height, fc.Rect); Graphics g = Graphics.FromImage(B); L.Render(g, Rect, fc); int X = Tools.ToIntDef(Request.QueryString["X"], 0); int Y = Tools.ToIntDef(Request.QueryString["Y"], 0); if (X > 0 && Y > 0) { fc.Rect = new Rectangle(0, 0, Width, Height); fc.ShowCursorLabel = true; fc.DrawCursor(g, X, Y); } } catch (Exception ex) { B = new Bitmap(Width, Height); Graphics g = Graphics.FromImage(B); g.Clear(Color.White); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; g.DrawString(ex.Message, new Font("Verdana", 12), new SolidBrush(Color.FromArgb(196, Color.Black)), new Rectangle(0, 0, Width, Height), sf); } //Create stock image to Bitmap Response.ContentType = "Image/" + Config.ImageFormat; //Create chart stream MemoryStream ms = new MemoryStream(); if (Config.ImageFormat == "Gif") { Type tQuantization = (Type)Application["Quantization"]; if (tQuantization == null) { Assembly[] ass = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly a in ass) { if (a.FullName.StartsWith("ImageQuantization")) { tQuantization = a.GetType("ImageQuantization.OctreeQuantizer"); } } Application["Quantization"] = tQuantization; } if (tQuantization != null) { object[] os; if (Config.TransparentColor.IsEmpty) { os = new object[] { B, ms, Config.GifColors } } ; else { os = new object[] { B, ms, Config.GifColors, Config.TransparentColor } }; tQuantization.InvokeMember("Save", BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, os); } if (ms.Length == 0) { B.Save(ms, ImageFormat.Png); } } else if (Config.ImageFormat == "Png") { B.Save(ms, ImageFormat.Png); } else if (Config.ImageFormat == "Jpeg") { B.Save(ms, ImageFormat.Jpeg); } //Output the chart stream to web browser ms.WriteTo(Response.OutputStream); }