This Flask-powered web application simulates weekly stock data, storing it alongside previous weeks in a SQLite database. The platform dynamically updates stock prices and volumes based on the previous week’s data, using custom logic to reflect realistic market changes. Users can navigate through historical data, showcasing the system’s ability to handle and manage time-series data efficiently.
CORE FEATURES
Weekly Stock Data Simulation
Historical Data Management
Time-series data navigation
VISUALS & SIMULATION
TECHNICAL IMPLEMENTATION
The platform dynamically simulates stock prices and volumes based on data from the previous week. Here’s how the logic is applied:
Retrieve the Latest Week
The application queries the database to find the most recent week of stock data using SQLAlchemy.
Calculate the Next Week
After identifying the latest week, the next week number is generated, and stock data for this new week will be simulated.
Stock Volume and Price Adjustment
For each stock in the latest week:
A random percentage change in volume is applied (within -20% to +20%).
Based on the new volume, the stock price is adjusted:
High Volume: If the volume exceeds 10,000, the price is slightly increased.
Low Volume: If the volume drops below 1,000, the price is slightly decreased.
Moderate Volume: If the volume falls in between, the price stays relatively stable.
Store New Data
After adjusting the stock’s volume and price, the new data for the next week is stored in the database, ensuring a historical record of stock performance is maintained.
from stocks import db
import random
class Stock(db.Model):
id = db.Column(db.Integer(), primary_key=True, nullable=False)
stock = db.Column(db.String(length=30), nullable=False)
price = db.Column(db.Float(), nullable=False)
volume = db.Column(db.Integer(), nullable=False)
week = db.Column(db.Integer(), nullable = False)
def update_week(self):
# Retrieve the last recorded week number for this stock
last_week_stock = Stock.query.filter_by(stock=self.stock).order_by(Stock.week.desc()).first()
if last_week_stock:
last_week = last_week_stock.week
else:
last_week = 0
# Increment the week number for this stock
self.week = last_week + 1
def __init__(self,stock,price,volume,week):
self.stock = stock
self.price = price
self.volume = volume
self.week = week
def update_price(self):
if self.volume > 10000:
self.price *= random.uniform(1.01,1.05)
elif self.volume < 1000:
self.price *= random.uniform(0.95,0.99)
else:
self.price *= random.uniform(0.99,1.01)
def simulate_weekly_volume(self):
change = random.uniform(-0.2,0.2) # Allow volume to increase or decrease by up to 20%
self.volume *= (1 + change) # Apply the change to volume
stocks = [
Stock("AAPL", 150.0, 10000), # Apple Inc. - Initial price $150, initial volume 10000
Stock("GOOGL", 2500.0, 5000), # Alphabet Inc. (Google) - Initial price $2500, initial volume 5000
Stock("MSFT", 300.0, 20000), # Microsoft Corporation - Initial price $300, initial volume 20000
Stock("AMZN", 3500.0, 8000), # Amazon.com Inc. - Initial price $3500, initial volume 8000
Stock("TSLA", 700.0, 15000), # Tesla Inc. - Initial price $700, initial volume 15000
Stock("FB", 350.0, 12000), # Meta Platforms Inc. (formerly Facebook) - Initial price $350, initial volume 12000
Stock("NVDA", 600.0, 10000), # NVIDIA Corporation - Initial price $600, initial volume 10000
Stock("BABA", 200.0, 18000), # Alibaba Group Holding Limited - Initial price $200, initial volume 18000
Stock("JPM", 150.0, 25000), # JPMorgan Chase & Co. - Initial price $150, initial volume 25000
Stock("V", 250.0, 15000), # Visa Inc. - Initial price $250, initial volume 15000
Stock("NFLX", 500.0, 10000), # Netflix Inc. - Initial price $500, initial volume 10000
Stock("INTC", 65.0, 30000), # Intel Corporation - Initial price $65, initial volume 30000
Stock("CRM", 220.0, 8000), # Salesforce.com Inc. - Initial price $220, initial volume 8000
Stock("DIS", 180.0, 15000), # The Walt Disney Company - Initial price $180, initial volume 15000
Stock("SBUX", 110.0, 20000), # Starbucks Corporation - Initial price $110, initial volume 20000
Stock("ADBE", 550.0, 7000), # Adobe Inc. - Initial price $550, initial volume 7000
Stock("CSCO", 55.0, 25000), # Cisco Systems Inc. - Initial price $55, initial volume 25000
Stock("PYPL", 200.0, 12000), # PayPal Holdings Inc. (duplicate) - Initial price $200, initial volume 12000
Stock("WMT", 140.0, 18000) # Walmart Inc. - Initial price $140, initial volume 18000
]
for stock in stocks:
existing_stock = Stock.query.filter_by(stock=stock.stock).first()
if not existing_stock:
db.session.add(stock)
db.session.commit()
SIMULATION CODE
with app.app_context():
latest_week = db.session.query(db.func.max(Stock.week)).scalar()
next_week = latest_week + 1
for stock_data in Stock.query.filter_by(week=latest_week):
volume_change = random.uniform(-0.2,0.2)
new_volume = stock_data.volume * (1 + volume_change)
if new_volume > 10000:
new_price = stock_data.price * random.uniform(1.01,1.05)
elif new_volume < 1000:
new_price = stock_data.price * random.uniform(0.95,0.99)
else:
new_price = stock_data.price * random.uniform(0.99,1.01)
new_volume = math.floor(new_volume * 100)/100
new_price = math.floor(new_price * 100) / 100
# Add a new_stock object
new_stock = Stock(stock=stock_data.stock,price=new_price,volume=new_volume, week=next_week)
db.session.add(new_stock)
db.session.commit()
DATA MANAGEMENT
The platform uses a SQLite database to store and manage historical stock data, including prices and volumes for each week.
Data Storage Structure:
Stock data is organized in a table with columns for stock name, price, volume, and week number, allowing for easy retrieval and analysis.
Routes:
Home Route (/ and /stocks_market):
This route displays stock data for the latest week or a specified week. It queries the database and returns relevant stock information, or shows a message if no data is found for the requested week.
Simulation Route (/simulate):
This route simulates the next week’s stock data. It retrieves the latest week, applies random changes to the stock volumes and prices, and stores the new records in the database for the next week.
By effectively managing this data through these routes, users can easily navigate different weeks and observe how stock prices and volumes evolve over time, enhancing the application’s functionality for tracking market trends.
FUTURE VERSION IMPROVEMENTS
Data Visualization (e.g., charts/graphs)
Enhanced Simulation Algorithms
Comparison Features for Multiple Stocks
Export Stock Data
Advanced Search Filters
Find the entire project here