Skip to content

Coding project for university course Software Craftsmanship for clean coding, creating a prototype algorithmic trading simulation for silver

Notifications You must be signed in to change notification settings

robertgsmith/algorithmic-trading-simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Silver Algorithmic Trading Simulation

Group 3: Ionut Bina, Robert Smith, Yutang Xing

Project Overview

This project implements a mock algorithmic trading application for silver (XAG/USD) that demonstrates:

  • Backend trading logic using multiple technical indicators
  • Real-time visualization of prices, indicators, and trading decisions
  • Performance tracking with metrics like Sharpe ratio

Features

Technical Indicators

  1. SMA (Simple Moving Average) - Trend identification

    • Short-term (20-day) vs Long-term (50-day)
    • Golden cross (buy) and death cross (sell) detection
  2. RSI (Relative Strength Index) - Momentum analysis

    • Oversold (< 30): Buy signal
    • Overbought (> 70): Sell signal
  3. MACD (Moving Average Convergence Divergence) - Trend strength

    • MACD line crossing signal line generates buy/sell signals

Signal Combination

  • Each indicator votes: +1 (buy), -1 (sell), or 0 (neutral)

  • Combined score ≥ +1 → Buy

  • Combined score ≤ -1 → Sell

  • Otherwise → Hold

Visualization Components

  1. Price Chart with SMA overlays and buy/sell markers
  2. RSI Chart with overbought/oversold zones
    1. MACD Chart with signal line
  3. Portfolio Value tracking over time

Project Structure

silver_trading_bot/
│
├── main.py                    # Main entry point
├── simulation_engine.py       # Simulation orchestrator
├── data/                      # Data directory
│   ├── input/                 # Input data
│   │   └── silver_prices.csv  # Historical price data
│   └── output/                # Output data (visualization)
├── trading_components/        # Trading components used by simulation_engine.py
│   ├── trading_algorithm.py   # Technical indicators & signals
│   ├── portfolio_manager.py   # Position & performance tracking
│   ├── data_loader.py         # CSV data handling
│   └── visualizer.py          # Plotting & visualization
└── README.md                  # This file

Configuration

Edit the following parameters in main.py:

DATA_FILE = "data/input/silver_prices.csv"  # Data file path
INITIAL_CAPITAL = 100000.0  # Starting capital
UPDATE_INTERVAL = 0.05  # Update delay in seconds
MAX_ITERATIONS = None  # None = all data (

Real-time vs Fast Mode

In simulation_engine.py, line in main():

# Fast mode (recommended)
simulation.run_simulation(real_time=False)

# Real-time mode with delays
simulation.run_simulation(real_time=True)

Algorithm Parameters

Customizable in trading_algorithm.py:

TradingAlgorithm(
    sma_short=20,           # Short-term SMA period
    sma_long=50,            # Long-term SMA period
    rsi_period=14,          # RSI calculation period
    rsi_oversold=30.0,      # RSI oversold threshold
    rsi_overbought=70.0,    # RSI overbought threshold
    macd_fast=12,           # MACD fast EMA
    macd_slow=26,           # MACD slow EMA
    macd_signal=9           # MACD signal line
)

Performance Metrics

The simulation calculates: 模拟计算:

  1. Total Return - Percentage gain/loss
  2. Sharpe Ratio - Risk-adjusted return
  3. Win Rate - Percentage of profitable trades
  4. Total Profit - Sum of all trade profits

Output

The simulation generates:

  1. Console output with trade logs and final summary

  2. PNG visualization (trading_simulation_results.png) showing:

    • Price chart with indicators
    • Buy/sell markers at decision points
    • RSI and MACD subplots
    • Portfolio value progression

Object-Oriented Design

  • TradingAlgorithm: Encapsulates indicator calculations
  • PortfolioManager: Manages positions and performance
  • DataLoader: Handles data preprocessing
  • TradingVisualizer: Manages all plotting
  • TradingSimulation: Orchestrates the entire system

About

Coding project for university course Software Craftsmanship for clean coding, creating a prototype algorithmic trading simulation for silver

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages