private void OnNewPrice(PriceBar price)
        {
            InvokeOnMainThread(() =>
            {
                // Update the last price, or append?
                double smaLastValue;
                if (_lastPrice.DateTime == price.DateTime)
                {
                    _ohlcDataSeries.Update(_ohlcDataSeries.Count - 1, price.Open, price.High, price.Low, price.Close);

                    smaLastValue = _sma50.Update(price.Close).Current;
                    _xyDataSeries.UpdateYAt(_xyDataSeries.Count - 1, smaLastValue);
                }
                else
                {
                    _ohlcDataSeries.Append(price.DateTime, price.Open, price.High, price.Low, price.Close);

                    smaLastValue = _sma50.Push(price.Close).Current;
                    _xyDataSeries.Append(price.DateTime, smaLastValue);

                    // If the latest appending point is inside the viewport (i.e. not off the edge of the screen)
                    // then scroll the viewport 1 bar, to keep the latest bar at the same place
                    var xaxis        = Runtime.GetNSObject <SCICategoryDateTimeAxis>(MainSurface.XAxes[0].Handle);
                    var visibleRange = Runtime.GetNSObject <SCIDoubleRange>(xaxis.VisibleRange.Handle);
                    if (visibleRange.Max > _ohlcDataSeries.Count)
                    {
                        MainSurface.XAxes[0].VisibleRange = new SCIDoubleRange(visibleRange.Min + 1, visibleRange.Max + 1);
                    }
                }

                _ohlcAxisMarker.Style.BackgroundColor = (price.Close >= price.Open ? StrokeUpColor : StrokeDownColor).ToColor();

                _ohlcAxisMarker.Position = price.Close;
                _smaAxisMarker.Position  = smaLastValue;

                _lastPrice = price;
            });
        }
        private void OnNewPrice(PriceBar price)
        {
            // Update the last price, or append?
            double smaLastValue;

            if (_lastPrice.DateTime == price.DateTime)
            {
                _ohlcDataSeries.Update(_ohlcDataSeries.Count - 1, price.Open, price.High, price.Low, price.Close);

                smaLastValue = _sma50.Update(price.Close).Current;
                _xyDataSeries.UpdateYAt(_xyDataSeries.Count - 1, smaLastValue);
            }
            else
            {
                _ohlcDataSeries.Append(price.DateTime, price.Open, price.High, price.Low, price.Close);

                smaLastValue = _sma50.Push(price.Close).Current;
                _xyDataSeries.Append(price.DateTime, smaLastValue);

                // If the latest appending point is inside the viewport (i.e. not off the edge of the screen)
                // then scroll the viewport 1 bar, to keep the latest bar at the same place
                var visibleRange = MainSurface.XAxes[0].VisibleRange;
                if (visibleRange.MaxAsDouble > _ohlcDataSeries.Count)
                {
                    visibleRange.SetMinMaxDouble(visibleRange.MinAsDouble + 1, visibleRange.MaxAsDouble + 1);
                }
            }

            Activity.RunOnUiThread(() =>
            {
                _ohlcAxisMarker.SetBackgroundColor((price.Close >= price.Open ? StrokeUpColor : StrokeDownColor).ToColor());
            });

            _ohlcAxisMarker.Y1Value = price.Close;
            _smaAxisMarker.Y1Value  = smaLastValue;

            _lastPrice = price;
        }
Esempio n. 3
0
        public void LoadRandomData(int candlesCount, int ticksPerCandle)
        {
            _ticksPerCandle = ticksPerCandle;

            _data = null;

            var dataSource = new RandomWalkGenerator();

            var ticksCount = (candlesCount + 1) * ticksPerCandle;

            _data = dataSource.GetRandomWalkSeries(ticksCount).YData.ToArray();

            _index = 0;
            //var baseDate = DateTime.Now;
            for (int j = 0; j < candlesCount; j++)
            {
                var date             = _baseTime.AddMinutes(j * 30);
                var volume           = _random.Next(100);
                var bidOrAsk         = _random.Next(2) == 0 ? BidOrAsk.Bid : BidOrAsk.Ask;
                var cumulativeVolume = default(double);

                var metaData = new CandlestickMetaData();
                metaData.AddTick(new CandleTick
                {
                    BidOrAsk  = bidOrAsk,
                    Price     = _data[_index],
                    Volume    = volume,
                    TimeStamp = date
                });

                cumulativeVolume += bidOrAsk.Equals(BidOrAsk.Ask) ? volume : -volume;

                var high = cumulativeVolume > 0 ? cumulativeVolume : 0;
                var low  = cumulativeVolume < 0 ? cumulativeVolume : 0;

                _dataSeries1.Append(date, cumulativeVolume, high, low, cumulativeVolume);
                _dataSeries0.Append(date, _data[_index], _data[_index], _data[_index], _data[_index], metaData);
                _candleCount = _dataSeries0.Count;

                for (int i = 0; i < ticksPerCandle; i++)
                {
                    _index++;

                    volume   = _random.Next(100);
                    bidOrAsk = _random.Next(2) == 0 ? BidOrAsk.Bid : BidOrAsk.Ask;

                    //date = date;
                    var newTick = _data[_index];
                    var open    = _dataSeries0.OpenValues[_candleCount - 1];
                    high = _dataSeries0.HighValues[_candleCount - 1];
                    high = high > newTick ? high : newTick;

                    low = _dataSeries0.LowValues[_candleCount - 1];
                    low = low < newTick ? low : newTick;

                    var meta = (CandlestickMetaData)_dataSeries0.Metadata[_candleCount - 1];

                    meta.AddTick(new CandleTick
                    {
                        BidOrAsk  = bidOrAsk,
                        Price     = newTick,
                        Volume    = volume,
                        TimeStamp = date
                    });

                    _dataSeries0.Update(_candleCount - 1, open, high, low, newTick);

                    cumulativeVolume += bidOrAsk.Equals(BidOrAsk.Ask) ? volume : -volume;

                    open = _dataSeries1.OpenValues[_candleCount - 1];
                    high = _dataSeries1.HighValues[_candleCount - 1];
                    high = high > cumulativeVolume ? high : cumulativeVolume;

                    low = _dataSeries1.LowValues[_candleCount - 1];
                    low = low < cumulativeVolume ? low : cumulativeVolume;

                    _dataSeries1.Update(_candleCount - 1, open, high, low, cumulativeVolume);
                }

                _filterDataSeries.Append(date, _movingAverage.Push(cumulativeVolume).Current);
            }
        }
Esempio n. 4
0
        private void HandleTicks(List <Tick> list)
        {
            List <Tick> ticks = new List <Tick>();

            try
            {
                ticks = list.FindAll(i => i.Board == Board && i.Seccode == Seccode &&
                                     DateTime.Parse(i.Tradetime) > _lastCandle.TradeTime);
            }
            catch
            {
                if (_lastCandle == null)
                {
                    return;
                }
            }

            if (ticks.Count == 0)
            {
                return;
            }

            foreach (var tick in ticks)
            {
                var tickTime = DateTime.Parse(tick.Tradetime);
                try
                {
                    if ((tickTime - _lastCandle.TradeTime).TotalMinutes >= _timeframe)
                    {
                        if (OhlcDataSeries == null)
                        {
                            OhlcDataSeries = new OhlcDataSeries <DateTime, double>();
                        }
                        if (XyDataSeries == null)
                        {
                            XyDataSeries = new XyDataSeries <DateTime, int>();
                        }
                        OhlcDataSeries.Append(tickTime, tick.Price, tick.Price, tick.Price, tick.Price);
                        XyDataSeries.Append(tickTime, (int)tick.Price);
                        _lastCandle = new Candle //init last candle
                        {
                            Time  = tick.Tradetime,
                            Open  = tick.Price,
                            High  = tick.Price,
                            Low   = tick.Price,
                            Close = tick.Price
                        };
                    }
                    else //else we update the last one
                    {
                        //if (_lastCandle.Close == tick.Price)
                        //{
                        //    continue;
                        //}
                        _lastCandle.Close = tick.Price;
                        _lastCandle.High  = _lastCandle.High < tick.Price ? tick.Price : _lastCandle.High;
                        _lastCandle.Low   = _lastCandle.Low > tick.Price ? tick.Price : _lastCandle.Low;
                        if (Board == "FUT")
                        {
                            _lastCandle.Volume++;
                        }
                        else
                        {
                            _lastCandle.Volume += (int)tick.Price;
                        }
                        OhlcDataSeries?.Update(OhlcDataSeries.Count - 1, _lastCandle.Open, _lastCandle.High,
                                               _lastCandle.Low,
                                               _lastCandle.Close);
                        XyDataSeries?.Update(XyDataSeries.Count - 1, _lastCandle.Volume);
                    }
                }
                catch (Exception)
                {
                    if (_lastCandle == null)
                    {
                        return;
                    }
                    throw;
                }
            }
        }