Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_pair"></param>
        /// <param name="_type"></param>
        /// <param name="_values">0:limit 1:start</param>
        /// <returns></returns>
        public override KLine[] GetKLines(string _pair, KLineType _type, params string[] _values)
        {
            string _typeText = "";

            switch (_type)
            {
            case KLineType.M1: _typeText = "1m"; break;

            case KLineType.M5: _typeText = "5m"; break;

            case KLineType.H1: _typeText = "h"; break;

            case KLineType.D1: _typeText = "1d"; break;

            default: throw new Exception($"KLine type:{_type.ToString()} not supported.");
            }

            string _url = $"/api/v1/trade/bucketed?binSize={_typeText}&partial=true&symbol={_pair}&count={_values[0]}&reverse=true";

            if (_values.Length > 1)
            {
                _url += $"&start={_values[1]}";
            }

            JToken _token = base.HttpCall(HttpCallMethod.Get, "GET", _url);

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

            IList <KLine> _result = new List <KLine>();
            JArray        _trades = _token.Value <JArray>();

            foreach (JToken _item in _trades)
            {
                KLine _line = new KLine();
                _line.DateTime = _item["timestamp"].Value <DateTime>();
                _line.Pair     = _pair;
                _line.Open     = _item["open"].Value <decimal>();
                _line.Close    = _item["close"].Value <decimal>();
                _line.High     = _item["high"].Value <decimal>();
                _line.Low      = _item["low"].Value <decimal>();
                _line.Volume   = _item["volume"].Value <decimal>();
                _line.Volume2  = _item["homeNotional"].Value <decimal>();
                _line.Count    = _item["trades"].Value <decimal>();

                _result.Add(_line);
            }

            return(_result.ToArray());
        }
Esempio n. 2
0
        public override KLine[] GetKLines(string _pair, KLineType _type, params string[] _values)
        {
            string _typeText = "";

            switch (_type)
            {
            case KLineType.M1: _typeText = "1"; break;

            case KLineType.H1: _typeText = "60"; break;

            case KLineType.D1: _typeText = "D"; break;

            default: throw new Exception($"KLine type:{_type.ToString()} not supported.");
            }

            string _url = $"/GET/v1/api/kline?pair={_pair}&type={_typeText}";

            if (_values.Length > 0)
            {
                _url += $"&count={_values[0]}";
            }

            JToken _token = base.HttpCall(HttpCallMethod.Get, "GET", _url);

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

            IList <KLine> _result = new List <KLine>();
            JArray        _trades = _token.Value <JArray>();

            foreach (JToken _item in _trades)
            {
                KLine _line = new KLine();
                _line.DateTime = DateTimePlus.JSTime2DateTime(_token[0].Value <long>() / 1000);
                _line.Pair     = _pair;
                _line.Open     = _item[1].Value <decimal>();
                _line.Close    = _item[4].Value <decimal>();
                _line.High     = _item[2].Value <decimal>();
                _line.Low      = _item[3].Value <decimal>();
                _line.Volume   = _item[5].Value <decimal>();

                _result.Add(_line);
            }

            return(_result.ToArray());
        }
Esempio n. 3
0
        internal static string GetKLineDataFilePath(string stockCode, KLineType kLineType, DateTime dt)
        {
            //方案1
            return string.Format(@"{0}\{1}\{2}\{3}.dat", StockKLineFolder, stockCode, kLineType.ToString(), dt.ToString("yyyy"));

            //方案2
            //string filepath = null;
            //switch (kLineType)
            //{
            //    /// 日线
            //    case KLineType.Daily:
            //        filepath = string.Format(@"{0}\{1}\{2}\{3}.dat", StockKLineFolder, stockCode, kLineType.ToString(), dt.ToString("yyyy"));
            //        break;
            //    /// 1分钟线
            //    case KLineType.Min1:
            //    /// 5分钟线
            //    case KLineType.Min5:
            //        filepath = string.Format(@"{0}\{1}\{2}\{3}.dat", StockKLineFolder, stockCode, kLineType.ToString(), dt.ToString("yyyyMM"));
            //        break;
            //}
            //return filepath;
        }
Esempio n. 4
0
        public override KLine[] GetKLines(string _pair, KLineType _type, params string[] _values)
        {
            string _typeText = "";

            switch (_type)
            {
            case KLineType.M1: _typeText = "M1"; break;

            case KLineType.M5: _typeText = "M5"; break;

            case KLineType.M15: _typeText = "M15"; break;

            case KLineType.M30: _typeText = "M30"; break;

            case KLineType.H1: _typeText = "H1"; break;

            case KLineType.H4: _typeText = "H4"; break;

            case KLineType.H6: _typeText = "H6"; break;

            case KLineType.D1: _typeText = "D1"; break;

            case KLineType.D7: _typeText = "W1"; break;

            case KLineType.MM: _typeText = "WN"; break;

            default: throw new Exception($"KLine type:{_type.ToString()} not supported.");
            }

            string _url = $"/v2/market/candles/{_typeText}/{_pair}";

            if (_values.Length > 0)
            {
                _url += $"?limit={_values[0]}";
            }
            if (_values.Length > 1)
            {
                _url += $"&before={_values[1]}";
            }

            JToken _token = base.HttpCall(HttpCallMethod.Get, "GET", _url);

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

            IList <KLine> _result = new List <KLine>();
            JArray        _trades = _token.Value <JArray>();

            foreach (JToken _item in _trades)
            {
                KLine _line = new KLine();
                _line.DateTime = DateTimePlus.JSTime2DateTime(_item["id"].Value <long>() / 1000);
                _line.Pair     = _pair;
                _line.Open     = _item["open"].Value <decimal>();
                _line.Close    = _item["close"].Value <decimal>();
                _line.High     = _item["high"].Value <decimal>();
                _line.Low      = _item["low"].Value <decimal>();
                _line.Count    = _item["count"].Value <decimal>();
                _line.Volume   = _item["base_vol"].Value <decimal>();
                _line.Volume2  = _item["quote_vol"].Value <decimal>();

                _result.Add(_line);
            }

            return(_result.ToArray());
        }
Esempio n. 5
0
        /// <summary>
        /// GetKLines
        /// </summary>
        /// <param name="_pair"></param>
        /// <param name="_type"></param>
        /// <param name="_values">0:limit 1:sort 2:start 3:end</param>
        /// <returns></returns>
        public override KLine[] GetKLines(string _pair, KLineType _type, params string[] _values)
        {
            string _typeText = "";

            switch (_type)
            {
            case KLineType.M1: _typeText = "1m"; break;

            case KLineType.M5: _typeText = "5m"; break;

            case KLineType.M15: _typeText = "15m"; break;

            case KLineType.M30: _typeText = "30m"; break;

            case KLineType.H1: _typeText = "1h"; break;

            case KLineType.H6: _typeText = "6h"; break;

            case KLineType.H12: _typeText = "12h"; break;

            case KLineType.D1: _typeText = "1D"; break;

            case KLineType.D7: _typeText = "7D"; break;

            case KLineType.D14: _typeText = "14D"; break;

            case KLineType.MM: _typeText = "1M"; break;

            default: throw new Exception($"KLine type:{_type.ToString()} not supported.");
            }

            string _url = $"/v2/candles/trade:{_typeText}:t{_pair.ToUpper()}/hist";

            if (_values.Length > 0)
            {
                _url += $"?limit={_values[0]}";
            }
            if (_values.Length > 1)
            {
                _url += $"&sort={_values[1]}";
            }
            if (_values.Length > 2)
            {
                _url += $"&start={_values[2]}";
            }
            if (_values.Length > 4)
            {
                _url += $"&end={_values[3]}";
            }

            JToken _token = base.HttpCall(HttpCallMethod.Get, "GET", _url);

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

            IList <KLine> _result = new List <KLine>();
            JArray        _trades = _token.Value <JArray>();

            foreach (JToken _item in _trades)
            {
                KLine _line = new KLine();
                _line.DateTime = DateTimePlus.JSTime2DateTime(_item[0].Value <long>() / 1000);
                _line.Pair     = _pair;
                _line.Open     = _item[1].Value <decimal>();
                _line.Close    = _item[2].Value <decimal>();
                _line.High     = _item[3].Value <decimal>();
                _line.Low      = _item[4].Value <decimal>();
                _line.Volume   = _item[5].Value <decimal>();

                _result.Add(_line);
            }

            return(_result.ToArray());
        }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_pair"></param>
        /// <param name="_type"></param>
        /// <param name="_values">0:size</param>
        /// <returns></returns>
        public override KLine[] GetKLines(string _pair, KLineType _type, params string[] _values)
        {
            string _typeText = "";

            switch (_type)
            {
            case KLineType.M1: _typeText = "1min"; break;

            case KLineType.M5: _typeText = "5min"; break;

            case KLineType.M15: _typeText = "15min"; break;

            case KLineType.M30: _typeText = "30min"; break;

            case KLineType.H1: _typeText = "60min"; break;

            case KLineType.D1: _typeText = "1day"; break;

            case KLineType.D7: _typeText = "1week"; break;

            case KLineType.MM: _typeText = "1mon"; break;

            case KLineType.YY: _typeText = "1year"; break;

            default: throw new Exception($"KLine type:{_type.ToString()} not supported.");
            }

            string _url = $" /market/history/kline?symbol={_pair}&period={_typeText}";

            if (_values.Length > 0)
            {
                _url += $"&size={_values[0]}";
            }

            JToken _token = base.HttpCall(HttpCallMethod.Get, "GET", _url);

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

            IList <KLine> _result = new List <KLine>();
            JArray        _trades = _token["data"].Value <JArray>();

            foreach (JToken _item in _trades)
            {
                KLine _line = new KLine();
                _line.DateTime = DateTimePlus.JSTime2DateTime(_token["id"].Value <long>() / 1000);
                _line.Pair     = _pair;
                _line.Open     = _item["open"].Value <decimal>();
                _line.Close    = _item["close"].Value <decimal>();
                _line.High     = _item["high"].Value <decimal>();
                _line.Low      = _item["low"].Value <decimal>();
                _line.Count    = _item["count"].Value <decimal>();
                _line.Volume   = _item["vol"].Value <decimal>();
                _line.Volume2  = _item["amount"].Value <decimal>();

                _result.Add(_line);
            }

            return(_result.ToArray());
        }
Esempio n. 7
0
        public override KLine[] GetKLines(string _pair, KLineType _type, params string[] _values)
        {
            string _typeText = "";

            switch (_type)
            {
            case KLineType.M1: _typeText = "1min"; break;

            case KLineType.M5: _typeText = "5min"; break;

            case KLineType.M15: _typeText = "15min"; break;

            case KLineType.M30: _typeText = "30min"; break;

            case KLineType.H1: _typeText = "1hour"; break;

            case KLineType.H4: _typeText = "4hour"; break;

            case KLineType.H6: _typeText = "6hour"; break;

            case KLineType.D1: _typeText = "1day"; break;

            case KLineType.D7: _typeText = "1week"; break;

            default: throw new Exception($"KLine type:{_type.ToString()} not supported.");
            }

            string _url = $"/v1/market/kline?market={_pair}&type={_typeText}";

            if (_values.Length > 0)
            {
                _url += $"?limit={_values[0]}";
            }
            if (_values.Length > 1)
            {
                _url += $"&before={_values[1]}";
            }

            JToken _token = base.HttpCall(HttpCallMethod.Get, "GET", _url);

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

            IList <KLine> _result = new List <KLine>();
            JArray        _trades = _token.Value <JArray>();

            foreach (JToken _item in _trades)
            {
                KLine _line = new KLine();
                _line.DateTime = DateTimePlus.JSTime2DateTime(_item[0].Value <long>());
                _line.Pair     = _pair;
                _line.Open     = _item[1].Value <decimal>();
                _line.Close    = _item[2].Value <decimal>();
                _line.High     = _item[3].Value <decimal>();
                _line.Low      = _item[4].Value <decimal>();
                _line.Volume   = _item[5].Value <decimal>();
                _line.Volume2  = _item[6].Value <decimal>();

                _result.Add(_line);
            }

            return(_result.ToArray());
        }
Esempio n. 8
0
        internal static IEnumerable<string> GetKLineDataFilePath(string stockCode, KLineType kLineType, DateTime bgnDt, DateTime endDt)
        {
            if (bgnDt > endDt)
            {
                throw new ArgumentOutOfRangeException("startDay", "startDay is less than endDay");
            }

            if (bgnDt == endDt)
            {
                return new List<string>() { GetKLineDataFilePath(stockCode, kLineType, bgnDt) };
            }

            List<string> result = new List<string>();
            string fileFolder = string.Format(@"{0}\{1}\{2}", StockKLineFolder, stockCode, kLineType.ToString());
            //方案1
            while (bgnDt.Year <= endDt.Year)
            {
                result.Add(fileFolder + string.Format(@"\{0}.dat", bgnDt.ToString("yyyy")));
                bgnDt = bgnDt.AddYears(1);
            }
            //方案2
            //switch (kLineType)
            //{
            //    /// 日线
            //    case KLineType.Daily:
            //        while (bgnDt.Year <= endDt.Year)
            //        {
            //            result.Add(fileFolder + string.Format(@"\{0}.dat", bgnDt.ToString("yyyy")));
            //            bgnDt = bgnDt.AddYears(1);
            //        }
            //        break;
            //    /// 1分钟线
            //    case KLineType.Min1:
            //    /// 5分钟线
            //    case KLineType.Min5:
            //        while ((12 * bgnDt.Year + bgnDt.Month) <= (12 * endDt.Year + endDt.Month))
            //        {
            //            result.Add(fileFolder + string.Format(@"\{0}.dat", bgnDt.ToString("yyyyMM")));
            //            bgnDt = bgnDt.AddMonths(1);
            //        }
            //        break;
            //}

            return result;
        }
Esempio n. 9
0
        internal static bool GetLatestKLineDataFilePath(string stockCode, KLineType kLineType, ref string latestFilePath)
        {
            latestFilePath = string.Empty;

            string todayFilePath = GetKLineDataFilePath(stockCode, kLineType, DateTime.Now);
            if (File.Exists(todayFilePath))
            {
                latestFilePath = todayFilePath;
                return true;
            }

            string fileFolder = string.Format(@"{0}\{1}\{2}", StockKLineFolder, stockCode, kLineType.ToString());
            if (Directory.Exists(fileFolder))
            {
                DirectoryInfo dir = new DirectoryInfo(fileFolder);
                FileInfo file = dir.GetFiles().OrderBy(p => p.CreationTime).LastOrDefault();
                if (file != null)
                {
                    latestFilePath = file.FullName;
                    return true;
                }
            }

            return false;
        }
Esempio n. 10
0
        private bool FolderExists(string stockCode, KLineType kLineType)
        {
            string folderpath = string.Format(@"{0}\{1}\{2}\", PathHelper.StockKLineFolder, stockCode, kLineType.ToString());

            return System.IO.Directory.Exists(folderpath);
        }