|
| 1 | +""" |
| 2 | +Analytics Resource |
| 3 | +
|
| 4 | +Price analytics and statistical analysis operations. |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Dict, Any, Optional |
| 8 | + |
| 9 | + |
| 10 | +class AnalyticsResource: |
| 11 | + """Resource for price analytics and statistics.""" |
| 12 | + |
| 13 | + def __init__(self, client): |
| 14 | + """Initialize analytics resource. |
| 15 | +
|
| 16 | + Args: |
| 17 | + client: OilPriceAPI client instance |
| 18 | + """ |
| 19 | + self.client = client |
| 20 | + |
| 21 | + def performance( |
| 22 | + self, |
| 23 | + commodity: Optional[str] = None, |
| 24 | + days: int = 30 |
| 25 | + ) -> Dict[str, Any]: |
| 26 | + """Get price performance analysis. |
| 27 | +
|
| 28 | + Args: |
| 29 | + commodity: Commodity code (if None, returns all commodities) |
| 30 | + days: Number of days for performance calculation |
| 31 | +
|
| 32 | + Returns: |
| 33 | + Performance metrics with returns, volatility, and trends |
| 34 | +
|
| 35 | + Example: |
| 36 | + >>> perf = client.analytics.performance("BRENT_CRUDE_USD", days=30) |
| 37 | + >>> print(f"30-day Return: {perf['return_pct']}%") |
| 38 | + >>> print(f"Volatility: {perf['volatility']}") |
| 39 | + >>> print(f"Trend: {perf['trend']}") |
| 40 | + """ |
| 41 | + params = {"days": days} |
| 42 | + if commodity: |
| 43 | + params["commodity"] = commodity |
| 44 | + |
| 45 | + response = self.client.request( |
| 46 | + method="GET", |
| 47 | + path="/v1/analytics/performance", |
| 48 | + params=params |
| 49 | + ) |
| 50 | + |
| 51 | + # Parse response |
| 52 | + if "data" in response: |
| 53 | + return response["data"] |
| 54 | + return response |
| 55 | + |
| 56 | + def statistics(self, commodity: str, days: int = 30) -> Dict[str, Any]: |
| 57 | + """Get statistical analysis for a commodity. |
| 58 | +
|
| 59 | + Args: |
| 60 | + commodity: Commodity code |
| 61 | + days: Number of days for statistical analysis |
| 62 | +
|
| 63 | + Returns: |
| 64 | + Statistical metrics (mean, median, std dev, min, max, etc.) |
| 65 | +
|
| 66 | + Example: |
| 67 | + >>> stats = client.analytics.statistics("WTI_USD", days=90) |
| 68 | + >>> print(f"Mean: ${stats['mean']:.2f}") |
| 69 | + >>> print(f"Std Dev: ${stats['std_dev']:.2f}") |
| 70 | + >>> print(f"Min: ${stats['min']:.2f}") |
| 71 | + >>> print(f"Max: ${stats['max']:.2f}") |
| 72 | + """ |
| 73 | + response = self.client.request( |
| 74 | + method="GET", |
| 75 | + path="/v1/analytics/statistics", |
| 76 | + params={ |
| 77 | + "commodity": commodity, |
| 78 | + "days": days |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + # Parse response |
| 83 | + if "data" in response: |
| 84 | + return response["data"] |
| 85 | + return response |
| 86 | + |
| 87 | + def correlation( |
| 88 | + self, |
| 89 | + commodity1: str, |
| 90 | + commodity2: str, |
| 91 | + days: int = 90 |
| 92 | + ) -> Dict[str, Any]: |
| 93 | + """Get correlation analysis between two commodities. |
| 94 | +
|
| 95 | + Args: |
| 96 | + commodity1: First commodity code |
| 97 | + commodity2: Second commodity code |
| 98 | + days: Number of days for correlation calculation |
| 99 | +
|
| 100 | + Returns: |
| 101 | + Correlation metrics and analysis |
| 102 | +
|
| 103 | + Example: |
| 104 | + >>> corr = client.analytics.correlation( |
| 105 | + ... "BRENT_CRUDE_USD", |
| 106 | + ... "WTI_USD", |
| 107 | + ... days=90 |
| 108 | + ... ) |
| 109 | + >>> print(f"Correlation: {corr['correlation']:.3f}") |
| 110 | + >>> print(f"P-value: {corr['p_value']:.4f}") |
| 111 | + """ |
| 112 | + response = self.client.request( |
| 113 | + method="GET", |
| 114 | + path="/v1/analytics/correlation", |
| 115 | + params={ |
| 116 | + "commodity1": commodity1, |
| 117 | + "commodity2": commodity2, |
| 118 | + "days": days |
| 119 | + } |
| 120 | + ) |
| 121 | + |
| 122 | + # Parse response |
| 123 | + if "data" in response: |
| 124 | + return response["data"] |
| 125 | + return response |
| 126 | + |
| 127 | + def trend(self, commodity: str, days: int = 30) -> Dict[str, Any]: |
| 128 | + """Get trend analysis for a commodity. |
| 129 | +
|
| 130 | + Args: |
| 131 | + commodity: Commodity code |
| 132 | + days: Number of days for trend analysis |
| 133 | +
|
| 134 | + Returns: |
| 135 | + Trend metrics with direction, strength, and momentum |
| 136 | +
|
| 137 | + Example: |
| 138 | + >>> trend = client.analytics.trend("NATURAL_GAS_USD", days=30) |
| 139 | + >>> print(f"Direction: {trend['direction']}") |
| 140 | + >>> print(f"Strength: {trend['strength']}") |
| 141 | + >>> print(f"Momentum: {trend['momentum']}") |
| 142 | + """ |
| 143 | + response = self.client.request( |
| 144 | + method="GET", |
| 145 | + path="/v1/analytics/trend", |
| 146 | + params={ |
| 147 | + "commodity": commodity, |
| 148 | + "days": days |
| 149 | + } |
| 150 | + ) |
| 151 | + |
| 152 | + # Parse response |
| 153 | + if "data" in response: |
| 154 | + return response["data"] |
| 155 | + return response |
| 156 | + |
| 157 | + def spread(self, commodity1: str, commodity2: str) -> Dict[str, Any]: |
| 158 | + """Get spread analysis between two commodities. |
| 159 | +
|
| 160 | + Args: |
| 161 | + commodity1: First commodity code |
| 162 | + commodity2: Second commodity code |
| 163 | +
|
| 164 | + Returns: |
| 165 | + Spread analysis with current spread and historical statistics |
| 166 | +
|
| 167 | + Example: |
| 168 | + >>> spread = client.analytics.spread("BRENT_CRUDE_USD", "WTI_USD") |
| 169 | + >>> print(f"Current Spread: ${spread['current']:.2f}") |
| 170 | + >>> print(f"Average Spread: ${spread['average']:.2f}") |
| 171 | + >>> print(f"Spread Percentile: {spread['percentile']}") |
| 172 | + """ |
| 173 | + response = self.client.request( |
| 174 | + method="GET", |
| 175 | + path="/v1/analytics/spread", |
| 176 | + params={ |
| 177 | + "commodity1": commodity1, |
| 178 | + "commodity2": commodity2 |
| 179 | + } |
| 180 | + ) |
| 181 | + |
| 182 | + # Parse response |
| 183 | + if "data" in response: |
| 184 | + return response["data"] |
| 185 | + return response |
| 186 | + |
| 187 | + def forecast(self, commodity: str) -> Dict[str, Any]: |
| 188 | + """Get price forecast for a commodity. |
| 189 | +
|
| 190 | + Args: |
| 191 | + commodity: Commodity code |
| 192 | +
|
| 193 | + Returns: |
| 194 | + Forecast with predicted prices and confidence intervals |
| 195 | +
|
| 196 | + Example: |
| 197 | + >>> forecast = client.analytics.forecast("BRENT_CRUDE_USD") |
| 198 | + >>> print(f"7-day Forecast: ${forecast['7_day']['price']:.2f}") |
| 199 | + >>> print(f"30-day Forecast: ${forecast['30_day']['price']:.2f}") |
| 200 | + >>> print(f"Confidence: {forecast['confidence']}") |
| 201 | + """ |
| 202 | + response = self.client.request( |
| 203 | + method="GET", |
| 204 | + path="/v1/analytics/forecast", |
| 205 | + params={"commodity": commodity} |
| 206 | + ) |
| 207 | + |
| 208 | + # Parse response |
| 209 | + if "data" in response: |
| 210 | + return response["data"] |
| 211 | + return response |
0 commit comments