shivamsouravjha / stock-backend

11 stars 7 forks source link
go golang hacktoberfest stock-analysis stock-market

Stock Analysis API

This project is an API built using the Gin framework for analyzing stock data. It processes Excel files, fetches stock data, calculates various stock metrics, and compares them with peer companies to derive useful insights.

DALL·E 2024-09-29 14 31 43 - A blue gopher character dressed in traditional Indian attire like a kurta, sitting in front of multiple computer screens analyzing the Indian stock ma

Features

Requirements

Setup

  1. Clone the repository:

    git clone https://github.com/your-repo/stock-analysis-api.git
    cd stock-analysis-api
  2. Install Go dependencies:

    go mod tidy
  3. Set environment variables for MongoDB:

    export MONGO_URI="your_mongo_connection_string"
    export DATABASE="your_database_name"
    export COLLECTION="your_collection_name"
    export COMPANY_URL="your_company_api_url"
  4. Run the API:

    go run main.go

    The API will run on localhost:4000.

Endpoints

Upload Stock Excel Data

Request:

Upload Excel files through form data.

Response:

Returns parsed stock data along with calculated metrics in JSON format.

Example cURL:

curl -X POST http://localhost:4000/api/uploadXlsx   -F "files=@/path/to/your/excel_file.xlsx"

Sample Stock Analysis Flow

  1. Upload XLSX file: The file is parsed to extract stock information.
  2. Stock Processing: Stock data is fetched from MongoDB or external APIs.
  3. Comparison: Each stock is compared with its peers based on various financial parameters (e.g., PE, market cap).
  4. Trend Analysis: The stock's quarterly performance is analyzed.
  5. Final Score: A final score is calculated for the stock based on peer comparison, trend analysis, and other factors.

MongoDB Integration

The application connects to MongoDB to query existing stock data and update stock information if necessary. It uses a combination of text search and structured querying to retrieve relevant information about stocks.

To set up MongoDB, ensure the following:

  1. MongoDB is running locally or in the cloud.
  2. Environment variables MONGO_URI, DATABASE, and COLLECTION are set.

Key Components

Libraries Used

Graceful Shutdown

This project handles system interrupts and shuts down the server gracefully using the following signal handlers:

Example Stock Rating Logic

The application rates stocks based on multiple factors:

Example function for rating a stock:

func rateStock(stock map[string]interface{}) float64 {
    // Logic to calculate final score
}

Contribution

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Submit a pull request with detailed information about your changes.

Restore MongoDB Data

To restore a MongoDB backup using mongorestore:

  1. Ensure your backup files are in the correct directory structure. The .bson and .json files should be inside a directory named after the database you are restoring. For example:

    mongo_backup/
    └── mydatabase/
        ├── companies.bson
        ├── companies.metadata.json
  2. If the backup is on your local machine, copy it into the MongoDB container:

    docker cp ./mongo_backup/ mongodb_container:/data/db/backup
  3. Run the following command to restore the database:

    docker exec -it mongodb_container mongorestore --dir /data/db/backup/mydatabase

    This will restore the data from the mydatabase directory into your MongoDB instance.

Check Restored Data

Once the restore is complete, you can check the restored data by connecting to the MongoDB instance:


docker exec -it mongodb_container mongo