public static void SetTradeLimit(this Trade trade, Candle lastCandle, double price) { if (trade.CloseDateTime != null) { MessageBox.Show("Trade is now closed"); return; } if (trade.TradeDirection == TradeDirection.Long && ((trade.OrderPrice == null && price < lastCandle.CloseBid) || (trade.OrderPrice != null && trade.EntryPrice == null && (decimal)price < trade.OrderPrice.Value))) { MessageBox.Show("Invalid limit", "Invalid value", MessageBoxButton.OK); return; } if (trade.TradeDirection == TradeDirection.Short && ((trade.OrderPrice == null && price > lastCandle.CloseAsk) || (trade.OrderPrice != null && trade.EntryPrice == null && (decimal)price > trade.OrderPrice.Value))) { MessageBox.Show("Invalid limit", "Invalid value", MessageBoxButton.OK); return; } var date = lastCandle.CloseTime(); if (trade.EntryDateTime != null) { foreach (var limit in trade.LimitPrices.ToList()) { if (limit.Date == date) { trade.RemoveLimitPrice(trade.LimitPrices.IndexOf(limit)); } } trade.AddLimitPrice(date, (decimal)price); } else { trade.ClearLimitPrices(); trade.AddLimitPrice(date, (decimal)price); } }