Skip to content

SNMiguel/R-Cryptocurrency-Trading-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ R Cryptocurrency Trading Bot

A professional-grade algorithmic trading system built in R with advanced technical analysis, risk management, and paper trading capabilities.

R Version License Status Code Size


๐Ÿ“Š Overview

An end-to-end algorithmic trading system designed for cryptocurrency markets. Features real-time data acquisition, technical analysis, multiple trading strategies, comprehensive backtesting, risk management, and paper trading simulationโ€”all built from scratch in R.

โœจ Key Features

  • ๐Ÿ“ˆ Real-time & Historical Data - CryptoCompare API integration
  • ๐Ÿ”ฌ Technical Analysis - 10+ indicators (RSI, MACD, MA, Bollinger Bands)
  • ๐ŸŽฏ Multiple Strategies - MA Crossover, RSI Mean Reversion, customizable templates
  • ๐Ÿงช Professional Backtesting - Transaction costs, performance metrics, equity curves
  • ๐Ÿ›ก๏ธ Risk Management - Position sizing (Fixed, Kelly Criterion, ATR), stop-loss/take-profit
  • ๐Ÿ’ฐ Paper Trading - Test strategies with simulated capital
  • ๐Ÿ“Š Visualization - Publication-quality charts (equity curves, drawdowns, indicators)

๐ŸŽฏ Performance Highlights

Strategy: RSI Mean Reversion
Period: 90 days (Oct 2025 - Jan 2026)
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Initial Capital:    $10,000
Final Value:        $10,432
Total Return:       +4.32%
Win Rate:           25%
Sharpe Ratio:       -1.67
Max Drawdown:       -0.14%

๐Ÿ—๏ธ Architecture

crypto-trading-bot/
โ”œโ”€โ”€ config/                  # Configuration files
โ”œโ”€โ”€ data/                    # Data storage
โ”‚   โ”œโ”€โ”€ raw/                # Raw market data
โ”‚   โ””โ”€โ”€ processed/          # Processed data with indicators
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ data_acquisition/   # API integration & data fetching
โ”‚   โ”œโ”€โ”€ indicators/         # Technical indicators
โ”‚   โ”œโ”€โ”€ strategies/         # Trading strategies
โ”‚   โ”œโ”€โ”€ backtesting/        # Backtesting engine
โ”‚   โ”œโ”€โ”€ risk_management/    # Position sizing & risk tools
โ”‚   โ”œโ”€โ”€ trading/            # Paper trading simulator
โ”‚   โ””โ”€โ”€ visualization/      # Chart generation
โ”œโ”€โ”€ results/
โ”‚   โ”œโ”€โ”€ backtest_results/   # Backtest outputs
โ”‚   โ””โ”€โ”€ plots/              # Generated charts
โ”œโ”€โ”€ logs/                    # Application logs
โ””โ”€โ”€ tests/                   # Unit tests

Total Lines of Code: ~4,200
Total Files: 18
Development Time: 5 weeks


๐Ÿš€ Quick Start

Prerequisites

  • R (version 4.5+)
  • RStudio (recommended)

Installation

  1. Clone the repository
git clone https://github.com/SNMiguel/R-Cryptocurrency-Trading-Bot.git
cd R-Cryptocurrency-Trading-Bot
  1. Install dependencies
source("setup.R")
  1. Configure settings
# Edit config/config.R to customize:
# - Cryptocurrencies to track
# - Risk parameters
# - Initial capital
# - Trading mode (PAPER/LIVE)

Run Your First Backtest

# Load the system
source("demo_priority2.R")

# This will:
# 1. Download 90 days of BTC data
# 2. Calculate technical indicators
# 3. Run MA Crossover strategy
# 4. Run RSI Mean Reversion strategy
# 5. Compare results
# 6. Save backtest reports

Paper Trading Example

source("demo_priority3.R")

# This will:
# 1. Set up paper trading portfolio
# 2. Run strategies with stop-loss/take-profit
# 3. Generate risk reports
# 4. Create professional charts

๐Ÿ“š Documentation

Technical Indicators

Indicator Description Parameters
SMA/EMA Moving Averages Periods: 10, 20, 50, 200
RSI Relative Strength Index Period: 14, Oversold: 30, Overbought: 70
MACD Moving Average Convergence Divergence Fast: 12, Slow: 26, Signal: 9
Bollinger Bands Volatility bands Period: 20, Std Dev: 2
Volume Volume analysis MA Period: 20

Trading Strategies

1. Moving Average Crossover

strategy <- create_ma_crossover_strategy(
  fast_period = 10,
  slow_period = 20,
  ma_type = "SMA"
)

Logic: Buy when fast MA crosses above slow MA, sell when it crosses below.

2. RSI Mean Reversion

strategy <- create_rsi_strategy(
  rsi_period = 14,
  oversold = 30,
  overbought = 70
)

Logic: Buy when RSI < 30 (oversold), sell when RSI > 70 (overbought).

3. Custom Strategy

# Create your own strategy using the base template
my_strategy <- create_base_strategy(
  name = "My Strategy",
  parameters = list(...)
)

# Define signal generation
generate_signals.MyStrategy <- function(strategy, data) {
  # Your logic here
  return(data)
}

Risk Management

# Position sizing
position <- calculate_position_size_fixed(capital = 10000, risk_pct = 0.02)
position <- calculate_position_size_kelly(capital, win_rate, avg_win, avg_loss)

# Stop-loss & Take-profit
stop_loss <- calculate_stop_loss(entry_price = 90000, stop_pct = 0.02)
take_profit <- calculate_take_profit(entry_price = 90000, profit_pct = 0.05)

# Risk-reward ratio
rr_ratio <- calculate_risk_reward_ratio(entry_price, stop_loss, take_profit)

๐Ÿ“Š Example Results

Equity Curve

Preview

Drawdown Analysis

Preview

Strategy Comparison

Preview


๐Ÿงช Backtesting

Run a Backtest

# Get historical data
btc_data <- get_historical_days("BTC", limit = 90)

# Add indicators
btc_data <- add_all_indicators(btc_data)

# Create strategy
strategy <- create_ma_crossover_strategy(10, 20)

# Run backtest with transaction costs
results <- run_backtest(
  strategy = strategy,
  data = btc_data,
  initial_capital = 10000,
  commission = 0.001,    # 0.1%
  slippage = 0.0005      # 0.05%
)

# View results
print_backtest_results(results)

Performance Metrics

The backtesting engine calculates:

  • Returns: Total return, return percentage
  • Risk Metrics: Sharpe ratio, maximum drawdown, volatility
  • Trade Stats: Win rate, profit factor, avg win/loss
  • Execution Costs: Commission, slippage

๐Ÿ›ก๏ธ Risk Management Features

Position Sizing Methods

  1. Fixed Percentage

    • Risk fixed % of capital per trade
    • Simple and conservative
  2. Kelly Criterion

    • Optimal bet sizing based on edge
    • Maximizes long-term growth
  3. ATR-Based

    • Volatility-adjusted position sizing
    • Adapts to market conditions

Stop-Loss & Take-Profit

  • Automatic stop-loss calculation
  • Trailing stop support
  • Risk-reward ratio validation
  • Portfolio risk monitoring

๐Ÿ“ˆ Visualization

Generate professional charts:

# Equity curve
plot <- plot_equity_curve(results$equity_curve)
save_plot(plot, "my_equity_curve")

# Price with indicators
plot <- plot_price_with_indicators(data, show_ma = TRUE, show_bb = TRUE)
save_plot(plot, "price_chart")

# RSI indicator
plot <- plot_rsi(data, oversold = 30, overbought = 70)
save_plot(plot, "rsi")

# Trades on chart
plot <- plot_trades(data, trade_history)
save_plot(plot, "trades")

All charts saved as high-resolution PNG files (300 DPI).


๐Ÿ”ง Configuration

API Settings

# config/config.R
API_BASE_URL <- "https://min-api.cryptocompare.com"
CRYPTOCURRENCIES <- c("BTC", "ETH", "BNB", "SOL", "ADA")
BASE_CURRENCY <- "USD"

Risk Parameters

RISK_MANAGEMENT <- list(
  stop_loss_pct = 0.02,       # 2% stop-loss
  take_profit_pct = 0.05,     # 5% take-profit
  max_daily_loss_pct = 0.05,  # 5% max daily loss
  max_open_positions = 3
)

Position Sizing

POSITION_SIZING <- list(
  initial_capital = 10000,
  max_position_size = 0.10,   # 10% per trade
  min_position_size = 100
)

๐ŸŽ“ Learning Resources

For Beginners

  1. Start with demo_priority2.R - Learn backtesting basics
  2. Experiment with indicator parameters
  3. Try different cryptocurrencies
  4. Compare strategy performance

For Advanced Users

  1. Create custom strategies using base_strategy.R
  2. Optimize parameters with grid search
  3. Implement multi-indicator strategies
  4. Add your own risk management rules

๐Ÿ“Š Sample Output

=== BACKTEST RESULTS ===

Strategy: Moving Average Crossover

--- PERFORMANCE METRICS ---
Initial Capital:     $10,000.00
Final Value:         $9,338.02
Total Return:        -$661.98 (-6.62%)

--- TRADING ACTIVITY ---
Total Trades:        3
Completed Trades:    1
Win Rate:            0.00%

--- RISK METRICS ---
Sharpe Ratio:        -1.71
Max Drawdown:        -6.00%
Profit Factor:       0.00

--- TRANSACTION COSTS ---
Total Commission:    $27.36
Total Slippage:      $13.68
Total Costs:         $41.03

๐Ÿ—บ๏ธ Roadmap

โœ… Completed (Phase 1-3)

  • Real-time & historical data acquisition
  • Technical indicators (10+ indicators)
  • Multiple trading strategies
  • Professional backtesting engine
  • Risk management system
  • Paper trading simulator
  • Visualization tools

๐Ÿšง In Progress (Phase 4)

  • Live trading integration
  • Advanced order types (limit, stop)
  • Multi-asset portfolio management
  • Machine learning strategies
  • Real-time alerts & notifications

๐Ÿ”ฎ Future

  • Web interface (React + FastAPI)
  • Mobile app
  • Social trading features
  • Strategy marketplace
  • API for external integrations

๐Ÿค Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


โš ๏ธ Disclaimer

This software is for educational purposes only.

  • Cryptocurrency trading carries significant risk
  • Past performance does not guarantee future results
  • Always do your own research (DYOR)
  • Never invest more than you can afford to lose
  • The authors are not responsible for any financial losses

๐Ÿ™ Acknowledgments

  • CryptoCompare API - Real-time cryptocurrency data
  • R Community - Excellent packages (tidyverse, TTR, ggplot2)
  • Quantitative Finance - Algorithmic trading research

๐Ÿ“ง Contact

Chawana Smith


๐ŸŒŸ Support

If you find this project helpful, please consider:

  • โญ Starring the repository
  • ๐Ÿ› Reporting bugs
  • ๐Ÿ’ก Suggesting new features
  • ๐Ÿ“ข Sharing with others

Built with โค๏ธ using R

โญ Star this repo if you found it useful! โญ

Last Updated: January 2026 Version: 0.1.0 (Data Acquisition Phase)

About

Professional crypto trading system in R (4,200+ lines). Real-time data, technical analysis, multiple strategies, comprehensive backtesting, position sizing, and paper trading capabilities.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages