mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-08 13:50:01 +08:00
MAINT: Factor out downside risk calculation.
Move the downside risk calculation into the main risk module; so that the same calculation can eventually be used by both the period and cumulative calculations, to prevent implementation drift.
This commit is contained in:
@@ -30,6 +30,7 @@ from . risk import (
|
||||
alpha,
|
||||
check_entry,
|
||||
choose_treasury,
|
||||
downside_risk,
|
||||
)
|
||||
|
||||
log = logbook.Logger('Risk Cumulative')
|
||||
@@ -449,10 +450,9 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}"
|
||||
return np.std(daily_returns) * math.sqrt(252)
|
||||
|
||||
def calculate_downside_risk(self):
|
||||
rets = self.algorithm_returns
|
||||
mar = self.mean_returns
|
||||
downside_diff = (rets[rets < mar] - mar).valid()
|
||||
return np.std(downside_diff) * math.sqrt(252)
|
||||
return downside_risk(self.algorithm_returns,
|
||||
self.mean_returns,
|
||||
252)
|
||||
|
||||
def calculate_beta(self):
|
||||
"""
|
||||
|
||||
@@ -56,6 +56,7 @@ Risk Report
|
||||
"""
|
||||
|
||||
import logbook
|
||||
import math
|
||||
import numpy as np
|
||||
|
||||
from zipline.finance import trading
|
||||
@@ -103,6 +104,13 @@ def sharpe_ratio(algorithm_volatility, algorithm_return, treasury_return):
|
||||
return (algorithm_return - treasury_return) / algorithm_volatility
|
||||
|
||||
|
||||
def downside_risk(algorithm_returns, mean_returns, normalization_factor):
|
||||
rets = algorithm_returns
|
||||
mar = mean_returns
|
||||
downside_diff = (rets[rets < mar] - mar).valid()
|
||||
return np.std(downside_diff) * math.sqrt(normalization_factor)
|
||||
|
||||
|
||||
def sortino_ratio(algorithm_returns, algorithm_period_return, mar):
|
||||
"""
|
||||
http://en.wikipedia.org/wiki/Sortino_ratio
|
||||
|
||||
Reference in New Issue
Block a user