Пример #1
0
 public void OnMessage(ExecutionReport message, QuickFix.SessionID session)
 {
     logger.Info("EP: " + message.ToString());
     //System.Windows.Forms.Application.DoEvents();
     //try
     //{
     //    //logger.Debug("IN_EP: " + message.ToString());
     //}
     //catch (Exception ex)
     //{
     //    logger.Error("==ExecutionReport exception==");
     //    logger.Error(ex.ToString(), ex);
     //}
 }
Пример #2
0
        protected override void StartImpl()
        {
            ctSource = new CancellationTokenSource();
            var random = new Random();

            var nPoints = 10000000;

            var gbms =
                Instruments.ToDictionary(x => x,
                                         x => new GeometricalBrownianMotion(1.0, 0.95, 1.0, nPoints, 0, random));

            streamJob = Task.Run(async() =>
            {
                OnConnected();

                while (!ctSource.IsCancellationRequested)
                {
                    foreach (var instrument in Instruments)
                    {
                        try
                        {
                            var currentTickPrice = new TickPrice(instrument, DateTime.UtcNow, Math.Round((decimal)gbms[instrument].GenerateNextValue(), 6));

                            lock (syncRoot)
                            {
                                var trades         = new List <ExecutionReport>();
                                var executedOrders = new List <TradingSignal>();

                                foreach (var tradingSignal in ActualSignals[instrument.Name].Where(x => x.Volume > 0))
                                {
                                    if (tradingSignal.TradeType == TradeType.Buy &&
                                        currentTickPrice.Ask <= tradingSignal.Price)
                                    {
                                        var trade = new ExecutionReport(
                                            instrument,
                                            DateTime.UtcNow,
                                            tradingSignal.Price.Value,
                                            tradingSignal.Volume,
                                            TradeType.Buy,
                                            tradingSignal.OrderId,
                                            OrderExecutionStatus.Fill);

                                        trades.Add(trade);
                                        executedOrders.Add(tradingSignal);

                                        LykkeLog.WriteInfoAsync(nameof(StubExchange), nameof(StartImpl), nameof(streamJob),
                                                                $"EXECUTION of order {tradingSignal} by price {trade.Price}").Wait();
                                    }
                                    else if (tradingSignal.TradeType == TradeType.Sell &&
                                             currentTickPrice.Bid >= tradingSignal.Price)
                                    {
                                        var trade = new ExecutionReport(instrument,
                                                                        DateTime.UtcNow,
                                                                        tradingSignal.Price.Value,
                                                                        tradingSignal.Volume,
                                                                        TradeType.Sell,
                                                                        tradingSignal.OrderId,
                                                                        OrderExecutionStatus.Fill);

                                        trades.Add(trade);
                                        executedOrders.Add(tradingSignal);

                                        LykkeLog.WriteInfoAsync(nameof(StubExchange), nameof(streamJob), trade.ToString(),
                                                                $"EXECUTION of order {tradingSignal} by price {trade.Price}").Wait();
                                    }
                                }

                                foreach (var signal in executedOrders)
                                {
                                    ActualSignals[instrument.Name].Remove(signal);
                                    LykkeLog.WriteInfoAsync(nameof(StubExchange), nameof(StartImpl), nameof(streamJob),
                                                            $"Trading order {signal} was removed from actual signals as executed").Wait();
                                }

                                trades.ForEach(async x =>
                                {
                                    Positions[instrument.Name].AddTrade(x);
                                    try
                                    {
                                        await _tradeHandler.Handle(x);
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine(e);
                                    }
                                });


                                if (++counter % 100 == 0)
                                {
                                    LykkeLog.WriteInfoAsync(nameof(StubExchange), nameof(StartImpl), nameof(streamJob),
                                                            $"Step {counter}, total PnL: {Positions[instrument.Name].GetPnL(currentTickPrice.Mid)}").Wait();
                                }
                            }

                            // TODO: deal with awaitable. I don't want to wait here for Azure and Rabbit connections
                            await _tickPriceHandler.Handle(currentTickPrice);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }

                        await Task.Delay(config.PricesIntervalInMilliseconds, ctSource.Token);
                    }
                }

                OnStopped();
            });
        }