public override void PositionChange(Receiver receiver, SymbolInfo symbol, int desiredPosition, Iterable <LogicalOrder> inputOrders) { if (!IsRecovered) { if (HasFirstRecovery) { log.Warn("PositionChange event received while FIX was offline or recovering. Current connection status is: " + ConnectionStatus); return; } else { throw new ApplicationException("PositionChange event received prior to completing FIX recovery. Current connection status is: " + ConnectionStatus); } } var count = inputOrders == null ? 0 : inputOrders.Count; if (debug) { log.Debug("PositionChange " + symbol + ", desired " + desiredPosition + ", order count " + count); } var algorithm = GetAlgorithm(symbol.BinaryIdentifier); algorithm.SetDesiredPosition(desiredPosition); algorithm.SetLogicalOrders(inputOrders); CompareLogicalOrders(symbol); var tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); if (SyncTicks.Enabled) { tickSync.RemovePositionChange(); } }
private Yield FIXCancelOrder(PacketFIX4_4 packet) { var symbol = Factory.Symbol.LookupSymbol(packet.Symbol); if (debug) { log.Debug("FIXCancelOrder() for " + packet.Symbol + ". Original client id: " + packet.OriginalClientOrderId); } PhysicalOrder order = null; try { order = GetOrderById(symbol, packet.OriginalClientOrderId); } catch (ApplicationException) { if (debug) { log.Debug(symbol + ": Cannot cancel order by client id: " + packet.OriginalClientOrderId + ". Probably already filled or canceled. Should send a reject in this case."); } if (SyncTicks.Enabled) { var tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); tickSync.RemovePhysicalOrder(); } return(Yield.DidWork.Return); } // log.Info( packet.Symbol + ": Canceling order for client id: " + packet.OriginalClientOrderId); CancelOrder(symbol, order.BrokerOrder); SendExecutionReport(order, "6", 0.0, 0, 0, 0, (int)order.Size, TimeStamp.UtcNow, packet); SendPositionUpdate(order.Symbol, GetPosition(order.Symbol)); SendExecutionReport(order, "4", 0.0, 0, 0, 0, (int)order.Size, TimeStamp.UtcNow, packet); SendPositionUpdate(order.Symbol, GetPosition(order.Symbol)); return(Yield.DidWork.Repeat); }
public void OnCancelBrokerOrder(SymbolInfo symbol, object origBrokerOrder) { PhysicalOrder physicalOrder; try { physicalOrder = GetOrderById(origBrokerOrder); } catch (ApplicationException ex) { log.Warn("Order probably already canceled. " + ex.Message); if (SyncTicks.Enabled) { var tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); tickSync.RemovePhysicalOrder(); } return; } physicalOrder.OrderState = OrderState.Pending; if (debug) { log.Debug("OnCancelBrokerOrder " + physicalOrder); } var fixMsg = (FIXMessage4_4)FixFactory.Create(); string newClientOrderId = physicalOrder.LogicalOrderId + "." + GetUniqueOrderId(); fixMsg.SetOriginalClientOrderId((string)origBrokerOrder); fixMsg.SetClientOrderId(newClientOrderId); fixMsg.SetAccount(AccountNumber); fixMsg.SetSide(GetOrderSide(physicalOrder.Side)); fixMsg.AddHeader("F"); fixMsg.SetSymbol(physicalOrder.Symbol.Symbol); fixMsg.SetTransactTime(TimeStamp.UtcNow); SendMessage(fixMsg); }
protected override void ResendMessage(FIXTMessage1_1 textMessage) { var mbtMsg = (FIXMessage4_2)textMessage; if (SyncTicks.Enabled && !IsRecovered && mbtMsg.Type == "8") { switch (mbtMsg.OrderStatus) { case "E": case "6": case "A": var symbolInfo = Factory.Symbol.LookupSymbol(mbtMsg.Symbol); var tickSync = SyncTicks.GetTickSync(symbolInfo.BinaryIdentifier); //if (symbolInfo.FixSimulationType == FIXSimulationType.BrokerHeldStopOrder && // mbtMsg.ExecutionType == "D") // restated //{ // // Ignored order count. //} //else //{ // tickSync.AddPhysicalOrder("resend"); //} break; case "2": case "1": symbolInfo = Factory.Symbol.LookupSymbol(mbtMsg.Symbol); tickSync = SyncTicks.GetTickSync(symbolInfo.BinaryIdentifier); //tickSync.AddPhysicalFill("resend"); break; } } ResendMessageProtected(textMessage); }
public void SendFill(MessageFIX4_2 packetFIX) { var clientOrderId = 0L; long.TryParse(packetFIX.ClientOrderId, out clientOrderId); var originalClientOrderId = 0L; long.TryParse(packetFIX.ClientOrderId, out originalClientOrderId); if (debug) { log.Debug("SendFill( " + packetFIX.ClientOrderId + ")"); } var symbolInfo = Factory.Symbol.LookupSymbol(packetFIX.Symbol); var timeZone = new SymbolTimeZone(symbolInfo); SymbolAlgorithm algorithm; if (!TryGetAlgorithm(symbolInfo.BinaryIdentifier, out algorithm)) { log.Info("Fill received but OrderAlgorithm not found for " + symbolInfo + ". Ignoring."); return; } var fillPosition = packetFIX.LastQuantity * SideToSign(packetFIX.Side); if (GetSymbolStatus(symbolInfo)) { CreateOrChangeOrder order; if (OrderStore.TryGetOrderById(clientOrderId, out order)) { TimeStamp executionTime; if (UseLocalFillTime) { executionTime = TimeStamp.UtcNow; } else { executionTime = new TimeStamp(packetFIX.TransactionTime); } var configTime = executionTime; configTime.AddSeconds(timeZone.UtcOffset(executionTime)); var fill = Factory.Utility.PhysicalFill(fillPosition, packetFIX.LastPrice, configTime, executionTime, order.BrokerOrder, false, packetFIX.OrderQuantity, packetFIX.CumulativeQuantity, packetFIX.LeavesQuantity, IsRecovered, true); if (debug) { log.Debug("Sending physical fill: " + fill); } algorithm.OrderAlgorithm.ProcessFill(fill); algorithm.OrderAlgorithm.ProcessOrders(); TrySendStartBroker(symbolInfo, "position sync on fill"); } else { algorithm.OrderAlgorithm.IncreaseActualPosition(fillPosition); log.Notice("Fill id " + packetFIX.ClientOrderId + " not found. Must have been a manual trade."); if (SyncTicks.Enabled) { var tickSync = SyncTicks.GetTickSync(symbolInfo.BinaryIdentifier); tickSync.RemovePhysicalFill(packetFIX.ClientOrderId); } } } }
public void SetSymbol(string symbolString) { symbol = Factory.Symbol.LookupSymbol(symbolString); if (SyncTicks.Enabled) { tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); } }
public FillSimulatorPhysical(string name, SymbolInfo symbol, bool createSimulatedFills) { this.symbol = symbol; this.minimumTick = symbol.MinimumTick.ToLong(); this.tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); this.createSimulatedFills = createSimulatedFills; this.log = Factory.SysLog.GetLogger(typeof(FillSimulatorPhysical).FullName + "." + symbol.Symbol.StripInvalidPathChars() + "." + name); }
public OrderAlgorithmDefault(string name, SymbolInfo symbol, PhysicalOrderHandler brokerOrders) { this.log = Factory.SysLog.GetLogger(typeof(OrderAlgorithmDefault).FullName + "." + symbol.Symbol.StripInvalidPathChars() + "." + name); this.symbol = symbol; this.tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); this.physicalOrderHandler = brokerOrders; this.originalLogicals = new ActiveList <LogicalOrder>(); this.bufferedLogicals = new ActiveList <LogicalOrder>(); this.originalPhysicals = new ActiveList <PhysicalOrder>(); this.logicalOrders = new ActiveList <LogicalOrder>(); this.physicalOrders = new ActiveList <PhysicalOrder>(); }
public long Wait(SymbolInfo symbol, int expectedTicks, int timeout) { if (debug) { log.Debug("Wait"); } if (SyncTicks.Enabled) { tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); } long startTime = Factory.Parallel.TickCount; count = 0; while (Factory.Parallel.TickCount - startTime < timeout * 1000) { if (propagateException != null) { throw propagateException; } try { if (tickQueue.TryDequeue(ref tickBinary)) { tickIO.Inject(tickBinary); if (debug && countLog < 5) { log.Debug("Received a tick " + tickIO); countLog++; } count++; lastTick.Copy(tickIO); if (SyncTicks.Enabled) { tickSync.RemoveTick(); } if (count >= expectedTicks) { break; } } else { Thread.Sleep(100); } } catch (QueueException ex) { if (HandleQueueException(ex)) { break; } } } return(count); }
public bool VerifyState(BrokerState expectedBrokerState, ReceiverState expectedSymbolState, SymbolInfo symbol, int timeout) { if (debug) { log.Debug("VerifyFeed"); } if (SyncTicks.Enabled) { tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); } long startTime = Factory.TickCount; count = 0; TickBinary binary = new TickBinary(); while (Factory.TickCount - startTime < timeout * 1000) { if (propagateException != null) { throw propagateException; } try { if (!tickQueue.TryDequeue(ref binary)) { Thread.Sleep(100); } else { if (SyncTicks.Enabled) { tickSync.RemoveTick(); } } } catch (QueueException ex) { if (HandleQueueException(ex)) { break; } } if (brokerState == expectedBrokerState && receiverState == expectedSymbolState) { return(true); } } return(false); }
public FillSimulatorPhysical(string name, SymbolInfo symbol, bool createSimulatedFills, bool createActualFills, TriggerController triggers) { this.symbol = symbol; this.name = name; this.triggers = triggers; this.minimumTick = symbol.MinimumTick.ToLong(); this.tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); this.createSimulatedFills = createSimulatedFills; this.log = Factory.SysLog.GetLogger(typeof(FillSimulatorPhysical).FullName + "." + symbol.Symbol.StripInvalidPathChars() + "." + name); this.log.Register(this); this.createActualFills = createActualFills; fillLogic = new FillSimulatorLogic(name, symbol, FillCallback); IsChanged = true; PartialFillSimulation = symbol.PartialFillSimulation; }
private void FIXChangeOrder(PacketFIX4_4 packet) { var symbol = Factory.Symbol.LookupSymbol(packet.Symbol); PhysicalOrder origOrder = null; if (debug) { log.Debug("FIXChangeOrder() for " + packet.Symbol + ". Client id: " + packet.ClientOrderId + ". Original client id: " + packet.OriginalClientOrderId); } try { origOrder = GetOrderById(symbol, packet.OriginalClientOrderId); } catch (ApplicationException) { log.Warn(symbol + ": Cannot change order by client id: " + packet.OriginalClientOrderId + ". Probably already filled or canceled. Should send a reject in this case."); if (SyncTicks.Enabled) { var tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); tickSync.RemovePhysicalOrder(); } return; } var order = ConstructOrder(packet, packet.ClientOrderId); if (order.Side != origOrder.Side) { var message = "Cannot change " + origOrder.Side + " to " + order.Side; log.Error(message); OnRejectOrder(origOrder, message); return; } if (order.Type != origOrder.Type) { var message = "Cannot change " + origOrder.Type + " to " + order.Type; log.Error(message); OnRejectOrder(origOrder, message); return; } SendExecutionReport(order, "E", 0.0, 0, 0, 0, (int)order.Size, TimeStamp.UtcNow, packet); SendPositionUpdate(order.Symbol, GetPosition(order.Symbol)); SendExecutionReport(order, "5", 0.0, 0, 0, 0, (int)order.Size, TimeStamp.UtcNow, packet); SendPositionUpdate(order.Symbol, GetPosition(order.Symbol)); ChangeOrder(order, packet.OriginalClientOrderId); }
public FIXServerSymbolHandler(FIXSimulatorSupport fixSimulatorSupport, bool isPlayBack, string symbolString, Func <Yield> onHeartbeat, Func <SymbolInfo, Tick, Yield> onTick, Action <PhysicalFill, int, int, int> onPhysicalFill, Action <PhysicalOrder, string> onRejectOrder) { this.fixSimulatorSupport = fixSimulatorSupport; this.isPlayBack = isPlayBack; this.onHeartbeat = onHeartbeat; this.onTick = onTick; this.symbol = Factory.Symbol.LookupSymbol(symbolString); reader = Factory.TickUtil.TickReader(); reader.Initialize("Test\\MockProviderData", symbolString); fillSimulator = Factory.Utility.FillSimulator("FIX", symbol, false); fillSimulator.OnPhysicalFill = onPhysicalFill; fillSimulator.OnRejectOrder = onRejectOrder; tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); tickSync.ForceClear(); queueTask = Factory.Parallel.Loop("FIXServerSymbol-" + symbolString, OnException, ProcessQueue); firstHearbeat = true; }
protected override void RemoveTickSync(FIXTMessage1_1 textMessage) { var mbtMsg = (FIXMessage4_2)textMessage; if (SyncTicks.Enabled && mbtMsg.Type == "8") { switch (mbtMsg.OrderStatus) { case "E": case "6": case "0": { var symbolInfo = Factory.Symbol.LookupSymbol(mbtMsg.Symbol); var tickSync = SyncTicks.GetTickSync(symbolInfo.BinaryIdentifier); tickSync.RemovePhysicalOrder("offline"); } break; case "A": if (mbtMsg.ExecutionType == "D") { // Is it a Forex order? var symbolInfo = Factory.Symbol.LookupSymbol(mbtMsg.Symbol); var tickSync = SyncTicks.GetTickSync(symbolInfo.BinaryIdentifier); tickSync.RemovePhysicalOrder("offline"); } break; case "2": case "1": { var symbolInfo = Factory.Symbol.LookupSymbol(mbtMsg.Symbol); var tickSync = SyncTicks.GetTickSync(symbolInfo.BinaryIdentifier); tickSync.RemovePhysicalFill("offline"); } break; } } }
public SimulateSymbolSyncTicks(FIXSimulatorSupport fixSimulatorSupport, QuoteSimulatorSupport quoteSimulatorSupport, string symbolString, PartialFillSimulation partialFillSimulation, TimeStamp endTime, long id) { log.Register(this); this.id = id; this.fixSimulatorSupport = fixSimulatorSupport; this.quoteSimulatorSupport = quoteSimulatorSupport; this.onTick = quoteSimulatorSupport.OnTick; this.onEndTick = quoteSimulatorSupport.OnEndTick; this.PartialFillSimulation = partialFillSimulation; this.symbolString = symbolString; this.symbol = Factory.Symbol.LookupSymbol(symbolString); fillSimulator = Factory.Utility.FillSimulator("FIX", Symbol, false, true, null); fillSimulator.EnableSyncTicks = SyncTicks.Enabled; FillSimulator.OnPhysicalFill = fixSimulatorSupport.OnPhysicalFill; FillSimulator.OnRejectOrder = fixSimulatorSupport.OnRejectOrder; fillSimulator.PartialFillSimulation = partialFillSimulation; tickSync = SyncTicks.GetTickSync(Symbol.BinaryIdentifier); latency = new LatencyMetric("SimulateSymbolSyncTicks-" + symbolString.StripInvalidPathChars()); diagnoseMetric = Diagnose.RegisterMetric("Simulator"); if (debug) { log.Debug("Opening tick file for reading."); } reader = Factory.TickUtil.TickFile(); try { reader.Initialize("Test\\MockProviderData", symbolString, TickFileMode.Read); reader.EndTime = endTime; } catch (FileNotFoundException ex) { log.Info("File for symbol " + symbolString + " not found: " + ex.Message); } }
protected override void TrySendTick(SymbolInfo symbol, TickIO tick) { SendSide(symbol, tick, true); if (tick.IsQuote) { #if NOTUSED bool result; if (!isFirstTick.TryGetValue(symbol.BinaryIdentifier, out result)) { isFirstTick.Add(symbol.BinaryIdentifier, true); } else { var tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); tickSync.AddTick(tick); } #endif SendSide(symbol, tick, false); } var lastTick = lastTicks[symbol.BinaryIdentifier]; lastTick.Inject(tick.Extract()); }
public void SwitchBrokerState(string description, bool isOnline) { foreach (var kvp in symbolHandlers) { var symbolBinary = kvp.Key; var handler = kvp.Value; var tickSync = SyncTicks.GetTickSync(symbolBinary); tickSync.SetSwitchBrokerState(description); if (handler.IsOnline != isOnline) { handler.IsOnline = isOnline; if (!isOnline) { while (tickSync.SentPhyscialOrders) { tickSync.RemovePhysicalOrder("Rollback"); } while (tickSync.SentOrderChange) { tickSync.RemoveOrderChange(); } while (tickSync.SentPhysicalFillsCreated) { tickSync.RemovePhysicalFill("Rollback"); } while (tickSync.SentPositionChange) { tickSync.RemovePositionChange("Rollback"); } while (tickSync.SentWaitingMatch) { tickSync.RemoveWaitingMatch("Rollback"); } } } } }
public bool OnCancelBrokerOrder(CreateOrChangeOrder order) { if (!IsRecovered) { return(false); } if (debug) { log.Debug("OnCancelBrokerOrder " + order + ". Connection " + ConnectionStatus + ", IsOrderServerOnline " + isOrderServerOnline); } OrderStore.SetSequences(RemoteSequence, FixFactory.LastSequence); CreateOrChangeOrder createOrChangeOrder; try { createOrChangeOrder = OrderStore.GetOrderById(order.OriginalOrder.BrokerOrder); } catch (ApplicationException ex) { if (LogRecovery || !IsRecovery) { log.Info("Order probably already canceled. " + ex.Message); } if (SyncTicks.Enabled) { var tickSync = SyncTicks.GetTickSync(order.Symbol.BinaryIdentifier); tickSync.RemovePhysicalOrder(); } return(true); } createOrChangeOrder.ReplacedBy = order; if (!object.ReferenceEquals(order.OriginalOrder, createOrChangeOrder)) { throw new ApplicationException("Different objects!"); } SendCancelOrder(order, false); return(true); }
public long Verify(int expectedCount, Action <TickIO, TickIO, long> assertTick, SymbolInfo symbol, int timeout, Action action) { if (debug) { log.Debug("Verify"); } if (SyncTicks.Enabled) { tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); } long endTime = Factory.Parallel.TickCount + timeout * 1000; count = 0; while (Factory.Parallel.TickCount < endTime) { if (propagateException != null) { throw propagateException; } try { if (tickQueue.TryDequeue(ref tickBinary)) { tickIO.Inject(tickBinary); if (debug && countLog < 5) { log.Debug("Received a tick " + tickIO); countLog++; } startTime = Factory.TickCount; count++; if (count > 0 && assertTick != null) { assertTick(tickIO, lastTick, symbol.BinaryIdentifier); } lastTick.Copy(tickIO); if (!actionAlreadyRun && action != null) { actionAlreadyRun = true; action(); } if (SyncTicks.Enabled) { tickSync.RemoveTick(); } if (count >= expectedCount) { break; } } else { Thread.Sleep(100); } } catch (QueueException ex) { if (HandleQueueException(ex)) { break; } } } return(count); }
public VerifyFeedDefault(SymbolInfo symbol) { this.symbol = symbol; tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); tickPool = Factory.Parallel.TickPool(symbol); }
private void TryAddPhysicalOrder(CreateOrChangeOrder order) { var tickSync = SyncTicks.GetTickSync(order.Symbol.BinaryIdentifier); tickSync.AddPhysicalOrder(order); }