Ejemplo n.º 1
0
            /// <summary>
            /// Run sub-classed algorithm and return bars as enumerable.
            /// </summary>
            /// <param name="startTime">start of load range</param>
            /// <param name="endTime">end of load range</param>
            /// <returns>bars created from sub-classed algo</returns>
            public override IEnumerable <Bar> LoadData(DateTime startTime, DateTime endTime)
            {
#if true
                _algo.IsDataSource = true;

                if (_algo.CanRunAsChild && Info.ContainsKey(DataSourceParam.allowSync))
                {
                    // for child algorithms, we bypass the cache and run the
                    // child bar-for-bar and in sync with its parent

                    // we don't want to rely on the algorithm to provide a unique name.
                    // therefore, we replace the bar symbol with the algorithm's hash
                    var algoHashHex = string.Format("{0:X}", _algo.Name, _algo.GetHashCode());

                    foreach (var bar in _algo.Run(startTime, endTime))
                    {
                        var bar2 = new Bar(
                            algoHashHex,
                            bar.Time,
                            bar.Open, bar.High, bar.Low, bar.Close, bar.Volume, bar.HasOHLC,
                            bar.Bid, bar.Ask, bar.BidVolume, bar.AskVolume, bar.HasBidAsk,
                            bar.OptionExpiry, bar.OptionStrike, bar.OptionIsPut);

                        yield return(bar2);

                        if (!_algo.IsLastBar)
                        {
                            // the simulator core needs to know the next bar's
                            // timestamp. at the same time, we want to avoid
                            // child algorithms from running one bar ahead of
                            // the main algo. we fix this issue by returning
                            // a dummy bar, announcing the next timestamp.

                            var dummy = Bar.NewValue(
                                null, _algo.NextSimTime, 0.0);

                            yield return(dummy);
                        }
                    }

                    yield break;
                }
                else
                {
                    // for all other algorithms, we run the algo
                    // all at once and save the result in the cache

                    var algoNick = Info[DataSourceParam.nickName];

                    var cacheKey = new CacheId().AddParameters(
                        algoNick.GetHashCode(), // _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 bars = _algo.Run(startTime, endTime)
                                       .ToList();

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

                            return(bars);
                        }

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

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

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

                    CachedData = data;
                    foreach (var bar in data)
                    {
                        yield return(bar);
                    }
                }
#else
                var algoNick = Info[DataSourceParam.nickName];

                var cacheKey = new CacheId(null, "", 0,
                                           algoNick.GetHashCode(), // _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]));

                        _algo.StartTime        = startTime;
                        _algo.EndTime          = 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 " + algoNick);
                    }
                }

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

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

                Data = data;
#endif
            }