Spaces:
Sleeping
Sleeping
Update app/daily.py
Browse files- app/daily.py +38 -13
app/daily.py
CHANGED
|
@@ -8,7 +8,15 @@ import traceback
|
|
| 8 |
import io
|
| 9 |
|
| 10 |
from . import persist
|
| 11 |
-
from . import backblaze as b2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
# ============================================================
|
|
@@ -17,6 +25,9 @@ from . import backblaze as b2 # your existing B2 helper
|
|
| 17 |
def fetch_daily(symbol, date_end, date_start):
|
| 18 |
key = f"daily_{symbol}"
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
if persist.exists(key, "html"):
|
| 21 |
cached = persist.load(key, "html")
|
| 22 |
if cached:
|
|
@@ -24,13 +35,13 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 24 |
|
| 25 |
try:
|
| 26 |
# ----------------------------------------------------
|
| 27 |
-
# Date conversion
|
| 28 |
# ----------------------------------------------------
|
| 29 |
start = dt.strptime(date_start, "%d-%m-%Y").strftime("%Y-%m-%d")
|
| 30 |
end = dt.strptime(date_end, "%d-%m-%Y").strftime("%Y-%m-%d")
|
| 31 |
|
| 32 |
# ----------------------------------------------------
|
| 33 |
-
# Fetch data
|
| 34 |
# ----------------------------------------------------
|
| 35 |
df = yf.download(symbol + ".NS", start=start, end=end)
|
| 36 |
|
|
@@ -60,7 +71,7 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 60 |
df["MA50"] = df["Close"].rolling(50).mean()
|
| 61 |
|
| 62 |
# ====================================================
|
| 63 |
-
# PRICE + VOLUME CHART
|
| 64 |
# ====================================================
|
| 65 |
buf = io.BytesIO()
|
| 66 |
|
|
@@ -74,15 +85,22 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 74 |
plt.legend()
|
| 75 |
plt.grid(True)
|
| 76 |
plt.tight_layout()
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
plt.close()
|
| 79 |
|
| 80 |
buf.seek(0)
|
| 81 |
-
price_key = f"daily/{symbol}_price_volume.
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
# ====================================================
|
| 85 |
-
# MOVING AVERAGE CHART
|
| 86 |
# ====================================================
|
| 87 |
buf = io.BytesIO()
|
| 88 |
|
|
@@ -95,15 +113,22 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 95 |
plt.legend()
|
| 96 |
plt.grid(True)
|
| 97 |
plt.tight_layout()
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
plt.close()
|
| 100 |
|
| 101 |
buf.seek(0)
|
| 102 |
-
ma_key = f"daily/{symbol}_ma.
|
| 103 |
-
|
|
|
|
| 104 |
|
| 105 |
# ====================================================
|
| 106 |
-
# TABLE
|
| 107 |
# ====================================================
|
| 108 |
rows = ""
|
| 109 |
for r in df.tail(100).itertuples():
|
|
@@ -152,4 +177,4 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 152 |
return html
|
| 153 |
|
| 154 |
except Exception:
|
| 155 |
-
return f"<pre>{traceback.format_exc()}</pre>"
|
|
|
|
| 8 |
import io
|
| 9 |
|
| 10 |
from . import persist
|
| 11 |
+
from . import backblaze as b2
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# ============================================================
|
| 15 |
+
# CONFIG
|
| 16 |
+
# ============================================================
|
| 17 |
+
IMAGE_FORMAT = "png"
|
| 18 |
+
IMAGE_EXT = "png"
|
| 19 |
+
DPI = 150
|
| 20 |
|
| 21 |
|
| 22 |
# ============================================================
|
|
|
|
| 25 |
def fetch_daily(symbol, date_end, date_start):
|
| 26 |
key = f"daily_{symbol}"
|
| 27 |
|
| 28 |
+
# --------------------------------------------------------
|
| 29 |
+
# Cache
|
| 30 |
+
# --------------------------------------------------------
|
| 31 |
if persist.exists(key, "html"):
|
| 32 |
cached = persist.load(key, "html")
|
| 33 |
if cached:
|
|
|
|
| 35 |
|
| 36 |
try:
|
| 37 |
# ----------------------------------------------------
|
| 38 |
+
# Date conversion
|
| 39 |
# ----------------------------------------------------
|
| 40 |
start = dt.strptime(date_start, "%d-%m-%Y").strftime("%Y-%m-%d")
|
| 41 |
end = dt.strptime(date_end, "%d-%m-%Y").strftime("%Y-%m-%d")
|
| 42 |
|
| 43 |
# ----------------------------------------------------
|
| 44 |
+
# Fetch daily data
|
| 45 |
# ----------------------------------------------------
|
| 46 |
df = yf.download(symbol + ".NS", start=start, end=end)
|
| 47 |
|
|
|
|
| 71 |
df["MA50"] = df["Close"].rolling(50).mean()
|
| 72 |
|
| 73 |
# ====================================================
|
| 74 |
+
# PRICE + VOLUME CHART
|
| 75 |
# ====================================================
|
| 76 |
buf = io.BytesIO()
|
| 77 |
|
|
|
|
| 85 |
plt.legend()
|
| 86 |
plt.grid(True)
|
| 87 |
plt.tight_layout()
|
| 88 |
+
|
| 89 |
+
plt.savefig(
|
| 90 |
+
buf,
|
| 91 |
+
format=IMAGE_FORMAT,
|
| 92 |
+
dpi=DPI,
|
| 93 |
+
bbox_inches="tight"
|
| 94 |
+
)
|
| 95 |
plt.close()
|
| 96 |
|
| 97 |
buf.seek(0)
|
| 98 |
+
price_key = f"daily/{symbol}_price_volume.{IMAGE_EXT}"
|
| 99 |
+
b2.upload_file("eshanhf", price_key, buf.getvalue())
|
| 100 |
+
price_url = f"{b2.S3_ENDPOINT}/eshanhf/{price_key}"
|
| 101 |
|
| 102 |
# ====================================================
|
| 103 |
+
# MOVING AVERAGE CHART
|
| 104 |
# ====================================================
|
| 105 |
buf = io.BytesIO()
|
| 106 |
|
|
|
|
| 113 |
plt.legend()
|
| 114 |
plt.grid(True)
|
| 115 |
plt.tight_layout()
|
| 116 |
+
|
| 117 |
+
plt.savefig(
|
| 118 |
+
buf,
|
| 119 |
+
format=IMAGE_FORMAT,
|
| 120 |
+
dpi=DPI,
|
| 121 |
+
bbox_inches="tight"
|
| 122 |
+
)
|
| 123 |
plt.close()
|
| 124 |
|
| 125 |
buf.seek(0)
|
| 126 |
+
ma_key = f"daily/{symbol}_ma.{IMAGE_EXT}"
|
| 127 |
+
b2.upload_file("eshanhf", ma_key, buf.getvalue())
|
| 128 |
+
ma_url = f"{b2.S3_ENDPOINT}/eshanhf/{ma_key}"
|
| 129 |
|
| 130 |
# ====================================================
|
| 131 |
+
# TABLE (Last 100 days)
|
| 132 |
# ====================================================
|
| 133 |
rows = ""
|
| 134 |
for r in df.tail(100).itertuples():
|
|
|
|
| 177 |
return html
|
| 178 |
|
| 179 |
except Exception:
|
| 180 |
+
return f"<pre>{traceback.format_exc()}</pre>"
|