Exemplo n.º 1
0
        public async Task <OhlcData> GetOhlcAsync(OhlcContext context)
        {
            var pair   = context.Pair;
            var market = context.Market;

            var timeStampStart = (long)context.Range.UtcFrom.ToUnixTimeStamp();
            var timeStampEnd   = (long)context.Range.UtcTo.ToUnixTimeStamp();

            var period = ConvertToPoloniexInterval(market);

            var api = ApiProvider.GetApi(context);
            var r   = await api.GetChartDataAsync(pair.ToTicker(this), timeStampStart, timeStampEnd, period).ConfigureAwait(false);

            var ohlc     = new OhlcData(market);
            var seriesid = OhlcUtilities.GetHash(pair, market, Network);

            foreach (var ohlcEntry in r)
            {
                ohlc.Add(new OhlcEntry(seriesid, ohlcEntry.date.ToUtcDateTime(), this)
                {
                    Open            = ohlcEntry.open,
                    Close           = ohlcEntry.close,
                    Low             = ohlcEntry.low,
                    High            = ohlcEntry.high,
                    VolumeTo        = ohlcEntry.quoteVolume,
                    VolumeFrom      = ohlcEntry.volume,
                    WeightedAverage = ohlcEntry.weightedAverage
                });
            }

            return(ohlc);
        }
Exemplo n.º 2
0
        public async Task <OhlcData> GetOhlcAsync(OhlcContext context)
        {
            var api = ApiProvider.GetApi(context);

            var resolution = ConvertToBitMexInterval(context.Market);
            var startDate  = context.Range.UtcFrom;
            var endDate    = context.Range.UtcTo;

            var r = await api.GetTradeHistoryAsync(context.Pair.Asset1.ToRemoteCode(this), resolution, startDate, endDate).ConfigureAwait(false);

            var ohlc     = new OhlcData(context.Market);
            var seriesId = OhlcUtilities.GetHash(context.Pair, context.Market, Network);

            foreach (var instrActive in r)
            {
                ohlc.Add(new OhlcEntry(seriesId, instrActive.timestamp, this)
                {
                    Open            = instrActive.open,
                    Close           = instrActive.close,
                    Low             = instrActive.low,
                    High            = instrActive.high,
                    VolumeTo        = instrActive.volume,
                    VolumeFrom      = instrActive.volume,
                    WeightedAverage = (instrActive.vwap ?? 0) // BUG: what to set if vwap is NULL?
                });
            }

            return(ohlc);
        }
Exemplo n.º 3
0
        public async Task <OhlcData> GetOhlcAsync(OhlcContext context)
        {
            var api = ApiProvider.GetApi(context);

            var pairCode = context.Pair.ToTicker(this);

            var interval  = ConvertToBinanceInterval(context.Market);
            var startDate = (long)(context.Range.UtcFrom.ToUnixTimeStamp() * 1000);
            var endDate   = (long)(context.Range.UtcTo.ToUnixTimeStamp() * 1000);

            var r = await api.GetCandlestickBarsAsync(pairCode, interval, startDate, endDate).ConfigureAwait(false);

            var ohlc = new OhlcData(context.Market);

            var seriesId = OhlcUtilities.GetHash(context.Pair, context.Market, Network);

            foreach (var rEntry in r)
            {
                var dateTime = ((long)(rEntry[0] / 1000)).ToUtcDateTime();
                ohlc.Add(new OhlcEntry(seriesId, dateTime, this)
                {
                    Open            = rEntry[1],
                    Close           = rEntry[4],
                    Low             = rEntry[3],
                    High            = rEntry[2],
                    VolumeTo        = rEntry[7], // Quote asset volume
                    VolumeFrom      = rEntry[5], // Volume
                    WeightedAverage = 0          // BUG: no WeightedAverage data returned from API.
                });
            }

            ohlc.Reverse();

            return(ohlc);
        }
Exemplo n.º 4
0
        public async Task <OhlcData> GetOhlcAsync(OhlcContext context)
        {
            var api          = GdaxApiProvider.GetApi(context);
            var currencyCode = context.Pair.ToTicker(this);

            var ohlc     = new OhlcData(context.Market);
            var seriesId = OhlcUtilities.GetHash(context.Pair, context.Market, Network);

            var granularitySeconds = GetSeconds(context.Market);
            var maxNumberOfCandles = 200;

            var tsFrom = (long)context.Range.UtcFrom.ToUnixTimeStamp();
            var tsTo   = (long)context.Range.UtcTo.ToUnixTimeStamp();
            var tsStep = maxNumberOfCandles * granularitySeconds;

            var currTsTo   = tsTo;
            var currTsFrom = tsTo - tsStep;

            while (currTsTo > tsFrom)
            {
                var candles = await api.GetCandlesAsync(currencyCode, currTsFrom.ToUtcDateTime(), currTsTo.ToUtcDateTime(), granularitySeconds).ConfigureAwait(false);

                foreach (var candle in candles)
                {
                    var dateTime = ((long)candle[0]).ToUtcDateTime();
                    ohlc.Add(new OhlcEntry(seriesId, dateTime, this)
                    {
                        Low             = (double)candle[1],
                        High            = (double)candle[2],
                        Open            = (double)candle[3],
                        Close           = (double)candle[4],
                        VolumeTo        = (long)candle[5],
                        VolumeFrom      = (long)candle[5],
                        WeightedAverage = 0 // Is not provided by API.
                    });
                }

                currTsTo = currTsFrom;

                if (currTsTo - tsStep >= tsFrom)
                {
                    currTsFrom -= tsStep;
                }
                else
                {
                    currTsFrom = tsFrom;
                }

                ApiHelpers.EnterRate(this, context);
            }

            return(ohlc);
        }
Exemplo n.º 5
0
        public async Task <OhlcData> GetOhlcAsync(OhlcContext context)
        {
            var api = ApiProvider.GetApi(context);

            var krakenTimeInterval = ConvertToKrakenInterval(context.Market);

            // BUG: "since" is not implemented. Need to be checked.
            var r = await api.GetOhlcDataAsync(context.Pair.ToTicker(this, ""), krakenTimeInterval).ConfigureAwait(false);

            CheckResponseErrors(r);

            var ohlc     = new OhlcData(context.Market);
            var seriesId = OhlcUtilities.GetHash(context.Pair, context.Market, Network);

            if (r.result.pairs.Count != 0)
            {
                foreach (var ohlcResponse in r.result.pairs.FirstOrDefault().Value.OrderByDescending(x => x.time))
                {
                    var time = ((long)ohlcResponse.time).ToUtcDateTime();

                    // BUG: ohlcResponse.volume is double ~0.2..10.2, why do we cast to long?
                    ohlc.Add(new OhlcEntry(seriesId, time, this)
                    {
                        Open            = ohlcResponse.open,
                        Close           = ohlcResponse.close,
                        Low             = ohlcResponse.low,
                        High            = ohlcResponse.high,
                        VolumeTo        = ohlcResponse.volume, // Cast to long should be revised.
                        VolumeFrom      = ohlcResponse.volume,
                        WeightedAverage = ohlcResponse.vwap    // Should be checked.
                    });
                }
            }
            else
            {
                throw new ApiResponseException("No OHLC data received", this);
            }

            return(ohlc);
        }
Exemplo n.º 6
0
        public async Task <OhlcData> GetOhlcAsync(OhlcContext context)
        {
            var range  = context.Range;
            var market = context.Market;
            var pair   = context.Pair;

            var limit = range.GetDistanceInResolutionTicks();
            var toTs  = range.UtcTo.GetSecondsSinceUnixEpoch();

            var api = GetApi <ICryptoCompareApi>();

            CryptoCompareSchema.HistoricListResult apir = null;

            switch (market)
            {
            case TimeResolution.Hour:
                apir = await api.GetHistoricalHourlyAsync(pair.Asset1.ToRemoteCode(this), pair.Asset2.ToRemoteCode(this), Name, "prime", "false", "true", 0, limit, toTs).ConfigureAwait(false);

                break;

            case TimeResolution.Day:
                apir = await api.GetHistoricalDayAsync(pair.Asset1.ToRemoteCode(this), pair.Asset2.ToRemoteCode(this), Name, "prime", "false", "true", 0, limit, toTs, "false").ConfigureAwait(false);

                break;

            case TimeResolution.Minute:
                apir = await api.GetHistoricalMinuteAsync(pair.Asset1.ToRemoteCode(this), pair.Asset2.ToRemoteCode(this), Name, "prime", "false", "true", 0, limit, toTs).ConfigureAwait(false);

                break;
            }

            if (apir.IsError())
            {
                return(null);
            }

            var r        = new OhlcData(market);
            var seriesid = OhlcUtilities.GetHash(pair, market, Network);
            var from     = apir.TimeFrom;
            var to       = apir.TimeTo;

            foreach (var i in apir.Data.Where(x => x.time >= from && x.time <= to))
            {
                var t = ((double)i.time).UnixTimestampToDateTime();

                r.Add(new OhlcEntry(seriesid, t, this)
                {
                    Open       = i.open,
                    Close      = i.close,
                    Low        = i.low,
                    High       = i.high,
                    VolumeFrom = i.volumefrom,
                    VolumeTo   = i.volumeto
                });
            }

            if (!string.IsNullOrWhiteSpace(apir.ConversionType.conversionSymbol))
            {
                r.ConvertedFrom = apir.ConversionType.conversionSymbol.ToAsset(this);
            }

            return(r);
        }
        private OhlcData Convert(TimeRange range)
        {
            Ctx.Status("Converting @" + Ctx.PrimaryApiProvider.Title + " " + Ctx.CurrencyConversionApiProvider.Title + " [1]");

            var pc = new OhlcContext(new AssetPair(Ctx.Pair.Asset1, Ctx.AssetIntermediary), Ctx.TimeResolution, range, L);
            var r1 = ApiCoordinator.GetOhlc(Ctx.PrimaryApiProvider, pc);

            if (r1.IsNull)
            {
                return(null);
            }

            var d1 = r1.Response;

            Ctx.Status("Converting @" + Ctx.PrimaryApiProvider.Title + " " + Ctx.CurrencyConversionApiProvider.Title + " [2]");

            var pc2 = new OhlcContext(new AssetPair(Ctx.AssetIntermediary, Ctx.Pair.Asset2), Ctx.TimeResolution, range, L);

            var r2 = ApiCoordinator.GetOhlc(Ctx.CurrencyConversionApiProvider, pc2);

            if (r2.IsNull)
            {
                return(null);
            }

            var d2 = r2.Response;

            if (d1.IsEmpty() || d2.IsEmpty())
            {
                return(null);
            }

            if (d1.Count != d2.Count)
            {
                return(null);
            }

            var ohcldata = new OhlcData(_adapter.TimeResolution)
            {
                ConvertedFrom = Ctx.AssetIntermediary,
                Network       = Ctx.PrimaryApiProvider.Network
            };

            var seriesid = OhlcUtilities.GetHash(Ctx.Pair, range.TimeResolution, ohcldata.Network);

            foreach (var i in d1)
            {
                var i2 = d2.FirstOrDefault(x => x.DateTimeUtc == i.DateTimeUtc);
                if (i2 == null)
                {
                    return(null);
                }

                ohcldata.Add(new OhlcEntry(seriesid, i.DateTimeUtc, Ctx.PrimaryApiProvider, Ctx.CurrencyConversionApiProvider, Ctx.AssetIntermediary)
                {
                    Open            = i.Open * i2.Open,
                    Close           = i.Close * i2.Close,
                    High            = i.High * i2.High,
                    Low             = i.Low * i2.Low,
                    VolumeTo        = 0,
                    VolumeFrom      = i2.VolumeFrom,
                    WeightedAverage = 0
                });
            }

            return(ohcldata);
        }