/// <summary> /// fill against bid and ask rather than trade /// </summary> /// <param name="k"></param> /// <param name="c"></param> /// <param name="bidask"></param> /// <param name="fillOpg"></param> /// <returns></returns> public StatusType Fill(Tick k, BrokerModel c, bool bidask, bool fillOpg) { if (!bidask) { return(Fill(k, c, fillOpg)); } // buyer has to match with seller and vice verca bool ok = Direction == Direction.Long ? k.HasAsk : k.HasBid; if (!ok) { return(StatusType.OFF_QUOTES); } //Get price and spread from tick and transaction costs decimal p = Direction == Direction.Long ? k.Ask : k.Bid; p = ImpactPriceSpread(p, c.GetSpread(this)); int s = Direction == Direction.Long ? k.AskSize : k.BidSize; if (k.Symbol != Symbol) { return(StatusType.OK); } if (!fillOpg && (ValidInstruct == OrderInstructionType.OPG)) { return(StatusType.INVALID_TRADE_PARAMETERS); } if (Created.AddMilliseconds(c.GetLatencyInMilliseconds(this)) > k.TickDateTime) { return(StatusType.OK); } if ((Type == OrderType.Limit && Direction == Direction.Long && (p <= LimitPrice)) || // buy limit (Type == OrderType.Limit && Direction == Direction.Short && (p >= LimitPrice)) || // sell limit (Type == OrderType.Stop && Direction == Direction.Long && (p >= StopPrice)) || // buy stop (Type == OrderType.Stop && Direction == Direction.Short && (p <= StopPrice)) || // sell stop (Type == OrderType.StopLimit && Direction == Direction.Long && (p >= StopPrice) && (p <= LimitPrice)) || // buy stop limit (Type == OrderType.StopLimit && Direction == Direction.Short && (p <= StopPrice) && (p >= LimitPrice)) || // sell stop limit Type == OrderType.Market) { //Get trade price (factoring in slippage) Xprice = ImpactPriceSlippage(p, c.GetSlippage(this)); //Set commissions Commission = c.GetCommission(this); Xsize = Math.Abs(Xsize); Xsize = (s >= UnsignedSize ? UnsignedSize : s) * (Direction == Direction.Long ? 1 : -1); Xtime = k.Time; Xdate = k.Date; return(StatusType.ORDER_FILLED); } return(StatusType.OK); }
public StatusType Fill(Tick t, BrokerModel c, bool fillOpg) { if (!t.IsTrade) { return(StatusType.OK); } if (t.Symbol != Symbol) { return(StatusType.OK); } if (!fillOpg && (ValidInstruct == OrderInstructionType.OPG)) { return(StatusType.INVALID_TRADE_PARAMETERS); } //Set price P and add negatively impacting spread decimal p = ImpactPriceSpread(t.Trade, c.GetSpread(this)); if ((Type == OrderType.Limit && Direction == Direction.Long && (p <= LimitPrice)) || // buy limit (Type == OrderType.Limit && Direction == Direction.Short && (p >= LimitPrice)) || // sell limit (Type == OrderType.Stop && Direction == Direction.Long && (p >= StopPrice)) || // buy stop (Type == OrderType.Stop && Direction == Direction.Short && (p <= StopPrice)) || // sell stop (Type == OrderType.StopLimit && Direction == Direction.Long && (p >= StopPrice) && (p <= LimitPrice)) || // buy stop limit (Type == OrderType.StopLimit && Direction == Direction.Short && (p <= StopPrice) && (p >= LimitPrice)) || // sell stop limit Type == OrderType.Market) { //Get trade price (factoring in slippage) Xprice = ImpactPriceSlippage(p, c.GetSlippage(this)); //Set commissions Commission = c.GetCommission(this); //Add execution details Xsize = t.Size >= UnsignedSize ? UnsignedSize : t.Size; Xsize = Math.Abs(Xsize); Xsize *= Direction == Direction.Long ? 1 : -1; Xtime = t.Time; Xdate = t.Date; return(StatusType.ORDER_FILLED); } return(StatusType.OK); }