public void TestBuyThenCloseTradeWithTransactionFee() { var runner = new StrategyRunner(_fakeBrokersCandlesService, _broker, 10000M); var strategy = new StrategyBuyThenClose(); var trades = runner.Run(strategy); Assert.AreEqual(1, trades.Count); Assert.AreEqual(3030M, trades[0].EntryPrice); Assert.AreEqual(5020M, trades[0].ClosePrice); //total = amount + (amount * fee) // = amount * (1 + fee) var bought = trades[0].EntryQuantity.Value * trades[0].EntryPrice.Value; var buyFee = bought * 0.01m; Assert.AreEqual(10000M, bought + buyFee); var sold = trades[0].ClosePrice.Value * trades[0].EntryQuantity.Value; var sellFee = sold * 0.01m; Assert.AreEqual(sold - bought - buyFee - sellFee, trades[0].NetProfitLoss); Assert.AreEqual(runner.Balance, 10000M + trades[0].NetProfitLoss); Assert.IsFalse(strategy.Trades.AnyOpen); Assert.IsFalse(strategy.Trades.AnyOrders); Assert.AreEqual(1, strategy.Trades.ClosedTrades.Count()); }
public void TestTotalValue() { var runner = new StrategyRunner(_fakeBrokersCandlesService, _broker, 10000M); var s = new StrategyTotalValue(); var trades = runner.Run(s); var trade1Cost = (s.Trade1.EntryPrice.Value * s.Trade1.EntryQuantity) + (s.Trade1.EntryPrice.Value * s.Trade1.EntryQuantity) * 0.001M; var trade2Cost = (s.Trade2.EntryPrice.Value * s.Trade2.EntryQuantity) + (s.Trade2.EntryPrice.Value * s.Trade2.EntryQuantity) * 0.001M; Func <decimal, decimal> trade1Value = p => (s.Trade1.EntryQuantity.Value * p) - (s.Trade1.EntryQuantity.Value * p) * 0.001M; Func <decimal, decimal> trade2Value = p => (s.Trade2.EntryQuantity.Value * p) - (s.Trade2.EntryQuantity.Value * p) * 0.001M; Assert.AreEqual(2, trades.Count); Assert.AreEqual(10000M, s.TotalValuesBeforeTrades[3500]); Assert.AreEqual(10000M - trade1Cost + trade1Value(3020M), s.TotalValuesAfterTrades[3500]); var price = 4020M; Assert.AreEqual(10000M - trade1Cost + trade1Value(price), s.TotalValuesBeforeTrades[4500]); Assert.AreEqual(10000M - trade1Cost + trade1Value(price) - trade2Cost + trade2Value(price), s.TotalValuesAfterTrades[4500]); price = 7020M; Assert.AreEqual(10000M - trade1Cost + trade1Value(price) - trade2Cost + trade2Value(price), s.TotalValuesBeforeTrades[7500]); Assert.AreEqual(10000M - trade1Cost + trade1Value(price) - trade2Cost + trade2Value(price), s.TotalValuesAfterTrades[7500]); }
public void TestBuyWithIndicator() { var runner = new StrategyRunner(_fakeBrokersCandlesService, _broker, 10000M); var strategy = new StrategyBuyWithIndicator(); var trades = runner.Run(strategy); Assert.AreEqual(1, trades.Count); Assert.AreEqual(5030M, trades[0].EntryPrice); Assert.IsTrue(strategy.Trades.AnyOpen); Assert.IsFalse(strategy.Trades.AnyOrders); Assert.AreEqual(0, strategy.Trades.ClosedTrades.Count()); }
public void TestBuyThenCloseTrade() { var runner = new StrategyRunner(_fakeBrokersCandlesService, _broker, 10000M); var strategy = new StrategyBuyThenClose(); var trades = runner.Run(strategy); Assert.AreEqual(1, trades.Count); Assert.AreEqual(3030M, trades[0].EntryPrice); Assert.AreEqual(5020M, trades[0].ClosePrice); Assert.AreEqual(((5020M * 10000M) / 3030M) - 10000M, trades[0].NetProfitLoss); Assert.IsFalse(strategy.Trades.AnyOpen); Assert.IsFalse(strategy.Trades.AnyOrders); Assert.AreEqual(1, strategy.Trades.ClosedTrades.Count()); }
public void TestBuyWithLimit() { var runner = new StrategyRunner(_fakeBrokersCandlesService, _broker, 10000M); var strategy = new StrategyBuyWithLimit(); var trades = runner.Run(strategy); Assert.AreEqual(1, trades.Count); Assert.AreEqual(3030M, trades[0].EntryPrice); Assert.AreEqual(7000M, trades[0].ClosePrice); Assert.AreEqual(8000M, trades[0].CloseDateTime.Value.Ticks); Assert.AreEqual((Math.Round((7000M * 10000M) / 3030M) - 10000M), Math.Round(trades[0].NetProfitLoss.Value)); Assert.IsFalse(strategy.Trades.AnyOpen); Assert.IsFalse(strategy.Trades.AnyOrders); Assert.AreEqual(1, strategy.Trades.ClosedTrades.Count()); }
public void TestExpiredOrder() { var runner = new StrategyRunner(_fakeBrokersCandlesService, _broker, 10000M); var strategy = new StrategyBuyWithExpiredOrder(3000); var trades = runner.Run(strategy); Assert.AreEqual(1, trades.Count); Assert.AreEqual(null, trades[0].EntryPrice); Assert.AreEqual(null, trades[0].EntryDateTime); Assert.IsFalse(strategy.Trades.AnyOpen); Assert.IsFalse(strategy.Trades.AnyOrders); Assert.AreEqual(1, strategy.Trades.ClosedTrades.Count()); Assert.IsTrue(strategy.Trades.AllTrades.All(x => x.Trade.EntryPrice == null)); Assert.AreEqual(10000M, runner.Balance); }
public void TestBuyWithOrder() { var runner = new StrategyRunner(_fakeBrokersCandlesService, _broker, 10000M); var strategy = new StrategyBuyWithOrder(); var trades = runner.Run(strategy); Assert.AreEqual(1, trades.Count); Assert.AreEqual(6000M, trades[0].EntryPrice); Assert.AreEqual(7000, trades[0].EntryDateTime.Value.Ticks); Assert.IsTrue(strategy.Trades.AnyOpen); Assert.IsFalse(strategy.Trades.AnyOrders); Assert.AreEqual(0, strategy.Trades.ClosedTrades.Count()); Assert.IsTrue(strategy.Trades.AllTrades.All(x => x.Trade.EntryPrice != null)); Assert.AreNotEqual(10000M, runner.Balance); }
public void TestBuyCloseTwoMarkets() { var runner = new StrategyRunner(_fakeBrokersCandlesService, _broker, 10000M); var strategy = new StrategyBuyCloseTwoMarkets(); var trades = runner.Run(strategy); Assert.AreEqual(2, trades.Count); Assert.AreEqual(8030M, trades[0].EntryPrice); Assert.AreEqual(7000M, trades[0].ClosePrice); Assert.AreEqual(4030M, trades[1].EntryPrice); Assert.AreEqual(6000M, trades[1].ClosePrice); Assert.IsFalse(strategy.Trades.AnyOpen); Assert.IsFalse(strategy.Trades.AnyOrders); Assert.AreEqual(2, strategy.Trades.ClosedTrades.Count()); }
static void Main(string[] args) { System.Console.WriteLine("Welcome to the Kaplan-Meier estimator program!"); SetParams(); var patientReader = new ExcelPatientReader(Path.Combine(Directory.GetCurrentDirectory(), PatientFileName), PatientSheetName, hasHeader: true); patientReader.RegisterParser(@"Patientnummer", (value, patient) => patient.Id = int.Parse(value)); patientReader.RegisterParser(@"EventFreeSurvival_event (metastasis or death)", (value, patient) => patient.CensorEvent = (EventFreeSurvival)Enum.Parse(typeof(EventFreeSurvival), value)); patientReader.RegisterParser(@"reg_t_fu_months", (value, patient) => patient.CensorEventTime = (int)Math.Floor(double.Parse(value))); var geneReader = new CSVGeneReader(Path.Combine(Directory.GetCurrentDirectory(), GeneFileName)); var patients = patientReader.ReadToEnd().ToDictionary(patient => patient.Id); var genes = geneReader.GetGenes().ToList(); ISplitStrategy[] splitStrategies = null; if (Time == "before") { splitStrategies = new ISplitStrategy[] { new T0_TopNPercentSplitStrategy(Percentile, patients) }; } if (Time == "after") { splitStrategies = new ISplitStrategy[] { new T2_TopNPercentSplitStrategy(Percentile, patients) }; } if (Time == "diff") { splitStrategies = new ISplitStrategy[] { new T0T2Diff_TopNPercentSplitStrategy(Percentile, patients) }; } string currentDir = Directory.GetCurrentDirectory(); foreach (var strategy in splitStrategies) { if (Directory.Exists(Path.Combine(currentDir, strategy.Name))) { Directory.Delete(Path.Combine(currentDir, strategy.Name), true); } DirectoryInfo targetDir = Directory.CreateDirectory(Path.Combine(currentDir, strategy.Name)); if (!targetDir.Exists) { targetDir.Create(); } StrategyRunner runner = new StrategyRunner(strategy); var orderedResults = runner.Run(genes).ToArray(); int resultsCount = orderedResults.Length; for (int index = 0; index < resultsCount; index++) { var result = orderedResults[index]; result.FDR = (result.Estimate.PValue * resultsCount) / (index + 1); } File.WriteAllText(Path.Combine(targetDir.FullName, "Results.csv"), "Illumina ID, P-Value, -Log(P-Value), FDR, GroupSize" + Environment.NewLine); File.AppendAllLines(Path.Combine(targetDir.FullName, "Results.csv"), orderedResults.Select((result, index) => string.Format("{0}, {1:E}, {2}, {3}, {4}", result.GeneId, result.Estimate.PValue, -Math.Log10(result.Estimate.PValue), (result.Estimate.PValue * resultsCount) / (index + 1), result.GroupSize))); var logRankDir = targetDir.CreateSubdirectory("Results_LogRank"); ExcelWritter excelOutput = new ExcelWritter(logRankDir, orderedResults, result => - Math.Log10(result.Estimate.PValue)); excelOutput.WriteExcelFiles(); orderedResults = orderedResults.OrderBy(result => result.FDR).ToArray(); var fdrRankDir = targetDir.CreateSubdirectory("Results_FDR"); excelOutput = new ExcelWritter(fdrRankDir, orderedResults, result => result.FDR); excelOutput.WriteExcelFiles(); System.Console.WriteLine("Output is ready at {0}", targetDir.FullName); System.Console.ReadLine(); } }