public void FactorFiles_CanBeGenerated_Accurately() { // Arrange var yahooEvents = _yahooDataDownloader.DownloadSplitAndDividendData(_symbol, Parse.DateTime("01/01/1970"), DateTime.MaxValue); var filePath = LeanData.GenerateRelativeFactorFilePath(_symbol); var tolerance = 0.00001m; if (!File.Exists(filePath)) { throw new ArgumentException("This test requires an already calculated factor file." + "Try using one of the pre-existing factor files "); } var originalFactorFileInstance = FactorFile.Read(PermTick, Market); // we limit events to the penultimate time in our factor file (last one is 2050) var lastValidRow = originalFactorFileInstance.SortedFactorFileData.Reverse().Skip(1).First(); // Act var newFactorFileInstance = _factorFileGenerator.CreateFactorFile(yahooEvents.Where(data => data.Time.AddDays(-1) <= lastValidRow.Key).ToList()); var earliestDate = originalFactorFileInstance.SortedFactorFileData.First().Key; var latestDate = originalFactorFileInstance.SortedFactorFileData.Last().Key; // Assert Assert.AreEqual(originalFactorFileInstance.SortedFactorFileData.Count, newFactorFileInstance.SortedFactorFileData.Count); for (var i = earliestDate; i < latestDate; i = i.AddDays(1)) { FactorFileRow expected = null; FactorFileRow actual = null; originalFactorFileInstance.SortedFactorFileData.TryGetValue(i, out expected); newFactorFileInstance.SortedFactorFileData.TryGetValue(i, out actual); if (expected == null || actual == null) { Assert.IsTrue(actual == null); Assert.IsTrue(expected == null); } else { Assert.IsTrue(Math.Abs(expected.PriceFactor - actual.PriceFactor) < tolerance); Assert.IsTrue(Math.Abs(expected.SplitFactor - actual.SplitFactor) < tolerance); } } }
/// <summary> /// Write the factor file to the correct place in the default Data folder /// </summary> /// <param name="symbol">The symbol this factor file represents</param> public void WriteToCsv(Symbol symbol) { var filePath = LeanData.GenerateRelativeFactorFilePath(symbol); var csv = new StringBuilder(); foreach (var kvp in SortedFactorFileData) { var newLine = string.Format("{0:yyyyMMdd},{1},{2}", kvp.Key, kvp.Value.PriceFactor, kvp.Value.SplitFactor); csv.AppendLine(newLine); } File.WriteAllText(filePath, csv.ToString()); }
/// <summary> /// Write the factor file to the correct place in the default Data folder /// </summary> /// <param name="symbol">The symbol this factor file represents</param> public void WriteToCsv(Symbol symbol) { var filePath = LeanData.GenerateRelativeFactorFilePath(symbol); File.WriteAllLines(filePath, ToCsvLines()); }
/// <summary> /// Write the factor file to the correct place in the default Data folder /// </summary> /// <param name="symbol">The symbol this factor file represents</param> public void WriteToFile(Symbol symbol) { var filePath = LeanData.GenerateRelativeFactorFilePath(symbol); File.WriteAllLines(filePath, GetFileFormat()); }