Skip to content

Commit

Permalink
Merge pull request #16 from AlexCatarino/fix-universe
Browse files Browse the repository at this point in the history
Shift universe data 12 hours earlier
  • Loading branch information
Martin-Molinero authored Apr 25, 2023
2 parents 6da8408 + 56a0513 commit 232612e
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 9 deletions.
5 changes: 4 additions & 1 deletion BrainCompanyFilingLanguageMetricsUniverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public override BaseData Reader(SubscriptionDataConfig config, string line, Date
data.ManagementDiscussionAnalyasisOfFinancialConditionAndResultsOfOperations = BrainCompanyFilingLanguageMetrics.Parse(csv.Skip(24).Take(11).ToList());

data.Symbol = new Symbol(SecurityIdentifier.Parse(csv[0]), csv[1]);
data.Time = date;
// We need to convert the time since the date is in UTC, and AddUniverse sets the ExchangeTimeZone to TimeZones.NewYork
// Subtract 12 hours to match the BrainCompanyFilingLanguageMetricsBase EndTime
data.Time = date.ConvertFromUtc(config.ExchangeTimeZone).AddHours(-12);
data.Value = csv[4].IfNotNullOrEmpty(0m, s => decimal.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture));

return data;
Expand Down Expand Up @@ -150,6 +152,7 @@ public override List<Resolution> SupportedResolutions()
/// <returns>The <see cref="T:NodaTime.DateTimeZone" /> of this data type</returns>
public override DateTimeZone DataTimeZone()
{
// TODO: AddUniverse does not take this into account
return TimeZones.Utc;
}
}
Expand Down
5 changes: 4 additions & 1 deletion BrainSentimentIndicatorUniverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ public override BaseData Reader(SubscriptionDataConfig config, string line, Date
SentimentalBuzzVolume30Days = csv[11].IfNotNullOrEmpty<decimal?>(s => decimal.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture)),

Symbol = new Symbol(SecurityIdentifier.Parse(csv[0]), csv[1]),
Time = date,
// We need to convert the time since the date is in UTC, and AddUniverse sets the ExchangeTimeZone to TimeZones.NewYork
// Subtract 12 hours to match the BrainSentimentIndicatorBase EndTime
Time = date.ConvertFromUtc(config.ExchangeTimeZone).AddHours(-12),
Value = sentiment7Days ?? 0m
};
}
Expand Down Expand Up @@ -180,6 +182,7 @@ public override List<Resolution> SupportedResolutions()
/// <returns>The <see cref="T:NodaTime.DateTimeZone" /> of this data type</returns>
public override DateTimeZone DataTimeZone()
{
// TODO: AddUniverse does not take this into account
return TimeZones.Utc;
}
}
Expand Down
58 changes: 58 additions & 0 deletions BrainSentimentRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.DataSource;

namespace QuantConnect.Algorithm.CSharp
{
public class BrainSentimentRegressionAlgorithm : QCAlgorithm
{
public Symbol _aapl, _customDataSymbol;
public DateTime _last;

public override void Initialize()
{
// Data ADDED via universe selection is added with Daily resolution.
UniverseSettings.Resolution = Resolution.Daily;

SetStartDate(2021, 2, 1);
SetEndDate(2021, 2, 10);
SetCash(100000);

_aapl = AddEquity("AAPL").Symbol;
_customDataSymbol = AddData<BrainSentimentIndicator7Day>(_aapl).Symbol;

// add a custom universe data source (defaults to usa-equity)
AddUniverse<BrainSentimentIndicatorUniverse>("BrainSentimentIndicatorUniverse", Resolution.Daily, (data) => {
_last = Time;
return Enumerable.Empty<Symbol>();
});
}

public override void OnData(Slice slice)
{
var data = slice.Get<BrainSentimentIndicator7Day>();
if (data.ContainsKey(_customDataSymbol) && Time !=_last)
{
throw new Exception($"Universe and data time is not matching");
}
}
}
}
5 changes: 4 additions & 1 deletion BrainStockRankingUniverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public override BaseData Reader(SubscriptionDataConfig config, string line, Date
Rank21Days = csv[6].IfNotNullOrEmpty<decimal?>(s => decimal.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture)),

Symbol = new Symbol(SecurityIdentifier.Parse(csv[0]), csv[1]),
Time = date,
// We need to convert the time since the date is in UTC, and AddUniverse sets the ExchangeTimeZone to TimeZones.NewYork
// Subtract 12 hours to match the BrainStockRankingBase EndTime
Time = date.ConvertFromUtc(config.ExchangeTimeZone).AddHours(-12),
Value = rank2Days ?? 0m
};
}
Expand Down Expand Up @@ -139,6 +141,7 @@ public override List<Resolution> SupportedResolutions()
/// <returns>The <see cref="T:NodaTime.DateTimeZone" /> of this data type</returns>
public override DateTimeZone DataTimeZone()
{
// TODO: AddUniverse does not take this into account
return TimeZones.Utc;
}
}
Expand Down
2 changes: 2 additions & 0 deletions QuantConnect.DataSource.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<None Remove="BrainCompanyFilingLanguageMetricsUniverseSelectionAlgorithm.cs" />
<Compile Remove="BrainSentimentIndicatorUniverseSelectionAlgorithm.cs" />
<None Remove="BrainSentimentIndicatorUniverseSelectionAlgorithm.cs" />
<Compile Remove="BrainSentimentRegressionAlgorithm.cs" />
<None Remove="BrainSentimentRegressionAlgorithm.cs" />
<Compile Remove="BrainStockRankingUniverseSelectionAlgorithm.cs" />
<None Remove="BrainStockRankingUniverseSelectionAlgorithm.cs" />
<None Remove=".gitignore" />
Expand Down
20 changes: 16 additions & 4 deletions tests/BrainSentimentIndicatorUniverseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public void ReaderTest()
var factory = new BrainSentimentIndicatorUniverse();
var line = "AAPL R735QTJ8XC9X,AAPL,869,516,0.1196,,,5101,3176,0.0976,-0.169,0.0888";

var config = CreateSubscriptionDataConfig();
var date = new DateTime(2022, 04, 21);
var data = (BrainSentimentIndicatorUniverse)factory.Reader(null, line, date, false);
Assert.AreEqual(date + TimeSpan.FromDays(1), data.EndTime);
var data = (BrainSentimentIndicatorUniverse)factory.Reader(config, line, date, false);
Assert.AreEqual(date.ConvertFromUtc(config.ExchangeTimeZone) + TimeSpan.FromHours(12), data.EndTime);
Assert.AreEqual(0.1196, data.Sentiment7Days);
Assert.AreEqual(0.0976, data.Sentiment30Days);
Assert.AreEqual(516, data.SentimentalArticleMentions7Days);
Expand All @@ -56,9 +57,10 @@ public void ReaderNullSentiment7DaysTest()
var factory = new BrainSentimentIndicatorUniverse();
var line = "CNCE VO2R14MRA2XX,CNCE,,,,,,22,14,0.323100,-0.945800,-0.745700";

var config = CreateSubscriptionDataConfig();
var date = new DateTime(2022, 04, 21);
var data = (BrainSentimentIndicatorUniverse)factory.Reader(null, line, date, false);
Assert.AreEqual(date + TimeSpan.FromDays(1), data.EndTime);
var data = (BrainSentimentIndicatorUniverse)factory.Reader(config, line, date, false);
Assert.AreEqual(date.ConvertFromUtc(config.ExchangeTimeZone) + TimeSpan.FromHours(12), data.EndTime);
Assert.AreEqual("CNCE", data.Symbol.Value);

// Value is 0 because 7-day Sentiment is null
Expand Down Expand Up @@ -174,5 +176,15 @@ private IEnumerable<BrainSentimentIndicatorUniverse> CreateNewSelection()
}
};
}

private SubscriptionDataConfig CreateSubscriptionDataConfig() => new(
typeof(BrainSentimentIndicatorUniverse),
Symbol.None,
Resolution.Daily,
TimeZones.NewYork,
TimeZones.NewYork,
true,
true,
false);
}
}
15 changes: 13 additions & 2 deletions tests/BrainStockRankingUniverseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public void ReaderTest(bool liveMode)
var factory = new BrainStockRankingUniverse();
var line = "AAPL R735QTJ8XC9X,AAPL,1,2,,,20";

var config = CreateSubscriptionDataConfig();
var now = new DateTime(2022, 04, 21);
var data = (BrainStockRankingUniverse)factory.Reader(null, line, now, liveMode);
Assert.AreEqual(now + TimeSpan.FromDays(1), data.EndTime);
var data = (BrainStockRankingUniverse)factory.Reader(config, line, now, liveMode);
Assert.AreEqual(now.ConvertFromUtc(config.ExchangeTimeZone) + TimeSpan.FromHours(12), data.EndTime);
Assert.AreEqual(1, data.Rank2Days);
Assert.AreEqual(2, data.Rank3Days);
Assert.AreEqual(null, data.Rank5Days);
Expand Down Expand Up @@ -129,5 +130,15 @@ private IEnumerable<BrainStockRankingUniverse> CreateNewSelection()
}
};
}

private SubscriptionDataConfig CreateSubscriptionDataConfig() => new(
typeof(BrainStockRankingUniverse),
Symbol.None,
Resolution.Daily,
TimeZones.NewYork,
TimeZones.NewYork,
true,
true,
false);
}
}

0 comments on commit 232612e

Please sign in to comment.