public Trade CreateMarketEntry(string broker, decimal entryPrice, DateTime entryTime, TradeDirection direction, decimal amount, string market, string baseAsset, decimal?stop, decimal?limit, Timeframe?timeframe = null, string strategies = null, string comments = null, bool alert = false, CalculateOptions calculateOptions = CalculateOptions.Default, TradeUpdateMode updateMode = TradeUpdateMode.Default) { var trade = new Trade { Broker = broker, CalculateOptions = calculateOptions }; if (stop != null) { trade.AddStopPrice(entryTime, stop.Value); } if (limit != null) { trade.AddLimitPrice(entryTime, limit.Value); } trade.Market = market; trade.BaseAsset = baseAsset; trade.TradeDirection = direction; trade.EntryPrice = entryPrice; trade.EntryDateTime = entryTime; trade.EntryQuantity = amount; trade.Timeframe = timeframe; trade.Alert = alert; trade.Comments = comments; trade.Strategies = strategies; trade.UpdateMode = updateMode; return(trade); }
public Trade CreateOrder(string broker, decimal entryOrder, Candle latestCandle, TradeDirection direction, decimal amount, string market, string baseAsset, DateTime?orderExpireTime, decimal?stop, decimal?limit, CalculateOptions calculateOptions = CalculateOptions.Default) { var orderDateTime = latestCandle.CloseTime(); var trade = new Trade { CalculateOptions = calculateOptions }; trade.SetOrder(orderDateTime, entryOrder, market, direction, amount, orderExpireTime); if (stop != null) { trade.AddStopPrice(orderDateTime, stop.Value); } if (limit != null) { trade.AddLimitPrice(orderDateTime, limit.Value); } trade.Broker = broker; trade.BaseAsset = baseAsset; if (direction == TradeDirection.Long) { trade.OrderType = (float)entryOrder <= latestCandle.CloseAsk ? OrderType.LimitEntry : OrderType.StopEntry; } else { trade.OrderType = (float)entryOrder <= latestCandle.CloseBid ? OrderType.StopEntry : OrderType.LimitEntry; } return(trade); }
public static void TrailIndicatorValues(Trade trade, Candle candle, IndicatorValues indicatorValues) { if (trade.EntryDateTime == null || trade.EntryPrice == null || !indicatorValues.HasValue || trade.InitialStop == null) { return; } // Get current var currentIndicatorValue = (decimal)indicatorValues.Value; // Get value at the time of the trade var index = indicatorValues.Values.BinarySearchGetItem(i => indicatorValues.Values[i].TimeTicks, 0, trade.EntryDateTime.Value.Ticks, BinarySearchMethod.PrevLowerValueOrValue); if (index == -1) { return; } var initialIndicator = indicatorValues[index]; if (initialIndicator == null) { return; } var initialIndicatorValue = initialIndicator.Value; var stopDist = Math.Abs((decimal)initialIndicatorValue - trade.InitialStop.Value); var newStop = trade.TradeDirection == TradeDirection.Long ? currentIndicatorValue - stopDist : currentIndicatorValue + stopDist; if (trade.StopPrice != newStop) { if (trade.TradeDirection == TradeDirection.Long && newStop > trade.StopPrice) { trade.AddStopPrice(candle.CloseTime(), newStop); } else if (trade.TradeDirection == TradeDirection.Short && newStop < trade.StopPrice) { trade.AddStopPrice(candle.CloseTime(), newStop); } } }
public static void ClearStop(this Trade trade, Candle lastCandle) { if (trade.CloseDateTime != null) { MessageBox.Show("Trade is now closed"); return; } if (trade.EntryPrice != null) { MessageBox.Show("Trade is now open so cannot clear stop"); return; } if (trade.StopPrices.Count > 0) { var date = lastCandle.CloseTime(); trade.AddStopPrice(date, null); } }
public static void SetTradeStop(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 stop", "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 stop", "Invalid value", MessageBoxButton.OK); return; } var date = lastCandle.CloseTime(); trade.AddStopPrice(date, (decimal)price); }