예제 #1
0
        /// <summary>
        /// Get the FX Market history implied by the OHLC timeseries
        /// </summary>
        /// <param name="fiat"></param>
        /// <param name="cpList"></param>
        /// <param name="startDate"></param>
        /// <param name="freq"></param>
        /// <returns></returns>
        public void UpdateFXMarketHistory(FXMarketHistory fxmh, Currency fiat, DateTime startDate,
                                          Frequency freq = Frequency.Hour4)
        {
            ResetReadFiles();
            // Need To Duplicate the market in order to have "clean" dates
            List <CurrencyPair> cpList   = new List <CurrencyPair>(fxmh.CpList);
            List <Currency>     fiatList = new List <Currency> {
            };

            foreach (CurrencyPair cp in cpList)
            {
                CurrencyPairTimeSeries cpts = new CurrencyPairTimeSeries(cp, freq);
                CryptoFiatPair         cfp  = cp.GetCryptoFiatPair;
                if (!cfp.IsNone)
                {
                    FillFXMarketHistory(fxmh, cpts, startDate);
                    if (cfp.Fiat != fiat)
                    {
                        CurrencyPairTimeSeries cpts2 = new CurrencyPairTimeSeries(cfp.Crypto, fiat, freq);
                        FillFXMarketHistory(fxmh, cpts2, startDate);
                        if (!fiatList.Contains(cfp.Fiat))
                        {
                            fiatList.Add(cfp.Fiat);
                        }
                    }
                }
            }
            foreach (Currency fiat_i in fiatList)
            {
                CurrencyPairTimeSeries cptsFiatPair = new CurrencyPairTimeSeries(fiat_i, fiat, freq);
                FillFXMarketHistory(fxmh, cptsFiatPair, startDate);
            }
        }
예제 #2
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);
        }
예제 #3
0
        /// <summary>
        /// Get the FX Market history implied by the OHLC timeseries
        /// </summary>
        /// <param name="fiat"></param>
        /// <param name="cpList"></param>
        /// <param name="startDate"></param>
        /// <param name="freq"></param>
        /// <returns></returns>
        public FXMarketHistory GetFXMarketHistory(Currency fiat, List <CurrencyPair> cpList, DateTime startDate,
                                                  Frequency freq = Frequency.Hour4)
        {
            // Need To Duplicate the market in order to have "clean" dates
            FXMarketHistory fxmh     = new FXMarketHistory(freq);
            List <Currency> fiatList = new List <Currency> {
            };

            foreach (CurrencyPair cp in cpList)
            {
                CurrencyPairTimeSeries cpts = new CurrencyPairTimeSeries(cp, freq);
                FillFXMarketHistory(fxmh, cpts, startDate);
                CryptoFiatPair cfp = cp.GetCryptoFiatPair;
                if (cfp.Fiat != fiat)
                {
                    CurrencyPairTimeSeries cpts2 = new CurrencyPairTimeSeries(cfp.Crypto, fiat, freq);
                    FillFXMarketHistory(fxmh, cpts2, startDate);
                    if (!fiatList.Contains(cfp.Fiat))
                    {
                        fiatList.Add(cfp.Fiat);
                    }
                }
            }
            foreach (Currency fiat_i in fiatList)
            {
                CurrencyPairTimeSeries cptsFiatPair = new CurrencyPairTimeSeries(fiat_i, fiat, freq);
                FillFXMarketHistory(fxmh, cptsFiatPair, startDate);
            }
            return(fxmh);
        }
예제 #4
0
        public void WriteFXHistory(CurrencyPairTimeSeries cpts)
        {
            string pathLib = GetFXLibraryPath(cpts);

            this.PublishInfo($"Saving FX: {cpts.CurPair.ToString()}");
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Time,Close");
            IEnumerable <DateTime> DateList = Data.GetAllDates();

            //DateList = cpts.Freq.GetSchedule(DateList.First(), DateList.Last(), true);
            foreach (DateTime date in DateList)
            {
                double close    = Data.GetQuote(date, cpts.CurPair).Item2.Rate;
                Int64  dateUnix = StaticLibrary.DateTimeToUnixTimeStamp(date);
                //if (StaticLibrary.UnixTimeStampToDateTime(dateUnix + FXMinimunFrequency.GetFrequency(true)) < DateTime.UtcNow)
                if (StaticLibrary.UnixTimeStampToDateTime(dateUnix) < DateTime.UtcNow)
                {
                    sb.AppendLine($"{dateUnix},{close}");
                }
                else
                {
                    this.PublishInfo($"Stopped at line: {StaticLibrary.UnixTimeStampToDateTime(dateUnix)}");
                }
            }
            File.WriteAllText(pathLib, sb.ToString());
        }
예제 #5
0
        public Dictionary <DateTime, double> LoadData(CurrencyPairTimeSeries cpts, int depth = 100)
        {
            if (File.Exists(GetFXLibraryPath(cpts)) && !Data.CpList.Contains(cpts.CurPair))
            {
                ReadFXHistory(cpts);
            }
            Dictionary <DateTime, double> dict = new Dictionary <DateTime, double> {
            };
            Frequency freq = cpts.Freq;

            if (freq < FXMinimunFrequency || freq == Frequency.None)
            {
                freq = FXMinimunFrequency;
            }
            List <DateTime> dateList = freq.GetSchedule(DateTime.UtcNow, depth);
            bool            doSave   = false;

            foreach (DateTime date in dateList)
            {
                var item = GetFullData(cpts.CurPair, date);
                doSave = doSave || item.Item2;
                dict.Add(date, item.Item1);
            }
            if (doSave)
            {
                WriteFXHistory(cpts);
            }
            return(dict);
        }
예제 #6
0
        /// <summary>
        /// Update and save new OHLC data if needed
        /// </summary>
        /// <param name="cpts"></param>
        private void UpdateAndSaving(CurrencyPairTimeSeries cpts)
        {
            bool doSave = true;

            if (OHLCData.ContainsKey(cpts.GetTimeSeriesKey()))
            {
                DateTime lastDate = StaticLibrary.UnixTimeStampToDateTime(OHLCData[cpts.GetTimeSeriesKey()].Last().Time);
                if (DateTime.UtcNow.Subtract(lastDate).TotalSeconds > 2 * cpts.Freq.GetFrequency(inSecs: true))
                {
                    if (UseInternet)
                    {
                        UpdateData(cpts);
                    }
                }
                else
                {
                    doSave = false;
                }
            }
            else
            {
                throw new NotImplementedException("You should have loaded the data before you got there (readed from csv or downloaded)");
            }
            if (doSave && SaveableFrequency(cpts.Freq))
            {
                SaveOHLC(cpts);
            }
        }
예제 #7
0
        /// <summary>
        /// Add to loaded OHLC data the new information from Kraken
        /// </summary>
        /// <param name="cpts"></param>
        private void UpdateData(CurrencyPairTimeSeries cpts)
        {
            string      cpID        = cpts.CurPair.GetRequestID();
            List <OHLC> newData     = GetKrakenOHLC(cpts.CurPair, cpts.Freq).Pairs[cpts.CurPair.GetRequestID()];
            List <OHLC> currentData = OHLCData[cpts.GetTimeSeriesKey()];
            int         lastDate    = currentData.Last().Time;

            foreach (OHLC item in newData)
            {
                if (lastDate < item.Time)
                {
                    currentData.Add(item);
                }
            }
        }
예제 #8
0
        /// <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();
        }
예제 #9
0
 public void LoadPrices(List <ITimeSeriesKey> TimeSeriesKeyList, bool useLowerFrequencies = false)
 {
     foreach (ITimeSeriesKey itsk in TimeSeriesKeyList)
     {
         if (itsk.GetKeyType() == TimeSeriesKeyType.CurrencyPair)
         {
             CurrencyPairTimeSeries cpts = CurrencyPairTimeSeries.RequestIDToCurrencyPairTimeSeries(itsk.GetTimeSeriesKey());
             if (!cpts.CurPair.IsIdentity)
             {
                 if (!cpts.CurPair.IsFiatPair)
                 {
                     KrakenData.LoadOHLC(itsk, useLowerFrequencies);
                 }
                 else
                 {
                     FXData.LoadData(cpts);
                 }
             }
         }
     }
 }
예제 #10
0
        /// <summary>
        /// Write additional OHLC information locally
        /// </summary>
        /// <param name="cpts"></param>
        private void SaveOHLC(CurrencyPairTimeSeries cpts)
        {
            string pathLib = GetOHLCLibraryPath(cpts.CurPair, cpts.Freq);

            this.PublishInfo($"Saving OHLC: {cpts.CurPair.ToString()} {cpts.Freq.ToString()}");
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Time,Open,High,Low,Close,Volume,Vwap,Count");
            foreach (OHLC item in OHLCData[cpts.GetTimeSeriesKey()])
            {
                //Console.WriteLine($"Saving data: {UnixTimeStampToDateTime(item.Time)}");
                if (StaticLibrary.UnixTimeStampToDateTime(item.Time + cpts.Freq.GetFrequency(true)) < DateTime.UtcNow)
                {
                    sb.AppendLine($"{item.Time},{item.Open},{item.High},{item.Low},{item.Close},{item.Volume},{item.Vwap},{item.Count}");
                }
                else
                {
                    this.PublishInfo($"Stopped at line: {StaticLibrary.UnixTimeStampToDateTime(item.Time)}");
                }
            }
            File.WriteAllText(pathLib, sb.ToString());
        }
예제 #11
0
        /// <summary>
        /// Auxiliary function in order to fill in the FX Market History (including the increasing Frequency feature)
        /// </summary>
        /// <param name="fxmh"></param>
        /// <param name="cpts"></param>
        /// <param name="startDate"></param>
        private void FillFXMarketHistory(FXMarketHistory fxmh, CurrencyPairTimeSeries cpts, DateTime startDate)
        {
            List <Tuple <DateTime, double> > ts = GetTimeSeries(cpts, isIndex: false, startDate: startDate);

            if (ts.Count > 0)
            {
                foreach (Tuple <DateTime, double> item in ts)
                {
                    fxmh.AddQuote(item.Item1, new XChangeRate(item.Item2, cpts.CurPair));
                }
            }
            DateTime firstDate = fxmh.GetFirstDate(cpts.CurPair);

            if (firstDate > startDate)
            {
                CurrencyPairTimeSeries newCpts = (CurrencyPairTimeSeries)cpts.Clone();
                newCpts.IncreaseFreq();
                if (newCpts.Freq != Frequency.None)
                {
                    FillFXMarketHistory(fxmh, newCpts, startDate);
                }
            }
        }
예제 #12
0
 /// <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);
             }
         }
     }
 }
예제 #13
0
        /// <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);
                }
            }
        }
예제 #14
0
        private bool ReadFXHistory(CurrencyPairTimeSeries cpts)
        {
            string pathLib = GetFXLibraryPath(cpts);

            if (!File.Exists(pathLib))
            {
                cpts    = cpts.GetCloneWithInverseCcyPair();
                pathLib = GetFXLibraryPath(cpts);
            }
            if (!File.Exists(pathLib))
            {
                return(false);
            }
            if (ReadFiles.Contains(cpts.GetFullName()))
            {
                return(true);
            }
            ReadFiles.Add(cpts.GetFullName());
            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);
                    Data.AddQuote(StaticLibrary.UnixTimeStampToDateTime(ohlc.Time), new XChangeRate((double)ohlc.Close, cpts.CurPair));
                }
            }
            return(false);
        }
예제 #15
0
 public string GetFXLibraryPath(CurrencyPairTimeSeries cpts)
 {
     return($"{FXPath}\\{cpts.CurPair.GetRequestID()}.csv");
 }