import math
import statistics
def calculate_profit_factor_and_sharpe(historical_positions):
"""
计算 Profit Factor 和 Sharpe Ratio 基于历史位置数据。
:param historical_positions: 列表,每个元素是一个字典,包含 'pnl' (利润/损失) 和 'roi' (回报率,百分比形式)。
:return: 元组 (profit_factor, sharpe_ratio)
"""
if not historical_positions:
return 0.0, 0.0
# 计算 Profit Factor
total_profit = sum(pos['pnl'] for pos in historical_positions if pos['pnl'] > 0)
total_loss = sum(pos['pnl'] for pos in historical_positions if pos['pnl'] < 0)
profit_factor = total_profit / abs(total_loss) if total_loss != 0 else 0.0 # 避免除以零
# 计算 Sharpe Ratio
returns = [pos['roi'] / 100 for pos in historical_positions] # 将 ROI 转换为小数形式
mean_return = statistics.mean(returns)
std_return = statistics.stdev(returns) if len(returns) > 1 else 0.0 # 标准差,样本大小 >1
n = len(returns)
if std_return == 0:
sharpe_ratio = 0.0
else:
sharpe_ratio = mean_return / std_return * math.sqrt(365 / n) # 年化 Sharpe,无风险率为0
return profit_factor, sharpe_ratio
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)