Exemplo n.º 1
0
        public static TimeSpan GetTimeZone(this BinaryMetaInfo metaInfo, bool isUtc, SecurityId securityId, IExchangeInfoProvider exchangeInfoProvider)
        {
            if (isUtc)
            {
                return(metaInfo.ServerOffset);
            }

            var board = exchangeInfoProvider.GetExchangeBoard(securityId.BoardCode);

            return(board == null ? metaInfo.LocalOffset : board.TimeZone.BaseUtcOffset);
        }
Exemplo n.º 2
0
        private void RefreshCharts()
        {
            Chart.ClearAreas();

            _areaComb = new ChartArea();

            var yAxis = _areaComb.YAxises.First();

            yAxis.AutoRange    = true;
            Chart.IsAutoRange  = true;
            Chart.IsAutoScroll = true;

            Chart.AddArea(_areaComb);

            _timeframe = TimeSpan.FromMinutes(((ComboBoxItem)Timeframe.SelectedItem).Tag.To <int>());

            var id = (SecurityId)Securities.SelectedItem;

            _security = new Security
            {
                Id        = id.ToStringId(),
                PriceStep = id.SecurityCode.StartsWith("RI", StringComparison.InvariantCultureIgnoreCase) ? 10 : 0.01m,
                Board     = _exchangeInfoProvider.GetExchangeBoard(id.BoardCode) ?? ExchangeBoard.Associated
            };

            _tradeGenerator = new RandomWalkTradeGenerator(id);
            _tradeGenerator.Init();
            _tradeGenerator.Process(_security.ToMessage());

            var series = new CandleSeries(
                typeof(TimeFrameCandle),
                _security,
                _timeframe);

            _candleElement = new ChartCandleElement {
                FullTitle = "Candles"
            };
            Chart.AddElement(_areaComb, _candleElement, series);

            LoadData(_security);
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        protected override void OnInnerAdapterNewOutMessage(Message message)
        {
            switch (message.Type)
            {
            case MessageTypes.Security:
            {
                var secMsg   = (SecurityMessage)message;
                var security = _securityStorage.LookupById(secMsg.SecurityId);

                if (security == null)
                {
                    security = secMsg.ToSecurity(_exchangeInfoProvider);
                }
                else
                {
                    security.ApplyChanges(secMsg, _exchangeInfoProvider, OverrideSecurityData);
                }

                _securityStorage.Save(security, OverrideSecurityData);
                break;
            }

            case MessageTypes.Board:
            {
                var boardMsg = (BoardMessage)message;
                var board    = _exchangeInfoProvider.GetExchangeBoard(boardMsg.Code);

                if (board == null)
                {
                    board = _exchangeInfoProvider.GetOrCreateBoard(boardMsg.Code, code =>
                        {
                            var exchange = _exchangeInfoProvider.GetExchange(boardMsg.ExchangeCode) ?? boardMsg.ToExchange(new Exchange
                            {
                                Name = boardMsg.ExchangeCode
                            });

                            return(new ExchangeBoard
                            {
                                Code = code,
                                Exchange = exchange
                            });
                        });
                }

                board.ApplyChanges(boardMsg);

                _exchangeInfoProvider.Save(board.Exchange);
                _exchangeInfoProvider.Save(board);
                break;
            }

            case MessageTypes.Portfolio:
            {
                var portfolioMsg = (PortfolioMessage)message;

                if (portfolioMsg.Error != null)
                {
                    break;
                }

                var portfolio = _positionStorage.GetPortfolio(portfolioMsg.PortfolioName) ?? new Portfolio
                {
                    Name = portfolioMsg.PortfolioName
                };

                portfolioMsg.ToPortfolio(portfolio, _exchangeInfoProvider);
                _positionStorage.Save(portfolio);

                break;
            }

            case MessageTypes.PositionChange:
            {
                var positionMsg = (PositionChangeMessage)message;

                if (positionMsg.IsMoney())
                {
                    var portfolio = _positionStorage.GetPortfolio(positionMsg.PortfolioName) ?? new Portfolio
                    {
                        Name = positionMsg.PortfolioName
                    };

                    portfolio.ApplyChanges(positionMsg, _exchangeInfoProvider);
                    _positionStorage.Save(portfolio);
                }
                else
                {
                    var position = GetPosition(positionMsg.SecurityId, positionMsg.PortfolioName);

                    if (position == null)
                    {
                        break;
                    }

                    if (!positionMsg.DepoName.IsEmpty())
                    {
                        position.DepoName = positionMsg.DepoName;
                    }

                    if (positionMsg.LimitType != null)
                    {
                        position.LimitType = positionMsg.LimitType;
                    }

                    if (!positionMsg.Description.IsEmpty())
                    {
                        position.Description = positionMsg.Description;
                    }

                    position.ApplyChanges(positionMsg);
                    _positionStorage.Save(position);
                }

                break;
            }
            }

            base.OnInnerAdapterNewOutMessage(message);
        }
Exemplo n.º 4
0
        private void LoadData()
        {
            _lastPrice = 0m;

            _candles.Clear();
            var id = _securityId.ToSecurityId();

            _security = new Security
            {
                Id        = _securityId,
                PriceStep = _priceStep,
                Board     = _exchangeInfoProvider.GetExchangeBoard(id.BoardCode) ?? ExchangeBoard.Associated
            };

            Chart.Reset(new IChartElement[] { _candleElement });

            var storage = new StorageRegistry();

            var maxDays = 50;

            //BusyIndicator.IsBusy = true;

            var path = _historyPath;

            _curCandleNum = 0;

            Task.Factory.StartNew(() =>
            {
                var date = DateTime.MinValue;

                foreach (var tick in storage.GetTickMessageStorage(_security, new LocalMarketDataDrive(path)).Load())
                {
                    if (date != tick.ServerTime.Date)
                    {
                        date = tick.ServerTime.Date;

                        //var str = $"Loading ticks for {date:dd MMM yyyy}...";
                        //this.GuiAsync(() => BusyIndicator.BusyContent = str);

                        if (--maxDays == 0)
                        {
                            break;
                        }
                    }

                    AppendTick(tick);
                }
            })
            .ContinueWith(t =>
            {
                //this.GuiAsync(() => BusyIndicator.IsBusy = false);

                for (var i = 0; i < _candles.Count; i += _candlesPacketSize)
                {
                    var data = new ChartDrawData();

                    var candles = _candles.GetRange(i, Math.Min(_candlesPacketSize, _candles.Count - i)).Select(c => c.ToCandle(_tfSpan, _security));

                    foreach (var candle in candles)
                    {
                        candle.State = CandleStates.Finished;

                        var group = data.Group(candle.OpenTime);

                        group.Add(_candleElement, candle);

                        if (_indicatorElement != null)
                        {
                            group.Add(_indicatorElement, _indicator.Process((double)candle.ClosePrice));
                        }
                    }

                    Chart.Draw(data);
                }
            })
            .ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    Error(t.Exception.Message);
                }

                _dataIsLoaded = true;

                //this.GuiAsync(() => BusyIndicator.IsBusy = false);

                Chart.IsAutoRange = false;
                //_area.YAxises.FirstOrDefault().Do(a => a.AutoRange = false);
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
Exemplo n.º 5
0
        private void LoadData()
        {
            _candle    = null;
            _lastPrice = 0m;
            _allCandles.Clear();

            var id = new SecurityIdGenerator().Split(SecurityId.Text);

            _security = new Security
            {
                Id        = SecurityId.Text,
                PriceStep = _priceStep,
                Board     = _exchangeInfoProvider.GetExchangeBoard(id.BoardCode)
            };

            Chart.Reset(new IChartElement[] { _candleElement1 });

            var storage = new StorageRegistry();

            var maxDays = 2;

            BusyIndicator.IsBusy = true;

            var path = HistoryPath.Folder;

            Task.Factory.StartNew(() =>
            {
                var date = DateTime.MinValue;

                foreach (var tick in storage.GetTickMessageStorage(_security, new LocalMarketDataDrive(path)).Load())
                {
                    AppendTick(_security, tick);

                    _lastTime = tick.ServerTime;

                    if (date != tick.ServerTime.Date)
                    {
                        date = tick.ServerTime.Date;

                        var str = date.To <string>();
                        this.GuiAsync(() => BusyIndicator.BusyContent = str);

                        maxDays--;

                        if (maxDays == 0)
                        {
                            break;
                        }
                    }
                }
            })
            .ContinueWith(t =>
            {
                if (t.Exception != null)
                {
                    Error(t.Exception.Message);
                }

                this.GuiAsync(() =>
                {
                    BusyIndicator.IsBusy = false;
                    Chart.IsAutoRange    = false;
                    _areaComb.YAxises.First().AutoRange = false;
                });
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }