Introduction
This lab task simulates a complete project lifecycle for a data-driven engineering application. You will perform the analysis, development, and evaluation of a Python script that uses rapid application development (RAD) techniques by integrating two distinct APIs: Alpha Vantage for financial data and Google Gemini for AI-powered analysis. This project mirrors real-world tasks where programmers must analyse a problem, develop a functional prototype, and evaluate its performance.
Part A: Analysis of the Engineering Application (LO1)
Learning Outcome 1: Analyse an engineering application to facilitate the development of a software solution using RAD techniques.
This section requires you to analyse the problem domain—real-time financial data analysis. You will provide a structured interpretation of the application requirements by identifying the necessary data sources (APIs), selecting appropriate tools (Python libraries), and establishing the software requirements for the project, such as secure API key management. This setup phase is a critical analysis step before development begins.
Step 1: Analyse and Establish Software Requirements
To begin, you must analyse the core components of our application. We require:
- A source for real-time gold price data.
- A tool for intelligent text analysis.
- A method for data visualization.
- A secure environment for our code and credentials.
Following industry practice, we will use a dedicated project folder and a virtual environment to manage dependencies.
Open your terminal or command prompt and run these commands:
# Create a new directory and navigate into it
mkdir lab1-gold-analysis
cd lab1-gold-analysis
# Create a virtual environment
python3 -m venv venv
# Activate the virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
.\venv\Scripts\activate
Step 2: Tool Selection and Setup
Based on our analysis, we will use several Python libraries that facilitate rapid application development. Install the required libraries:
pip install requests google-generativeai matplotlib python-dotenv
Step 3: Get and Secure API Keys
A key requirement of this application is interfacing with external services. This requires API keys, which must be managed securely—a standard industry practice. Create a .env file in your project folder to store your keys. This provides a structured interpretation of how secrets should be handled separately from code.
A. Alpha Vantage API Key (for Gold Data)
- Go to the Alpha Vantage website and get your free API key.
B. Google Gemini API Key
- Go to Google AI Studio, sign in, and create a new API key.
C. Store Your Keys Securely
Add your keys to the .env file you created:
# .env file content
ALPHA_VANTAGE_API_KEY=YOUR_KEY_HERE
GEMINI_API_KEY=YOUR_KEY_HERE
Part B: Development of the Software Solution (LO2)
Learning Outcome 2: Develop a software solution for an engineering application using RAD techniques.
In this section, you will develop a software solution by writing Python code. You will demonstrate a valid application of RAD tools and components (the requests, google-generativeai, and matplotlib libraries) to create a functional program. The goal is to build a solution that makes effective and efficient use of the RAD tool capabilities to fetch, process, analyse, and visualize data.
Step 4: Develop Data Fetching Module
Create a file named main.py. We will now develop the first part of our software solution to retrieve data from the Alpha Vantage API.
# main.py
import os
import requests
from dotenv import load_dotenv
from datetime import datetime, timedelta
import google.generativeai as genai
import matplotlib.pyplot as plt
# Load environment variables from .env file
load_dotenv()
def get_gold_prices():
"""Fetches intraday gold prices from Alpha Vantage."""
print("Fetching gold price data...")
ALPHA_VANTAGE_KEY = os.getenv("ALPHA_VANTAGE_API_KEY")
url = 'https://www.alphavantage.co/query'
params = {
'function': 'TIME_SERIES_INTRADAY',
'symbol': 'GLD',
'interval': '1min',
'apikey': ALPHA_VANTAGE_KEY,
'outputsize': 'compact'
}
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
time_series_key = 'Time Series (1min)'
if time_series_key not in data:
print("Error: Could not find time series data in the response.", data)
return None
time_series = data[time_series_key]
prices = []
thirty_minutes_ago = datetime.now() - timedelta(minutes=30)
for timestamp_str, values in time_series.items():
timestamp_dt = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')
if timestamp_dt > thirty_minutes_ago:
prices.append((timestamp_dt, float(values['4. close'])))
prices.reverse()
print(f"Successfully fetched {len(prices)} data points.")
return prices
except requests.exceptions.RequestException as e:
print(f"An error occurred while fetching data: {e}")
return None
Step 5: Develop AI Analysis Module
Next, we will leverage the Gemini API as a RAD tool to quickly add complex analytical capabilities to our application.
# Configure the Gemini API client
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)
def analyze_prices_with_gemini(price_data):
"""Sends price data to Gemini for analysis."""
if not price_data:
return "No price data available for analysis."
print("\nSending data to Gemini for analysis...")
formatted_data = "\n".join([f"Time: {ts.strftime('%H:%M')}, Price: ${price:.2f}" for ts, price in price_data])
prompt = f"""
You are a financial analyst. Below is price data for the GLD ETF
over the last 30 minutes.
Data:
{formatted_data}
Provide a brief analysis of this data. What is the general trend?
Mention the highest and lowest price points. Conclude with a short summary.
"""
try:
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content(prompt)
return response.text
except Exception as e:
return f"An error occurred communicating with Gemini: {e}"
Step 6: Develop Data Visualization Module
To demonstrate the results effectively, develop a plotting function. This demonstrates the efficient use of `matplotlib` to translate raw data into an easily understandable format.
def plot_prices(price_data):
"""Plots the price data using Matplotlib."""
if not price_data:
print("No data to plot.")
return
print("Generating plot...")
timestamps = [item[0] for item in price_data]
prices = [item[1] for item in price_data]
plt.figure(figsize=(10, 6))
plt.plot(timestamps, prices, marker='o', linestyle='-', color='gold')
plt.title('Gold Price (GLD ETF) - Last 30 Minutes')
plt.xlabel('Time')
plt.ylabel('Price (USD)')
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Step 7: Finalise and Demonstrate the Software Solution
Finally, assemble the components into a complete, runnable script. This main block will execute the functions in sequence to demonstrate the full capability of the software solution.
if __name__ == "__main__":
# 1. Develop the data fetching part of the solution
gold_price_data = get_gold_prices()
if gold_price_data:
# 2. Develop the AI analysis part of the solution
analysis = analyze_prices_with_gemini(gold_price_data)
print("\n--- Gemini AI Financial Analysis ---")
print(analysis)
print("------------------------------------")
# 3. Demonstrate the visualization component
plot_prices(gold_price_data)
else:
print("\nCould not retrieve data. Exiting program.")
Run your completed script from the terminal:
python main.py
Submission
To complete this lab, please submit the following items:
- Your completed Python script:
main.py - Your project dependencies file. Create it by running this command in your terminal:
Then submit the generatedpip freeze > requirements.txtrequirements.txtfile. - A screenshot of your output, showing both the terminal with the printed Gemini analysis and the Matplotlib plot window.