private string getCSVLine(string csvFormat, StockBase stockBase, CSVOutputSettings csvOutputSettings) { csvFormat = csvFormat.Replace(CSVOutputSettings.STOCK_SYMBOL, stockBase.Symbol); csvFormat = csvFormat.Replace(CSVOutputSettings.DESCRIPTION_SYMBOL, stockBase.Description); csvFormat = csvFormat.Replace(CSVOutputSettings.OPEN_SYMBOL, stockBase.Open.ToString()); csvFormat = csvFormat.Replace(CSVOutputSettings.HIGH_SYMBOL, stockBase.High.ToString()); csvFormat = csvFormat.Replace(CSVOutputSettings.LOW_SYMBOL, stockBase.Low.ToString()); csvFormat = csvFormat.Replace(CSVOutputSettings.CLOSE_SYMBOL, stockBase.Close.ToString()); csvFormat = csvFormat.Replace(CSVOutputSettings.NFBS_SYMBOL, stockBase.NetForeignBuy.ToString()); if (stockBase is SectorItem) { string volume; if (csvOutputSettings.UseSectorValueAsVolume) { volume = stockBase.Value.ToString(CultureInfo.CreateSpecificCulture("en-US")); } else { volume = stockBase.Volume.ToString(CultureInfo.CreateSpecificCulture("en-US")); } csvFormat = csvFormat.Replace(CSVOutputSettings.VOLUME_SYMBOL, volume); } else { csvFormat = csvFormat.Replace(CSVOutputSettings.VOLUME_SYMBOL, stockBase.Volume.ToString(CultureInfo.CreateSpecificCulture("en-US"))); } //Debug.WriteLine(csvFormat); return(csvFormat); }
public void ConvertToCSV(PSEDocument pseDocument, CSVOutputSettings csvOutputSettings) { //Debug.WriteLine(csvOutputSettings.ToString()); List <string> csvOutput = new List <string>(); string tradeDate = pseDocument.TradeDate.ToString(csvOutputSettings.DateFormat); string csvFormat = csvOutputSettings.CSVFormat.Replace(CSVOutputSettings.DATE_SYMBOL, tradeDate); csvFormat = csvFormat.Replace(",", csvOutputSettings.Delimiter); foreach (SectorItem sectorItem in pseDocument.Sectors) { // sectors //sectorItem.Volume = sectorItem.Volume / csvOutputSettings.SectorVolumeDivider; sectorItem.Value = Math.Truncate(sectorItem.Value / csvOutputSettings.SectorVolumeDivider); //sectorItem.NetForeignBuy = Math.Truncate(sectorItem.NetForeignBuy / csvOutputSettings.SectorVolumeDivider); csvOutput.Add(getCSVLine(csvFormat, sectorItem, csvOutputSettings)); //sub sectors foreach (SubSectorItem subsectorItem in sectorItem.SubSectors) { // stocks foreach (StockItem stockItem in subsectorItem.Stocks) { if (stockItem.Open > 0) { csvOutput.Add(getCSVLine(csvFormat, stockItem, csvOutputSettings)); } } } } // write to file string filename = csvOutputSettings.OutputDirectory + Helpers.GetDirectorySeparator() + csvOutputSettings.Filename; using (StreamWriter writer = new StreamWriter(filename)) { try { foreach (string item in csvOutput) { writer.WriteLine(item); } } finally { writer.Flush(); writer.Close(); } }; if (OnConvertComplete != null) { OnConvertComplete(pseDocument, new ConvertEventArgs(csvOutputSettings)); } }
public object Clone() { CSVOutputSettings result = new CSVOutputSettings(); result.CSVFormat = this.CSVFormat; result.Delimiter = this.Delimiter; result.DateFormat = this.DateFormat; result.Filename = this.Filename; result.OutputDirectory = this.OutputDirectory; result.UseSectorValueAsVolume = this.UseSectorValueAsVolume; return(result); }
public static void Convert <T>(PSEDocument pseDocument, T outputSettings) { if (outputSettings is CSVOutputSettings) { var converter = new CSVConverter(); converter.Execute(pseDocument, outputSettings as CSVOutputSettings); } else if (outputSettings is AmiOutputSettings) { var converter = new AmibrokerConverter(); converter.Execute(pseDocument, outputSettings as AmiOutputSettings); } else if (outputSettings is MetaOutputSettings) { var metaOutputSettings = outputSettings as MetaOutputSettings; CSVOutputSettings csvOutputSettings = new CSVOutputSettings(); csvOutputSettings.DateFormat = "yyyymmdd"; csvOutputSettings.CSVFormat = "{S},M,{D},{O},{H},{L},{C},{V},{F}"; csvOutputSettings.Delimiter = ","; csvOutputSettings.Filename = "tmp.csv"; csvOutputSettings.SectorVolumeDivider = metaOutputSettings.SectorVolumeDivider; csvOutputSettings.OutputDirectory = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); csvOutputSettings.UseSectorValueAsVolume = metaOutputSettings.UseSectorValueAsVolume; CSVConverter csvConverter = new CSVConverter(); csvConverter.Execute(pseDocument, csvOutputSettings); string csvFile = csvOutputSettings.OutputDirectory + "\\" + csvOutputSettings.Filename; MetastockConverter converter = new MetastockConverter(pseDocument, csvFile, metaOutputSettings); converter.Execute(pseDocument, metaOutputSettings); } else { throw new Exception("Unsupported setting type."); } }