This project is a Python-based options strategy calculator and automated trading bot that uses sentiment analysis, market data, option pricing, and real-time trading to help users determine and execute the most profitable options strategies based on current market conditions.
The program employs two primary strategies for calculating profits: Straddle and Strangle. The strategy choice is influenced by sentiment analysis of a given news text, using a pre-trained FinBERT model to determine whether the sentiment is positive, neutral, or negative. Based on the sentiment, the program chooses whether to implement a long or short version of the strategies.
Additionally, the program can automatically execute trades in the RIT trading simulator, enabling seamless integration from analysis to execution.
- Sentiment Analysis with FinBERT - Utilizes FinBERT, a model fine-tuned for financial text sentiment analysis.
- Straddle and Strangle Option Strategies - Calculates profit for different option strategies based on market moves.
- Market Data Integration - Retrieves market data such as the current price and option premiums from the RIT API.
- Automated Trading Execution - Places orders directly in the RIT trading simulator based on calculated strategies.
- Profit Calculation - Calculates and prints the profit for both call and put options, for long and short strategies.
The entry point of the program. It fetches the current market price and applies the option strategies based on the news sentiment. It calculates the potential profit for each strategy, determines the best strategy, and optionally executes trades.
- Fetches the current market price.
- Analyzes news sentiment using FinBERT.
- Chooses the best strategy (Straddle or Strangle) based on sentiment and profitability.
- Displays the profit for both call and put options.
- Automatically places orders in the RIT trading simulator.
Defines the Option class that represents both call and put options. It includes a method calculate_profit that calculates the profit for an option based on whether it is a long or short position.
Optionclass initializes with type (CALL or PUT), strike price, premium, and quantity.- The
calculate_profitmethod computes the profit for either a call or put option based on the market price.
Handles the sentiment analysis by using the FinBERT model to classify the sentiment of a given news text as positive, negative, or neutral. This sentiment influences the option strategy selection.
- Loads the FinBERT model from the
yiyanghkust/finbert-tonepretrained model. - The
get_sentimentfunction classifies the sentiment into one of three categories: positive, negative, or neutral.
Contains the main logic for the two option strategies: Straddle and Strangle. Each strategy has methods for calculating the profit based on market moves and whether the position is long or short.
- Straddle Strategy: Buys a call and put option with the same strike price.
- Strangle Strategy: Buys a call and put option with different strike prices, typically 5 points apart.
- Both strategies have methods for
long(buying options) andshort(selling options) positions. maximize_profitfunction uses sentiment analysis and profitability calculations to determine which strategy to choose.
Contains utility functions for interacting with the RIT trading simulator API to fetch market data, retrieve option premiums, and execute trades. New functionality includes the ability to place orders and fetch current positions.
get_tick: Fetches the current market price (tick) from the RIT API.get_option_premiums: Fetches the current call and put premiums for a given ticker.place_order: Places market or limit orders for a specified ticker in the RIT trading simulator.get_position: Retrieves the current position for a specific ticker.
Defines custom exceptions for handling API-related errors in the program.
The program can now directly execute trades in the RIT trading simulator based on the calculated strategies.
Using the place_order and get_position functions, it can:
- Fetch current market conditions and option premiums.
- Choose the optimal strategy based on sentiment and profitability.
- Execute trades automatically for both long and short positions.
- Fetch the current tick and option premiums.
- Analyze news sentiment.
- Select the best strategy (e.g., Long Straddle).
- Place orders for the call and put options at the calculated strike prices.
from utils import place_order, get_position
place_order("RTM", "BUY", quantity=10)
position = get_position("RTM") print(f"Current Position for RTM: {position}")
In the Straddle strategy, the trader buys a call option and a put option with the same strike price and expiration date. The goal is to profit from large price movements, either up or down.
- If the stock price moves significantly in either direction, the trader profits from one of the options (either the call or the put).
- The
Straddleclass defines both long and short versions of this strategy. A long straddle profits when the stock price moves significantly in either direction. A short straddle profits when the price stays near the strike price.
The Strangle strategy is similar to the straddle but uses different strike prices for the call and put options. A typical strangle strategy might involve buying a call option with a strike price above the current stock price and a put option with a strike price below the current stock price.
- The aim is to profit from large price movements in either direction, but the options are cheaper than a straddle since they are out of the money.
- The
Strangleclass also defines long and short positions for the strategy.
The FinBERT model is used to determine the sentiment of a given news text. The sentiment influences whether the strategy will be long or short.
- If the sentiment is positive, the program opts for long positions on the best strategy (either long straddle or long strangle).
- If the sentiment is negative, the program opts for short positions (either short straddle or short strangle).
- If the sentiment is neutral, no adjustments are made to the strategy.
The maximize_profit function calculates the profit for both straddle and strangle strategies and selects the best-performing one. It then applies the sentiment analysis results to determine whether to go long or short on the selected strategy.
Before running the program, ensure the following dependencies are installed:
- Python 3.x
requestsfor API interaction.torchandtransformersfor sentiment analysis with FinBERT.
You can install the required dependencies using pip:
pip install requests torch transformers- Make sure the external API that provides the market data is running at
http://localhost:9999. - Save the files (
main.py,option.py,sentiment.py,strategy.py,utils.py,exceptions.py) in the same directory. - Run the
main.pyfile:
python main.pyCurrent Market Price: 100
Sentiment Analysis Result: positive
Call Option Profit: $200.00
Put Option Profit: $50.00
Total Strategy Profit: $250.00This program helps to determine the best options strategy (Straddle or Strangle) based on market data and sentiment analysis, maximizing the potential profit. It can be extended with more advanced strategies, more complex sentiment analysis, or integration with other market data sources.