private List <MarketCandle> ParseMarketCandle(JToken array)
        {
            List <MarketCandle> candles = new List <MarketCandle>();

            foreach (JArray item in array)
            {
                MarketCandle candle = new MarketCandle
                {
                    Timestamp          = CryptoUtility.UnixTimeStampToDateTimeSeconds(item[0].ConvertInvariant <long>()),
                    OpenPrice          = item[1].ConvertInvariant <decimal>(),
                    HighPrice          = item[2].ConvertInvariant <decimal>(),
                    LowPrice           = item[3].ConvertInvariant <decimal>(),
                    ClosePrice         = item[4].ConvertInvariant <decimal>(),
                    BaseCurrencyVolume = item[5].ConvertInvariant <double>()
                };

                candles.Add(candle);
            }

            return(candles);
        }
Exemple #2
0
        /// <summary>
        /// Get market candles
        /// </summary>
        /// <param name="exchange">Exchange name</param>
        /// <param name="marketName">Market name</param>
        /// <param name="before">Optional date to restrict data to before this date</param>
        /// <param name="after">Optional date to restrict data to after this date</param>
        /// <param name="periods">Periods</param>
        /// <returns>Market candles</returns>
        public async Task <IEnumerable <MarketCandle> > GetMarketCandlesAsync(string exchange, string marketName, DateTime?before, DateTime?after, params int[] periods)
        {
            await new SynchronizationContextRemover();

            List <MarketCandle> candles = new List <MarketCandle>();
            string periodString         = string.Join(",", periods);
            string beforeDateString     = (before == null ? string.Empty : "&before=" + (long)before.Value.UnixTimestampFromDateTimeSeconds());
            string afterDateString      = (after == null ? string.Empty : "&after=" + (long)after.Value.UnixTimestampFromDateTimeSeconds());
            string url   = "/markets/" + exchange + "/" + marketName + "/ohlc?periods=" + periodString + beforeDateString + afterDateString;
            JToken token = await MakeCryptowatchRequestAsync(url);

            foreach (JProperty prop in token)
            {
                foreach (JToken candleToken in prop.Value)
                {
                    MarketCandle candle = this.ParseCandle(candleToken, marketName, 0, 1, 2, 3, 4, 0, TimestampType.UnixSeconds, 5);
                    candle.PeriodSeconds = prop.Name.ConvertInvariant <int>();
                    candles.Add(candle);
                }
            }

            return(candles);
        }
Exemple #3
0
        protected override async Task <IEnumerable <MarketCandle> > OnGetCandlesAsync(string symbol, int periodSeconds, DateTime?startDate = null, DateTime?endDate = null, int?limit = null)
        {
            List <MarketCandle> candles = new List <MarketCandle>();
            string periodString         = PeriodSecondsToString(periodSeconds);

            limit     = limit ?? (limit > 2160 ? 2160 : limit);
            endDate   = endDate ?? DateTime.UtcNow.AddMinutes(1.0);
            startDate = startDate ?? endDate.Value.Subtract(TimeSpan.FromDays(1.0));

            //market period(15m, 20m, 30m, 1h, 2h, 3h, 4h, 6h, 8h, 12h, 1d) count(default: 1000, max: 999999) lasthours(default: 24, max: 2160)
            //"result":[{"TimeStamp":"2014-07-31 10:15:00","Open":"0.00000048","High":"0.00000050","Low":"0.00000048","Close":"0.00000049","Volume":"594804.73036048","BaseVolume":"0.11510368" }, ...
            JToken result = await MakeJsonRequestAsync <JToken>("/public/getcandles?market=" + symbol + "&period=" + periodString + (limit == null ? string.Empty : "&lasthours=" + limit));

            foreach (JToken jsonCandle in result)
            {
                MarketCandle candle = this.ParseCandle(jsonCandle, symbol, periodSeconds, "Open", "High", "Low", "Close", "Timestamp", TimestampType.Iso8601, "BaseVolume", "Volume");
                if (candle.Timestamp >= startDate && candle.Timestamp <= endDate)
                {
                    candles.Add(candle);
                }
            }
            return(candles);
        }
        public override IEnumerable <MarketCandle> GetCandles(string symbol, int periodSeconds, DateTime?startDate = null, DateTime?endDate = null)
        {
            // https://api.kraken.com/0/public/OHLC
            // pair = asset pair to get OHLC data for, interval = time frame interval in minutes(optional):, 1(default), 5, 15, 30, 60, 240, 1440, 10080, 21600, since = return committed OHLC data since given id(optional.exclusive)
            // array of array entries(<time>, <open>, <high>, <low>, <close>, <vwap>, <volume>, <count>)
            symbol    = NormalizeSymbol(symbol);
            startDate = startDate ?? DateTime.UtcNow.Subtract(TimeSpan.FromDays(1.0));
            endDate   = endDate ?? DateTime.UtcNow;
            JObject json = MakeJsonRequest <JObject>("/0/public/OHLC?pair=" + symbol + "&interval=" + periodSeconds / 60 + "&since=" + startDate);

            CheckError(json);
            if (json["result"].Children().Count() != 0)
            {
                JProperty prop = json["result"].Children().First() as JProperty;
                foreach (JArray jsonCandle in prop.Value)
                {
                    MarketCandle candle = new MarketCandle
                    {
                        ClosePrice      = jsonCandle[4].ConvertInvariant <decimal>(),
                        ExchangeName    = Name,
                        HighPrice       = jsonCandle[2].ConvertInvariant <decimal>(),
                        LowPrice        = jsonCandle[3].ConvertInvariant <decimal>(),
                        Name            = symbol,
                        OpenPrice       = jsonCandle[1].ConvertInvariant <decimal>(),
                        PeriodSeconds   = periodSeconds,
                        Timestamp       = CryptoUtility.UnixTimeStampToDateTimeSeconds(jsonCandle[0].ConvertInvariant <long>()),
                        VolumePrice     = jsonCandle[6].ConvertInvariant <double>(),
                        VolumeQuantity  = jsonCandle[6].ConvertInvariant <double>() * jsonCandle[4].ConvertInvariant <double>(),
                        WeightedAverage = jsonCandle[5].ConvertInvariant <decimal>()
                    };
                    if (candle.Timestamp >= startDate.Value && candle.Timestamp <= endDate.Value)
                    {
                        yield return(candle);
                    }
                }
            }
        }
        /// <summary>
        /// Parse market candle from JToken
        /// </summary>
        /// <param name="named">Named item</param>
        /// <param name="token">JToken</param>
        /// <param name="marketSymbol">Symbol</param>
        /// <param name="periodSeconds">Period seconds</param>
        /// <param name="openKey">Open key</param>
        /// <param name="highKey">High key</param>
        /// <param name="lowKey">Low key</param>
        /// <param name="closeKey">Close key</param>
        /// <param name="timestampKey">Timestamp key</param>
        /// <param name="timestampType">Timestamp type</param>
        /// <param name="baseVolumeKey">Base currency volume key</param>
        /// <param name="quoteVolumeKey">Quote currency volume key</param>
        /// <param name="weightedAverageKey">Weighted average key</param>
        /// <returns>MarketCandle</returns>
        internal static MarketCandle ParseCandle(this INamed named, JToken token, string marketSymbol, int periodSeconds, object openKey, object highKey, object lowKey,
                                                 object closeKey, object timestampKey, TimestampType timestampType, object baseVolumeKey, object quoteVolumeKey = null, object weightedAverageKey = null)
        {
            MarketCandle candle = new MarketCandle
            {
                ClosePrice    = token[closeKey].ConvertInvariant <decimal>(),
                ExchangeName  = named.Name,
                HighPrice     = token[highKey].ConvertInvariant <decimal>(),
                LowPrice      = token[lowKey].ConvertInvariant <decimal>(),
                Name          = marketSymbol,
                OpenPrice     = token[openKey].ConvertInvariant <decimal>(),
                PeriodSeconds = periodSeconds,
                Timestamp     = CryptoUtility.ParseTimestamp(token[timestampKey], timestampType)
            };

            token.ParseVolumes(baseVolumeKey, quoteVolumeKey, candle.ClosePrice, out decimal baseVolume, out decimal convertVolume);
            candle.BaseCurrencyVolume  = (double)baseVolume;
            candle.QuoteCurrencyVolume = (double)convertVolume;
            if (weightedAverageKey != null)
            {
                candle.WeightedAverage = token[weightedAverageKey].ConvertInvariant <decimal>();
            }
            return(candle);
        }
Exemple #6
0
        protected override async Task <IEnumerable <MarketCandle> > OnGetCandlesAsync(string symbol, int periodSeconds, DateTime?startDate = null, DateTime?endDate = null, int?limit = null)
        {
            List <MarketCandle> candles = new List <MarketCandle>();
            string periodString;

            if (periodSeconds <= 900)
            {
                periodString = "15m"; periodSeconds = 900;
            }
            else if (periodSeconds <= 1200)
            {
                periodString = "20m"; periodSeconds = 1200;
            }
            else if (periodSeconds <= 1800)
            {
                periodString = "30m"; periodSeconds = 1800;
            }
            else if (periodSeconds <= 3600)
            {
                periodString = "1h"; periodSeconds = 3600;
            }
            else if (periodSeconds <= 7200)
            {
                periodString = "2h"; periodSeconds = 7200;
            }
            else if (periodSeconds <= 10800)
            {
                periodString = "3h"; periodSeconds = 10800;
            }
            else if (periodSeconds <= 14400)
            {
                periodString = "4h"; periodSeconds = 14400;
            }
            else if (periodSeconds <= 21600)
            {
                periodString = "6h"; periodSeconds = 21600;
            }
            else if (periodSeconds <= 43200)
            {
                periodString = "12h"; periodSeconds = 43200;
            }
            else
            {
                periodString = "1d"; periodSeconds = 86400;
            }

            limit     = limit ?? (limit > 2160 ? 2160 : limit);
            symbol    = NormalizeSymbol(symbol);
            endDate   = endDate ?? DateTime.UtcNow;
            startDate = startDate ?? endDate.Value.Subtract(TimeSpan.FromDays(1.0));

            //market period(15m, 20m, 30m, 1h, 2h, 3h, 4h, 6h, 8h, 12h, 1d) count(default: 1000, max: 999999) lasthours(default: 24, max: 2160)
            //"result":[{"TimeStamp":"2014-07-31 10:15:00","Open":"0.00000048","High":"0.00000050","Low":"0.00000048","Close":"0.00000049","Volume":"594804.73036048","BaseVolume":"0.11510368" }, ...
            JToken result = await MakeJsonRequestAsync <JObject>("/public/getcandles?market=" + symbol + "&period=" + periodString + (limit == null ? string.Empty : "&lasthours=" + limit));

            result = CheckError(result);
            foreach (JToken jsonCandle in result)
            {
                MarketCandle candle = new MarketCandle
                {
                    ExchangeName   = this.Name,
                    Name           = symbol,
                    Timestamp      = jsonCandle["TimeStamp"].ConvertInvariant <DateTime>(),
                    OpenPrice      = jsonCandle["Open"].ConvertInvariant <decimal>(),
                    HighPrice      = jsonCandle["High"].ConvertInvariant <decimal>(),
                    LowPrice       = jsonCandle["Low"].ConvertInvariant <decimal>(),
                    ClosePrice     = jsonCandle["Close"].ConvertInvariant <decimal>(),
                    PeriodSeconds  = periodSeconds,
                    VolumePrice    = jsonCandle["BaseVolume"].ConvertInvariant <double>(),
                    VolumeQuantity = jsonCandle["Volume"].ConvertInvariant <double>()
                };
                if (candle.Timestamp >= startDate && candle.Timestamp <= endDate)
                {
                    candles.Add(candle);
                }
            }
            return(candles);
        }
Exemple #7
0
        public override IEnumerable <MarketCandle> GetCandles(string symbol, int periodSeconds, DateTime?startDate = null, DateTime?endDate = null, int?limit = null)
        {
            if (limit != null)
            {
                throw new APIException("Limit parameter not supported");
            }

            // https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-WAVES&tickInterval=day
            // "{"success":true,"message":"","result":[{"O":0.00011000,"H":0.00060000,"L":0.00011000,"C":0.00039500,"V":5904999.37958770,"T":"2016-06-20T00:00:00","BV":2212.16809610} ] }"
            string periodString;

            switch (periodSeconds)
            {
            case 60: periodString = "oneMin"; break;

            case 300: periodString = "fiveMin"; break;

            case 1800: periodString = "thirtyMin"; break;

            case 3600: periodString = "hour"; break;

            case 86400: periodString = "day"; break;

            case 259200: periodString = "threeDay"; break;

            case 604800: periodString = "week"; break;

            default:
                if (periodSeconds > 604800)
                {
                    periodString = "month";
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Period seconds must be 60,300,1800,3600,86400, 259200 or 604800");
                }
                break;
            }
            symbol    = NormalizeSymbol(symbol);
            endDate   = endDate ?? DateTime.UtcNow;
            startDate = startDate ?? endDate.Value.Subtract(TimeSpan.FromDays(1.0));
            JToken result = MakeJsonRequest <JToken>("pub/market/GetTicks?marketName=" + symbol + "&tickInterval=" + periodString, BaseUrl2);

            result = CheckError(result);
            if (result is JArray array)
            {
                foreach (JToken jsonCandle in array)
                {
                    MarketCandle candle = new MarketCandle
                    {
                        ClosePrice     = jsonCandle["C"].ConvertInvariant <decimal>(),
                        ExchangeName   = Name,
                        HighPrice      = jsonCandle["H"].ConvertInvariant <decimal>(),
                        LowPrice       = jsonCandle["L"].ConvertInvariant <decimal>(),
                        Name           = symbol,
                        OpenPrice      = jsonCandle["O"].ConvertInvariant <decimal>(),
                        PeriodSeconds  = periodSeconds,
                        Timestamp      = jsonCandle["T"].ConvertInvariant <DateTime>(),
                        VolumePrice    = jsonCandle["BV"].ConvertInvariant <double>(),
                        VolumeQuantity = jsonCandle["V"].ConvertInvariant <double>()
                    };
                    if (candle.Timestamp >= startDate && candle.Timestamp <= endDate)
                    {
                        yield return(candle);
                    }
                }
            }
        }
Exemple #8
0
        public CandleEvent Convert(ExchangeSharp.MarketCandle foreignCandle)
        {
            var candle = new Candle(foreignCandle);

            return(new CandleEvent(candle, foreignCandle.Timestamp.ToNodaTime().ToUnixTimeTicks()));
        }