public void UpdateStopLoss(QuoteBar latestQuote) { return; if ((latestQuote.Bid.Close - _trailingStop > StopTicket.Get(OrderField.StopPrice) && TradeDirection > 0) || (latestQuote.Ask.Close + _trailingStop < StopTicket.Get(OrderField.StopPrice) && TradeDirection < 0)) { StopTicket.Update( new UpdateOrderFields { StopPrice = OrderUtil.RoundOrderPrices(null, (TradeDirection > 0 ? latestQuote.Bid.Close : latestQuote.Ask.Close) + TradeDirection * _trailingStop) } ); } }
public void EnterTradeSignal(QuoteBar data, bool isWarmingUp) { EnterSignal.Scan(data); if (!isWarmingUp && IsTradable && (EnterSignal.Signal == SignalType.Long || EnterSignal.Signal == SignalType.Short) && _security.Exchange.ExchangeOpen) { //Creates a new trade profile once it enters a trade var profile = new TradeProfile(_symbol, _security.VolatilityModel.Volatility, _risk, data.Price, _maximumTradeSize); if (!profile.IsSpreadTradable(data)) { return; } profile.ExitSignal = ExitSignal.ExitSignalFactory(profile); if (profile.Quantity > 0 && _tradeProfiles.Count == 0) { var hmmPrediction = 1m;// _hmmPositionSizing.PredictionRisk(); var quantity = (int)((int)EnterSignal.Signal * profile.Quantity * hmmPrediction); var askLimit = data.Ask.Close - (1m / 10000m); var bidLimit = data.Bid.Close + (1m / 10000m); var limitPrice = EnterSignal.Signal == SignalType.Long ? askLimit : bidLimit; try { profile.OpenTicket = _orderMethods.MarketOrder(_symbol, quantity, false, ((int)EnterSignal.Signal).ToString()); //profile.OpenTicket = _orderMethods.LimitOrder(_symbol, quantity, OrderUtil.RoundOrderPrices(_security, limitPrice), ((int)EnterSignal.Signal).ToString()); var stopPrice = data.Close - (int)EnterSignal.Signal * profile.DeltaStopLoss; profile.StopTicket = _orderMethods.StopMarketOrder(_symbol, -quantity, OrderUtil.RoundOrderPrices(_security, stopPrice)); } catch (Exception ex) { Console.WriteLine(ex.Message); } //var stopPrice = profile.OpenTicket.AverageFillPrice - (int)EnterSignal.Signal * profile.DeltaStopLoss; /*Console.WriteLine("{0} {1} {2} {3}", * profile.OpenTicket.OrderEvents.Select((oe) => oe.Direction).First() == OrderDirection.Buy ? "Buy " : "Sell", * profile.OpenTicket.AverageFillPrice, * profile.StopTicket.Get(OrderField.StopPrice), * Math.Abs(data.Ask.Close - data.Bid.Close) * 10000 * );*/ /*profile.StopTicket = _orderMethods.StopLimitOrder(_symbol, -(int) EnterSignal.Signal * profile.Quantity, * profile.OpenTicket.AverageFillPrice - (int) EnterSignal.Signal * profile.DeltaStopLoss, * profile.OpenTicket.AverageFillPrice - (int) EnterSignal.Signal * profile.DeltaStopLoss);*/ _tradeProfiles.Add(profile); } } }