Skip to content

This project, MLTrader Strategy, automates trading decisions using sentiment analysis of news articles, aiming to capitalize on market sentiment. It is integrated with Alpaca brokerage for execution and Yahoo finance for backtesting.

License

Notifications You must be signed in to change notification settings

ameynarwadkar/Sentiment-Trading-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MLTrader Strategy using Sentiment Analysis for Trading

The provided Python code implements an automated trading strategy called MLTrader, which employs sentiment analysis of news articles to make trading decisions. It utilizes the Alpaca brokerage for executing trades and Yahoo finance data for backtesting. This document outlines the functionality and workflow of the MLTrader strategy.

Code Overview: The code is divided into several parts:

  1. Import Statements:

    • Imports necessary modules and libraries including those from the lumibot package, Alpaca API, and finbert_utils for sentiment analysis.
  2. API Credentials and Configuration:

    • Defines API credentials and base URL for Alpaca paper trading.
  3. MLTrader Class:

    • Inherits from the Strategy class provided by lumibot.
    • Initializes with parameters like symbol and cash_at_risk.
    • Implements methods for position sizing, fetching dates, retrieving sentiment from news, and handling trading iterations.
  4. Trading Iteration:

    • Inside the on_trading_iteration method, it calculates position size, retrieves sentiment, and decides whether to buy, sell, or hold based on sentiment analysis results and available cash.
  5. Backtesting:

    • Sets start and end dates for backtesting.
    • Initializes Alpaca broker and MLTrader strategy with specified parameters.
    • Executes backtesting using YahooDataBacktesting class.

Explanation of MLTrader Strategy: The MLTrader strategy aims to automate trading decisions based on sentiment analysis of news related to a specified symbol (default is "SPY" - SPDR S&P 500 ETF Trust). The strategy consists of the following steps:

  1. Initialization:

    • Sets the trading symbol and cash at risk percentage.
    • Establishes connection with the Alpaca API.
  2. Position Sizing:

    • Calculates the quantity of shares to be traded based on available cash and last price of the symbol.
  3. Date Retrieval:

    • Fetches current and three days prior dates for fetching news.
  4. Sentiment Analysis:

    • Retrieves news articles within the specified date range.
    • Utilizes sentiment analysis to determine the probability and sentiment (positive/negative) of the news.
  5. Trading Decision:

    • Decides whether to buy or sell based on the sentiment analysis results and available cash.
    • Implements a bracket order with take-profit and stop-loss prices.
  6. Backtesting:

    • Conducts backtesting of the MLTrader strategy using historical data from Yahoo finance.

Conclusion: The MLTrader strategy utilizes sentiment analysis of news articles to automate trading decisions, aiming to capitalize on sentiment-driven market movements. By integrating with the Alpaca API for execution and Yahoo finance for backtesting, it provides a comprehensive framework for testing and deploying automated trading strategy.

About

This project, MLTrader Strategy, automates trading decisions using sentiment analysis of news articles, aiming to capitalize on market sentiment. It is integrated with Alpaca brokerage for execution and Yahoo finance for backtesting.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages