/// <summary> /// Read already downloaded OHLC data /// </summary> /// <param name="curPair"></param> /// <param name="freq"></param> /// <param name="item"></param> /// <returns></returns> private GetOHLCResult ReadOHLC(CurrencyPair curPair, Frequency freq, string item = "Close") { string pathLib = GetOHLCLibraryPath(curPair, freq); GetOHLCResult res = new GetOHLCResult { Pairs = new Dictionary <string, List <OHLC> > { { curPair.GetRequestID(), new List <OHLC> { } } } }; List <string[]> csv = StaticLibrary.LoadCsvFile(pathLib); bool isHeaders = true; string[] headers = null; foreach (string[] array in csv) { if (isHeaders) { headers = array; isHeaders = false; } else { OHLC ohlc = DataLibraryStaticLibrary.ReadOHLCItems(array, headers); res.Pairs[curPair.GetRequestID()].Add(ohlc); } } return(res); }
public void CurPair_RequestID() { CurrencyPair cp = new CurrencyPair(Currency.EUR, Currency.BCH); string reqID = cp.GetRequestID(); CurrencyPair cp2 = CurrencyPair.RequestIDToCurrencyPair(reqID); Assert.IsTrue(cp.Equals(cp2)); }
/// <summary> /// Request the OHLC data from Kraken (looping requests if needed) /// </summary> /// <param name="curPair"></param> /// <param name="freq"></param> /// <param name="count"></param> /// <returns></returns> private GetOHLCResult GetKrakenOHLC(CurrencyPair curPair, Frequency freq = Frequency.Hour4, int count = 10) { this.PublishInfo($"Kraken API Request : OHLC {curPair.ToString()} - {freq.ToString()}"); try { return(KrakenApi.GetOHLC(curPair.GetRequestID(), freq.GetFrequency())); } catch (System.Net.WebException wex) { this.PublishError(wex.Message); // No Internet => wex.Message = "The remote name could not be resolved: 'api.kraken.com'" count--; if (count < 1) { throw new Exception($"Unable to Download Krarken OHLC for: {curPair.ToString()}"); } else { System.Threading.Thread.Sleep(5000); } return(GetKrakenOHLC(curPair, freq, count)); } }
public string GetTimeSeriesKey() { return($"{CurPair.GetRequestID()}{Sep}{Freq.ToString()}"); }
public string GetOHLCLibraryPath(CurrencyPair curPair, Frequency freq) { return($"{Path}OHLC\\{curPair.GetRequestID()}_{freq.GetFrequency()}.csv"); }
/// <summary> /// Create, Update and Save OHLC data as much as needed /// </summary> /// <param name="cpts"></param> private void LoadOHLCCore(CurrencyPairTimeSeries cpts) { CurrencyPair ccyPair = cpts.CurPair; Frequency freq = cpts.Freq; if (OHLCData.ContainsKey(cpts.GetTimeSeriesKey())) { UpdateAndSaving(cpts); } else { if (SaveableFrequency(freq) && File.Exists(GetOHLCLibraryPath(ccyPair, freq))) { OHLCData[cpts.GetTimeSeriesKey()] = ReadOHLC(ccyPair, freq).Pairs[ccyPair.GetRequestID()]; UpdateAndSaving(cpts); } else { OHLCData[cpts.GetTimeSeriesKey()] = GetKrakenOHLC(ccyPair, freq).Pairs[ccyPair.GetRequestID()]; SaveOHLC(cpts); } } }