Esempio n. 1
0
            //---------- API
            #region public DataSourceAlgorithm(Dictionary<DataSourceValue, string> info)
            /// <summary>
            /// Create and initialize new data source for algorithm quotes.
            /// </summary>
            /// <param name="info">info dictionary</param>
            public DataSourceAlgorithm(Dictionary <DataSourceParam, string> info) : base(info)
            {
                if (!Info.ContainsKey(DataSourceParam.symbolAlgo))
                {
                    throw new Exception(string.Format("{0}: {1} missing mandatory symbolAlgo key", GetType().Name, info[DataSourceParam.nickName]));
                }

                var items     = info[DataSourceParam.symbolAlgo].Split(",").ToList();
                var algoName  = items[0];
                var algoParam = items.Count > 1 ? items[1] : null;

                _algo = (Algorithm)AlgorithmLoader.InstantiateAlgorithm(algoName);

                if (_algo == null)
                {
                    throw new Exception(string.Format("DataSourceAlgorithm: failed to instantiate algorithm {0}", info[DataSourceParam.nickName2]));
                }

                _algo.SubclassedParam      = algoParam;
                Info[DataSourceParam.name] = _algo.Name;

                // by default, we run the data source through the cache,
                // if it was instantiated from an info dictionary
                // Info[DataSourceParam.allowSync] = "true";
            }
            //---------- API
            #region public DataSourceAlgorithm(Dictionary<DataSourceValue, string> info)
            /// <summary>
            /// Create and initialize new data source for algorithm quotes.
            /// </summary>
            /// <param name="info">info dictionary</param>
            public DataSourceAlgorithm(Dictionary <DataSourceParam, string> info) : base(info)
            {
                _algoName = Info[DataSourceParam.dataFeed]
                            .Split(':')
                            .Last();

                var algo = (SubclassableAlgorithm)AlgorithmLoader.InstantiateAlgorithm(_algoName);

                if (algo == null)
                {
                    throw new Exception(string.Format("DataSourceAlgorithm: failed to instantiate algorithm {0}", _algoName));
                }

                Info[DataSourceParam.name] = algo.Name;
            }
            /// <summary>
            /// Load data into memory.
            /// </summary>
            /// <param name="startTime">start of load range</param>
            /// <param name="endTime">end of load range</param>
            override public void LoadData(DateTime startTime, DateTime endTime)
            {
                var cacheKey = new CacheId(null, "", 0,
                                           _algoName.GetHashCode(),
                                           startTime.GetHashCode(),
                                           endTime.GetHashCode());

                List <Bar> retrievalFunction()
                {
                    try
                    {
                        DateTime t1 = DateTime.Now;
                        Output.WriteLine(string.Format("DataSourceAlgorithm: generating data for {0}...", Info[DataSourceParam.nickName]));

                        var algo = (SubclassableAlgorithm)AlgorithmLoader.InstantiateAlgorithm(_algoName);

                        algo.SubclassedStartTime = startTime;
                        algo.SubclassedEndTime   = endTime;
                        algo.ParentDataSource    = this;

                        algo.SubclassedData = new List <Bar>();;

                        algo.Run();

                        DateTime t2 = DateTime.Now;
                        Output.WriteLine(string.Format("DataSourceAlgorithm: finished after {0:F1} seconds", (t2 - t1).TotalSeconds));

                        return(algo.SubclassedData);
                    }

                    catch
                    {
                        throw new Exception("DataSourceAlgorithm: failed to run sub-classed algorithm " + _algoName);
                    }
                }

                List <Bar> data = Cache <List <Bar> > .GetData(cacheKey, retrievalFunction);

                if (data.Count == 0)
                {
                    throw new Exception(string.Format("DataSourceNorgate: no data for {0}", Info[DataSourceParam.nickName]));
                }

                Data = data;
            }