private void saveDataBtn_Click(object sender, EventArgs e) { const short constNumberOfRowInBatchToSave = 500; try { this.ShowMessage(""); //if (!DataFixValid()) return; saveDataBtn.Enabled = false; common.system.ShowCurrorWait(); System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); //Only update changed data databases.baseDS.priceDataDataTable tbl = new databases.baseDS.priceDataDataTable(); databases.baseDS.priceDataDataTable sourceTbl = (databases.baseDS.priceDataDataTable)priceDataSource.DataSource; progressBar.Visible = true; progressBar.Value = 0; progressBar.Maximum = sourceTbl.Count; int batchCount = Settings.sysNumberOfItemsInBatchProcess; for (int idx = 0; idx < sourceTbl.Count; idx++) { progressBar.Value++; Application.DoEvents(); if (sourceTbl[idx].RowState == DataRowState.Unchanged) { continue; } tbl.ImportRow(sourceTbl[idx]); batchCount--; if (batchCount == 0) { DataAccess.Libs.UpdateData(tbl); tbl.Clear(); batchCount = constNumberOfRowInBatchToSave; } } DataAccess.Libs.UpdateData(tbl); sourceTbl.AcceptChanges(); watch.Stop(); this.ShowMessage(Languages.Libs.GetString("finished") + " : " + tbl.Count.ToString() + " - " + common.dateTimeLibs.TimeSpan2String(watch.Elapsed)); } catch (Exception er) { this.ShowError(er); } finally { common.system.ShowCurrorDefault(); saveDataBtn.Enabled = true; progressBar.Visible = false; } }
public static void GenPriceData(string stockCode) { DateTime dt = DateTime.Now; decimal lastHighPrice = 0, lastLowPrice = 0, lastClosePrice = 0, lastOpenPrice = 0, lastVolume = 0; databases.baseDS.priceDataSumRow dayPriceRow = myDailyPrice.GetData(stockCode, dt); if (dayPriceRow == null) { databases.baseDS.priceDataRow priceRow = databases.DbAccess.GetLastPriceData(stockCode); lastHighPrice = priceRow.highPrice; lastLowPrice = priceRow.lowPrice; lastClosePrice = priceRow.closePrice; lastOpenPrice = priceRow.openPrice; lastVolume = 0; } else { lastHighPrice = dayPriceRow.highPrice; lastLowPrice = dayPriceRow.lowPrice; lastClosePrice = dayPriceRow.closePrice; lastOpenPrice = dayPriceRow.openPrice; lastVolume = dayPriceRow.volume; } priceDataTbl.Clear(); importPriceTbl.Clear(); databases.importDS.importPriceRow importRow = importPriceTbl.NewimportPriceRow(); databases.AppLibs.InitData(importRow); decimal highPrice = lastHighPrice + lastHighPrice * (decimal)common.system.Random(-4, 5) / 100; decimal lowPrice = lastLowPrice + lastLowPrice * (decimal)common.system.Random(-4, 5) / 100; decimal closePrice = lastClosePrice + lastClosePrice * (decimal)common.system.Random(-5, 5) / 100; importRow.volume = lastVolume + common.system.Random(0, 100); importRow.onDate = dt; importRow.stockCode = stockCode; importPriceTbl.AddimportPriceRow(importRow); Imports.Libs.AddImportPrice(importPriceTbl, priceDataTbl); databases.DbAccess.UpdateData(priceDataTbl); // In VN culture : start of week is Monday (not Sunday) databases.AppLibs.AggregatePriceData(priceDataTbl, vnCulture, null); myDailyPrice.UpdateData(importRow); }
private void saveDataBtn_Click(object sender, EventArgs e) { const short constNumberOfRowInBatchToSave = 500; try { this.ShowMessage(""); //if (!DataFixValid()) return; saveDataBtn.Enabled = false; common.system.ShowCurrorWait(); System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); //Only update changed data databases.baseDS.priceDataDataTable tbl = new databases.baseDS.priceDataDataTable(); databases.baseDS.priceDataDataTable sourceTbl = (databases.baseDS.priceDataDataTable)priceDataSource.DataSource; progressBar.Visible = true; progressBar.Value =0; progressBar.Maximum = sourceTbl.Count; int batchCount = Settings.sysNumberOfItemsInBatchProcess; for (int idx = 0; idx < sourceTbl.Count; idx++) { progressBar.Value++; Application.DoEvents(); if (sourceTbl[idx].RowState == DataRowState.Unchanged) continue; tbl.ImportRow(sourceTbl[idx]); batchCount--; if (batchCount == 0) { DataAccess.Libs.UpdateData(tbl); tbl.Clear(); batchCount = constNumberOfRowInBatchToSave; } } DataAccess.Libs.UpdateData(tbl); sourceTbl.AcceptChanges(); watch.Stop(); this.ShowMessage(Languages.Libs.GetString("finished") + " : " + tbl.Count.ToString() +" - " + common.dateTimeLibs.TimeSpan2String(watch.Elapsed) ); } catch (Exception er) { this.ShowError(er); } finally { common.system.ShowCurrorDefault(); saveDataBtn.Enabled = true; progressBar.Visible = false; } }
public databases.tmpDS.priceDiagnoseDataTable DiagnosePrice_CloseAndNextOpen(DateTime frDate, DateTime toDate, string timeScaleCode, string exchangeCode,string code, double variancePerc, double variance,byte precision) { try { databases.baseDS.priceDataDataTable priceDataTbl = new databases.baseDS.priceDataDataTable(); databases.tmpDS.priceDiagnoseDataTable priceDiagnoseTbl = new databases.tmpDS.priceDiagnoseDataTable(); if (code != null && code.Trim() != "") { priceDataTbl.Clear(); databases.DbAccess.LoadData(priceDataTbl, timeScaleCode, frDate, toDate, code.Trim()); application.AppLibs.DiagnosePrice_CloseAndNextOpen(priceDataTbl, variancePerc, variance,precision, priceDiagnoseTbl); } else { databases.tmpDS.stockCodeDataTable codeTbl = new databases.tmpDS.stockCodeDataTable(); databases.DbAccess.LoadStockCode_ByStockExchange(codeTbl, exchangeCode, AppTypes.CommonStatus.Enable); for (int idx = 0; idx < codeTbl.Count; idx++) { priceDataTbl.Clear(); databases.DbAccess.LoadData(priceDataTbl, timeScaleCode, frDate, toDate, codeTbl[idx].code); application.AppLibs.DiagnosePrice_CloseAndNextOpen(priceDataTbl, variancePerc, variance,precision, priceDiagnoseTbl); } codeTbl.Dispose(); } priceDataTbl.Dispose(); return priceDiagnoseTbl; } catch (Exception ex) { WriteSysLogLocal("WS088", ex); } return null; }
//Use the idea from http://www.codeproject.com/KB/database/CsvReader.aspx by Sebastien Lorion public static bool CSV_ImportParse(string csvFileName, char delimiter, common.dateTimeLibs.DateTimeFormats dateDataFormat, string marketCode, CultureInfo dataCulture, databases.baseDS.priceDataDataTable priceDataTbl, ImportRowHandler ImportRowFunc, OnUpdatePriceData onUpdateDataFunc, OnEndImportPriceData onEndImportFunc) { importStat myImportStat = new importStat(); myImportStat.Reset(); myImportStat.dateDataFormat = dateDataFormat; myImportStat.srcCulture = dataCulture; databases.baseDS.stockCodeDataTable stockCodeTbl = new databases.baseDS.stockCodeDataTable(); databases.baseDS.priceDataRow priceDataRow; DataRowView[] foundRows; databases.DbAccess.LoadData(stockCodeTbl, AppTypes.CommonStatus.Enable); DataView stockCodeView = new DataView(stockCodeTbl); stockCodeView.Sort = stockCodeTbl.codeColumn.ColumnName; bool fCanceled = false; DateTime lastPriceDate = common.Consts.constNullDate; importOHLCV data; // open the file "data.csv" which is a CSV file with headers using (CsvReader csv = new CsvReader(new StreamReader(csvFileName), true, delimiter)) { // missing fields will not throw an exception, // but will instead be treated as if there was a null value csv.MissingFieldAction = MissingFieldAction.ReplaceByNull; int fieldCount = csv.FieldCount; if (fieldCount < 7) { return(false); } while (csv.ReadNextRecord()) { Application.DoEvents(); myImportStat.dataCount++; data = ImportRowFunc(csv, myImportStat); if (myImportStat.cancel) { fCanceled = true; break; } if (data == null) { myImportStat.errorCount++; continue; } //Assume that all price must be valid if (data.Open <= 0 || data.High <= 0 || data.Low <= 0 || data.Close <= 0) { continue; } foundRows = stockCodeView.FindRows(data.code); if (foundRows.Length == 0) { //Try to add new stock code Libs.AddNewCode(data.code, marketCode, stockCodeTbl); databases.DbAccess.UpdateData(stockCodeTbl); } // Ignore all data that was in database //if (!foundLastPriceDate) //{ // lastPriceDate = FindLastPriceDate(data.code); // foundLastPriceDate = true; //} if (lastPriceDate != common.Consts.constNullDate && data.dateTime <= lastPriceDate) { continue; } if (priceDataTbl.FindBystockCodeonDate(data.code, data.dateTime) != null) { myImportStat.errorCount++; continue; } myImportStat.updateCount++; priceDataRow = priceDataTbl.NewpriceDataRow(); databases.AppLibs.InitData(priceDataRow); priceDataRow.stockCode = data.code; priceDataRow.onDate = data.dateTime; //Try to fix some error in data priceDataRow.openPrice = (decimal)data.Open; priceDataRow.highPrice = (decimal)data.High; priceDataRow.lowPrice = (decimal)data.Low; priceDataRow.closePrice = (decimal)data.Close; priceDataRow.volume = (decimal)data.Volume; priceDataTbl.AddpriceDataRow(priceDataRow); if (onUpdateDataFunc != null) { onUpdateDataFunc(priceDataRow, myImportStat); } } } if (fCanceled) { priceDataTbl.Clear(); return(false); } if (onEndImportFunc != null) { onEndImportFunc(priceDataTbl); } return(true); }