Ejemplo n.º 1
0
        private async Task Initialize()
        {
            _grids      = new Dictionary <string, OptionsGrid>();
            _activities = new Dictionary <string, List <double> >();
            Dictionary <string, DateTime> lastreport = new Dictionary <string, DateTime>();

            foreach (var instrument in _settings.Instruments)
            {
                lastreport.Add(instrument.Name, DateTime.UtcNow);
                var activity = await _activityManager.GetActivityByName(instrument.Name, instrument.ActivityFileName);

                _activities.Add(instrument.Name, activity.ActivityArray.ToList());

                var grid = new OptionsGrid(instrument.TimeToFirstOption, instrument.OptionLen, instrument.PriceSize, instrument.NPriceIndex, instrument.NTimeIndex,
                                           instrument.MarginHit, instrument.MarginMiss, instrument.MaxPayoutCoeff, instrument.BookingFee, instrument.HasWeekend);

                var history = _historyHolder.GetHistory(instrument.Name);
                if (history != null && history.Length > 0)
                {
                    var currentPrice = history.Last();
                    await _logger.WriteInfoAsync("Calculator.Initialize", null, $"Current Price[{instrument.Name}] Date: {currentPrice.Date.ToDateTimeString()}");

                    grid.InitiateGrid(_activities[instrument.Name], history.ToList(), instrument.Delta, instrument.MovingWindow, currentPrice, instrument.SmileVar);
                    _grids.Add(instrument.Name, grid);

                    Timer instrumentTimer = new Timer(instrument.Period);
                    instrumentTimer.Elapsed += (sender, args) =>
                    {
                        instrumentTimer.Stop();
                        if (isDisposing)
                        {
                            return;
                        }

                        var now       = DateTime.UtcNow;
                        var newPrices = _priceCache.GetPrices(instrument.Name);

                        Price newPrice;
                        if (newPrices.Length > 0)
                        {
                            var lastPrice = newPrices.Last();
                            newPrice = new Price
                            {
                                Date = now,
                                Ask  = lastPrice.Ask,
                                Bid  = lastPrice.Bid
                            };
                        }
                        else
                        {
                            var lastHistoryPrice = _historyHolder.GetHistory(instrument.Name).Last();
                            newPrice = new Price
                            {
                                Date = now,
                                Ask  = lastHistoryPrice.Ask,
                                Bid  = lastHistoryPrice.Bid
                            };
                        }
                        _grids[instrument.Name].UpdateCoefficients(newPrices.ToList(), newPrice, instrument.SmileVar);
                        if (newPrices.Length > 0 && DateTime.UtcNow > lastreport[instrument.Name].AddMinutes(30))
                        {
                            string msg = $"{DateTime.UtcNow.ToTimeString()}[{instrument.Name}] Updated. New prices size:{newPrices.Length}. Current Price:{newPrice.Date.ToDateTimeString()}";
                            Console.WriteLine(msg);
                            _logger.WriteInfoAsync("Calculator.instrumentTimer.Elapsed", null, msg);
                            lastreport[instrument.Name] = DateTime.UtcNow;
                        }

                        instrumentTimer.Start();
                    };
                    instrumentTimer.Start();
                }
            }
            StartSubscriber();
        }