public void ProcessFill(SymbolInfo symbol, LogicalFillBinary fill) { SymbolReceiver symbolReceiver; if (!symbolsRequested.TryGetValue(symbol.BinaryIdentifier, out symbolReceiver)) { throw new InvalidOperationException("Can't find symbol request for " + symbol); } var symbolAlgorithm = GetAlgorithm(symbol.BinaryIdentifier); if (!symbolAlgorithm.OrderAlgorithm.IsBrokerOnline) { if (debug) { log.Debug("Broker offline but sending fill anyway for " + symbol + " to receiver: " + fill); } } if (debug) { log.Debug("Sending fill event for " + symbol + " to receiver: " + fill); } var item = new EventItem(symbol, EventType.LogicalFill, fill); symbolReceiver.Agent.SendEvent(item); }
private void client_ExecDetails(object sender, ExecDetailsEventArgs e) { log.InfoFormat("Execution: {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}", e.Contract.Symbol, e.Execution.AccountNumber, e.Execution.ClientId, e.Execution.Exchange, e.Execution.ExecutionId, e.Execution.Liquidation, e.Execution.OrderId, e.Execution.PermId, e.Execution.Price, e.Execution.Shares, e.Execution.Side, e.Execution.Time); SymbolInfo symbol = Factory.Symbol.LookupSymbol(e.Contract.Symbol); SymbolHandler symbolHandler = symbolHandlers[symbol.BinaryIdentifier]; TimeStamp executionTime = new TimeStamp(e.Execution.Time); int logicalOrderId = GetLogicalOrderId(e.Execution.OrderId); double change = e.Execution.Side == ExecutionSide.Bought ? e.Execution.Shares : -e.Execution.Shares; double positionBefore = symbolHandler.Position; symbolHandler.AddPosition(change); if (trace) { log.Trace("Changed symbol position: " + positionBefore + " + " + change + " = " + symbolHandler.Position); } LogicalFillBinary binary = new LogicalFillBinary(symbolHandler.Position, e.Execution.Price, executionTime, logicalOrderId); if (debug) { log.Debug("Sending logical fill: " + binary); } receiver.OnEvent(symbol, (int)EventType.LogicalFill, binary); }
public bool OnLogicalFill(SymbolInfo symbol, LogicalFillBinary fill) { log.Info("Got Logical Fill of " + symbol + " at " + fill.Price + " for " + fill.Position); actualPositions[symbol.BinaryIdentifier] = fill.Position; if (SyncTicks.Enabled) { tickSync.RemovePhysicalFill(fill); } return(true); }
public void ProcessFill(SymbolInfo symbol, LogicalFillBinary fill) { lock ( openOrdersLocker) { if (debug) { log.Debug("Sending fill event for " + symbol + " to receiver: " + fill); } while (!receiver.OnEvent(symbol, (int)EventType.LogicalFill, fill)) { Factory.Parallel.Yield(); } } }
public bool OnProcessFill(LogicalFill fill) { if (debug) { log.Debug(model + ": OnProcessFill: " + fill); } if (fill.IsSimulated) { if (debug) { log.Debug("Ignoring fill since it's a simulated fill meaning that the strategy already exited via a money management exit like stop loss or target profit, etc."); } return(true); } if (model is Portfolio) { var portfolio = (Portfolio)model; var portfolioPosition = portfolio.Result.Position; fill = new LogicalFillBinary(portfolioPosition.Current, portfolioPosition.Price, fill.Time, fill.UtcTime, fill.OrderId, fill.OrderSerialNumber, fill.OrderPosition, false); if (debug) { log.Debug("For portfolio, converted to fill: " + fill); } } if (transactionDebug && !model.QuietMode && !(model is PortfolioInterface)) { transactionLog.Debug(model.Name + "," + model.Data.SymbolInfo + "," + fill); } if (fill.Position != position.Current) { if (position.IsFlat) { EnterComboTrade(fill); } else if (fill.Position == 0) { if (model is Strategy) { var strategy = model as Strategy; LogicalOrder filledOrder; strategy.TryGetOrderById(fill.OrderId, out filledOrder); if (filledOrder.TradeDirection != TradeDirection.Change) { ExitComboTrade(fill); } } else { ExitComboTrade(fill); } } else if ((fill.Position > 0 && position.IsShort) || (fill.Position < 0 && position.IsLong)) { // The signal must be opposite. Either -1 / 1 or 1 / -1 if (model is Strategy) { var strategy = model as Strategy; LogicalOrder filledOrder; strategy.TryGetOrderById(fill.OrderId, out filledOrder); if (filledOrder.TradeDirection == TradeDirection.Change) { ChangeComboSize(fill); } else { ExitComboTrade(fill); EnterComboTrade(fill); } } else { ExitComboTrade(fill); EnterComboTrade(fill); } } else { // Instead it has increased or decreased position size. ChangeComboSize(fill); } } position.Change(model.Data.SymbolInfo, fill); if (model is Strategy) { Strategy strategy = (Strategy)model; if (debug) { log.Debug("Changing strategy result position to " + position.Current); } strategy.Result.Position.Copy(position); } // if( model is Portfolio) { // Portfolio portfolio = (Portfolio) model; // double tempNetPortfolioEquity = 0; // tempNetPortfolioEquity += portfolio.Performance.Equity.ClosedEquity; // tempNetPortfolioEquity -= portfolio.Performance.Equity.StartingEquity; // } return(true); }
private void ProcessFill(LogicalFillBinary fill, bool isCompletePhysicalFill) { if (debug) { log.Debug("ProcessFill() logical: " + fill); } int orderId = fill.OrderId; if (orderId == 0) { // This is an adjust-to-position market order. // Position gets set via SetPosition instead. return; } var filledOrder = FindLogicalOrder(orderId); if (debug) { log.Debug("Matched fill with order: " + filledOrder); } var isCompleteLogicalFill = filledOrder.Position == Math.Abs(fill.Position); if (filledOrder.TradeDirection == TradeDirection.Change) { var strategyPosition = filledOrder.StrategyPosition; var orderPosition = filledOrder.Type == OrderType.BuyLimit || filledOrder.Type == OrderType.BuyMarket || filledOrder.Type == OrderType.BuyStop ? filledOrder.Position : -filledOrder.Position; if (debug) { log.Debug("Change order fill = " + orderPosition + ", strategy = " + strategyPosition + ", fill = " + fill.Position); } isCompleteLogicalFill = orderPosition + strategyPosition == fill.Position; if (!isCompleteLogicalFill) { var change = fill.Position - filledOrder.StrategyPosition; filledOrder.Position = Math.Abs(orderPosition - change); if (debug) { log.Debug("Changing order to position: " + filledOrder.Position); } } } if (isCompleteLogicalFill) { try { if (debug) { log.Debug("Marking order id " + filledOrder.Id + " as completely filled."); } filledOrders.Add(filledOrder.SerialNumber, TimeStamp.UtcNow.Internal); originalLogicals.Remove(filledOrder); CleanupAfterFill(filledOrder); } catch (ApplicationException) { } } UpdateOrderCache(filledOrder, fill); if (onProcessFill != null) { if (debug) { log.Debug("Sending logical fill for " + symbol + ": " + fill); } onProcessFill(symbol, fill); } if (isCompletePhysicalFill && !isCompleteLogicalFill) { if (debug) { log.Debug("Found complete physical fill but incomplete logical fill."); } ProcessMissingPhysical(filledOrder); } if (isCompleteLogicalFill) { if (debug) { log.Debug("Performing extra compare."); } lock ( performCompareLocker) { PerformCompareInternal(); } } }
public void ProcessFill(PhysicalFill physical, int totalSize, int cumulativeSize, int remainingSize) { if (debug) { log.Debug("ProcessFill() physical: " + physical); } // log.Warn( "ProcessFill() physical: " + physical); physicalOrders.Remove(physical.Order); var isCompletePhysicalFill = physical.Order.Size == 0; if (isCompletePhysicalFill) { if (debug) { log.Debug("Physical order completely filled: " + physical.Order); } physicalOrders.Remove(physical.Order); } else { if (debug) { log.Debug("Physical order partially filled: " + physical.Order); } } LogicalFillBinary fill; try { var logical = FindLogicalOrder(physical.Order.LogicalOrderId); desiredPosition += physical.Size; if (debug) { log.Debug("Adjusting symbol position to desired " + desiredPosition + ", physical fill was " + physical.Size); } var position = logical.StrategyPosition + physical.Size; if (debug) { log.Debug("Creating logical fill with position " + position + " from strategy position " + logical.StrategyPosition); } fill = new LogicalFillBinary( position, physical.Price, physical.Time, physical.UtcTime, physical.Order.LogicalOrderId, physical.Order.LogicalSerialNumber, logical.Position, physical.IsSimulated); } catch (ApplicationException) { if (debug) { log.Debug("Leaving symbol position at desired " + desiredPosition + ", since this was an adjustment market order."); } if (debug) { log.Debug("Skipping logical fill for an adjustment market order."); } if (debug) { log.Debug("Performing extra compare."); } lock ( performCompareLocker) { PerformCompareInternal(); } TryRemovePhysicalFill(physical); return; } if (debug) { log.Debug("Fill price: " + fill); } ProcessFill(fill, isCompletePhysicalFill); }
public bool OnPositionChange(LogicalFillBinary fill) { throw new NotImplementedException(); }
public void OnPositionChange(SymbolInfo symbol, LogicalFillBinary fill) { }