Skip to content

Commit

Permalink
Some refactor and cleanup (#8014)
Browse files Browse the repository at this point in the history
* Some refactor and cleanup

- Some refactor and cleanups
- Improve live trading CI tests speed

* Minor tweak for live trading data feed log
  • Loading branch information
Martin-Molinero committed May 10, 2024
1 parent 061d5a9 commit 1c32144
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using QuantConnect.Interfaces;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Util;

namespace QuantConnect.Algorithm.CSharp
{
Expand Down Expand Up @@ -64,33 +65,8 @@ public override void Initialize()

Consolidate<BaseData>(es.Symbol, dataTime =>
{
var start = es.Exchange.Hours.GetPreviousMarketOpen(dataTime, ExtendedMarketHours);
var end = es.Exchange.Hours.GetNextMarketClose(start, ExtendedMarketHours);
if (ExtendedMarketHours)
{
// market might open at 16:30 and close again at 17:00 but we are not interested in using the close so we skip it here
while (end.Date == start.Date)
{
end = es.Exchange.Hours.GetNextMarketClose(end, ExtendedMarketHours);
}
} else
{
// Let's not consider regular market gaps like when market closes at 16:15 and opens again at 16:30
while (true)
{
var potentialEnd = es.Exchange.Hours.GetNextMarketClose(end, ExtendedMarketHours);
if (potentialEnd.Date != end.Date)
{
break;
}
end = potentialEnd;
}
}
var period = end - start;
// based on the given data time we return the start time of it's bar and the expected period size
return new CalendarInfo(start, period);
return LeanData.GetDailyCalendar(dataTime, es.Exchange, ExtendedMarketHours);
}, bar => Assert(bar));
}

Expand Down
42 changes: 40 additions & 2 deletions Common/Data/Consolidators/Calendar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand Down Expand Up @@ -91,7 +91,7 @@ public static class Calendar
/// <summary>
/// Calendar Info for storing information related to the start and period of a consolidator
/// </summary>
public struct CalendarInfo
public readonly struct CalendarInfo
{
/// <summary>
/// Calendar Start
Expand All @@ -103,6 +103,11 @@ public struct CalendarInfo
/// </summary>
public readonly TimeSpan Period;

/// <summary>
/// Calendar End
/// </summary>
public readonly DateTime End => Start + Period;

/// <summary>
/// Constructor for CalendarInfo; used for consolidation calendar
/// </summary>
Expand All @@ -113,5 +118,38 @@ public CalendarInfo(DateTime start, TimeSpan period)
Start = start;
Period = period;
}

public override string ToString()
{
return $"{Start} {Period}";
}

public override bool Equals(object obj)
{
if (obj is not CalendarInfo other)
{
return false;
}
return Start == other.Start && Period == other.Period;
}

public override int GetHashCode()
{
unchecked
{
var hashCode = Start.GetHashCode();
return (hashCode * 397) ^ Period.GetHashCode();
}
}

public static bool operator ==(CalendarInfo left, CalendarInfo right)
{
return left.Equals(right);
}

public static bool operator !=(CalendarInfo left, CalendarInfo right)
{
return !(left == right);
}
}
}
8 changes: 4 additions & 4 deletions Common/Data/Consolidators/QuoteBarConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

using System;
using QuantConnect.Data.Market;
using Python.Runtime;
using QuantConnect.Data.Market;

namespace QuantConnect.Data.Consolidators
{
Expand All @@ -26,7 +26,7 @@ namespace QuantConnect.Data.Consolidators
public class QuoteBarConsolidator : PeriodCountConsolidatorBase<QuoteBar, QuoteBar>
{
/// <summary>
/// Initializes a new instance of the <see cref="TickQuoteBarConsolidator"/> class
/// Initializes a new instance of the <see cref="QuoteBarConsolidator"/> class
/// </summary>
/// <param name="period">The minimum span of time before emitting a consolidated bar</param>
public QuoteBarConsolidator(TimeSpan period)
Expand All @@ -35,7 +35,7 @@ public QuoteBarConsolidator(TimeSpan period)
}

/// <summary>
/// Initializes a new instance of the <see cref="TickQuoteBarConsolidator"/> class
/// Initializes a new instance of the <see cref="QuoteBarConsolidator"/> class
/// </summary>
/// <param name="maxCount">The number of pieces to accept before emitting a consolidated bar</param>
public QuoteBarConsolidator(int maxCount)
Expand All @@ -44,7 +44,7 @@ public QuoteBarConsolidator(int maxCount)
}

/// <summary>
/// Initializes a new instance of the <see cref="TickQuoteBarConsolidator"/> class
/// Initializes a new instance of the <see cref="QuoteBarConsolidator"/> class
/// </summary>
/// <param name="maxCount">The number of pieces to accept before emitting a consolidated bar</param>
/// <param name="period">The minimum span of time before emitting a consolidated bar</param>
Expand Down
70 changes: 3 additions & 67 deletions Common/Data/Market/QuoteBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,59 +368,6 @@ public override BaseData Reader(SubscriptionDataConfig config, string line, Date
return new QuoteBar { Symbol = config.Symbol, Period = config.Increment };
}

private static bool HasShownWarning;

/// <summary>
/// "Scaffold" code - If the data being read is formatted as a TradeBar, use this method to deserialize it
/// TODO: Once all Forex data refactored to use QuoteBar formatted data, remove this method
/// </summary>
/// <param name="config">Symbols, Resolution, DataType, </param>
/// <param name="line">Line from the data file requested</param>
/// <param name="date">Date of this reader request</param>
/// <returns><see cref="QuoteBar"/> with the bid/ask prices set to same values</returns>
[Obsolete("All Forex data should use Quotes instead of Trades.")]
private QuoteBar ParseTradeAsQuoteBar(SubscriptionDataConfig config, DateTime date, string line)
{
if (!HasShownWarning)
{
Logging.Log.Error("QuoteBar.ParseTradeAsQuoteBar(): Data formatted as Trade when Quote format was expected. Support for this will disappear June 2017.");
HasShownWarning = true;
}

var quoteBar = new QuoteBar
{
Period = config.Increment,
Symbol = config.Symbol
};

var csv = line.ToCsv(5);
if (config.Resolution == Resolution.Daily || config.Resolution == Resolution.Hour)
{
// hourly and daily have different time format, and can use slow, robust c# parser.
quoteBar.Time = DateTime.ParseExact(csv[0], DateFormat.TwelveCharacter, CultureInfo.InvariantCulture).ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
}
else
{
//Fast decimal conversion
quoteBar.Time = date.Date.AddMilliseconds(csv[0].ToInt32()).ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
}

// the Bid/Ask bars were already create above, we don't need to recreate them but just set their values
quoteBar.Bid.Open = csv[1].ToDecimal();
quoteBar.Bid.High = csv[2].ToDecimal();
quoteBar.Bid.Low = csv[3].ToDecimal();
quoteBar.Bid.Close = csv[4].ToDecimal();

quoteBar.Ask.Open = csv[1].ToDecimal();
quoteBar.Ask.High = csv[2].ToDecimal();
quoteBar.Ask.Low = csv[3].ToDecimal();
quoteBar.Ask.Close = csv[4].ToDecimal();

quoteBar.Value = quoteBar.Close;

return quoteBar;
}

/// <summary>
/// Parse a quotebar representing a future with a scaling factor
/// </summary>
Expand Down Expand Up @@ -454,7 +401,7 @@ public QuoteBar ParseFuture(SubscriptionDataConfig config, StreamReader streamRe
/// <returns><see cref="QuoteBar"/> with the bid/ask set to same values</returns>
public QuoteBar ParseOption(SubscriptionDataConfig config, string line, DateTime date)
{
return ParseQuote(config, date, line, OptionUseScaleFactor(config.Symbol));
return ParseQuote(config, date, line, LeanData.OptionUseScaleFactor(config.Symbol));
}

/// <summary>
Expand All @@ -467,18 +414,7 @@ public QuoteBar ParseOption(SubscriptionDataConfig config, string line, DateTime
public QuoteBar ParseOption(SubscriptionDataConfig config, StreamReader streamReader, DateTime date)
{
// scale factor only applies for equity and index options
return ParseQuote(config, date, streamReader, useScaleFactor: OptionUseScaleFactor(config.Symbol));
}

/// <summary>
/// Helper method that defines the types of options that should use scale factor
/// </summary>
/// <param name="symbol"></param>
/// <returns></returns>
private static bool OptionUseScaleFactor(Symbol symbol)
{
return symbol.SecurityType == SecurityType.Option ||
symbol.SecurityType == SecurityType.IndexOption;
return ParseQuote(config, date, streamReader, useScaleFactor: LeanData.OptionUseScaleFactor(config.Symbol));
}

/// <summary>
Expand Down Expand Up @@ -651,7 +587,7 @@ private QuoteBar ParseQuote(SubscriptionDataConfig config, DateTime date, string
Symbol = config.Symbol
};

var csv = line.ToCsv(10);
var csv = line.ToCsv(11);
if (config.Resolution == Resolution.Daily || config.Resolution == Resolution.Hour)
{
// hourly and daily have different time format, and can use slow, robust c# parser.
Expand Down

0 comments on commit 1c32144

Please sign in to comment.