Spaces:
Sleeping
Sleeping
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
|
| 5 |
+
def format_currency(value):
|
| 6 |
+
"""Format number as currency"""
|
| 7 |
+
return f"${value:,.2f}"
|
| 8 |
+
|
| 9 |
+
def format_percentage(value):
|
| 10 |
+
"""Format number as percentage"""
|
| 11 |
+
return f"{value:+.2f}%"
|
| 12 |
+
|
| 13 |
+
def get_market_status():
|
| 14 |
+
"""Get current market status"""
|
| 15 |
+
now = datetime.now()
|
| 16 |
+
if now.weekday() >= 5: # Weekend
|
| 17 |
+
return "Market Closed"
|
| 18 |
+
|
| 19 |
+
market_open = now.replace(hour=9, minute=30, second=0, microsecond=0)
|
| 20 |
+
market_close = now.replace(hour=16, minute=0, second=0, microsecond=0)
|
| 21 |
+
|
| 22 |
+
if now < market_open:
|
| 23 |
+
return "Pre-Market"
|
| 24 |
+
elif now > market_close:
|
| 25 |
+
return "After-Hours"
|
| 26 |
+
else:
|
| 27 |
+
return "Market Open"
|
| 28 |
+
|
| 29 |
+
def calculate_support_resistance(prices):
|
| 30 |
+
"""Calculate simple support and resistance levels"""
|
| 31 |
+
if len(prices) < 5:
|
| 32 |
+
return {"support": prices[-1] * 0.95, "resistance": prices[-1] * 1.05}
|
| 33 |
+
|
| 34 |
+
support = min(prices[-5:]) * 0.98
|
| 35 |
+
resistance = max(prices[-5:]) * 1.02
|
| 36 |
+
|
| 37 |
+
return {
|
| 38 |
+
"support": round(support, 2),
|
| 39 |
+
"resistance": round(resistance, 2)
|
| 40 |
+
}
|