|
| 1 | +/* |
| 2 | + * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. |
| 3 | + * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using QuantConnect.Interfaces; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Linq; |
| 20 | +using QuantConnect.Algorithm; |
| 21 | +using QuantConnect.Data; |
| 22 | +using QuantConnect.DataSource; |
| 23 | +using QuantConnect.Data.UniverseSelection; |
| 24 | +using QuantConnect.Securities; |
| 25 | + |
| 26 | +namespace QuantConnect.DataLibrary.Tests |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Example algorithm of a custom universe selection using coarse data and adding TiingoNews |
| 30 | + /// If conditions are met will add the underlying and trade it |
| 31 | + /// </summary> |
| 32 | + public class CoarseTiingoNewsUniverseSelectionAlgorithm : QCAlgorithm |
| 33 | + { |
| 34 | + private const int NumberOfSymbols = 3; |
| 35 | + private List<Symbol> _symbols; |
| 36 | + |
| 37 | + public override void Initialize() |
| 38 | + { |
| 39 | + SetStartDate(2014, 03, 24); |
| 40 | + SetEndDate(2014, 04, 07); |
| 41 | + |
| 42 | + UniverseSettings.FillForward = false; |
| 43 | + |
| 44 | + AddUniverse(new CustomDataCoarseFundamentalUniverse(UniverseSettings, CoarseSelectionFunction)); |
| 45 | + |
| 46 | + _symbols = new List<Symbol>(); |
| 47 | + } |
| 48 | + |
| 49 | + // sort the data by daily dollar volume and take the top 'NumberOfSymbols' |
| 50 | + public IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse) |
| 51 | + { |
| 52 | + // sort descending by daily dollar volume |
| 53 | + var sortedByDollarVolume = coarse.OrderByDescending(x => x.DollarVolume); |
| 54 | + |
| 55 | + // take the top entries from our sorted collection |
| 56 | + var top = sortedByDollarVolume.Take(NumberOfSymbols); |
| 57 | + |
| 58 | + // we need to return only the symbol objects |
| 59 | + return top.Select(x => QuantConnect.Symbol.CreateBase(typeof(TiingoNews), x.Symbol, x.Symbol.ID.Market)); |
| 60 | + } |
| 61 | + |
| 62 | + public override void OnData(Slice data) |
| 63 | + { |
| 64 | + var articles = data.Get<TiingoNews>(); |
| 65 | + |
| 66 | + foreach (var kvp in articles) |
| 67 | + { |
| 68 | + var news = kvp.Value; |
| 69 | + if (news.Title.IndexOf("Stocks Drop", 0, StringComparison.CurrentCultureIgnoreCase) != -1) |
| 70 | + { |
| 71 | + if (!Securities.ContainsKey(kvp.Key.Underlying)) |
| 72 | + { |
| 73 | + // add underlying we want to trade |
| 74 | + AddSecurity(kvp.Key.Underlying); |
| 75 | + _symbols.Add(kvp.Key.Underlying); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + foreach (var symbol in _symbols) |
| 81 | + { |
| 82 | + if (Securities[symbol].HasData) |
| 83 | + { |
| 84 | + SetHoldings(symbol, 1m / _symbols.Count); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public override void OnSecuritiesChanged(SecurityChanges changes) |
| 90 | + { |
| 91 | + changes.FilterCustomSecurities = false; |
| 92 | + Log($"{Time} {changes}"); |
| 93 | + } |
| 94 | + |
| 95 | + private class CustomDataCoarseFundamentalUniverse : CoarseFundamentalUniverse |
| 96 | + { |
| 97 | + public CustomDataCoarseFundamentalUniverse(UniverseSettings universeSettings, Func<IEnumerable<CoarseFundamental>, IEnumerable<Symbol>> selector) |
| 98 | + : base(universeSettings, selector) |
| 99 | + { } |
| 100 | + |
| 101 | + public override IEnumerable<SubscriptionRequest> GetSubscriptionRequests(Security security, DateTime currentTimeUtc, DateTime maximumEndTimeUtc, |
| 102 | + ISubscriptionDataConfigService subscriptionService) |
| 103 | + { |
| 104 | + var config = subscriptionService.Add( |
| 105 | + typeof(TiingoNews), |
| 106 | + security.Symbol, |
| 107 | + UniverseSettings.Resolution, |
| 108 | + UniverseSettings.FillForward, |
| 109 | + UniverseSettings.ExtendedMarketHours, |
| 110 | + dataNormalizationMode: UniverseSettings.DataNormalizationMode); |
| 111 | + return new[]{new SubscriptionRequest(isUniverseSubscription: false, |
| 112 | + universe: this, |
| 113 | + security: security, |
| 114 | + configuration: config, |
| 115 | + startTimeUtc: currentTimeUtc, |
| 116 | + endTimeUtc: maximumEndTimeUtc)}; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments