|
| 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 QuantConnect.Data; |
| 17 | +using QuantConnect.DataSource; |
| 18 | + |
| 19 | +namespace QuantConnect.Algorithm.CSharp |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Demonstration algorithm showing how to use BLS Economic Surveys custom datasets |
| 23 | + /// (CPI, CES, PPI, JOLTS) for signal generation and trading. |
| 24 | + /// </summary> |
| 25 | + public class BLSEconomicSurveysAlgorithm : QCAlgorithm |
| 26 | + { |
| 27 | + private Symbol _cpiSymbol; |
| 28 | + private Symbol _cesSymbol; |
| 29 | + private Symbol _ppiSymbol; |
| 30 | + private Symbol _spySymbol; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Initializes the algorithm with custom data subscriptions. |
| 34 | + /// </summary> |
| 35 | + public override void Initialize() |
| 36 | + { |
| 37 | + SetStartDate(2000, 1, 1); |
| 38 | + SetEndDate(2002, 6, 1); |
| 39 | + SetCash(100000); |
| 40 | + |
| 41 | + // Add a tradeable equity for order execution |
| 42 | + _spySymbol = AddEquity("SPY", Resolution.Daily).Symbol; |
| 43 | + |
| 44 | + // Subscribe to BLS economic surveys (unlinked custom data) |
| 45 | + _cpiSymbol = AddData<BLSEconomicSurveysCpi>("CPI", Resolution.Daily).Symbol; |
| 46 | + _cesSymbol = AddData<BLSEconomicSurveysCes>("CES", Resolution.Daily).Symbol; |
| 47 | + _ppiSymbol = AddData<BLSEconomicSurveysPpi>("PPI", Resolution.Daily).Symbol; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Handles data events. Uses CPI and employment data to generate trading signals. |
| 52 | + /// </summary> |
| 53 | + /// <param name="slice">The current data slice</param> |
| 54 | + public override void OnData(Slice slice) |
| 55 | + { |
| 56 | + // Check for CPI data |
| 57 | + if (slice.ContainsKey(_cpiSymbol)) |
| 58 | + { |
| 59 | + var cpi = slice.Get<BLSEconomicSurveysCpi>(_cpiSymbol); |
| 60 | + Log($"{Time} - CPI AllItems: {cpi.AllItems}, CoreCpi: {cpi.CoreCpi}, Energy: {cpi.Energy}"); |
| 61 | + |
| 62 | + // Simple signal: if energy CPI is rising faster than core, reduce equity exposure |
| 63 | + if (cpi.Energy.HasValue && cpi.CoreCpi.HasValue && cpi.Energy > cpi.CoreCpi * 1.5m) |
| 64 | + { |
| 65 | + if (Portfolio[_spySymbol].Invested) |
| 66 | + { |
| 67 | + SetHoldings(_spySymbol, 0.5); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Check for employment data |
| 73 | + if (slice.ContainsKey(_cesSymbol)) |
| 74 | + { |
| 75 | + var ces = slice.Get<BLSEconomicSurveysCes>(_cesSymbol); |
| 76 | + Log($"{Time} - CES TotalNonfarm: {ces.TotalNonfarm}, AvgHourlyEarnings: {ces.AverageHourlyEarnings}"); |
| 77 | + |
| 78 | + // Simple signal: go long when nonfarm payrolls are strong |
| 79 | + if (ces.TotalNonfarm.HasValue && ces.TotalNonfarm > 130000m && !Portfolio[_spySymbol].Invested) |
| 80 | + { |
| 81 | + SetHoldings(_spySymbol, 1); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Check for PPI data |
| 86 | + if (slice.ContainsKey(_ppiSymbol)) |
| 87 | + { |
| 88 | + var ppi = slice.Get<BLSEconomicSurveysPpi>(_ppiSymbol); |
| 89 | + Log($"{Time} - PPI AllCommodities: {ppi.AllCommodities}, FarmProducts: {ppi.FarmProducts}"); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments