Skip to content

e-Reese/alpaca-trader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Options Strategies with Alpaca

This project provides a modular Python implementation for trading options spread strategies using the Alpaca API. The code supports bear put spreads, bull put spreads, bull call spreads, and iron condors, and is organized to be maintainable, reusable, and easy to understand.

Requirements

  • Python 3.8+
  • alpaca-py
  • pandas
  • numpy
  • scipy
  • plotly
  • python-dotenv

Installation

  1. Clone the repository
  2. Install dependencies:
pip install alpaca-py pandas numpy scipy plotly python-dotenv
  1. Create a .env file in the root directory with your Alpaca API credentials:
ALPACA_API_KEY="your_api_key"
ALPACA_SECRET_KEY="your_secret_key"

Configuration

Configuration parameters are stored in config.py. You can modify these settings to adjust:

  • Underlying symbol
  • Strike price range
  • Risk-free rate
  • Buying power limit
  • Open interest threshold
  • Expiration date range
  • Target profit percentage
  • Stop-loss thresholds

Usage

To run:

python main.py

To run a specific strategy:

python main.py --strategy iron_condor

Available strategies: bear_put, bull_put, bull_call, iron_condor, gamma_scalp, strategy_manager, all

This will:

  1. Connect to Alpaca API
  2. Fetch market data and display conditions
  3. Find suitable options for the selected strategy
  4. Place the orders
  5. Monitor the spread and potentially roll or rinse based on exit criteria

Strategy Overviews

Strategy Manager

The strategy manager evaluates multiple options strategies and selects the one with the lowest risk-to-reward ratio. Currently, it compares:

  • Bear put spreads
  • Bull put spreads

How the Strategy Manager Works

The strategy manager follows these steps:

  1. Evaluation Phase (without executing trades):

    • Evaluates bear put spread strategy:
      • Fetches available put options for the configured underlying symbol
      • Filters options based on criteria (expiration, IV, delta, etc.)
      • Pairs short and long put options to form valid bear put spreads
      • Checks if the spread risk is within buying power limits
    • Evaluates bull put spread strategy using the same approach
  2. Risk Analysis:

    • For bear put spreads:
      • Calculates risk as (Long put premium - Short put premium) * contract size
      • Calculates max profit as (Short put premium - Long put premium) * contract size
    • For bull put spreads:
      • Calculates risk as (Strike price difference - Net premium received) * contract size
      • Calculates max profit as (Short put premium - Long put premium) * contract size
    • Computes risk-to-reward ratio for each strategy (risk divided by max profit)
  3. Strategy Selection:

    • Selects the strategy with the lower risk-to-reward ratio
    • If both strategies have equal risk-to-reward ratios or no valid spreads are found, no strategy is selected
  4. Execution:

    • Places an order for the selected strategy using the appropriate order placement function

To use the strategy manager:

python main.py --strategy strategy_manager

Bear Put Spread

A bear put spread is a bearish options strategy that involves:

  1. Buying a put option at a higher strike price (long put)
  2. Selling a put option at a lower strike price (short put)

This strategy:

  • Has a limited profit potential if the stock price falls
  • Has a limited risk if the stock price rises
  • Costs less than buying a standalone put option

Bull Put Spread

A bull put spread is a bullish options strategy that involves:

  1. Selling a put option at a higher strike price (short put)
  2. Buying a put option at a lower strike price (long put)

This strategy:

  • Generates income through the premium received
  • Has a limited profit potential if the stock price rises or stays above the short put strike
  • Has a limited risk if the stock price falls below the long put strike
  • Benefits from time decay

Bull Call Spread

A bull call spread is a bullish options strategy that involves:

  1. Buying a call option at a lower strike price (long call)
  2. Selling a call option at a higher strike price (short call)

This strategy:

  • Has a limited profit potential if the stock price rises above the long call strike
  • Has a limited risk, with max loss equal to the net premium paid
  • Is less expensive than buying a standalone call option
  • Benefits from upward price movement while limiting cost

Iron Condor

An iron condor is a market-neutral options strategy that involves:

  1. Selling a put option at a higher strike price (short put)
  2. Buying a put option at a lower strike price (long put)
  3. Selling a call option at a lower strike price (short call)
  4. Buying a call option at a higher strike price (long call)

This strategy:

  • Creates a range where the strategy is profitable (between short put and short call)
  • Has a limited profit potential (maximum is the net premium received)
  • Has a limited risk (defined by the difference between strikes in either spread)
  • Benefits from time decay and low volatility
  • Works best in sideways or range-bound markets

Implementation Details

The code implements for both strategies:

  • Option selection based on expiration, IV, delta, and various Greek criteria
  • Risk management through buying power limits
  • Exit criteria based on price, delta, and IV thresholds
  • Position monitoring and potential rolling/rinsing

Strategy Manager Implementation

The strategy manager is implemented in strategies/strategy_manager.py and includes:

  • StrategyManager class with methods for:

    • Evaluating bear put spreads and bull put spreads independently
    • Calculating risk and risk-to-reward ratios for each strategy
    • Selecting the optimal strategy based on risk metrics
    • Executing the selected strategy
  • Key methods:

    • _evaluate_bear_put_spread() and _evaluate_bull_put_spread(): Evaluate strategies without executing trades
    • _calculate_bear_put_risk() and _calculate_bull_put_risk(): Calculate risk for each strategy
    • _calculate_risk_reward_ratio(): Calculate risk-to-reward ratio
    • evaluate_strategies(): Compare strategies and select the optimal one
    • run_optimal_strategy(): Execute the selected strategy

Disclaimer

This code is for educational purposes only. It is not financial advice. Use at your own risk.

About

Auto trading on Alpaca.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages