|
| 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.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using QuantConnect.Algorithm.Framework.Selection; |
| 19 | +using QuantConnect.Data; |
| 20 | +using QuantConnect.Data.Custom.Benzinga; |
| 21 | +using QuantConnect.Data.UniverseSelection; |
| 22 | + |
| 23 | +namespace QuantConnect.Algorithm.CSharp |
| 24 | +{ |
| 25 | + public class BenzingaNewsAlgorithm : QCAlgorithm |
| 26 | + { |
| 27 | + // Predefine a dictionary of words with scores to scan for in the description |
| 28 | + // of the news article |
| 29 | + private readonly Dictionary<string, double> _words = new Dictionary<string, double>() |
| 30 | + { |
| 31 | + {"bad", -0.5}, {"good", 0.5}, |
| 32 | + {"negative", -0.5}, {"great", 0.5}, |
| 33 | + {"growth", 0.5}, {"fail", -0.5}, |
| 34 | + {"failed", -0.5}, {"success", 0.5}, |
| 35 | + {"nailed", 0.5}, {"beat", 0.5}, |
| 36 | + {"missed", -0.5}, {"slipped", -0.5}, |
| 37 | + {"outperforming", 0.5}, {"underperforming", -0.5}, |
| 38 | + {"outperform", 0.5}, {"underperform", -0.5} |
| 39 | + }; |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. |
| 43 | + /// </summary> |
| 44 | + public override void Initialize() |
| 45 | + { |
| 46 | + SetStartDate(2018, 10, 12); |
| 47 | + SetEndDate(2018, 11, 25); |
| 48 | + SetCash(100000); |
| 49 | + |
| 50 | + UniverseSettings.Resolution = Resolution.Daily; |
| 51 | + AddUniverseSelection(new CoarseFundamentalUniverseSelectionModel(CoarseSelector)); |
| 52 | + } |
| 53 | + |
| 54 | + public IEnumerable<Symbol> CoarseSelector(IEnumerable<CoarseFundamental> coarse) |
| 55 | + { |
| 56 | + // Add Benzinga news data from the filtered coarse selection |
| 57 | + var symbols = coarse.Where(x => x.HasFundamentalData && x.DollarVolume > 50000000) |
| 58 | + .Select(x => x.Symbol) |
| 59 | + .Take(10); |
| 60 | + |
| 61 | + foreach (var symbol in symbols) |
| 62 | + { |
| 63 | + AddData<BenzingaNews>(symbol); |
| 64 | + } |
| 65 | + |
| 66 | + return symbols; |
| 67 | + } |
| 68 | + |
| 69 | + public override void OnData(Slice data) |
| 70 | + { |
| 71 | + // Get all Benzinga data and loop over it |
| 72 | + foreach (var article in data.Get<BenzingaNews>().Values) |
| 73 | + { |
| 74 | + // Get the list of matching words we have sentiment definitions for |
| 75 | + var intersection = article.Contents.ToLowerInvariant() |
| 76 | + .Split(' ') |
| 77 | + .Intersect(_words.Keys); |
| 78 | + |
| 79 | + // Get the sentiment score |
| 80 | + var sentimentScore = intersection.Select(x => _words[x]).Sum(); |
| 81 | + |
| 82 | + // Set holdings equal to 1/10th of the sentiment score we get |
| 83 | + SetHoldings(article.Symbol.Underlying, sentimentScore / 10); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public override void OnSecuritiesChanged(SecurityChanges changes) |
| 88 | + { |
| 89 | + foreach (var r in changes.RemovedSecurities.Where(x => x.Symbol.SecurityType == SecurityType.Equity)) |
| 90 | + { |
| 91 | + // If removed from the universe, liquidate and remove the custom data from the algorithm |
| 92 | + Liquidate(r.Symbol); |
| 93 | + RemoveSecurity(QuantConnect.Symbol.CreateBase(typeof(BenzingaNews), r.Symbol, Market.USA)); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments