Exemplo n.º 1
0
        public static void GenPriceData(string stockCode)
        {
            DateTime dt = DateTime.Now;
            decimal  lastHighPrice = 0, lastLowPrice = 0, lastClosePrice = 0, lastOpenPrice = 0, lastVolume = 0;

            data.baseDS.priceDataSumRow dayPriceRow = myDailyPrice.GetData(stockCode, dt);
            if (dayPriceRow == null)
            {
                data.baseDS.priceDataRow priceRow = application.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();

            data.importDS.importPriceRow importRow = importPriceTbl.NewimportPriceRow();
            application.DbAccess.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.openPrice = lastOpenPrice;
            importRow.highPrice = Math.Max(lastHighPrice, highPrice);
            if (lowPrice < importRow.highPrice)
            {
                importRow.lowPrice = lowPrice;
            }
            else
            {
                importRow.lowPrice = importRow.highPrice;
            }

            if (closePrice >= importRow.lowPrice && closePrice <= importRow.highPrice)
            {
                importRow.closePrice = closePrice;
            }
            else
            {
                importRow.closePrice = (importRow.lowPrice + importRow.highPrice) / 2;
            }
            importRow.volume    = lastVolume + common.system.Random(0, 100);
            importRow.onDate    = dt;
            importRow.stockCode = stockCode;
            importPriceTbl.AddimportPriceRow(importRow);

            imports.libs.AddImportPrice(importPriceTbl, priceDataTbl);
            application.DbAccess.UpdateData(priceDataTbl);
            // In VN culture : start of week is Monday (not Sunday)
            imports.libs.AggregatePriceData(priceDataTbl, vnCulture, null);

            myDailyPrice.UpdateData(importRow);
        }
Exemplo n.º 2
0
        //Use the idea from http://www.codeproject.com/KB/database/CsvReader.aspx by Sebastien Lorion
        public static bool ImportOHLCV_CSV(string csvFileName, char delimiter, common.dateTimeLibs.DateTimeFormats dataDateFormat,
                                           string stockExchangeForNewCode, CultureInfo culture,
                                           data.baseDS.priceDataDataTable priceDataTbl,
                                           ImportRow ImportRowFunc,
                                           OnUpdatePriceData onUpdateDataFunc,
                                           OnEndImportPriceData onEndImportFunc)
        {
            libs.importStat myImportStat = new libs.importStat();
            myImportStat.Reset();
            myImportStat.dateFormat = dataDateFormat;
            myImportStat.culture    = culture;
            data.baseDS.stockCodeDataTable stockCodeTbl = new data.baseDS.stockCodeDataTable();
            data.baseDS.priceDataRow       priceDataRow;

            DataRowView[] foundRows;
            application.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, stockExchangeForNewCode, stockCodeTbl);
                        application.DbAccess.UpdateData(stockCodeTbl);
                    }

                    // Ignore all data that was in database
                    //if (!foundLastPriceDate)
                    //{
                    //    lastPriceDate = libs.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();
                    commonClass.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);
        }