Exporting Real-Time Quotes to Excel

Best practices, methodologies, and principles for developing robust software.
Post Reply
User avatar
paypal56_ab6mk6y7
Site Admin
Posts: 47
Joined: Sat Oct 26, 2024 3:05 pm

Exporting Real-Time Quotes to Excel

Post by paypal56_ab6mk6y7 »

**Description:**
A script or program needs to be developed that will automatically export real-time quotes from Investing or TradingView websites to an Excel file. The obtained data will be used for further information processing.

**Requirements:**

1. **Data Sources:**
- Choose one or both websites (Investing or TradingView) to obtain quotes.

2. **Functionality:**
- Automatic collection of real-time quotes.
- Saving data in Excel format (.xlsx).
- Option to select the time interval (e.g., every minute, every 5 minutes, etc.).

3. **Technical Details:**
- Use an API if available to retrieve data.
- If an API is not available, scrape data from web pages.
- Handle potential errors and exceptions during data export.
- Regularly update data (this can be implemented using a task scheduler).

4. **Output Data:**
- Structure of the Excel file:
- Date/Time
- Ticker
- Opening Price
- Current Price
- Highest Price
- Lowest Price
- Trading Volume

5. **Additional Requirements:**
- Documentation for installing and running the script/program.
- Usage examples and descriptions of possible settings.

**Expected Result:**
The output should be a complete script or program that automatically exports real-time quotes to Excel for further analysis and data processing.
User avatar
paypal56_ab6mk6y7
Site Admin
Posts: 47
Joined: Sat Oct 26, 2024 3:05 pm

Re: Exporting Real-Time Quotes to Excel

Post by paypal56_ab6mk6y7 »

### Explanation of the Completed Code

1. **Graphical Interface**:
- A window is created using `tkinter`, allowing users to input tickers and the data fetching interval.
- The "Start Fetching" button initiates the data collection process, where users enter tickers (comma-separated) and the request interval.

2. **Quote Collection**:
- The `start_fetching` function retrieves the input data, validates it, saves the configuration, and starts an infinite loop to collect quotes.
- The `get_quotes` function is used to obtain the latest quotes from the website.

3. **Saving and Visualization**:
- Quotes are saved in an Excel file using `save_to_excel`.
- Quote graphs are displayed using `plot_data` with `matplotlib`.

### Additional Improvements

- **Error Handling**: It is crucial to handle potential errors that may arise during requests to websites to avoid program hangs.
- **Stopping the Program**: Add the ability to stop data collection, for example, through a "Stop" button or by handling a program termination signal.
- **Data Visualization**: More advanced visualization can be implemented to improve data perception (e.g., interactive charts).
- **Additional Features**: Expand the functionality of the program by adding the ability to filter data or configure various chart parameters.

### Example of the Complete Code

```python

Code: Select all

import requests
import pandas as pd
from bs4 import BeautifulSoup
import time
import logging
import tkinter as tk
from tkinter import messagebox
import configparser
import matplotlib.pyplot as plt

# Set up logging
logging.basicConfig(filename='quotes.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def get_quotes(ticker):
    try:
        url = f"https://www.investing.com/equities/{ticker.lower()}"
        response = requests.get(url)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, 'html.parser')
        
        price = soup.find('span', {'class': 'text-2xl'})
        if price:
            return price.text.strip()
        else:
            logging.warning(f'Price not found for ticker: {ticker}')
            return None
    except Exception as e:
        logging.error(f'Error fetching quotes for {ticker}: {e}')
        return None

def save_to_excel(data):
    try:
        df = pd.DataFrame(data, columns=['Date/Time', 'Ticker', 'Current Price'])
        df.to_excel('quotes.xlsx', index=False, engine='openpyxl')
    except Exception as e:
        logging.error(f'Error saving to Excel: {e}')

def plot_data(data):
    df = pd.DataFrame(data, columns=['Date/Time', 'Ticker', 'Current Price'])
    for ticker in df['Ticker'].unique():
        ticker_data = df[df['Ticker'] == ticker]
        plt.plot(ticker_data['Date/Time'], ticker_data['Current Price'], label=ticker)
    
    plt.xlabel('Date/Time')
    plt.ylabel('Price')
    plt.title('Stock Prices Over Time')
    plt.legend()
    plt.show()

def save_config(tickers, interval):
    config = configparser.ConfigParser()
    config['Settings'] = {
        'tickers': ','.join(tickers),
        'interval': interval
    }
    with open('config.ini', 'w') as configfile:
        config.write(configfile)

def load_config():
    config = configparser.ConfigParser()
    config.read('config.ini')
    tickers = config['Settings']['tickers'].split(',')
    interval = int(config['Settings']['interval'])
    return tickers, interval

def start_fetching():
    tickers_input = tickers_entry.get()
    interval_input = interval_entry.get()

    if not tickers_input or not interval_input.isdigit():
        messagebox.showerror("Error", "Please enter valid tickers and interval.")
        return

    tickers = [ticker.strip() for ticker in tickers_input.split(',')]
    interval = int(interval_input)
    
    save_config(tickers, interval)  # Save configuration
    data = []

    while True:
        for ticker in tickers:
            current_price = get_quotes(ticker)
            if current_price:
                current_time = pd.Timestamp.now()
                data.append([current_time, ticker, current_price])
                logging.info(f'Successfully fetched and saved data for {ticker}: {current_price}')
            else:
                logging.warning(f'Failed to fetch data for {ticker}')
        
        save_to_excel(data)
        plot_data(data)  # Display chart
        time.sleep(interval)

app = tk.Tk()
app.title("Real-time Quote Fetcher")

tk.Label(app, text="Enter Tickers (comma-separated):").pack()
tickers_entry = tk.Entry(app)
tickers_entry.pack()

tk.Label(app, text="Enter Interval (seconds):").pack()
interval_entry = tk.Entry(app)
interval_entry.pack()

start_button = tk.Button(app, text="Start Fetching", command=start_fetching)
start_button.pack()

app.mainloop()
```

### Conclusion

This program will effectively collect real-time quotes with the ability to display data in graphical format. If desired, you can continue to expand functionality by adding new features or improving existing ones.
Post Reply