public List <Tuple <DateTime, double> > GetTimeSeries(ITimeSeriesKey itsk, bool isIndex, DateTime startDate) { List <Tuple <DateTime, double> > res = new List <Tuple <DateTime, double> >(); double value; double lastItemValue = Double.NaN; double lastTSValue = 10000; foreach (OHLC item in GetOHLCTimeSeries(itsk, StaticLibrary.DateTimeToUnixTimeStamp(startDate))) { DateTime itemTime = StaticLibrary.UnixTimeStampToDateTime(item.Time); itemTime = itsk.GetFrequency().Add(itemTime); if (itemTime > DateTime.UtcNow) { itemTime = new DateTime(9999, 1, 1); } if (itemTime > startDate) { if (!isIndex) { value = (double)item.Close; } else { value = Double.IsNaN(lastItemValue) ? lastTSValue : lastTSValue * (double)item.Close / lastItemValue; lastItemValue = (double)item.Close; lastTSValue = value; } res.Add(new Tuple <DateTime, double>(itemTime, value)); } } return(res); }
public List <Tuple <DateTime, double> > GetFXTimeSeries(ITimeSeriesKey itsk, DateTime startDate) { List <Tuple <DateTime, double> > res = new List <Tuple <DateTime, double> >(); CurrencyPairTimeSeries cpts = CurrencyPairTimeSeries.RequestIDToCurrencyPairTimeSeries(itsk.GetTimeSeriesKey()); if (!Data.CpList.Contains(cpts.CurPair)) { bool loadTs = !ReadFXHistory(cpts); if (!loadTs) { return(res); } } Frequency fq = itsk.GetFrequency(); if (fq.IsInferiorFrequency(Frequency.Day1)) { fq = Frequency.Day1; } List <DateTime> schedule = fq.GetSchedule(DateTime.UtcNow, ScheduleDepth).Where(x => x > startDate).ToList(); bool doSave = false; foreach (DateTime date in schedule) { var item = GetFullData(cpts.CurPair, date); doSave = doSave || item.Item2; res.Add(new Tuple <DateTime, double>(date, item.Item1)); } if (doSave) { WriteFXHistory(cpts); } return(res); }
public void AddTimeSeries(ITimeSeriesKey itsk, TimeSeries ts) { Dictionary[itsk.GetFullName()] = ts; //_globalMin = Double.IsNaN(_globalMin) ? ts.GetMin() : Math.Min(_globalMin, ts.GetMin()); //_globalMax = Double.IsNaN(_globalMax) ? ts.GetMax() : Math.Max(_globalMax, ts.GetMax()); StartDate = StartDate > ts.StartDate ? StartDate : ts.StartDate; EndDate = EndDate < ts.EndDate ? EndDate : ts.EndDate; }
/// <summary> /// Get OHLC timeseries (possibly post-processed into an index) /// </summary> /// <param name="itsk"></param> /// <param name="isIndex"></param> /// <returns></returns> public List <Tuple <DateTime, double> > GetTimeSeries(ITimeSeriesKey itsk, bool isIndex, DateTime startDate) { TimeSeriesKeyType type = itsk.GetKeyType(); if (type == TimeSeriesKeyType.CurrencyPair) { CurrencyPairTimeSeries cpts = CurrencyPairTimeSeries.RequestIDToCurrencyPairTimeSeries(itsk.GetTimeSeriesKey()); if (!cpts.IsFiatPair) { return(KrakenData.GetTimeSeries(itsk, isIndex, startDate)); } else { return(FXData.GetFXTimeSeries(itsk, startDate)); } } throw new NotImplementedException(); }
public List <Tuple <DateTime, double> > GetTimeSeries(ITimeSeriesKey itsk, bool isIndex, DateTime startDate) { List <Tuple <DateTime, double> > res = new List <Tuple <DateTime, double> >(); if (itsk.GetKeyType() == TimeSeriesKeyType.AllocationHistory) { double value; double lastTSValue = Double.NaN; Allocation prevAlloc = null; Currency ccyRef = itsk.GetCurrencyRef(); IEnumerable <DateTime> DateList = History.Keys.Where(x => x >= startDate); DateList = itsk.GetFrequency().GetSchedule(DateList.First(), DateList.Last(), true); foreach (DateTime date in DateList) { History.TryGetValue(date, out Allocation alloc); if (alloc != null) { if (!isIndex) { double amount = History[date].Total.Amount; value = amount; } else { if (Double.IsNaN(lastTSValue)) { value = 10000; } else { double returnAlloc = History[date].GetReturn(prevAlloc, ccyRef); value = Double.IsNaN(lastTSValue) ? 10000 : lastTSValue * (1 + returnAlloc); } prevAlloc = (Allocation)History[date].Clone(); lastTSValue = value; } res.Add(new Tuple <DateTime, double>(date, value)); } } } return(res); }
public ITimeSeriesProvider GetTimeSeriesProvider(ITimeSeriesKey itsk) { ITimeSeriesProvider iTSP = null; switch (itsk.GetKeyType()) { case TimeSeriesKeyType.None: break; case TimeSeriesKeyType.CurrencyPair: iTSP = DataProvider; break; case TimeSeriesKeyType.Allocation: case TimeSeriesKeyType.AllocationHistory: iTSP = AH; break; default: break; } return(iTSP); }
public List <Tuple <DateTime, double> > GetTimeSeries(ITimeSeriesKey itsk, bool isIndex, DateTime startDate) { List <Tuple <DateTime, double> > res = new List <Tuple <DateTime, double> >(); double value; double lastItemValue = Double.NaN; double lastTSValue = 10000; foreach (Tuple <DateTime, double> item in GetFXTimeSeries(itsk, startDate)) { if (!isIndex) { value = item.Item2; } else { value = Double.IsNaN(lastItemValue) ? lastTSValue : lastTSValue * item.Item2 / lastItemValue; lastItemValue = item.Item2; lastTSValue = value; } res.Add(new Tuple <DateTime, double>(item.Item1, value)); } return(res); }
/// <summary> /// Create, Update and Save OHLC data (as much as needed) /// </summary> /// <param name="itsk"></param> /// <param name="useLowerFrequencies"></param> public void LoadOHLC(ITimeSeriesKey itsk, bool useLowerFrequencies = false) { if (itsk.GetKeyType() == TimeSeriesKeyType.CurrencyPair) { CurrencyPairTimeSeries cpts = CurrencyPairTimeSeries.RequestIDToCurrencyPairTimeSeries(itsk.GetTimeSeriesKey()); if (!cpts.CurPair.IsIdentity) { if (cpts.Freq == Frequency.None) { cpts.Freq = SavingMinimumFrequency.GetNextFrequency(); } List <Frequency> freqList = (useLowerFrequencies && SaveableFrequency(cpts.Freq)) ? cpts.Freq.GetFrequencyList() : new List <Frequency> { cpts.Freq }; foreach (Frequency item in freqList) { CurrencyPairTimeSeries newCpts = (CurrencyPairTimeSeries)cpts.Clone(); newCpts.Freq = item; LoadOHLCCore(newCpts); } } } }
/// <summary> /// Get OHLC TimeSeries untreated /// </summary> /// <param name="itsk"></param> /// <returns></returns> private List <OHLC> GetOHLCTimeSeries(ITimeSeriesKey itsk, Int64?startDate = null, Int64?endDate = null) { Int64 startDateUnix;; if (!startDate.HasValue) { startDateUnix = StaticLibrary.DateTimeToUnixTimeStamp(new DateTime(2000, 1, 1)); } else { startDateUnix = startDate.Value; } Int64 endDateUnix; if (!endDate.HasValue) { endDateUnix = StaticLibrary.DateTimeToUnixTimeStamp(new DateTime(3000, 1, 1)); } else { endDateUnix = endDate.Value; } try { LoadOHLC(itsk); List <OHLC> res = OHLCData[itsk.GetTimeSeriesKey()]; int FreqInSecs = itsk.GetFrequency().GetFrequency(inSecs: true); res = res.Where(x => startDateUnix - FreqInSecs <= x.Time && x.Time < endDateUnix && x.Low > 0) .ToList(); if (res.Count() == 0) { return(GetOHLCTimeSeries(itsk.GetNextFrequency(), startDateUnix, endDateUnix)); } else if (res.First().Time <= startDateUnix) { return(res); } else { ITimeSeriesKey nextFreqTSK = itsk.GetNextFrequency(); if (nextFreqTSK.GetFrequency() == Frequency.None) { return new List <OHLC> { } } ; else { List <OHLC> prevRes = GetOHLCTimeSeries(nextFreqTSK, startDate, res.First().Time); prevRes.AddRange(res); return(prevRes); } } } catch (Exception e) { this.PublishWarning(e.Message); return(new List <OHLC> { }); } }
public ITimeSeries GetTimeSeries(ITimeSeriesKey itsk) { return Dictionary[itsk.GetFullName()]; }