Skip to content

Commit

Permalink
Standarize implementation
Browse files Browse the repository at this point in the history
- Standarize implementation. See QuantConnect/Lean#7837
- Adjust and fix example algorithms
  • Loading branch information
Martin-Molinero committed Mar 11, 2024
1 parent 60456ce commit 2b3faf2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
1 change: 0 additions & 1 deletion QuiverCongressDataPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ public override BaseData Clone()
{
Symbol = Symbol,
Time = Time,
EndTime = EndTime,
RecordDate = RecordDate,
ReportDate = ReportDate,
TransactionDate = TransactionDate,
Expand Down
27 changes: 7 additions & 20 deletions QuiverQuantCongressUniverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Orders;
using static QuantConnect.StringExtensions;
Expand All @@ -26,7 +27,7 @@ namespace QuantConnect.DataSource
/// <summary>
/// Universe Selection helper class for QuiverQuant Congress dataset
/// </summary>
public class QuiverQuantCongressUniverse : QuiverCongressDataPoint
public class QuiverQuantCongressUniverse : QuiverCongress
{
/// <summary>
/// Return the URL string source of the file. This will be converted to a stream
Expand All @@ -46,7 +47,8 @@ public override SubscriptionDataSource GetSource(SubscriptionDataConfig config,
"universe",
$"{date.ToStringInvariant(DateFormat.EightCharacter)}.csv"
),
SubscriptionTransportMedium.LocalFile
SubscriptionTransportMedium.LocalFile,
FileFormat.FoldingCollection
);
}

Expand All @@ -64,7 +66,7 @@ public override BaseData Reader(SubscriptionDataConfig config, string line, Date
var amount = csv[7].IfNotNullOrEmpty<decimal?>(s => decimal.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture));
var maximumAmount = csv[8].IfNotNullOrEmpty<decimal?>(s => decimal.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture));

return new QuiverQuantCongressUniverse
return new QuiverCongressDataPoint
{
RecordDate = Parse.DateTimeExact(csv[2], "yyyyMMdd"),
ReportDate = Parse.DateTimeExact(csv[3], "yyyyMMdd"),
Expand All @@ -88,12 +90,7 @@ public override BaseData Reader(SubscriptionDataConfig config, string line, Date
/// </summary>
public override string ToString()
{
return Invariant($"{Symbol}({ReportDate}) :: ") +
Invariant($"Transaction Date: {TransactionDate} ") +
Invariant($"Representative: {Representative} ") +
Invariant($"House: {House} ") +
Invariant($"Transaction: {Transaction} ") +
Invariant($"Amount: {Amount}");
return Invariant($"{EndTime:yyyyMMdd}: {string.Join(",", Data.Select(x => x.ToString()))}");
}

/// <summary>
Expand All @@ -113,17 +110,7 @@ public override BaseData Clone()
Symbol = Symbol,
Time = Time,
EndTime = EndTime,
RecordDate = RecordDate,
ReportDate = ReportDate,
TransactionDate = TransactionDate,
Representative = Representative,
Transaction = Transaction,
Amount = Amount,
MaximumAmount = MaximumAmount,
House = House,
Party = Party,
District = District,
State = State,
Data = Data,
};
}
}
Expand Down
20 changes: 17 additions & 3 deletions QuiverQuantCongressUniverseSelectionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,32 @@ public override void Initialize()
SetCash(100000);

// add a custom universe data source (defaults to usa-equity)
AddUniverse<QuiverQuantCongressUniverse>("QuiverQuantCongresssUniverse", Resolution.Daily, data =>
var universe = AddUniverse<QuiverQuantCongressUniverse>(data =>
{
foreach (var datum in data)
foreach (QuiverQuantCongressUniverse datum in data)
{
Log($"{datum.Symbol},{datum.Representative},{datum.Amount},{datum.Transaction}");
}

// define our selection criteria
return from d in data
return from QuiverQuantCongressUniverse d in data
where d.Amount > 200000 && d.Transaction == OrderDirection.Buy
select d.Symbol;
});

var history = History(universe, 1).ToList();
if (history.Count != 1)
{
throw new System.Exception($"Unexpected historical data count!");
}
foreach (var dataForDate in history)
{
var coarseData = dataForDate.ToList();
if (coarseData.Count < 1)
{
throw new System.Exception($"Unexpected historical universe data!");
}
}
}

public override void OnSecuritiesChanged(SecurityChanges changes)
Expand Down
10 changes: 9 additions & 1 deletion QuiverQuantCongressUniverseSelectionAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ def Initialize(self):
self.SetCash(100000)

# add a custom universe data source (defaults to usa-equity)
self.AddUniverse(QuiverQuantCongressUniverse, "QuiverQuantCongresssUniverse", Resolution.Daily, self.UniverseSelection)
universe = self.AddUniverse(QuiverQuantCongressUniverse, self.UniverseSelection)

history = self.History(universe, TimeSpan(1, 0, 0, 0))
if len(history) != 1:
raise ValueError(f"Unexpected history count {len(history)}! Expected 1")

for dataForDate in history:
if len(dataForDate) < 1:
raise ValueError(f"Unexpected historical universe data!")

def UniverseSelection(self, data):
for datum in data:
Expand Down

0 comments on commit 2b3faf2

Please sign in to comment.