Esempio n. 1
0
        public override void UpdateProvider(IDataUpdateMessage dataUpdateMsg, List <DataSource> dataSources, bool updateNonDSSymbols, bool deleteNonDSSymbols)
        {
            foreach (BarDataScale scale in this._dataStore.GetExistingBarScales())
            {
                if (_cancelTokenSource != null && _cancelTokenSource.IsCancellationRequested)
                {
                    dataUpdateMsg.DisplayUpdateMessage("Обновление провайдера отменено");
                    break;
                }

                dataUpdateMsg.DisplayUpdateMessage("Обновление таймфрейма " + scale.ToString());

                var visibleSymbols = from dataSource in dataSources
                                     where dataSource.BarDataScale == scale
                                     select SymbolDescription.DeserializeList(dataSource.DSString);

                List <SymbolDescription> symbols = new List <SymbolDescription>();

                foreach (List <SymbolDescription> visibleSymbol in visibleSymbols)
                {
                    symbols.AddRange(visibleSymbol);
                }

                if (updateNonDSSymbols)
                {
                    var nonDSSymbols = from symbol in _dataStore.GetExistingSymbols(scale.Scale, scale.BarInterval)
                                       where !symbols.Exists(x => x.FullCode == symbol)
                                       select GetSymbolDescription(symbol);

                    symbols.AddRange(nonDSSymbols);
                }

                DataSource dsVirtual = new DataSource(this);

                dsVirtual.Name         = "VirtualDSr";
                dsVirtual.BarDataScale = scale;

                dsVirtual.DSString = SymbolDescription.SerializeList(symbols);

                UpdateDataSource(dsVirtual, dataUpdateMsg);

                if (deleteNonDSSymbols)
                {
                    dataUpdateMsg.DisplayUpdateMessage("--------------");
                    dataUpdateMsg.DisplayUpdateMessage("Удаление истории инструментов не входящих ни в один набор данных данного таймфрейма:");

                    var nonDSSymbols = from symbol in _dataStore.GetExistingSymbols(scale.Scale, scale.BarInterval)
                                       where !symbols.Exists(x => x.FullCode == symbol)
                                       select symbol;

                    foreach (string symbol in nonDSSymbols)
                    {
                        lock (_locker)
                            _dataStore.RemoveFile(symbol, scale.Scale, scale.BarInterval);
                        dataUpdateMsg.DisplayUpdateMessage(string.Format("[DELETED] Инструмент {0} - История удалена", symbol));
                    }

                    if (nonDSSymbols.Count() == 0)
                    {
                        dataUpdateMsg.DisplayUpdateMessage(string.Format("[NA] Инструменты для удаления не найдены"));
                    }
                }

                dataUpdateMsg.DisplayUpdateMessage("--------------");
            }
        }
Esempio n. 2
0
        public override void UpdateProvider(IDataUpdateMessage dataUpdateMsg, List<DataSource> dataSources, bool updateNonDSSymbols, bool deleteNonDSSymbols)
        {
            this._cancelUpdate = false;

            try
            {
                dataUpdateMsg.DisplayUpdateMessage("Updating daily data from Google Finance...");

                // User requested 'Cancel update'
                if (this._cancelUpdate) return;

                // NOTE: Since only Daily data is made available by Google, this code does not support multiple bar scales
                // If your vendor supports different bar scales, you will need to handle it
                // by looping for all existing bar data scales in BarDataStore!

                // Create a list of all "visible" symbols of the provider
                List<string> allVisibleSymbols = new List<string>();
                foreach (DataSource ds in dataSources)
                    foreach (string sym in ds.Symbols)
                        if (!allVisibleSymbols.Contains(sym))
                            allVisibleSymbols.Add(sym);

                // If 'Update Non DS Symbols' is selected in DM, need to build the *complete* symbol list (incl. which have been deleted from existing DataSets)
                List<string> allExistingSymbols = new List<string>();
                allExistingSymbols.AddRange(allVisibleSymbols);
                IList<string> existingSymbols = this._dataStore.GetExistingSymbols(scale, barinterval);
                if (updateNonDSSymbols)
                {
                    foreach (string str in existingSymbols)
                        if (!allExistingSymbols.Contains(str))
                            allExistingSymbols.Add(str);
                }

                // Create a virtual DataSource on-the-fly that contains the entire symbol list of the provider
                DataSource ds_ = new DataSource(this);
                ds_.BarDataScale = new BarDataScale(scale, barinterval);
                ds_.DSString = allExistingSymbols.ToString();

                // Update all by creating a virtual DataSet
                this._updating = true;
                this.UpdateDataSource(ds_, dataUpdateMsg);

                // Delete symbols not present in existing DataSets
                if (deleteNonDSSymbols)
                {
                    int num = 0;
                    string str3 = "";
                    foreach (string str in existingSymbols)
                    {
                        if (!allVisibleSymbols.Contains(str))
                        {
                            num++;
                            // It's important to specify the right BarScale/Interval here (see note above re: multiple bar data scales)
                            this._dataStore.RemoveFile(str, scale, barinterval);
                            if (str3 != "")
                            {
                                str3 += ", ";
                            }
                            str3 = str3 + str;
                        }
                    }
                    if (num > 0)
                    {
                        dataUpdateMsg.DisplayUpdateMessage("Deleted Symbols " + str3);
                        dataUpdateMsg.DisplayUpdateMessage("Deleted " + num + " Symbol data files");
                    }
                }
                dataUpdateMsg.DisplayUpdateMessage("");

            }
            catch (Exception e)
            {
                dataUpdateMsg.DisplayUpdateMessage("Error: " + e.Message);
            }
            finally
            {
                this._updating = false;
                _dataUpdateMsg = null;
            }
        }
Esempio n. 3
0
        public override void UpdateDataSource(DataSource ds, IDataUpdateMessage dataUpdateMsg)
        {
            _cancelTokenSource = new CancellationTokenSource();

            dataUpdateMsg.ReportUpdateProgress(0);

            if (string.IsNullOrWhiteSpace(ds.DSString))
            {
                List <SymbolDescription> symbolDescriptions = (from symbol in ds.Symbols
                                                               select GetSymbolDescription(symbol)).ToList();

                ds.DSString = SymbolDescription.SerializeList(symbolDescriptions);
            }

            List <SymbolDescription> updateRequired = (from description in SymbolDescription.DeserializeList(ds.DSString)
                                                       where UpdateRequired(description, ds.BarDataScale)
                                                       select description).ToList();

            dataUpdateMsg.DisplayUpdateMessage(string.Format("Количество инструментов требующих обновления: {0}", updateRequired.Count));

            if (updateRequired.Count > 0)
            {
                dataUpdateMsg.DisplayUpdateMessage("Запуск обновления инструментов:");

                Task[] tasks = new Task[updateRequired.Count];

                for (int i = 0; i < updateRequired.Count; i++)
                {
                    dataUpdateMsg.DisplayUpdateMessage(string.Format("[START] Инструмент: {0} - Обновление запущено", updateRequired[i].FullCode));

                    tasks[i] = Task.Factory.StartNew((object updateRequiredSymbol) =>
                    {
                        string symbol = (string)updateRequiredSymbol;

                        DateTime currentDate = DateTime.Now;

                        Bars bars = new Bars(symbol, ds.Scale, ds.BarInterval);

                        _dataStore.LoadBarsObject(bars);

                        try
                        {
                            string suffix = GetSuffix(ds.BarDataScale);

                            int corrections = 0;
                            bars.AppendWithCorrections(GetHistory(ds.BarDataScale, symbol, suffix), out corrections);

                            if (bars.Count > 0 && bars.Date[bars.Count - 1] > currentDate)
                            {
                                bars.Delete(bars.Count - 1);
                            }

                            lock (_locker)
                                _dataStore.SaveBarsObject(bars);

                            dataUpdateMsg.DisplayUpdateMessage(string.Format("[COMPLETE] Инструмент: {0} - Обновление завершено", symbol));
                        }
                        catch (Exception exception)
                        {
                            logger.Error(exception);
                            dataUpdateMsg.DisplayUpdateMessage(string.Format("[ERROR] Инструмент {0} - {1}", symbol, exception.Message));
                        }
                    }, updateRequired[i].FullCode);
                }

                try
                {
                    Task.WaitAll(tasks);
                }
                catch (AggregateException exception)
                {
                    exception.Handle((inner) =>
                    {
                        if (inner is OperationCanceledException)
                        {
                            return(true);
                        }
                        else
                        {
                            logger.Error(inner);
                            return(false);
                        }
                    });
                }
            }
        }
Esempio n. 4
0
        public override void UpdateDataSource(DataSource ds, IDataUpdateMessage dataUpdateMsg)
        {
            this._dataUpdateMsg = dataUpdateMsg;
            this._cancelUpdate = false;
            this._updating = true;
            Bars barsNew; // The Bars object for a newly downloaded symbol, or just the new data for an existing/updated symbol

            /* Main loop */
            try
            {
                // User requested 'Cancel update'
                if (this._cancelUpdate) return;

                List<string> up2date = new List<string>();
                List<string> updateRequired = new List<string>();
                List<string> newSymbols = new List<string>();
                string sym;

                foreach (string s in ds.Symbols)
                {
                    sym = s;

                    if (!this.UpdateRequired(ds, sym))
                        up2date.Add(sym);
                    else if (this._dataStore.ContainsSymbol(sym, ds.Scale, ds.BarInterval))
                        updateRequired.Add(sym);
                    else
                        if (!this._dataStore.ContainsSymbol(sym, ds.Scale, ds.BarInterval))
                            newSymbols.Add(sym);

                }
                // For debugging:
                dataUpdateMsg.DisplayUpdateMessage(
                    "Up-to-date symbols: " + up2date.Count.ToString() + ", " +
                    "Update required for: " + updateRequired.Count.ToString() + ", " +
                    "New symbols: " + newSymbols.Count.ToString());

                /* 1. Symbols already up-to-date */

                // Process symbols which require no data update
                if (up2date.Count > 0)
                {
                    string alreadyUp2Date = null;
                    foreach (string str in up2date)
                        alreadyUp2Date += str + ",";

                    dataUpdateMsg.DisplayUpdateMessage("Symbols already up to date: " + alreadyUp2Date);
                    dataUpdateMsg.ReportUpdateProgress((up2date.Count * 100) / ds.Symbols.Count);
                }

                /* 2. Symbols to update */

                Bars bars1;

                if (updateRequired.Count > 0)
                {
                    foreach (string s in updateRequired)
                    {
                        try
                        {
                            if (!this._cancelUpdate)
                            {
                                bars1 = new Bars(s, scale, barinterval);

                                if (_dataStore.ContainsSymbol(s, ds.Scale, ds.BarInterval))
                                    _dataStore.LoadBarsObject(bars1);

                                barsNew = zaglushka.RequestData(ds, s, bars1.Date[bars1.Count - 1], DateTime.Now, int.MaxValue, true);
                                this.LoadAndUpdateBars(ref bars1, barsNew);
                            }
                            else
                                return;
                        }
                        catch (Exception e)
                        {
                            dataUpdateMsg.DisplayUpdateMessage("Error: " + e.Message);
                        }
                    }
                }

                DateTime maxValue = DateTime.MaxValue;
                string[] newSymbolsArray = new string[newSymbols.Count];

                if (newSymbols.Count > 0)
                {
                    foreach (string str in newSymbols)
                    {
                        DateTime timeOfNewSymbol = this._dataStore.SymbolLastUpdated(str, ds.Scale, ds.BarInterval); //.Date;
                        if (timeOfNewSymbol < maxValue)
                            maxValue = timeOfNewSymbol;
                    }
                }

                dataUpdateMsg.DisplayUpdateMessage("Updating...");

                /* 3. New symbols */

                // Load the Bars object from BarDataStore for updating
                Bars bars2;

                foreach (string s in newSymbols)
                {
                    try
                    {
                        if (!this._cancelUpdate)
                        {
                            bars2 = new Bars(s, ds.Scale, ds.BarInterval);
                            _dataStore.LoadBarsObject(bars2);

                            // Google doesn't provide company names, so we'll get them from Yahoo!
                            if (zaglushka.GetCompanyName(s) != null)
                                bars2.SecurityName = zaglushka.GetCompanyName(s);

                            // After some trial and error, I figured out that setting the starting date to 1/1/1971 allows to fetch all available data
                            barsNew = zaglushka.RequestData(ds, s, DateTime.MinValue, DateTime.Now, int.MaxValue, true);
                            dataUpdateMsg.DisplayUpdateMessage("Symbol: " + s + ", existing bars : " + bars2.Count + ", new bars: " + barsNew.Count);
                            this.LoadAndUpdateBars(ref bars2, barsNew);
                        }
                        else
                            return;
                    }
                    catch (Exception e)
                    {
                        dataUpdateMsg.DisplayUpdateMessage("Error: " + e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                dataUpdateMsg.DisplayUpdateMessage("Error: " + e.Message);
            }
            finally
            {
                this._updating = false;
            }
        }