-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreport_generator.py
114 lines (87 loc) · 3.43 KB
/
report_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# report_generator.py
import pandas as pd
import numpy as np
from datetime import datetime
import os
from pathlib import Path
from RiskVisualization import RiskVisualizer
class ReportGenerator:
def __init__(self, data_path='output/btc_raw_data.csv'):
"""Initialize report generator"""
self.output_dir = 'output/report/'
self.figures_dir = 'output/figures/'
# Create output directories
Path(self.output_dir).mkdir(parents=True, exist_ok=True)
# Initialize components
self.visualizer = RiskVisualizer(data_path)
self.data = pd.read_csv(data_path)
def generate_report(self):
"""Generate analysis report with visualizations"""
print("Generating visualizations...")
self.visualizer.generate_all_plots()
# Calculate metrics
returns = pd.Series(self.data['Returns']).dropna()
volatility = returns.std() * np.sqrt(252)
var_95 = returns.quantile(0.05)
var_99 = returns.quantile(0.01)
print("Generating report...")
markdown_content = f"""# Bitcoin Risk Analysis Report for Yala Protocol
Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
## 1. Market Analysis
### Price and Volatility Trends

The above graph shows Bitcoin's price history and its 30-day rolling volatility. Key observations:
- Current annualized volatility: {volatility:.2%}
- Historical price patterns show cyclical volatility regimes
### Volatility Regime Analysis

The volatility regime analysis identifies three distinct market states:
- Low volatility: < {volatility*0.7:.2%}
- Medium volatility: {volatility*0.7:.2%} - {volatility*1.3:.2%}
- High volatility: > {volatility*1.3:.2%}
## 2. Risk Analysis
### Return Distribution

Key risk metrics:
- Value at Risk (95%): {var_95:.2%}
- Value at Risk (99%): {var_99:.2%}
- Annualized Volatility: {volatility:.2%}
### Drawdown Analysis

### Recovery Patterns

The analysis of recovery patterns shows:
- Median recovery time after significant drops
- Pattern of price rebounds
- Typical recovery trajectories
## 3. Protocol Parameters
### Interest Rate Model

Recommended protocol parameters:
- Initial LTV: {min(0.8, 1/(1 + volatility)):.2%}
- Liquidation threshold: {min(0.85, 1/(1 + volatility * 0.8)):.2%}
- Base interest rate: {max(0.03, volatility * 0.5):.2%}
## 4. Risk Management Recommendations
1. Dynamic Parameters
- Adjust LTV based on volatility regimes
- Implement graduated liquidation thresholds
- Use adaptive interest rates
2. Risk Monitoring
- Track volatility regime changes
- Monitor drawdown patterns
- Analyze recovery metrics
3. Safety Measures
- Multi-stage liquidation process
- Grace period for margin calls
- Emergency circuit breakers
"""
# Save report
report_path = os.path.join(self.output_dir, 'risk_analysis_report.md')
with open(report_path, 'w') as f:
f.write(markdown_content)
print(f"Report saved to {report_path}")
def main():
generator = ReportGenerator()
generator.generate_report()
if __name__ == "__main__":
main()