Support for custom summary statistics at runtime
This commit is contained in:
@@ -18,6 +18,9 @@ from AlgorithmImports import *
|
||||
### </summary>
|
||||
class StatisticsResultsAlgorithm(QCAlgorithm):
|
||||
|
||||
MostTradedSecurityStatistic = "Most Traded Security"
|
||||
MostTradedSecurityTradeCountStatistic = "Most Traded Security Trade Count"
|
||||
|
||||
def Initialize(self):
|
||||
self.SetStartDate(2013, 10, 7)
|
||||
self.SetEndDate(2013, 10, 11)
|
||||
@@ -32,7 +35,9 @@ class StatisticsResultsAlgorithm(QCAlgorithm):
|
||||
self.fastIbmEma = self.EMA(self.spy, 10, Resolution.Minute)
|
||||
self.slowIbmEma = self.EMA(self.spy, 30, Resolution.Minute)
|
||||
|
||||
def OnData(self, data):
|
||||
self.trade_counts = {self.spy: 0, self.ibm: 0}
|
||||
|
||||
def OnData(self, data: Slice):
|
||||
if not self.slowSpyEma.IsReady: return
|
||||
|
||||
if self.fastSpyEma > self.slowSpyEma:
|
||||
@@ -55,3 +60,50 @@ class StatisticsResultsAlgorithm(QCAlgorithm):
|
||||
# Access a single statistic
|
||||
self.Log(f"Total trades so far: {statistics[PerformanceMetrics.TotalTrades]}")
|
||||
self.Log(f"Sharpe Ratio: {statistics[PerformanceMetrics.SharpeRatio]}")
|
||||
|
||||
# --------
|
||||
|
||||
# We can also set custom summary statistics:
|
||||
|
||||
if all(count == 0 for count in self.trade_counts.values()):
|
||||
if StatisticsResultsAlgorithm.MostTradedSecurityStatistic in statistics:
|
||||
raise Exception(f"Statistic {StatisticsResultsAlgorithm.MostTradedSecurityStatistic} should not be set yet")
|
||||
if StatisticsResultsAlgorithm.MostTradedSecurityTradeCountStatistic in statistics:
|
||||
raise Exception(f"Statistic {StatisticsResultsAlgorithm.MostTradedSecurityTradeCountStatistic} should not be set yet")
|
||||
else:
|
||||
# The current most traded security should be set in the summary
|
||||
most_trade_security, most_trade_security_trade_count = self.GetMostTradeSecurity()
|
||||
self.CheckMostTradedSecurityStatistic(statistics, most_trade_security, most_trade_security_trade_count)
|
||||
|
||||
# Update the trade count
|
||||
self.trade_counts[orderEvent.Symbol] += 1
|
||||
|
||||
# Set the most traded security
|
||||
most_trade_security, most_trade_security_trade_count = self.GetMostTradeSecurity()
|
||||
self.SetSummaryStatistic(StatisticsResultsAlgorithm.MostTradedSecurityStatistic, most_trade_security)
|
||||
self.SetSummaryStatistic(StatisticsResultsAlgorithm.MostTradedSecurityTradeCountStatistic, most_trade_security_trade_count)
|
||||
|
||||
# Re-calculate statistics:
|
||||
statistics = self.Statistics.Summary
|
||||
|
||||
# Let's keep track of our custom summary statistics after the update
|
||||
self.CheckMostTradedSecurityStatistic(statistics, most_trade_security, most_trade_security_trade_count)
|
||||
|
||||
|
||||
def CheckMostTradedSecurityStatistic(self, statistics: Dict[str, str], mostTradedSecurity: Symbol, tradeCount: int):
|
||||
mostTradedSecurityStatistic = statistics[StatisticsResultsAlgorithm.MostTradedSecurityStatistic]
|
||||
mostTradedSecurityTradeCountStatistic = statistics[StatisticsResultsAlgorithm.MostTradedSecurityTradeCountStatistic]
|
||||
self.Log(f"Most traded security: {mostTradedSecurityStatistic}")
|
||||
self.Log(f"Most traded security trade count: {mostTradedSecurityTradeCountStatistic}")
|
||||
|
||||
if mostTradedSecurityStatistic != mostTradedSecurity:
|
||||
raise Exception(f"Most traded security should be {mostTradedSecurity} but it is {mostTradedSecurityStatistic}")
|
||||
|
||||
if mostTradedSecurityTradeCountStatistic != str(tradeCount):
|
||||
raise Exception(f"Most traded security trade count should be {tradeCount} but it is {mostTradedSecurityTradeCountStatistic}")
|
||||
|
||||
def GetMostTradeSecurity(self) -> Tuple[Symbol, int]:
|
||||
most_trade_security = max(self.trade_counts, key=lambda symbol: self.trade_counts[symbol])
|
||||
most_trade_security_trade_count = self.trade_counts[most_trade_security]
|
||||
return most_trade_security, most_trade_security_trade_count
|
||||
|
||||
|
||||
Reference in New Issue
Block a user