tosin2013's picture
working code
1b4fca8
|
raw
history blame
2.49 kB
metadata
title: Forex Research
emoji: 🔥
colorFrom: indigo
colorTo: yellow
sdk: gradio
sdk_version: 4.11.0
app_file: app.py
pinned: false

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference Overview:

This Python code implements a forex trading tool that combines technical analysis with sentiment analysis to generate trading signals. It leverages the Backtrader library for backtesting, the Alpha Vantage API for fetching forex data and sentiment data, and Gradio for creating a user-friendly interface.

Key Features:

  • Trend-following strategy: Employs a simple moving average crossover strategy to identify potential buy and sell signals.
  • Sentiment analysis: Incorporates sentiment analysis from news data to refine trade decisions.
  • Backtesting capabilities: Allows users to test the strategy's performance on historical data.
  • User-friendly interface: Provides a Gradio interface for easy interaction and visualization of results.

Code Structure:

  1. Import Necessary Libraries:
    • gradio, pandas, backtrader, requests
  2. Define Instructions:
    • Reads Markdown-formatted instructions from a file for user guidance.
  3. TrendFollowingStrategy Class:
    • Implements the trend-following strategy logic using a moving average crossover.
    • Tracks trade details (count, wins, losses) and logs trade information.
  4. Data Fetching Functions:
    • fetch_forex_intraday: Retrieves forex data from Alpha Vantage.
    • fetch_sentiment_data: Fetches sentiment data from Alpha Vantage.
    • analyze_sentiment: Analyzes sentiment data for a given ticker.
    • load_data: Converts fetched data into a Backtrader data feed.
  5. Trade Decision Function:
    • should_trade: Determines whether to make a trade based on technical signals and sentiment analysis.
  6. Backtesting Function:
    • run_backtest: Runs a backtest using specified parameters and returns performance statistics and trade decisions.
  7. Interface Creation:
    • Sets up Gradio interface elements for user input and output.
    • Launches the interface, allowing users to interact with the tool.

How to Use:

  1. Obtain an API key from Alpha Vantage.
  2. Run the Python code.
  3. Enter your API key in the Gradio interface.
  4. Select the desired currency pair and time interval.
  5. Click the "Run" button to initiate the backtest.
  6. Review the generated backtest results and trade decisions.