public static IEnumerable <Trade> GetFallingSlopesEnum(IEnumerable <Trade> trades, double percent, TimeSpan timeSpan) { var tradeSet = new TradeSetDropWatcher(timeSpan, percent); foreach (Trade trade in trades) { tradeSet.AddTrade(trade); if (trade.IsFallingSlope(tradeSet)) { tradeSet.ClearSet(); yield return(trade); } } }
public static void RunStrategy( IEnumerable <Trade> trades, double buyPercent, TimeSpan timeSpan, double takeProfitPercent, double stopLossPercent) { var TradeSet = new TradeSetDropWatcher(timeSpan, buyPercent); var BuyTradesList = new List <Trade>(); var toDelete = new List <Trade>(); double budget = 100.0; double profit = 0; var currentTrade = new Trade(0, 0, 0, ""); Console.WriteLine($"|Action: " + $"|Date: " + $"|Type: " + $"|Price: " + $"|Amount: " + $"|Profit/loss: " + $"|Total profit: |"); foreach (Trade trade in trades) { foreach (Trade buyTrade in BuyTradesList) { if (trade.Price - buyTrade.Price > buyTrade.Price * takeProfitPercent / 100) { profit += trade.Price * buyTrade.Amount; Console.WriteLine($"|Selling at {takeProfitPercent}% rise " + $"|{DateTimeOffset.FromUnixTimeSeconds(trade.Date).UtcDateTime} " + $"|sell " + $"|{trade.Price,-7}" + $"|{Math.Round(buyTrade.Amount, 4),-8}" + $"|{Math.Round(trade.Price * buyTrade.Amount, 4),-13}" + $"|{Math.Round(profit, 4), -10} |"); toDelete.Add(buyTrade); } if (buyTrade.Price - trade.Price > buyTrade.Price * stopLossPercent / 100) { profit += trade.Price * buyTrade.Amount; Console.WriteLine($"|Stop loss at {stopLossPercent}% drop " + $"|{DateTimeOffset.FromUnixTimeSeconds(trade.Date).UtcDateTime} " + $"|sell " + $"|{trade.Price,-7}" + $"|{Math.Round(buyTrade.Amount, 4),-8}" + $"|{Math.Round(trade.Price * buyTrade.Amount, 4),-13}" + $"|{Math.Round(profit, 4),-10} |"); toDelete.Add(buyTrade); } } foreach (Trade tradeToDelete in toDelete) { BuyTradesList.Remove(tradeToDelete); } TradeSet.AddTrade(trade); if (trade.IsFallingSlope(TradeSet)) { TradeSet.ClearSet(); var buyTrade = new Trade(budget / trade.Price, trade.Date, trade.Price, "buy"); BuyTradesList.Add(buyTrade); profit -= budget; Console.WriteLine($"|Buying at {buyPercent}% drop " + $"|{DateTimeOffset.FromUnixTimeSeconds(trade.Date).UtcDateTime} " + $"|buy " + $"|{trade.Price,-7}" + $"|{Math.Round(buyTrade.Amount, 4),-8}" + $"|{Math.Round(-budget, 4),-13}" + $"|{Math.Round(profit, 4),-10} |"); } toDelete.Clear(); currentTrade = trade; } if (BuyTradesList.Count() != 0) { foreach (Trade buyTrade in BuyTradesList) { profit += currentTrade.Price * buyTrade.Amount; Console.WriteLine($"|Selling remaining trade" + $"|{DateTimeOffset.FromUnixTimeSeconds(currentTrade.Date).UtcDateTime} " + $"|sell " + $"|{currentTrade.Price,-7}" + $"|{Math.Round(buyTrade.Amount, 4),-8}" + $"|{Math.Round(currentTrade.Price * buyTrade.Amount, 4),-13}" + $"|{Math.Round(profit, 4),-10} |"); } } Console.WriteLine($"Total profit of strategy {profit}"); Console.Write("Press enter to exit"); var _ = Console.ReadLine(); }
public bool IsFallingSlope(TradeSetDropWatcher tradeSet) { return(tradeSet.TradesList.Any(t => Price - t.Price <= -tradeSet.ProcentDivHundred * t.Price)); }