private static bool IsValidAction(TradeSignal action) { if (action != null && !string.IsNullOrWhiteSpace(action.Currency) && action.BuyPriceMax > 0 && action.BuyPriceMin > 0 && action.SellPrice?.Any() == true) { return(true); } return(false); }
public static Analysis Analyze(TradeSignal action) { if (action == null || ReferenceEquals(action, TradeSignal.Empty)) { return(null); } if (!double.IsNaN(action.StopLoss) && action.StopLoss < 0) { throw new InvalidOperationException("StopLoss is less than zero."); } var buyPrice = action.BuyPriceMin; double sellFraction = 1.0 / action.SellPrice.Length; // Assume we bought one coin double earned = 0.0; foreach (var sellPrice in action.SellPrice) { if (sellPrice > 0.0) { earned += sellPrice * sellFraction; } else { throw new InvalidOperationException("SellPrice is less than zero."); } } double potentialGains = (earned - buyPrice) / buyPrice; double riskReward = double.NaN; if (!double.IsNaN(action.StopLoss)) { riskReward = (buyPrice - action.StopLoss) / (earned - buyPrice); } var result = new Analysis { Currency = action.Currency, PossibleGains = potentialGains, RiskReward = riskReward, }; return(result); }
public static TradeSignal ProcessMessage(string message) { if (string.IsNullOrWhiteSpace(message)) { return(TradeSignal.Empty); } var lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); var result = new TradeSignal(); int i = 0; while (i < lines.Length) { var line = lines[i]; if (line.StartsWith(CoinKeyword)) { var coin = RemoveKeyword(line, CoinKeyword); int bracketIndex = coin.IndexOf("("); if (bracketIndex > -1) { coin = coin.Substring(0, bracketIndex); } result.Currency = coin; } else if (line.StartsWith(BuyKeyword)) { var buyPrice = RemoveKeyword(line, BuyKeyword); if (buyPrice.Contains("-")) { var range = buyPrice.Split('-'); double min, max; if (double.TryParse(range[0], out min) && double.TryParse(range[1], out max)) { result.BuyPriceMin = min; result.BuyPriceMax = max; } } else { double price; if (double.TryParse(buyPrice, out price)) { result.BuyPriceMin = result.BuyPriceMax = price; } } } else if (line.StartsWith(SellKeyword)) { var sellList = new List <double>(); int j = i + 1; while (j < lines.Length) { line = lines[j]; if (line.StartsWith(MoneyMark)) { var clearLine = line.Replace(MoneyMark, string.Empty); var sellPrice = double.Parse(clearLine); sellList.Add(sellPrice); j++; } else { break; } } result.SellPrice = sellList.ToArray(); } else if (line.StartsWith(StopLossKeyword)) { var stopLoss = RemoveKeyword(line, StopLossKeyword); result.StopLoss = double.Parse(stopLoss); } else { if (line.Contains(ShortTermKeyword)) { result.Term = Term.Short; } else if (line.Contains(MiddleTermKeyword)) { result.Term = Term.Middle; } else if (line.Contains(LongTermKeyword)) { result.Term = Term.Short; } } i++; } if (IsValidAction(result)) { return(result); } return(TradeSignal.Empty); }
static TradeSignal() { Empty = new TradeSignal(); }