130 lines
6.5 KiB
C#
130 lines
6.5 KiB
C#
//using System;
|
|
//using System.IO;
|
|
//using System.Collections.Generic;
|
|
//using Microsoft.Office.Interop.Excel;
|
|
//using MarketData.DataAccess;
|
|
//using MarketData.MarketDataModel;
|
|
//using MarketData.Generator;
|
|
//using MarketData.Utils;
|
|
|
|
//namespace MarketData.Helper
|
|
//{
|
|
// public class MACDHelperSheet
|
|
// {
|
|
// public MACDHelperSheet()
|
|
// {
|
|
// }
|
|
// public static bool GenerateMACDSheet(String strPathTemplateFile, String symbol)
|
|
// {
|
|
// Microsoft.Office.Interop.Excel.Application excelApp = null;
|
|
// Microsoft.Office.Interop.Excel.Workbook workbook = null;
|
|
// Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
|
|
// Microsoft.Office.Interop.Excel.Sheets worksheets = null;
|
|
// int rowOffset = 1;
|
|
// try
|
|
// {
|
|
// String currentWorkingDirectory = Directory.GetCurrentDirectory();
|
|
// if (!File.Exists(strPathTemplateFile))
|
|
// {
|
|
// MDTrace.WriteLine(LogLevel.DEBUG,"Cannot locate " + strPathTemplateFile);
|
|
// return false;
|
|
// }
|
|
// DateGenerator dateGenerator = new DateGenerator();
|
|
// int pricingDays = 120; // 90
|
|
// DateTime startDate = DateTime.Now;
|
|
// symbol = symbol.ToUpper();
|
|
// Dictionary<DateTime, Price> pricesByDate = new Dictionary<DateTime, Price>();
|
|
// startDate = dateGenerator.GetPrevBusinessDay(DateTime.Now); ;
|
|
// if (Utility.IsEpoch(startDate))
|
|
// {
|
|
// MDTrace.WriteLine(LogLevel.DEBUG,"No price dates for '" + symbol + "'");
|
|
// return false;
|
|
// }
|
|
// DateTime historicalDate = dateGenerator.GenerateHistoricalDate(startDate, pricingDays);
|
|
// Prices prices = PricingDA.GetPrices(symbol, startDate, historicalDate);
|
|
// if(null==prices||0==prices.Count)
|
|
// {
|
|
// MDTrace.WriteLine(LogLevel.DEBUG,"No price dates for '" + symbol + "'");
|
|
// return false;
|
|
// }
|
|
// Price latestPrice=prices[0];
|
|
// String companyName = PricingDA.GetNameForSymbol(symbol);
|
|
// MACDSetup macdSetupSlow = new MACDSetup(12, 26, 9);
|
|
// MACDSetup macdSetupFast = new MACDSetup(8, 17, 9);
|
|
// MACDSignals macdSignalsSlow = MACDGenerator.GenerateMACD(prices, macdSetupSlow);
|
|
// MACDSignals macdSignalsFast = MACDGenerator.GenerateMACD(prices, macdSetupFast);
|
|
// for (int index = 0; index < prices.Count; index++) pricesByDate.Add(prices[index].Date, prices[index]);
|
|
|
|
// String pathOutputFile = currentWorkingDirectory + "\\" + symbol + "-MACD-" + Utility.DateTimeToStringMMHDDHYYYY(latestPrice.Date);
|
|
// File.Delete(pathOutputFile + ".xlsx");
|
|
// excelApp = new Microsoft.Office.Interop.Excel.Application();
|
|
// excelApp.ScreenUpdating = false;
|
|
// workbook = excelApp.Workbooks.Open(strPathTemplateFile, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
|
|
// worksheets = workbook.Worksheets;
|
|
// worksheet = (Microsoft.Office.Interop.Excel.Worksheet)worksheets.get_Item("Sheet1");
|
|
// Microsoft.Office.Interop.Excel.ChartObjects chartObjects = worksheet.ChartObjects();
|
|
// Microsoft.Office.Interop.Excel.ChartObject macdFastChartObject = chartObjects.Item(1); // yes, chart 1 is the fast line
|
|
// Microsoft.Office.Interop.Excel.ChartObject macdSlowChartObject = chartObjects.Item(2);
|
|
// macdSlowChartObject.Chart.ChartTitle.Text = companyName + " (" + symbol + ") " + Utility.DateTimeToStringMMHDDHYYYY(prices[prices.Count - 1].Date) + " Thru " + Utility.DateTimeToStringMMHDDHYYYY(latestPrice.Date)+" MACD(12,26,9)";
|
|
// macdFastChartObject.Chart.ChartTitle.Text = companyName + " (" + symbol + ") " + Utility.DateTimeToStringMMHDDHYYYY(prices[prices.Count - 1].Date) + " Thru " + Utility.DateTimeToStringMMHDDHYYYY(latestPrice.Date) + " MACD(8,17,9)";
|
|
// for (int index = 0; index < macdSignalsSlow.Count; index++)
|
|
// {
|
|
// MACDSignal macdSignal = macdSignalsSlow[index];
|
|
// Price price = pricesByDate[macdSignal.Date];
|
|
// worksheet.Cells[index + 1 + rowOffset, 1] = macdSignal.Symbol;
|
|
// worksheet.Cells[index + 1 + rowOffset, 2] = macdSignal.Date;
|
|
// worksheet.Cells[index + 1 + rowOffset, 3] = macdSignal.Fast;
|
|
// worksheet.Cells[index + 1 + rowOffset, 4] = macdSignal.Slow;
|
|
// worksheet.Cells[index + 1 + rowOffset, 5] = macdSignal.Signal;
|
|
// worksheet.Cells[index + 1 + rowOffset, 6] = macdSignal.MACD;
|
|
// worksheet.Cells[index + 1 + rowOffset, 7] = macdSignal.Histogram;
|
|
// worksheet.Cells[index + 1 + rowOffset, 8] = price.Close;
|
|
// }
|
|
// for (int index = 0; index < macdSignalsFast.Count; index++)
|
|
// {
|
|
// MACDSignal macdSignal = macdSignalsFast[index];
|
|
// Price price = pricesByDate[macdSignal.Date];
|
|
// worksheet.Cells[index + 1 + rowOffset, 9] = macdSignal.Symbol;
|
|
// worksheet.Cells[index + 1 + rowOffset, 10] = macdSignal.Date;
|
|
// worksheet.Cells[index + 1 + rowOffset, 11] = macdSignal.Fast;
|
|
// worksheet.Cells[index + 1 + rowOffset, 12] = macdSignal.Slow;
|
|
// worksheet.Cells[index + 1 + rowOffset, 13] = macdSignal.Signal;
|
|
// worksheet.Cells[index + 1 + rowOffset, 14] = macdSignal.MACD;
|
|
// worksheet.Cells[index + 1 + rowOffset, 15] = macdSignal.Histogram;
|
|
// worksheet.Cells[index + 1 + rowOffset, 16] = price.Close;
|
|
// }
|
|
// workbook.SaveAs(pathOutputFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution, true, Type.Missing, Type.Missing, Type.Missing);
|
|
// return true;
|
|
// }
|
|
// catch (Exception exception)
|
|
// {
|
|
// MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString());
|
|
// return false;
|
|
// }
|
|
// finally
|
|
// {
|
|
// if (null != worksheet) ReleaseObject(worksheet);
|
|
// if (null != worksheets) ReleaseObject(worksheets);
|
|
// if (null != workbook) ReleaseObject(workbook);
|
|
// excelApp.Quit();
|
|
// }
|
|
// }
|
|
// private static void ReleaseObject(object obj)
|
|
// {
|
|
// try
|
|
// {
|
|
// System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
|
|
// obj = null;
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// MDTrace.WriteLine(LogLevel.DEBUG,ex.ToString());
|
|
// obj = null;
|
|
// }
|
|
// finally
|
|
// {
|
|
// GC.Collect();
|
|
// }
|
|
// }
|
|
// }
|
|
//} |