def test_sharpe_ratio(self, net_worths): scheme = RiskAdjustedReturns(return_algorithm='sharpe', risk_free_rate=0) returns = net_worths.diff() sharpe_ratio = scheme._sharpe_ratio(returns) expected_ratio = returns.mean() / (returns.std() + 1E-9) assert sharpe_ratio == expected_ratio
def test_sharpe_ratio(self, net_worths): scheme = RiskAdjustedReturns(return_algorithm='sharpe', risk_free_rate=0, window_size=1) returns = net_worths[-2:].pct_change().dropna() expected_ratio = (np.mean(returns) + 1E-9) / (np.std(returns) + 1E-9) sharpe_ratio = scheme._sharpe_ratio(returns) assert sharpe_ratio == expected_ratio
def test_sortino_ratio(self, net_worths): scheme = RiskAdjustedReturns(return_algorithm='sortino', risk_free_rate=0, target_returns=0) returns = net_worths.pct_change() sortino_ratio = scheme._sortino_ratio(returns) expected_return = returns.mean() downside_std = returns[returns < 0].std() expected_ratio = expected_return / downside_std assert sortino_ratio == expected_ratio
def test_sortino_ratio(self, net_worths): scheme = RiskAdjustedReturns(return_algorithm='sortino', risk_free_rate=0, target_returns=0, window_size=1) returns = net_worths[-2:].pct_change().dropna() downside_returns = returns.copy() downside_returns[returns < 0] = returns**2 expected_return = np.mean(returns) downside_std = np.sqrt(np.std(downside_returns)) expected_ratio = (expected_return + 1E-9) / (downside_std + 1E-9) sortino_ratio = scheme._sortino_ratio(returns) assert sortino_ratio == expected_ratio
def test_sortino_ratio(self, net_worths): scheme = RiskAdjustedReturns( return_algorithm='sortino', risk_free_rate=0, target_returns=0) returns = net_worths['net_worth'].diff() sortino_ratio = scheme._sortino_ratio(returns) downside_returns = pd.Series([0]) returns[returns < 0] = returns ** 2 expected_return = returns.mean() downside_std = np.sqrt(downside_returns.mean()) expected_ratio = expected_return / (downside_std + 1E-9) assert sortino_ratio == expected_ratio
def env(): context = { "base_instrument": USD, "actions": { "pairs": [USD / BTC], "stop_loss_percentages": [0.02, 0.04, 0.06], "take_profit_percentages": [0.01, 0.02, 0.03], "trade_sizes": 10, "trade_side": TradeSide.BUY, "trade_type": TradeType.MARKET, "order_listener": None }, "rewards": { "return_algorithm": "sharpe", "risk_free_rate": 0, "target_returns": 0 }, "exchanges": { "model_type": "FBM", "hurst": 0.61, "timeframe": "1d", "base_price": 7500, "base_volume": 12000 } } with TradingContext(**context): action_scheme = ManagedRiskOrders() reward_scheme = RiskAdjustedReturns() exchange = Exchange() portfolio = Portfolio(USD, [ Wallet(exchange, 100000 * USD), Wallet(exchange, 0 * BTC) ]) env = TradingEnvironment( portfolio=portfolio, action_scheme=action_scheme, reward_scheme=reward_scheme, window_size=14, enable_logger=False ) return env