Пример #1
0
        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);
        }
Пример #2
0
        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> > 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);
        }
Пример #4
0
        /// <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> {
                });
            }
        }