| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import { Position } from '../types/core'
- export interface RiskSummary {
- leverageRatio: number
- var: number
- maxDrawdown: number
- level: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL'
- score: number
- recommendations: string[]
- }
- export class RiskAssessor {
- /**
- * 真实的风险评估逻辑实现
- */
- evaluatePosition(position: Position): RiskSummary {
- const leverageRatio = position.leverage || 1
- const unrealizedPnL = position.unrealizedPnL || 0
- const notional = Math.abs(parseFloat(position.size)) * (position.markPrice || 0)
- // 1. 计算Value at Risk (VaR) - 使用简化的历史模拟法
- const volatility = this.estimateVolatility(position.symbol)
- const confidenceLevel = 0.95 // 95%置信度
- const timeHorizon = 1 // 1天
- const varValue = notional * volatility * Math.sqrt(timeHorizon) * this.getZScore(confidenceLevel)
- // 2. 计算最大回撤
- const entryPrice = parseFloat(position.entryPrice || '0')
- const currentPrice = position.markPrice || 0
- const priceChange = Math.abs(currentPrice - entryPrice) / entryPrice
- const maxDrawdown = notional * priceChange
- // 3. 综合风险评分 (0-100)
- const leverageScore = Math.min(leverageRatio * 10, 50) // 杠杆风险 (最高50分)
- const pnlScore = Math.min(Math.abs(unrealizedPnL) / 1000 * 20, 30) // PnL风险 (最高30分)
- const concentrationScore = Math.min(notional / 10000 * 20, 20) // 集中度风险 (最高20分)
- const totalScore = leverageScore + pnlScore + concentrationScore
- // 4. 确定风险等级
- let level: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL'
- if (totalScore < 25) level = 'LOW'
- else if (totalScore < 50) level = 'MEDIUM'
- else if (totalScore < 75) level = 'HIGH'
- else level = 'CRITICAL'
- // 5. 生成风险建议
- const recommendations = this.generateRecommendations(
- leverageRatio,
- unrealizedPnL,
- notional,
- level
- )
- return {
- leverageRatio,
- var: varValue,
- maxDrawdown,
- level,
- score: totalScore,
- recommendations,
- }
- }
- /**
- * 估算波动率 (简化实现)
- */
- private estimateVolatility(symbol: string): number {
- // 基于历史数据的简化波动率估算
- const volatilityMap: Record<string, number> = {
- 'BTC-USD': 0.04, // 4%日波动率
- 'ETH-USD': 0.05, // 5%日波动率
- 'SOL-USD': 0.08, // 8%日波动率
- }
- return volatilityMap[symbol] || 0.06 // 默认6%
- }
- /**
- * 获取置信度对应的Z分数
- */
- private getZScore(confidenceLevel: number): number {
- const zScoreMap: Record<number, number> = {
- 0.90: 1.28,
- 0.95: 1.645,
- 0.99: 2.33,
- }
- return zScoreMap[confidenceLevel] || 1.645
- }
- /**
- * 生成风险管理建议
- */
- private generateRecommendations(
- leverage: number,
- pnl: number,
- notional: number,
- level: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL'
- ): string[] {
- const recommendations: string[] = []
- // 杠杆相关建议
- if (leverage > 10) {
- recommendations.push('建议降低杠杆倍数至5倍以下')
- } else if (leverage > 5) {
- recommendations.push('监控杠杆风险,考虑适当降低')
- }
- // PnL相关建议
- if (pnl < -500) {
- recommendations.push('亏损较大,建议设置止损')
- } else if (pnl > 1000) {
- recommendations.push('盈利较好,建议部分止盈')
- }
- // 仓位规模建议
- if (notional > 50000) {
- recommendations.push('仓位规模较大,注意分散风险')
- }
- // 风险等级相关建议
- switch (level) {
- case 'CRITICAL':
- recommendations.push('风险等级极高,建议立即平仓')
- break
- case 'HIGH':
- recommendations.push('风险等级较高,建议减仓或对冲')
- break
- case 'MEDIUM':
- recommendations.push('风险等级中等,密切监控市场变化')
- break
- case 'LOW':
- recommendations.push('风险等级较低,可维持当前仓位')
- break
- }
- return recommendations
- }
- }
- export const riskAssessor = new RiskAssessor()
|