internal static void GenerateDayBarMessages(InstrumentOanda instrument, CancellationToken token, GetHistoryDelegate getHistory, Action <QuoteBase> newQuote)
        {
            var param = new HistoryRequestParameters();

            param.Symbol   = instrument.Name.ToUpper();
            param.FromTime = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day).AddDays(-5).AddMilliseconds(-1);
            param.ToTime   = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day);
            GenerateDayBarMessage(param, instrument, token, getHistory, newQuote);
        }
        private static void GenerateDayBarMessage(HistoryRequestParameters param, InstrumentOanda instrument, CancellationToken token, GetHistoryDelegate getHistory, Action <QuoteBase> newQuote)
        {
            try
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                param.HistoryType = HistoryDataTypes.Bid;
                param.Period      = (int)HistoryPeriod.Day;

                int count = 5000;
                List <BarHistoryItem> al = getHistory(param, param.FromTime, param.ToTime, (GranularityEnum)GetOandaTimeframe(param.Period), instrument, count);
                if (al == null || al.Count == 0)
                {
                    return;
                }

                if (token.IsCancellationRequested)
                {
                    return;
                }

                InstrumentDayBarMessage msg = new InstrumentDayBarMessage();
                msg.Symbol = param.Symbol;

                var bi0 = al[al.Count - 1];

                if (bi0 == null)
                {
                    return;
                }

                msg.Open  = bi0.Open;
                msg.High  = bi0.High;
                msg.Low   = bi0.Low;
                msg.Ticks = (long)bi0.Volume;

                if (al.Count > 1)
                {
                    msg.PreviousClosePrice = al[al.Count - 2].Close;
                }

                if (token.IsCancellationRequested)
                {
                    return;
                }

                newQuote(msg);
            }
            catch (Exception ex)
            {
            }
        }
示例#3
0
        public override IList <IHistoryItem> LoadHistory(HistoryRequestParameters requestParameters)
        {
            var result = new List <IHistoryItem>();

            if (!this.allSymbolsCache.TryGetValue(requestParameters.SymbolId, out var okexSymbol))
            {
                return(result);
            }

            if (requestParameters.Period == Period.TICK1)
            {
                var ticks = this.client.GetTickHistory(okexSymbol, requestParameters.CancellationToken, out string error);

                if (!string.IsNullOrEmpty(error))
                {
                    this.PushMessage(DealTicketGenerator.CreateRefuseDealTicket(error));
                    return(result);
                }

                long prevTickTime = default;

                for (int i = ticks.Length - 1; i >= 0; i--)
                {
                    var tick = ticks[i];

                    if (tick.Time >= requestParameters.FromTime && tick.Time <= requestParameters.ToTime)
                    {
                        var item = new HistoryItemLast()
                        {
                            Price     = tick.Price ?? default,
                            Volume    = tick.Size ?? default,
                            TicksLeft = tick.Time.Ticks
                        };

                        if (prevTickTime >= item.TicksLeft)
                        {
                            item.TicksLeft = prevTickTime + 1;
                        }

                        prevTickTime = item.TicksLeft;
                        result.Add(item);
                    }
                }
            }
            else
            {
                var  okexPeriod  = requestParameters.Period.ToOKEx();
                var  historyType = requestParameters.HistoryType.ToOKEx();
                var  after       = requestParameters.ToTime;
                bool keepGoing   = true;

                while (keepGoing)
                {
                    var candles = this.client.GetCandleHistory(okexSymbol, after, okexPeriod, historyType, requestParameters.CancellationToken, out string error);

                    if (!string.IsNullOrEmpty(error))
                    {
                        this.PushMessage(DealTicketGenerator.CreateRefuseDealTicket(error));
                        break;
                    }

                    foreach (var item in candles)
                    {
                        if (item.Time < requestParameters.FromTime)
                        {
                            keepGoing = false;
                            break;
                        }

                        if (requestParameters.ToTime >= item.Time)
                        {
                            result.Add(new HistoryItemBar()
                            {
                                Close     = item.Close,
                                Open      = item.Open,
                                High      = item.High,
                                Low       = item.Low,
                                TicksLeft = item.Time.Ticks,
                                Volume    = (okexSymbol.IsInverseContractSymbol ? item.CurrencyVolume : item.Volume) ?? default
                            });
示例#4
0
        public override IList <IHistoryItem> LoadHistory(HistoryRequestParameters requestParameters)
        {
            List <IHistoryItem> result = new List <IHistoryItem>();

            string   symbol = requestParameters.Symbol.Id;
            DateTime from   = requestParameters.FromTime;
            DateTime to     = requestParameters.ToTime;
            var      token  = requestParameters.CancellationToken;

            if (requestParameters.Period.BasePeriod == BasePeriod.Tick)
            {
                List <HitTrade> hitTrades = new List <HitTrade>();

                while (from < to)
                {
                    var trades = this.CheckHitResponse(this.restApi.GetTradesByTimestampAsync(symbol, HitSort.Asc, from, to, MAX_TRADES_BY_REQUEST, cancellationToken: token).Result, out HitError hitError, true);

                    if (hitError != null || token.IsCancellationRequested)
                    {
                        break;
                    }

                    hitTrades.AddRange(trades);

                    if (trades.Length < MAX_TRADES_BY_REQUEST)
                    {
                        break;
                    }

                    from = trades.Last().Timestamp;
                }

                long prevTimeTicks = 0;
                long prevTradeId   = 0;
                foreach (var hitTrade in hitTrades)
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    if (hitTrade.Id <= prevTradeId)
                    {
                        continue;
                    }

                    prevTradeId = hitTrade.Id;

                    var last = this.CreateHistoryItem(hitTrade);

                    if (last.TicksLeft <= prevTimeTicks)
                    {
                        last.TicksLeft = prevTimeTicks + 1;
                    }

                    prevTimeTicks = last.TicksLeft;

                    result.Add(last);
                }
            }
            else
            {
                var hitPeriod = this.ConvertPeriod(requestParameters.Period);

                var intervals = requestParameters.Interval.Split(TimeSpan.FromTicks(requestParameters.Period.Ticks * MAX_TRADES_BY_REQUEST));

                foreach (var interval in intervals)
                {
                    var candles = this.CheckHitResponse(this.restApi.GetCandlesAsync(symbol, interval.From, interval.To, hitPeriod, MAX_TRADES_BY_REQUEST, token).Result, out _, true);

                    if (candles == null)
                    {
                        return(null);
                    }

                    foreach (var candle in candles)
                    {
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        if (candle.Timestamp < from || candle.Timestamp > to)
                        {
                            continue;
                        }

                        var bar = this.CreateHistoryItem(candle);

                        result.Add(bar);
                    }
                }
            }

            return(result);
        }
示例#5
0
 public override IList <IHistoryItem> LoadHistory(HistoryRequestParameters requestParameters) => this.vendor.LoadHistory(requestParameters);
示例#6
0
        public override IList <IHistoryItem> LoadHistory(HistoryRequestParameters requestParameters)
        {
            List <IHistoryItem> result = new List <IHistoryItem>();

            string   symbol = requestParameters.Symbol.Id;
            DateTime from   = requestParameters.FromTime;
            DateTime to     = requestParameters.ToTime;
            var      token  = requestParameters.CancellationToken;

            if (requestParameters.Period.BasePeriod == BasePeriod.Tick)
            {
                List <HitTrade> hitTrades = new List <HitTrade>();

                while (from < to)
                {
                    var trades = this.CheckHitResponse(this.restApi.GetTradesByTimestampAsync(symbol, HitSort.Asc, from, to, 1000, cancellationToken: token).Result, out HitError hitError, true);

                    if (hitError != null || trades.Length == 0 || token.IsCancellationRequested)
                    {
                        break;
                    }

                    hitTrades.AddRange(trades);

                    from = trades.Last().Timestamp.AddSeconds(1);
                }


                long prevTimeTicks = 0;
                foreach (var hitTrade in hitTrades)
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    var last = this.CreateHistoryItem(hitTrade);

                    if (last.TicksLeft <= prevTimeTicks)
                    {
                        last.TicksLeft = prevTimeTicks + 1;
                    }

                    prevTimeTicks = last.TicksLeft;

                    result.Add(last);
                }
            }
            else
            {
                var hitPeriod = this.ConvertPeriod(requestParameters.Period);

                var candles = this.CheckHitResponse(this.restApi.GetCandlesAsync(symbol, hitPeriod, 1000, token).Result, out HitError hitError, true);
                if (hitError == null)
                {
                    foreach (var candle in candles)
                    {
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        if (candle.Timestamp < from || candle.Timestamp > to)
                        {
                            continue;
                        }

                        var bar = this.CreateHistoryItem(candle);

                        result.Add(bar);
                    }
                }
            }

            return(result);
        }
示例#7
0
        /// <summary>
        /// Called when user requests history in the trading platform
        /// </summary>
        public override IList <IHistoryItem> LoadHistory(HistoryRequestParameters requestParameters)
        {
            List <IHistoryItem> result = new List <IHistoryItem>();

            string symbol = requestParameters.SymbolId;

            long fromUnix = Core.Instance.TimeUtils.ConvertDateTimeToUnixMiliseconds(requestParameters.FromTime);
            long toUnix   = Core.Instance.TimeUtils.ConvertDateTimeToUnixMiliseconds(requestParameters.ToTime);

            Stack <List <IHistoryItem> > itemsStack = new Stack <List <IHistoryItem> >();

            try
            {
                if (requestParameters.Period.BasePeriod == BasePeriod.Tick)
                {
                    long currentToUnix = toUnix;

                    while (fromUnix < currentToUnix)
                    {
                        var trades = this.restApi.GetTrades(symbol, fromUnix, currentToUnix).Result;

                        if (trades.Length == 0)
                        {
                            break;
                        }

                        List <IHistoryItem> ticks = new List <IHistoryItem>();
                        foreach (var trade in trades)
                        {
                            var last = CreateHistoryItemLast(trade);

                            ticks.Add(last);
                        }

                        itemsStack.Push(ticks);

                        currentToUnix = trades.Last().Timestamp - 1;
                    }
                }
                else
                {
                    string timeFrame = GetTimeFrameFromPeriod(requestParameters.Period);
                    long   millisecondsInRequestPeriod = requestParameters.Period.Ticks / TimeSpan.TicksPerMillisecond;
                    long   currentToUnix = toUnix;

                    while (fromUnix < currentToUnix)
                    {
                        var candles = this.restApi.GetCandles(symbol, timeFrame, fromUnix, currentToUnix).Result;

                        if (candles.Length == 0)
                        {
                            break;
                        }

                        List <IHistoryItem> bars = new List <IHistoryItem>();

                        for (int i = 0; i < candles.Length; i++)
                        {
                            var bar = CreateHistoryItemBar(candles[i]);

                            bars.Add(bar);
                        }

                        itemsStack.Push(bars);

                        currentToUnix = candles.Last().Timestamp - millisecondsInRequestPeriod;
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException is WebException wex && wex.Response is HttpWebResponse httpWebResponse && httpWebResponse.StatusCode == (HttpStatusCode)429)
                {
                    var dealTicket = DealTicketGenerator.CreateRefuseDealTicket($"{BitfinexConsts.VENDOR_NAME} request limit reached. Limit will be reseted in 1 minute");
                    PushMessage(dealTicket);
                }
            }
            finally
            {
                while (itemsStack.Count > 0)
                {
                    var items = itemsStack.Pop();

                    for (int i = items.Count - 1; i >= 0; i--)
                    {
                        result.Add(items[i]);
                    }
                }
            }

            return(result);
        }