public async Task AuditCompletedOrdersForPair(ArbitragePair pair, CancellationToken cancellationToken) { foreach (var order in dbService.GetMakerOrdersByStatus("filled", pair.Id).Where(o => o.BaseExchange == baseExchange.Name && o.CounterExchange == counterExchange.Name)) { try { var baseOrder = await baseExchange.CheckOrder(order.BaseOrderUuid, pair.Symbol); var counterOrder = await counterExchange.CheckOrder(order.CounterOrderUuid, pair.Symbol); if (baseOrder != null && counterOrder != null) { decimal commission = 0m; if (baseOrder.Side == OrderSide.Buy) { commission = counterOrder.CostProceeds - baseOrder.CostProceeds; } else { commission = baseOrder.CostProceeds - counterOrder.CostProceeds; } dbService.UpdateOrder(order.Id, baseRate: baseOrder.AvgRate, counterRate: counterOrder.AvgRate, baseCost: baseOrder.CostProceeds, counterCost: counterOrder.CostProceeds, commission: commission, status: "complete", baseQuantityFilled: baseOrder.QuantityFilled, counterQuantityFilled: counterOrder.QuantityFilled); } // await EmailHelper.SendSimpleMailAsync(gmail, string.Format("Trade {0} - {1}", baseOrder.Symbol, commission), JsonConvert.SerializeObject(new { baseOrder = baseOrder, counterOrder = counterOrder }, Formatting.Indented)); } catch (Exception e) { logger.Error(e); } } }
public static void AuditCompletedOrdersForPair(IDbService dbService, IExchange baseExchange, IExchange counterExchange, ArbitragePair pair) { foreach (var order in dbService.GetMakerOrdersByStatus("filled", pair.Id).Where(o => o.BaseExchange == baseExchange.Name && o.CounterExchange == counterExchange.Name)) { try { var baseOrder = baseExchange.CheckOrder(order.BaseOrderUuid, pair.Symbol).Result; var counterOrder = counterExchange.CheckOrder(order.CounterOrderUuid, pair.Symbol).Result; if (baseOrder != null && counterOrder != null) { decimal commission = 0m; if (baseOrder.Side == OrderSide.Buy) { commission = counterOrder.CostProceeds - baseOrder.CostProceeds; } else { commission = baseOrder.CostProceeds - counterOrder.CostProceeds; } dbService.UpdateOrder(order.Id, baseRate: baseOrder.AvgRate, counterRate: counterOrder.AvgRate, baseCost: baseOrder.CostProceeds, counterCost: counterOrder.CostProceeds, commission: commission, status: "complete", baseQuantityFilled: baseOrder.QuantityFilled, counterQuantityFilled: counterOrder.QuantityFilled); } } catch (Exception e) { Console.WriteLine(e); } } }
public static void AuditOrder(string symbol, string baseUuid, string counterUuid, IExchange baseExchange, IExchange counterExchange) { var baseOrder = baseExchange.CheckOrder(baseUuid, symbol).Result; var counterOrder = counterExchange.CheckOrder(counterUuid, symbol).Result; if (baseOrder != null && counterOrder != null) { decimal commission = 0m; if (baseOrder.Side == "buy") { commission = counterOrder.CostProceeds - baseOrder.CostProceeds; } else { commission = baseOrder.CostProceeds - counterOrder.CostProceeds; } } }
public void AuditCompletedOrdersForPair(ArbitragePair pair, CancellationToken cancellationToken) { var orders = dbService.GetMakerOrdersByStatus("filled", pair.Id).Where(o => o.BaseExchange == baseExchange.Name && o.CounterExchange == counterExchange.Name); if (orders.Any()) { logger.Trace("Audit {0} orders for {1}", orders.Count(), pair.Symbol); } else { logger.Trace("No orders for {0}", pair.Symbol); } foreach (var order in orders) { try { var baseOrder = baseExchange.CheckOrder(order.BaseOrderUuid, pair.BaseSymbol).Result; var counterOrder = counterExchange.CheckOrder(order.CounterOrderUuid, pair.CounterSymbol).Result; if (baseOrder != null && counterOrder != null) { decimal commission = 0m; if (baseOrder.Side == OrderSide.Buy) { commission = counterOrder.CostProceeds - baseOrder.CostProceeds; } else { commission = baseOrder.CostProceeds - counterOrder.CostProceeds; } dbService.UpdateOrder(order.Id, baseRate: baseOrder.AvgRate, counterRate: counterOrder.AvgRate, baseCost: baseOrder.CostProceeds, counterCost: counterOrder.CostProceeds, commission: commission, status: "complete", baseQuantityFilled: baseOrder.QuantityFilled, counterQuantityFilled: counterOrder.QuantityFilled); } } catch (AggregateException ae) { ae.Handle((x) => { if (x is ApiException) // This we know how to handle. { string content = ((ApiException)x).Content; if (content.Contains("Order does not exist")) // Dont know why we are seeing this but update status { dbService.UpdateOrder(order.Id, status: "notexists"); } else { logger.Error(content); } } return(true); }); } catch (Exception e) { logger.Error(e); } } }