private void BuildHistoricalCacheForInstrument(Instrument InstrumentToCache)
        {
            HistoricalQuote oH = new HistoricalQuote(null, null, null);
            DateTime        oDataFromNeeded          = DateTime.Now;
            bool            bReadMore                = false;
            bool            bAllHistoricalDataNeeded = true;

            string FullPath = GetFullPathOfHistoricalDataForInstrument(InstrumentToCache);

            if (File.Exists(FullPath))
            {
                oH = (HistoricalQuote)DeserializeJSON.DeserializeObjectFromFile(FullPath);
                if (oH == null)
                {
                    return;
                }

                //get the latest date to see if we need to add
                if (oH.HistoricalQuoteDetails != null && oH.HistoricalQuoteDetails.Count > 0 && (DateTime.Now.Date - oH.HistoricalQuoteDetails.Max(h => h.Date).Date.AddDays(1).Date).Days >= 1)
                {
                    bReadMore = true;
                    bAllHistoricalDataNeeded = false;
                    oDataFromNeeded          = oH.HistoricalQuoteDetails.Max(h => h.Date).Date.AddDays(1).Date;
                }
                else
                {
                    //all is up to date!
                    bAllHistoricalDataNeeded = false;
                }
            }

            if (!bReadMore && bAllHistoricalDataNeeded)
            {
                try
                {
                    oH = GetHistoricalQuoteOnline(InstrumentToCache, m_oCurrentExchange);
                    SerializeJSONdata.SerializeObject(oH, FullPath);
                }
                catch (Exception ex)
                {
                    ImperaturGlobal.GetLog().Error(string.Format("Couldn't retreive and save data for {0}", InstrumentToCache.Symbol), ex);
                }
            }
            else if (bReadMore)
            {
                try
                {
                    HistoricalQuote oHnew = GetHistoricalQuoteOnline(InstrumentToCache, m_oCurrentExchange, oDataFromNeeded);

                    oH.HistoricalQuoteDetails.AddRange(oHnew.HistoricalQuoteDetails.Where(h => h.Date.Date > oDataFromNeeded.Date).ToList());
                    SerializeJSONdata.SerializeObject(oH, FullPath);
                }
                catch (Exception ex)
                {
                    ImperaturGlobal.GetLog().Error(string.Format("Couldn't retreive and save data for {0}", InstrumentToCache.Symbol), ex);
                }
            }
        }
Exemplo n.º 2
0
 private bool CreateSystemSettingsFile(ImperaturData SystemData)
 {
     if (SerializeJSONdata.SerializeObject(SystemData, string.Format(@"{0}\{1}", SystemData.SystemDirectory, SystemDataFile)))
     {
         return(true);
     }
     else
     {
         m_oLastErrorMessage = string.Format("Could not save settings file to {0}", SystemData.SystemDirectory);
         return(false);
     }
 }
Exemplo n.º 3
0
        private void UpdateQuotesFromExternalSource()
        {
            if (m_oQuotes == null)
            {
                //first try to read the file
                try
                {
                    string FileToRead = "";
                    foreach (string f in Directory.EnumerateFiles(string.Format(@"{0}\{1}\{2}", ImperaturGlobal.SystemData.SystemDirectory, ImperaturGlobal.SystemData.QuoteDirectory, ImperaturGlobal.SystemData.DailyQuoteDirectory), string.Format("{0}*", ImperaturGlobal.SystemData.QuoteFile), SearchOption.TopDirectoryOnly))
                    {
                        string time = f.Substring(f.Length - 5).Replace(";", ":");
                        string date = f.Substring(f.Length - 15).Substring(0, 10);
                        if (Convert.ToDateTime(string.Format("{0} {1}", date, time)).CompareTo(DateTime.Now.AddMinutes(
                                                                                                   -Convert.ToDouble(ImperaturGlobal.SystemData.QuoteRefreshTime)
                                                                                                   )) > 0)
                        {
                            FileToRead = f;
                            break;
                        }
                    }


                    if (FileToRead != "")
                    {
                        m_oQuotes = (List <Quote>)DeserializeJSON.DeserializeObjectFromFile(@FileToRead.ToString());
                    }
                    else
                    {
                        m_oQuotes = GetQuotesFromExternalSource(ImperaturGlobal.SystemData.ULR_Quotes).Where(x => x != null).ToList();
                        if (m_oQuotes.Count() > 0)
                        {
                            SerializeJSONdata.SerializeObject(m_oQuotes, string.Format(@"{0}\{1}\{2}\{3}{4}{5}", ImperaturGlobal.SystemData.SystemDirectory, ImperaturGlobal.SystemData.QuoteDirectory, ImperaturGlobal.SystemData.DailyQuoteDirectory, ImperaturGlobal.SystemData.QuoteFile, DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString().Replace(":", ";")));
                        }
                    }
                }
                catch (Exception ex)
                {
                    //read from external source
                    m_oQuotes = GetQuotesFromExternalSource(ImperaturGlobal.SystemData.ULR_Quotes).Where(x => x != null).ToList();
                    //save if results obtained
                }
            }
        }