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());
        }
        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 void WriteFeesMemory()
 {
     foreach (Currency ccy in AvailableCryptoCurrencies)
     {
         if (UpdateMemory[ccy])
         {
             if (!IsAcceptedCryptoCurrency(ccy))
             {
                 throw new Exception($"This data provider does not access the data from his Blockchain: {ccy.ToFullName()}");
             }
             this.PublishInfo($"Writing Fees History {ccy.ToFullName()}");
             StringBuilder sb = new StringBuilder();
             sb.AppendLine("Time,RefId,Fees");
             foreach (DateTime tx_date in FeesMemory[ccy].Keys)
             {
                 var item = FeesMemory[ccy][tx_date];
                 sb.AppendLine($"{StaticLibrary.DateTimeToUnixTimeStamp(tx_date)},{item.Item1},{item.Item2.GetBtc()}");
             }
             File.WriteAllText(GetPath(ccy), sb.ToString());
             UpdateMemory[ccy] = false;
         }
     }
 }
        public BitcoinValue GetTransactionFees(Transaction tx, List <string> depositAddresses)
        {
            Currency ccy     = tx.Received.Ccy;
            DateTime?timeKey = GetTransactionInMemory(tx);

            if (timeKey.HasValue)
            {
                return(FeesMemory[ccy][timeKey.Value].Item2);
            }
            UpdateMemory[ccy] = true;
            decimal  res         = 0;
            long     tx_date_ref = StaticLibrary.DateTimeToUnixTimeStamp(tx.Date);
            DateTime?tx_date     = null;

            foreach (string address in depositAddresses)
            {
                List <ApiTx> tx_add = GetAddressTransactions(address);
                foreach (ApiTx item in tx_add)
                {
                    if (item.GetAmountFromAddress(address).GetBtc() == (decimal)tx.Received.Amount)
                    {
                        if (StaticLibrary.DateTimeDistTest(tx.Date, item.Time, 4))
                        {
                            res     = item.GetFees().GetBtc();
                            tx_date = item.Time;
                        }
                    }
                }
            }
            BitcoinValue bv = new BitcoinValue(res);

            if (tx_date.HasValue)
            {
                FeesMemory[ccy][tx_date.Value] = new Tuple <string, BitcoinValue>(tx.ID, bv);
            }
            return(bv);
        }
        /// <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> {
                });
            }
        }