public void OnOrderFilled(SessionID sessionID, OrderMatch match) { // Send the order filled execution report to the owning session var message = _messageGenerator.CreateFillReport(match, _execIdGenerator()); _fixFacade.SendToTarget(message, sessionID); }
public bool Visit(OrderMatch msg) { Condition.Requires(msg.MakerOrderId, "msg.MakerOrderId").IsNotNull(); Condition.Requires(msg.TakerOrderId, "msg.TakerOrderId").IsNotNull(); Order order; if (_manager._openOrders.TryGetValue(msg.TakerOrderId, out order)) { if (_manager._openOrders.ContainsKey(msg.MakerOrderId)) { throw new Exception("Self trade"); } } else if (!_manager._openOrders.TryGetValue(msg.MakerOrderId, out order)) { return(false); } Condition.Requires(msg.Size, "msg.Size").IsLessOrEqual(order.Unfilled); order.Unfilled -= msg.Size; PublishUpdate(msg, order, new Fill() { Size = msg.Size, Price = msg.Price }, finished: false); // This is the only place where any overload of Visit() in MessageHandler returns true. return(true); }
// From fixprotocol.org: // CumQty: Currently executed shares for chain of orders. // LeavesQty: Amount of shares open for further execution. If the OrdStatus is Canceled, // DoneForTheDay, Expired, Calculated, or Rejected (in which case the order // is no longer active) then LeavesQty could be 0, // otherwise LeavesQty = OrderQty - CumQty. // LastShares: Quantity of shares bought/sold on this (last) fill. // // Also see http://www.onixs.biz/fix-dictionary/4.2/msgType_8_8.html // The general rule is: OrderQty <38> = CumQty <14> + LeavesQty <151>. public Message CreateFillReport(OrderMatch match, string execID) { var exReport = new ExecutionReport( new OrderID(match.OrderID.ToString(CultureInfo.InvariantCulture)), new ExecID(execID), new ExecTransType(ExecTransType.NEW), new ExecType(match.MatchType == MatchType.Full ? ExecType.FILL : ExecType.PARTIAL_FILL), new OrdStatus(match.MatchType == MatchType.Full ? OrdStatus.FILLED : OrdStatus.PARTIALLY_FILLED), new Symbol(match.Contract.Symbol), TranslateFixFields.Translate(match.MarketSide), new LeavesQty(match.RemainingQuantity), new CumQty(match.OriginalOrderQuantity - match.RemainingQuantity), new AvgPx(match.Price)) { ClOrdID = new ClOrdID(match.ClOrdID), OrderQty = new OrderQty(match.OriginalOrderQuantity), LastShares = new LastShares(match.MatchedQuantity), LastPx = new LastPx(match.Price) }; if (TradingAccount.IsSet(match.Account)) { exReport.SetField(new Account(match.Account.Name)); } return(exReport); }
public SendOrderFill(SessionMediator sessionMediator, OrderMatch orderMatch, FixSessionID sessionID) { _sessionMediator = sessionMediator; _orderMatch = orderMatch; _sessionID = sessionID; }
public void OnOrderFilled(SessionID sessionID, OrderMatch match) { // Send the order filled execution report to the owning session var message = _messageGenerator.CreateFillReport(match, _execIdGenerator()); _fixFacade.SendToTarget(message, sessionID); // TODO Send the trade message to other connections //var loggedInSessions = _sessionMediator.GetLoggedInSessions(); }
private static void AssertEqual(OrderMatch e, OrderMatch a) { const string messageBase = "Expected and actual matches differ on "; Assert.AreEqual(e.OrderID, a.OrderID, messageBase + "OrderID"); Assert.AreEqual(e.Price, a.Price, messageBase + "Price"); Assert.AreEqual(e.MatchedQuantity, a.MatchedQuantity, "MatchedQuantity"); Assert.AreEqual(e.RemainingQuantity, a.RemainingQuantity, "RemainingQuantity"); Assert.AreEqual(e.Contract, a.Contract, "Contract"); Assert.AreEqual(e.MatchType, a.MatchType, "MatchType"); }
public void OrderFilled(FixSessionID ownerSessionID, OrderMatch matchDetails) { // If we ever support owner details etc then filter those out is session != sessionID foreach (var sessionID in GetAllLoggedInSessions()) { var fixID = _sessionIDMap.GetBySecond(sessionID); Action <IFixMessageHandler> messageSendF = handler => handler.OnOrderFilled(fixID, matchDetails); _sessionRepository.SendMessageToHandler(sessionID, messageSendF); } }
/// <summary> /// Match using specified time-in-force logic. /// </summary> /// <param name="timeInForce">Order time in force</param> /// <returns>Matching effects</returns> public ExchangeUpdate Match() { var counterOrder = orderbook.Head; var nextOrder = default(Order); var updates = new ExchangeUpdate(asset, new DateTime(resultEffects.Quantum.Timestamp, DateTimeKind.Utc)); var tradeAssetAmount = 0L; var tradeQuoteAmount = 0L; //orders in the orderbook are already sorted by price and age, so we can iterate through them in natural order while (counterOrder != null) { //we need get next order here, otherwise Next will be null after the counter order removed nextOrder = counterOrder?.Next; //check that counter order price matches our order if (side == OrderSide.Sell && counterOrder.Price < takerOrder.Price || side == OrderSide.Buy && counterOrder.Price > takerOrder.Price) { break; } var availableOrderAmount = takerOrder.Amount - tradeAssetAmount; var match = new OrderMatch(this, availableOrderAmount, counterOrder); var matchUpdates = match.ProcessOrderMatch(); updates.Trades.Add(matchUpdates.trade); updates.OrderUpdates.Add(matchUpdates.counterOrder); tradeAssetAmount += matchUpdates.trade.Amount; tradeQuoteAmount += matchUpdates.trade.QuoteAmount; //stop if incoming order has been executed in full if (tradeAssetAmount == takerOrder.Amount) { break; } counterOrder = nextOrder; } RecordTrade(tradeAssetAmount, tradeQuoteAmount); if (timeInForce == TimeInForce.GoodTillExpire && PlaceReminderOrder()) { updates.OrderUpdates.Add(takerOrder.ToOrderInfo()); } return(updates); }
protected void Matches_ItemCommand(object sender, RepeaterCommandEventArgs e) { if (e.CommandName == "New") { OrderMatchList matches = GetMatches(); OrderMatch match = new OrderMatch(); matches.Insert(e.Item.ItemIndex + 1, match); DataSource = matches; DataBindChildren(); } else if (e.CommandName == "Delete") { OrderMatchList matches = GetMatches(); matches.RemoveAt(e.Item.ItemIndex); DataSource = matches; DataBindChildren(); } }
static void ChangeScriptOrder(string scriptName, int order, OrderMatch match = OrderMatch.EXACT) { // Iterate through all scripts (Might be a better way to do this?) foreach (MonoScript monoScript in MonoImporter.GetAllRuntimeMonoScripts()) { // If found our script if (monoScript.name == scriptName) { if (match == OrderMatch.EXACT) { // And it's not at the execution time we want already if (MonoImporter.GetExecutionOrder(monoScript) != order) { MonoImporter.SetExecutionOrder(monoScript, order); } break; } if (match == OrderMatch.LESSER_THAN) { // And it's not at the execution time we want already if (MonoImporter.GetExecutionOrder(monoScript) > order) { MonoImporter.SetExecutionOrder(monoScript, order); } break; } if (match == OrderMatch.GREATER_THAN) { // And it's not at the execution time we want already if (MonoImporter.GetExecutionOrder(monoScript) < order) { MonoImporter.SetExecutionOrder(monoScript, order); } break; } } } }
private void OnOrderMatched(OrderMatch matchDetails, FixSessionID sessionID) { var cmd = _commandFactory.CreateSendOrderFill(matchDetails, sessionID); _outputQueue.Enqueue(cmd); }
public EnumerableOrderComparisonAttribute(OrderMatch option) { Option = option; }
public void Execute() { //Execute matching price int x = 0; var asks = Asks.ToList(); List <Order> completedAsks = new List <Order>(); foreach (var bid in Bids) { x++; //asks = asks.Where(x => x.Price <= bid.Price).Where(x => x.Remaining != 0).ToList(); asks = asks.Where(x => x.Remaining != 0).Where(x => x.Price <= bid.Price).ToList(); if (asks.Count() == 0) { break; } Console.WriteLine($"Working bid No. {x}, remaining asks: {asks.Count()}"); for (int askIndex = 0; askIndex < Asks.Count(); askIndex++) { //if (askIndex >= asks.Count()) // break; var ask = Asks[askIndex]; if (ask.Remaining == 0) { continue; } if (bid.Price >= ask.Price) {//match decimal btcAmount = 0; OrderMatch match = new OrderMatch(ask.Id, bid.Id); if (bid.Remaining / ask.Price > ask.Remaining) { btcAmount = ask.Remaining; } else { btcAmount = bid.Remaining / ask.Price; } if (btcAmount > ask.Remaining) { var matches = OrderMatches.Where(x => x.AskId == ask.Id); Console.WriteLine("WTF?"); } //Add Match to list and fix ask and bid ask.Completed += btcAmount; bid.Completed += btcAmount * ask.Price; match.BTC = btcAmount; match.Price = ask.Price; match.EUR = btcAmount * ask.Price; OrderMatches.Add(match); //Console.WriteLine($"Working bid No. {x}, ask No. {askIndex}, bid remaining: {bid.Remaining}, ask remaining: {ask.Remaining}"); if (ask.Remaining == 0) { completedAsks.Add(ask); } if (bid.Remaining == 0) { break; } } } } Console.WriteLine($"Completed {completedAsks.Count()} Asks"); //foreach (var ask in removeAsks) // Asks.Remove(ask); }
private List <SampleOrderResult> GetSampleOrders(GridViewRow row, out int count) { GridView grid = (GridView)row.FindControl("SampleShippingCosts"); OrderRule rule = new OrderRule(Rules.DataKeys[row.RowIndex].Value.ToString()); rule.Matches.AddRange( ((BVModules_Shipping_Order_Rules_OrderMatchEditor)row.FindControl("OrderMatchEditor")).GetMatches()); rule.Value = Decimal.Parse(((TextBox)row.FindControl("ValueField")).Text); rule.ValueCustomProperty = ((DropDownList)row.FindControl("ValueCustomPropertyField")).SelectedValue; rule.ValueItemPropertyAsString = ((DropDownList)row.FindControl("ValueItemPropertyField")).SelectedValue; rule.ValuePackagePropertyAsString = ((DropDownList)row.FindControl("ValuePackagePropertyField")).SelectedValue; rule.ValuePropertyAsString = ((DropDownList)row.FindControl("ValueOrderPropertyField")).SelectedValue; count = 0; // Scan all placed orders List <SampleOrderResult> results = new List <SampleOrderResult>(); foreach (Order order in Order.FindByCriteria(new OrderSearchCriteria())) { Order heavyOrder = Order.FindByBvin(order.Bvin); // "Unship" all of the items so that the samples look like they // were just placed. Skip any orders with deleted items. bool skipOrder = false; foreach (LineItem lineitem in heavyOrder.Items) { if (lineitem.AssociatedProduct == null || lineitem.AssociatedProduct.ShippingMode == ShippingMode.None) { skipOrder = true; } else { lineitem.QuantityShipped = 0; } } if (skipOrder) { break; } if (rule.IsMatch(heavyOrder)) { count += 1; if (count > grid.PageSize * 5) { break; } SampleOrderResult result = new SampleOrderResult(); result.OrderNumber = order.OrderNumber; result.OrderDisplay = string.Format("<a href=\"{0}\" target=\"order\">{1}</a>", Page.ResolveUrl( string.Format("~/BVAdmin/Orders/ViewOrder.aspx?id={0}", order.Bvin)), order.OrderNumber); List <string> matchValues = new List <string>(); List <string> limitValues = new List <string>(); if (rule.IsDefaultRule) { matchValues.Add("n/a"); limitValues.Add("n/a"); } else { for (int index = 0; index < rule.Matches.Count; index++) { OrderMatch match = rule.Matches[index]; string matchValue = OrderPropertiesHelper.GetOrderPropertyValue(heavyOrder, match.OrderProperty, match.PackageProperty, match.ItemProperty, match.CustomProperty, "1").ToString(); if (string.IsNullOrEmpty(matchValue)) { matchValue = "(empty)"; } matchValues.Add(matchValue); string limitValue = OrderPropertiesHelper.GetOrderPropertyValue(heavyOrder, match.LimitOrderProperty, match.LimitPackageProperty, match.LimitItemProperty, match.LimitCustomProperty, match.Limit).ToString(); if (string.IsNullOrEmpty(limitValue)) { limitValue = "(empty)"; } limitValues.Add(limitValue); } } result.MatchValues = string.Join(", ", matchValues.ToArray()); result.LimitValues = string.Join(", ", limitValues.ToArray()); object value = OrderPropertiesHelper.GetOrderPropertyValue(heavyOrder, rule.ValueOrderProperty, rule.ValuePackageProperty, rule.ValueItemProperty, rule.ValueCustomProperty, "1"); result.Value = value == null ? "n/a" : value.ToString(); if (String.IsNullOrEmpty(result.Value)) { result.Value = "(empty)"; } ShippingRate rate = new ShippingRate(((OrderRulesEditor)NamingContainer).NameFieldText, string.Empty, string.Empty, 0, string.Empty); decimal?cost = rule.GetCost(heavyOrder); if (cost.HasValue) { rate.Rate = cost.Value; result.RateDisplay = rate.RateAndNameForDisplay; } else { result.RateDisplay = "Hidden"; } results.Add(result); } } results.Sort(); return(results); }
public ICommand CreateSendOrderFill(OrderMatch orderMatch, FixSessionID sessionID) { return(new SendOrderFill(_sessionMediator, orderMatch, sessionID)); }
protected void Matches_ItemCreated(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { OrderMatchList matches = (OrderMatchList)DataSource; OrderMatch match = matches[e.Item.ItemIndex]; DropDownList orderPropertyList = (DropDownList)e.Item.FindControl("MatchOrderPropertyField"); DropDownList itemPropertyList = (DropDownList)e.Item.FindControl("MatchItemPropertyField"); DropDownList packagePropertyList = (DropDownList)e.Item.FindControl("MatchPackagePropertyField"); DropDownList customPropertyList = (DropDownList)e.Item.FindControl("MatchCustomPropertyField"); DropDownList comparisonList = (DropDownList)e.Item.FindControl("MatchComparisonTypeField"); DropDownList limitOrderPropertyList = (DropDownList)e.Item.FindControl("LimitOrderPropertyField"); DropDownList limitPackagePropertyList = (DropDownList)e.Item.FindControl("LimitPackagePropertyField"); DropDownList limitItemPropertyList = (DropDownList)e.Item.FindControl("LimitItemPropertyField"); DropDownList limitCustomPropertyList = (DropDownList)e.Item.FindControl("LimitCustomPropertyField"); HelpLabel customPropertyLabel = (HelpLabel)e.Item.FindControl("MatchCustomPropertyLabel"); HelpLabel limitCustomPropertyLabel = (HelpLabel)e.Item.FindControl("LimitCustomPropertyLabel"); Label multiplierLabel = (Label)e.Item.FindControl("LimitMultiplierLabel"); HelpLabel limitLabel = (HelpLabel)e.Item.FindControl("LimitLabel"); TextBox limitField = (TextBox)e.Item.FindControl("LimitField"); BaseValidator limitRequired = (BaseValidator)e.Item.FindControl("LimitRequired"); BaseValidator limitNumeric = (BaseValidator)e.Item.FindControl("LimitNumeric"); orderPropertyList.Items.Clear(); orderPropertyList.Items.AddRange(GetMatchOrderProperties()); itemPropertyList.Items.Clear(); itemPropertyList.Items.AddRange(GetItemProperties()); itemPropertyList.Visible = false; packagePropertyList.Items.Clear(); packagePropertyList.Items.AddRange(GetPackageProperties()); packagePropertyList.Visible = false; customPropertyList.Visible = false; if (match.OrderProperty == OrderProperties.ItemProperty) { itemPropertyList.Visible = true; PrepareCustomPropertyField(customPropertyLabel, customPropertyList, match.ItemProperty); } else if (match.OrderProperty == OrderProperties.PackageProperty) { packagePropertyList.Visible = true; PrepareCustomPropertyField(customPropertyLabel, customPropertyList, match.PackageProperty); } else { PrepareCustomPropertyField(customPropertyLabel, customPropertyList, match.OrderProperty); } if (customPropertyList.Items.Count == 0) { customPropertyList.Items.Add(new ListItem("", match.CustomProperty)); } if (customPropertyList.Items.FindByValue(match.CustomProperty) == null) { match.CustomProperty = customPropertyList.Items[0].Value; } comparisonList.Items.Clear(); comparisonList.Items.AddRange(GetComparisons()); limitOrderPropertyList.Items.Clear(); limitOrderPropertyList.Items.AddRange(GetLimitOrderProperties()); limitItemPropertyList.Items.Clear(); limitItemPropertyList.Items.AddRange(GetItemProperties()); limitItemPropertyList.Visible = false; limitPackagePropertyList.Items.Clear(); limitPackagePropertyList.Items.AddRange(GetPackageProperties()); limitPackagePropertyList.Visible = false; limitCustomPropertyList.Visible = false; multiplierLabel.Visible = match.LimitOrderProperty != OrderProperties.FixedAmountOne; if (match.LimitOrderProperty == OrderProperties.ItemProperty) { limitItemPropertyList.Visible = true; PrepareCustomPropertyField(limitCustomPropertyLabel, limitCustomPropertyList, match.LimitItemProperty); PrepareLimitField(multiplierLabel, limitLabel, limitField, limitRequired, limitNumeric, match.LimitItemProperty); } else if (match.LimitOrderProperty == OrderProperties.PackageProperty) { limitPackagePropertyList.Visible = true; PrepareCustomPropertyField(limitCustomPropertyLabel, limitCustomPropertyList, match.LimitPackageProperty); PrepareLimitField(multiplierLabel, limitLabel, limitField, limitRequired, limitNumeric, match.LimitPackageProperty); } else { PrepareCustomPropertyField(limitCustomPropertyLabel, limitCustomPropertyList, match.LimitOrderProperty); PrepareLimitField(multiplierLabel, limitLabel, limitField, limitRequired, limitNumeric, match.LimitOrderProperty); } if (limitCustomPropertyList.Items.Count == 0) { limitCustomPropertyList.Items.Add(new ListItem("", match.LimitCustomProperty)); } if (limitCustomPropertyList.Items.FindByValue(match.LimitCustomProperty) == null) { match.LimitCustomProperty = limitCustomPropertyList.Items[0].Value; } } }
public EntityListOrderParameter(OrderMatch order) { this.order = order; }