Skip to content

Commit e71943d

Browse files
authored
Merge pull request QuantConnect#1 from QuantConnect/refactor-remove-custom-data-references
Migrate TiingoNews custom data references to DataSource repo
2 parents fad2de5 + c17494e commit e71943d

10 files changed

+766
-7
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
2+
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from AlgorithmImports import *
15+
from QuantConnect.DataSource import *
16+
17+
### <summary>
18+
### Example algorithm of a custom universe selection using coarse data and adding TiingoNews
19+
### If conditions are met will add the underlying and trade it
20+
### </summary>
21+
class CoarseTiingoNewsUniverseSelectionAlgorithm(QCAlgorithm):
22+
23+
def Initialize(self):
24+
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
25+
26+
self.SetStartDate(2014,3,24)
27+
self.SetEndDate(2014,4,7)
28+
29+
self.UniverseSettings.FillForward = False;
30+
31+
self.__numberOfSymbols = 3
32+
33+
self.AddUniverse(CustomDataCoarseFundamentalUniverse(self.UniverseSettings, self.CoarseSelectionFunction));
34+
35+
self._symbols = []
36+
37+
# sort the data by daily dollar volume and take the top 'NumberOfSymbols'
38+
def CoarseSelectionFunction(self, coarse):
39+
# sort descending by daily dollar volume
40+
sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
41+
42+
# return the symbol objects of the top entries from our sorted collection
43+
return [ Symbol.CreateBase(TiingoNews, x.Symbol, x.Symbol.ID.Market) for x in sortedByDollarVolume[:self.__numberOfSymbols] ]
44+
45+
def OnData(self, data):
46+
articles = data.Get(TiingoNews)
47+
48+
for kvp in articles:
49+
news = kvp.Value
50+
if "stocks drop" in news.Title.lower():
51+
if not self.Securities.ContainsKey(kvp.Key.Underlying):
52+
# add underlying we want to trade
53+
self.AddSecurity(kvp.Key.Underlying)
54+
self._symbols.append(kvp.Key.Underlying)
55+
56+
for symbol in self._symbols:
57+
if self.Securities[symbol].HasData:
58+
self.SetHoldings(symbol, 1.0 / len(self._symbols))
59+
60+
def OnSecuritiesChanged(self, changes):
61+
changes.FilterCustomSecurities = False
62+
self.Log(f"{self.Time} {changes}")
63+
64+
class CustomDataCoarseFundamentalUniverse(CoarseFundamentalUniverse):
65+
def GetSubscriptionRequests(self, security, currentTimeUtc, maximumEndTimeUtc, subscriptionService):
66+
us = self.UniverseSettings
67+
config = subscriptionService.Add(TiingoNews, security.Symbol, us.Resolution, us.FillForward, us.ExtendedMarketHours, True, False, False, us.DataNormalizationMode)
68+
return [ SubscriptionRequest(False, self, security, config, currentTimeUtc, maximumEndTimeUtc) ]

DataProcessing/DataProcessing.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net5.0</TargetFramework>
66
<AssemblyName>process</AssemblyName>
7+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
78
</PropertyGroup>
89

910
<ItemGroup>
10-
<PackageReference Include="QuantConnect.Common" Version="2.5.11800" />
11-
<PackageReference Include="QuantConnect.Compression" Version="2.5.11800" />
11+
<PackageReference Include="QuantConnect.Common" Version="2.5.*" />
12+
<PackageReference Include="QuantConnect.Compression" Version="2.5.*" />
1213
</ItemGroup>
1314

1415
<ItemGroup>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<RootNamespace>QuantConnect.DataSource.DataQueueHandlers</RootNamespace>
6+
<AssemblyName>QuantConnect.DataSource.DataQueueHandlers.TiingoNews</AssemblyName>
7+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="QuantConnect.Common" Version="2.5.*" />
12+
<PackageReference Include="QuantConnect.Compression" Version="2.5.*" />
13+
<PackageReference Include="QuantConnect.Lean.Engine" Version="2.5.*" />
14+
<PackageReference Include="protobuf-net" Version="3.0.29" />
15+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\QuantConnect.DataSource.csproj" />
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)