Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug #5030 Generation of CFD data not working #5112

Merged
merged 5 commits into from Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Tests/QuantConnect.Tests.csproj
Expand Up @@ -811,6 +811,7 @@
<Compile Include="Symbols.cs" />
<Compile Include="TestExtensions.cs" />
<Compile Include="Indicators\LinearWeightedMovingAverageTests.cs" />
<Compile Include="ToolBox\RandomDataGenerator\RandomDataGeneratorProgramTests.cs" />
<Compile Include="ToolBox\ToolBoxTests.cs" />
<Compile Include="ToolBox\PolygonHistoryTests.cs" />
<Compile Include="ToolBox\PolygonSymbolMapperTests.cs" />
Expand Down
@@ -0,0 +1,48 @@
/*
* 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;
using NUnit.Framework;
using QuantConnect.ToolBox.RandomDataGenerator;

namespace QuantConnect.Tests.ToolBox.RandomDataGenerator
{
[TestFixture]
public class RandomDataGeneratorProgramTests
{
[Test]
[TestCase("2020, 1, 1 00:00:00", "2020, 1, 1 00:00:00", "2020, 1, 1 00:00:00")]
[TestCase("2020, 1, 1 00:00:00", "2020, 2, 1 00:00:00", "2020, 1, 16 12:00:00")] // (31 days / 2) = 15.5 = 16 Rounds up to 12 pm
[TestCase("2020, 1, 1 00:00:00", "2020, 3, 1 00:00:00", "2020, 1, 31 00:00:00")] // (60 days / 2) = 30
[TestCase("2020, 1, 1 00:00:00", "2020, 6, 1 00:00:00", "2020, 3, 17 00:00:00")] // (152 days / 2) = 76

public void NextRandomGeneratedData(DateTime start, DateTime end, DateTime expectedMidPoint)
{
var randomValueGenerator = new RandomValueGenerator();
var midPoint = RandomDataGeneratorProgram.GetDateMidpoint(start, end);
var delistDate = RandomDataGeneratorProgram.GetDelistingDate(start, end, randomValueGenerator);

// midPoint and expectedMidPoint must be the same
Assert.AreEqual(expectedMidPoint, midPoint);

// start must be less than or equal to end
Assert.LessOrEqual(start, end);

// delistDate must be less than or equal to end
Assert.LessOrEqual(delistDate, end);
Assert.GreaterOrEqual(delistDate, midPoint);
}
}
}
23 changes: 21 additions & 2 deletions ToolBox/RandomDataGenerator/RandomDataGeneratorProgram.cs
Expand Up @@ -64,6 +64,26 @@ string dividendEveryQuarterPercentageString
Console.ReadKey();
}

public static DateTime GetDateMidpoint(DateTime start, DateTime end)
{
TimeSpan span = end.Subtract(start);
int span_time = (int)span.TotalMinutes;
double diff_span = -(span_time / 2.0);
DateTime start_time = end.AddMinutes(Math.Round(diff_span, 2, MidpointRounding.ToEven));

//Returns a DateTime object that is halfway between start and end
return start_time;
}

public static DateTime GetDelistingDate(DateTime start, DateTime end, RandomValueGenerator randomValueGenerator)
{
var mid_point = GetDateMidpoint(start, end);
var delist_Date = randomValueGenerator.NextDate(mid_point, end, null);

//Returns a DateTime object that is a random value between the mid_point and end
return delist_Date;
}

public static void GenerateRandomData(RandomDataGeneratorSettings settings, ConsoleLeveledOutput output)
{
// can specify a seed value in this ctor if determinism is desired
Expand All @@ -83,17 +103,16 @@ public static void GenerateRandomData(RandomDataGeneratorSettings settings, Cons
var count = 0;
var progress = 0d;
var previousMonth = -1;
var previousDay = settings.Start;

Func<Tick, DateTime> tickDay = (tick => new DateTime(tick.Time.Year, tick.Time.Month, tick.Time.Day));
Func<Data.BaseData, DateTime> dataDay = (data => new DateTime(data.Time.Year, data.Time.Month, data.Time.Day));

foreach (var currentSymbol in symbolGenerator.GenerateRandomSymbols())
{
// This is done so that we can update the symbol in the case of a rename event
var delistDate = GetDelistingDate(settings.Start, settings.End, randomValueGenerator);
var symbol = currentSymbol;
var willBeDelisted = randomValueGenerator.NextBool(1.0);
var delistDate = randomValueGenerator.NextDate(settings.Start.AddMonths(6), settings.End, null);
var monthsTrading = 0;

// Keep track of renamed symbols and the time they were renamed.
Expand Down