finenv / server /stock_price.py
Code0ut's picture
Upload folder using huggingface_hub
8372676 verified
Raw
History Blame Contribute Delete
8.76 kB
"""Fetch stock price data for a given symbol and exchange."""
import yfinance as yf
reliance_nse_stock_history =[1493.4000244140625, 1511.5, 1510.9000244140625, 1518.9000244140625, 1518.300048828125, 1519.4000244140625, 1518.9000244140625, 1549.0999755859375, 1546.5999755859375, 1535.9000244140625, 1539.699951171875, 1569.9000244140625, 1563.4000244140625, 1567.5, 1566.0999755859375, 1546.300048828125, 1538.800048828125, 1535.5999755859375, 1540.5999755859375, 1543.0, 1529.4000244140625, 1536.9000244140625, 1545.0, 1556.5, 1556.199951171875, 1542.300048828125, 1544.4000244140625, 1544.4000244140625, 1565.0999755859375, 1575.4000244140625, 1570.699951171875, 1558.199951171875, 1559.199951171875, 1545.5999755859375, 1539.800048828125, 1570.4000244140625, 1575.5999755859375, 1592.300048828125, 1578.0999755859375, 1507.5999755859375, 1504.199951171875, 1470.5999755859375, 1475.300048828125, 1483.199951171875, 1452.800048828125, 1458.800048828125, 1458.800048828125, 1457.9000244140625, 1413.5999755859375, 1394.0, 1404.5999755859375, 1402.5, 1386.0999755859375, 1380.5, 1396.699951171875, 1391.0, 1395.4000244140625, 1390.4000244140625, 1437.0999755859375, 1456.800048828125, 1443.4000244140625, 1450.800048828125, 1461.5999755859375, 1458.5, 1468.699951171875, 1448.9000244140625, 1419.5999755859375, 1437.0999755859375, 1423.0, 1441.300048828125, 1409.5, 1419.4000244140625, 1428.0, 1428.800048828125, 1398.5, 1406.800048828125, 1393.9000244140625, 1358.0, 1345.0, 1389.4000244140625, 1404.800048828125, 1424.0, 1408.800048828125, 1390.199951171875, 1392.199951171875, 1380.699951171875, 1395.0999755859375, 1397.5999755859375, 1408.0999755859375, 1384.800048828125, 1414.4000244140625, 1407.800048828125, 1411.800048828125, 1413.0999755859375, 1348.0999755859375, 1343.9000244140625, 1369.199951171875, 1350.5, 1304.699951171875, 1304.5999755859375]
reliance_bse_stock_history=[1493.9000244140625, 1511.75, 1510.5999755859375, 1518.8499755859375, 1517.9000244140625, 1519.6500244140625, 1518.6500244140625, 1549.0999755859375, 1545.949951171875, 1535.75, 1539.0999755859375, 1569.75, 1563.550048828125, 1566.8499755859375, 1565.9000244140625, 1546.4000244140625, 1539.0, 1538.4000244140625, 1540.9000244140625, 1542.550048828125, 1528.800048828125, 1535.300048828125, 1544.9000244140625, 1556.0, 1556.050048828125, 1541.800048828125, 1544.5999755859375, 1544.3499755859375, 1565.0999755859375, 1575.449951171875, 1570.9000244140625, 1557.949951171875, 1559.0, 1545.300048828125, 1540.800048828125, 1569.4000244140625, 1574.9000244140625, 1592.449951171875, 1577.449951171875, 1507.699951171875, 1504.0999755859375, 1470.300048828125, 1475.300048828125, 1483.300048828125, 1451.5, 1458.449951171875, 1457.5999755859375, 1413.25, 1393.4000244140625, 1403.9000244140625, 1401.800048828125, 1385.949951171875, 1381.050048828125, 1397.050048828125, 1391.9000244140625, 1395.9000244140625, 1390.1500244140625, 1437.8499755859375, 1456.5999755859375, 1443.4000244140625, 1450.8499755859375, 1461.4000244140625, 1458.550048828125, 1468.550048828125, 1449.8499755859375, 1419.9000244140625, 1436.4000244140625, 1422.800048828125, 1441.25, 1410.800048828125, 1420.050048828125, 1427.9000244140625, 1430.1500244140625, 1398.199951171875, 1405.449951171875, 1394.300048828125, 1358.3499755859375, 1345.550048828125, 1389.800048828125, 1405.199951171875, 1424.449951171875, 1409.050048828125, 1391.0999755859375, 1391.8499755859375, 1380.5999755859375, 1395.050048828125, 1396.449951171875, 1408.5, 1385.3499755859375, 1414.550048828125, 1407.25, 1411.449951171875, 1412.550048828125, 1348.25, 1344.25, 1368.8499755859375, 1350.8499755859375, 1304.75, 1304.6500244140625]
axisbank_bse_stock_history=[1222.0999755859375, 1223.75, 1224.9000244140625, 1242.75, 1249.3499755859375, 1265.25, 1270.050048828125, 1285.25, 1275.3499755859375, 1268.5999755859375, 1266.0, 1290.1500244140625, 1287.3499755859375, 1280.25, 1275.8499755859375, 1259.449951171875, 1270.9000244140625, 1280.300048828125, 1282.8499755859375, 1273.5999755859375, 1275.0, 1278.800048828125, 1272.449951171875, 1286.300048828125, 1284.199951171875, 1219.6500244140625, 1224.6500244140625, 1229.699951171875, 1230.550048828125, 1233.0999755859375, 1224.699951171875, 1226.699951171875, 1228.050048828125, 1232.050048828125, 1246.0, 1268.699951171875, 1274.300048828125, 1267.5, 1285.8499755859375, 1293.5999755859375, 1295.8499755859375, 1286.8499755859375, 1271.949951171875, 1274.25, 1261.9000244140625, 1298.5, 1294.550048828125, 1307.550048828125, 1293.0999755859375, 1284.3499755859375, 1295.3499755859375, 1260.0999755859375, 1314.449951171875, 1320.5, 1364.3499755859375, 1370.25, 1317.8499755859375, 1355.550048828125, 1338.3499755859375, 1330.6500244140625, 1341.550048828125, 1341.3499755859375, 1356.4000244140625, 1347.699951171875, 1340.0, 1333.800048828125, 1358.25, 1357.050048828125, 1377.050048828125, 1355.949951171875, 1368.25, 1386.550048828125, 1387.6500244140625, 1402.5, 1394.699951171875, 1383.8499755859375, 1373.1500244140625, 1351.050048828125, 1350.699951171875, 1315.9000244140625, 1288.4000244140625, 1315.050048828125, 1259.050048828125, 1233.75, 1197.25, 1214.75, 1229.0999755859375, 1252.9000244140625, 1206.6500244140625, 1204.25, 1170.6500244140625, 1192.0, 1222.449951171875, 1205.199951171875, 1161.949951171875, 1192.8499755859375, 1198.1500244140625, 1245.3499755859375, 1250.699951171875]
axisbank_nse_stock_history=[1222.5, 1221.5999755859375, 1225.199951171875, 1241.5999755859375, 1249.5999755859375, 1265.4000244140625, 1270.4000244140625, 1285.199951171875, 1275.800048828125, 1269.0, 1266.300048828125, 1290.199951171875, 1287.300048828125, 1279.699951171875, 1275.699951171875, 1258.0, 1270.699951171875, 1280.0, 1282.5, 1273.800048828125, 1275.9000244140625, 1278.5999755859375, 1272.699951171875, 1286.0999755859375, 1284.800048828125, 1219.5999755859375, 1224.699951171875, 1229.800048828125, 1230.5999755859375, 1233.199951171875, 1225.0, 1226.300048828125, 1228.199951171875, 1232.0, 1246.0, 1269.4000244140625, 1274.4000244140625, 1266.9000244140625, 1285.800048828125, 1293.800048828125, 1295.5, 1286.800048828125, 1272.0, 1274.199951171875, 1262.0, 1298.800048828125, 1298.800048828125, 1294.199951171875, 1307.5, 1293.5, 1284.9000244140625, 1294.800048828125, 1258.0, 1315.800048828125, 1319.800048828125, 1363.699951171875, 1370.4000244140625, 1311.5, 1356.199951171875, 1338.699951171875, 1330.5999755859375, 1341.5999755859375, 1341.4000244140625, 1356.699951171875, 1347.300048828125, 1340.0, 1332.300048828125, 1358.300048828125, 1357.199951171875, 1377.0, 1356.5999755859375, 1368.300048828125, 1386.699951171875, 1387.5999755859375, 1403.0, 1395.5, 1383.9000244140625, 1372.300048828125, 1351.300048828125, 1349.0999755859375, 1315.800048828125, 1288.300048828125, 1314.699951171875, 1255.800048828125, 1234.5, 1197.300048828125, 1214.699951171875, 1228.0999755859375, 1253.199951171875, 1207.0, 1203.9000244140625, 1170.5999755859375, 1192.699951171875, 1222.0999755859375, 1205.199951171875, 1161.300048828125, 1193.0999755859375, 1197.9000244140625, 1245.300048828125, 1250.0999755859375]
def get_stock_price(symbol, exchange, days=None):
try:
symbol = symbol.upper()
# Convert Enum → string if needed
if hasattr(exchange, "value"):
exchange = exchange.value
exchange = exchange.lower()
if exchange == "nse":
ticker = symbol + ".NS"
elif exchange == "bse":
ticker = symbol + ".BO"
else:
return []
stock = yf.Ticker(ticker)
period = f"{days}d" if days else "5d"
data = stock.history(period=period)
if not data.empty:
return data['Close'].tolist()
print("Using fallback for:", ticker)
except Exception as e:
print("ERROR:", str(e),"yfinance API failed. Using fallback data if available.(This may be due to rate limits or network issues, please use RELIANCE/NSE or RELIANCE/BSE or AXISBANK/NSE or AXISBANK/BSE for stock symbol and exchange.)", flush=True)
# ✅ fallback always runs if API fails
if symbol == "RELIANCE" and exchange == "nse":
return reliance_nse_stock_history[-days:] if days else reliance_nse_stock_history
if symbol == "AXISBANK" and exchange == "nse":
return axisbank_nse_stock_history[-days:] if days else axisbank_nse_stock_history
if symbol == "RELIANCE" and exchange == "bse":
return reliance_bse_stock_history[-days:] if days else reliance_bse_stock_history
if symbol == "AXISBANK" and exchange == "bse":
return axisbank_bse_stock_history[-days:] if days else axisbank_bse_stock_history
return []