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.
- Python 3.8+
- alpaca-py
- pandas
- numpy
- scipy
- plotly
- python-dotenv
- Clone the repository
- Install dependencies:
pip install alpaca-py pandas numpy scipy plotly python-dotenv
- Create a
.envfile in the root directory with your Alpaca API credentials:
ALPACA_API_KEY="your_api_key"
ALPACA_SECRET_KEY="your_secret_key"
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
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:
- Connect to Alpaca API
- Fetch market data and display conditions
- Find suitable options for the selected strategy
- Place the orders
- Monitor the spread and potentially roll or rinse based on exit criteria
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
The strategy manager follows these steps:
-
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
- Evaluates bear put spread strategy:
-
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
- Calculates risk as
- 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
- Calculates risk as
- Computes risk-to-reward ratio for each strategy (risk divided by max profit)
- For bear put spreads:
-
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
-
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
A bear put spread is a bearish options strategy that involves:
- Buying a put option at a higher strike price (long put)
- 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
A bull put spread is a bullish options strategy that involves:
- Selling a put option at a higher strike price (short put)
- 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
A bull call spread is a bullish options strategy that involves:
- Buying a call option at a lower strike price (long call)
- 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
An iron condor is a market-neutral options strategy that involves:
- Selling a put option at a higher strike price (short put)
- Buying a put option at a lower strike price (long put)
- Selling a call option at a lower strike price (short call)
- 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
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
The strategy manager is implemented in strategies/strategy_manager.py and includes:
-
StrategyManagerclass 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 ratioevaluate_strategies(): Compare strategies and select the optimal onerun_optimal_strategy(): Execute the selected strategy
This code is for educational purposes only. It is not financial advice. Use at your own risk.