How I Built a Silver Price Prediction Model and Shipped it Live
The Problem
If you've ever tried to check silver prices in India, you know the pain β most global APIs give prices in USD per troy ounce, which means nothing to an Indian buyer who thinks in βΉ per gram. I wanted to build something that not only predicts tomorrow's silver price but also shows accurate Indian market rates with GST included.
π Try it live: Silver Price Prediction App
What I Built
A full-stack ML application that does two things:
- Fetches real-time silver prices and converts them to Indian market rates (βΉ per gram, per 10g, per kg) β including import duties and 3% GST
- Predicts next-day prices using a Lasso Regression model trained on historical data
| Feature | Description |
|---|---|
| β Live Prices | Real-time data from MetalpriceAPI |
| β Indian Market | Prices in INR with import duties + GST |
| β ML Predictions | Next-day price forecast |
| β REST API | JSON endpoints for integration |
| β Multi-source Fallback | MetalpriceAPI β Yahoo Finance β Local CSV |
How the Architecture Works
The system follows a simple three-stage pipeline:
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β MetalpriceAPI ββββββΆβ Flask App ββββββΆβ ML Model β
β (Live Prices) β β (Conversion) β β (Prediction) β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β β β
βΌ βΌ βΌ
USD/oz prices INR conversion Next-day forecast
+ GST (3%)The price conversion pipeline is where it gets interesting. You can't just multiply USD Γ exchange rate β Indian silver prices include layers of additional costs:
- Fetch USD price per troy ounce from MetalpriceAPI
- Convert using live USD/INR exchange rate
- Convert troy ounce to grams (Γ· 31.1035)
- Add Import Duty (+6%)
- Add Local Premium (+10%)
- Add GST (+3%)
The result? Prices that match GoodReturns.in (Hyderabad rates) within βΉ5-10 accuracy.
The ML Model
I experimented with several regression models and landed on Lasso Regression for its simplicity and strong performance:
| Metric | Value |
|---|---|
| Algorithm | Lasso Regression |
| RΒ² Score | 0.9836 |
| Library | scikit-learn 1.7.0 |
The model uses a rich set of features engineered from historical price data:
- Lag features: Closing prices from 1, 2, 3, 5, and 7 days ago
- Moving averages: 5-day, 10-day, and 20-day windows
- Technical indicators: RSI, MACD, and Bollinger Bands
An RΒ² of 0.98 means the model explains 98% of the variance in price movements β which is excellent for a regression task on financial data.
Data Source Strategy
One lesson I learned early: never rely on a single data source. APIs go down, rate limits kick in, and free tiers expire. So I built a three-tier fallback system:
| Priority | Source | Why |
|---|---|---|
| 1 | MetalpriceAPI | Most accurate, 24-hour cache |
| 2 | Yahoo Finance | Free backup (XAGUSD=X ticker) |
| 3 | Local CSV | Offline fallback for historical data |
The 24-hour caching layer ensures I'm not burning through API credits on every request.
API Design
The Flask app exposes two clean endpoints:
GET /api/predict β Next-day price prediction
GET /api/current-price β Current market price with GSTSample response:
{
"success": true,
"market": "India",
"currency": "INR",
"with_gst": {
"per_10_grams": 3650,
"per_kg": 365000
},
"gst_rate": "3%"
}Shipping to Production
Getting the model from my laptop to a live URL involved deploying to Hugging Face Spaces. The deployment is straightforward β push the Flask app with the trained model artifacts, and HF Spaces handles the rest.
# Clone and run locally
git clone https://github.com/danishsyed-dev/Silver-Price-Prediction.git
cd Silver-Price-Prediction
pip install -r requirements.txt
python app.pyKey Takeaways
- Domain knowledge matters more than model complexity β Understanding how Indian silver pricing works (import duty, GST, local premiums) was more valuable than trying fancier ML models
- Build fallback systems β External APIs will fail; plan for it from day one
- Cache aggressively β 24-hour caching reduced my API costs to nearly zero
- Ship early β Deploying to Hugging Face Spaces took less than 10 minutes and gave me a shareable URL instantly
β οΈ Disclaimer
This project is for educational purposes only. Actual silver prices at jewellers may include making charges (8-20%), wastage charges, purity variations (925, 999), and local market premiums. Do not use for actual trading decisions.