Skip to content

Commit 124af72

Browse files
Improve HistoryRequestBenchmark algorithm
- The HistoryRequestBenchmark will now perform a daily and minute history request at end of day. - C# performance is at ~15k data points per second - Python performance is at ~11k data points per second
1 parent a55181a commit 124af72

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

Algorithm.CSharp/Benchmarks/HistoryRequestBenchmark.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,29 @@
1313
* limitations under the License.
1414
*/
1515

16-
using QuantConnect.Data;
16+
using System.Linq;
1717

1818
namespace QuantConnect.Algorithm.CSharp.Benchmarks
1919
{
2020
public class HistoryRequestBenchmark : QCAlgorithm
2121
{
22-
private Symbol symbol;
22+
private Symbol _symbol;
2323
public override void Initialize()
2424
{
25-
SetStartDate(2015, 01, 01);
25+
SetStartDate(2010, 01, 01);
2626
SetEndDate(2018, 01, 01);
2727
SetCash(10000);
28-
symbol = AddEquity("SPY", Resolution.Hour).Symbol;
28+
_symbol = AddEquity("SPY").Symbol;
2929
}
3030

31-
public override void OnData(Slice data)
31+
public override void OnEndOfDay()
3232
{
33-
History(symbol, 2, Resolution.Daily);
34-
History(symbol, 4, Resolution.Minute);
33+
var minuteHistory = History(_symbol, 60, Resolution.Minute);
34+
var lastHourHigh = minuteHistory.Select(minuteBar => minuteBar.High).DefaultIfEmpty(0).Max();
35+
var dailyHistory = History(_symbol, 1, Resolution.Daily).First();
36+
var dailyHigh = dailyHistory.High;
37+
var dailyLow = dailyHistory.Low;
38+
var dailyOpen = dailyHistory.Open;
3539
}
3640
}
3741
}

Algorithm.Python/Benchmarks/HistoryRequestBenchmark.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@
2626
class HistoryRequestBenchmark(QCAlgorithm):
2727

2828
def Initialize(self):
29+
self.SetStartDate(2010, 1, 1)
30+
self.SetEndDate(2018, 1, 1)
31+
self.SetCash(10000)
32+
self.symbol = self.AddEquity("SPY").Symbol
2933

30-
self.SetStartDate(2015, 1, 1)
31-
self.SetEndDate(2018, 1, 1)
32-
self.SetCash(10000)
33-
self.symbol = self.AddEquity("SPY", Resolution.Hour).Symbol
34-
35-
def OnData(self, data):
36-
self.History([self.symbol], 2, Resolution.Daily)
37-
self.History([self.symbol], 4, Resolution.Minute)
34+
def OnEndOfDay(self):
35+
minuteHistory = self.History([self.symbol], 60, Resolution.Minute)
36+
lastHourHigh = 0
37+
for index, row in minuteHistory.loc["SPY"].iterrows():
38+
if lastHourHigh < row["high"]:
39+
lastHourHigh = row["high"]
40+
41+
dailyHistory = self.History([self.symbol], 1, Resolution.Daily).loc["SPY"].head()
42+
dailyHistoryHigh = dailyHistory["high"]
43+
dailyHistoryLow = dailyHistory["low"]
44+
dailyHistoryOpen = dailyHistory["open"]

0 commit comments

Comments
 (0)