/// <summary> /// FetchHistoricalData /// </summary> /// <param name="symbol">Financial Instrument Symbol</param> /// <param name="startDate">Format: YYYY.MM.dd</param> public static void FetchHistoricalData(DateTime startDate, DateTime endDate, string symbol, string name, string sector) { string date = endDate.ToString("yyyy.MM.dd"); JArray dataArray = JArray.Parse(getData(symbol, date)); DateTime controlDate = DateTime.MinValue; using (InvestmentAnalysisContext context = new InvestmentAnalysisContext()) { context.Configuration.AutoDetectChangesEnabled = false; context.Configuration.ValidateOnSaveEnabled = false; foreach (JToken data in dataArray.Children()) { controlDate = DateTime.Parse(data[5].Value <String>()); if (!(controlDate > startDate)) { break; } HistoricalDataBlock historicalDataBlock = context.HistoricalDataBlocks.Where(q => q.Symbol.Equals(symbol) && q.RecordDate == controlDate).SingleOrDefault(); if (null == historicalDataBlock) { historicalDataBlock = new HistoricalDataBlock(); historicalDataBlock.Symbol = symbol; historicalDataBlock.Name = name; historicalDataBlock.Sector = sector; historicalDataBlock.MinPrice = decimal.Parse(data[1].Value <string>()); historicalDataBlock.MaxPrice = decimal.Parse(data[2].Value <string>()); historicalDataBlock.LastPrice = decimal.Parse(data[3].Value <string>()); historicalDataBlock.Volume = long.Parse(data[4].Value <String>().Remove(data[4].Value <String>().IndexOf(',')).Replace(".", "")); historicalDataBlock.RecordDate = DateTime.Parse(data[5].Value <String>()); context.Entry(historicalDataBlock).State = System.Data.Entity.EntityState.Added; } } context.SaveChanges(); } if (controlDate > startDate) { FetchHistoricalData(startDate, controlDate, symbol, name, sector); } }
private static TotalTransactionInfo GetTotalTransactionInfo(string symbol, HistoricalDataBlock[] historicalDataBlocks, int fasterPeriod, int slowerPeriod, int window = 0) { Console.WriteLine("*********************************************"); Console.WriteLine("SYMBOL = {0}, WINDOW = {1}, FASTER_PERIOD = {2}, SLOWER_PERIOD = {3}", symbol, window, fasterPeriod, slowerPeriod); //HistoricalDataBlock[] historicalDataBlocks = HelperMethods.GetHistoricalDataBlock(symbol, window); double[] data = historicalDataBlocks.Select(c => (double)c.LastPrice).ToArray(); double[] emaFaster = ExponentialMovingAverage.Calculate(data, fasterPeriod).Skip(slowerPeriod - fasterPeriod).ToArray(); double[] emaSlower = ExponentialMovingAverage.Calculate(data, slowerPeriod); //Console.WriteLine("BLOCK_LENGTH = {0}, DATA_LENGTH = {1}, EMA_FASTER_LENGTH = {2}, EMA_SLOWER_LENGTH = {3}", // historicalDataBlocks.Length, data.Length, emaFaster.Length, emaSlower.Length); List <PositionInfo> _positionInfoList = new List <PositionInfo>(); string previousSign = string.Empty; string currentSign = string.Empty; bool longPosition = false; bool shortPosition = false; bool changePosition = false; string positionChangeInfo = string.Empty; for (int i = 0; i < emaFaster.Length; i++) { if (emaFaster[i] > emaSlower[i]) { currentSign = "+"; } else if (emaFaster[i] < emaSlower[i]) { currentSign = "-"; } else { currentSign = "="; } longPosition = ("-".Equals(previousSign) || "=".Equals(previousSign)) && "+".Equals(currentSign); shortPosition = ("+".Equals(previousSign) || "=".Equals(previousSign)) && "-".Equals(currentSign); changePosition = longPosition || shortPosition; HistoricalDataBlock historicalData = historicalDataBlocks[slowerPeriod + i - 1]; positionChangeInfo = string.Empty; if (changePosition) { string type = longPosition ? "LONG" : "SHORT"; positionChangeInfo = "*****" + " # " + historicalData.LastPrice.ToString("F") + " # " + type; _positionInfoList.Add(new PositionInfo() { Type = type, Date = historicalData.RecordDate, Price = (double)historicalData.LastPrice }); } //Console.WriteLine(historicalData.RecordDate.ToString("yyyy-MM-dd") + " # " // + string.Format("{0:F4}", emaFaster[i]) + " # " + string.Format("{0:F4}", emaSlower[i]) + " # " // + currentSign + " # " + positionChangeInfo); previousSign = currentSign; } //Console.WriteLine("*********************************************"); Console.WriteLine("TOTAL POSITION COUNT: " + _positionInfoList.Count); double _safe = 0.00; if (1 == _positionInfoList.Count % 2) { _positionInfoList.RemoveAt(_positionInfoList.Count - 1); } if (_positionInfoList.Count > 0) { double price = 0.00; for (int i = 0; i < _positionInfoList.Count; i++) { //Console.WriteLine(positionInfoList[i].ToString()); price = _positionInfoList[i].Price; if ("SHORT".Equals(_positionInfoList[i].Type)) { price = (-1) * price; } _safe = _safe + price; } Console.WriteLine("TOTAL POSITION PROFIT / LOSS: " + _safe); } return(new TotalTransactionInfo { safe = _safe, positionInfoList = _positionInfoList, ratio = (((_safe) / (_positionInfoList[0].Price * 100)).ToString("F")) }); //TotalTransactionInfo totalTransactionInfo = new TotalTransactionInfo(); //x.positionInfoList = _positionInfoList; //x.safe = _safe; //x.ratio = (((_safe) / (_positionInfoList[0].Price * 100)).ToString("F")) ; //return _safe; //Console.WriteLine("*********************************************"); }