alvarobartt / investpy

Financial Data Extraction from Investing.com with Python
https://investpy.readthedocs.io/
MIT License
1.63k stars 377 forks source link

Suggestion for ticker function #174

Open antamosan opened 4 years ago

antamosan commented 4 years ago

Hello,

Following another recently closed thread, I would like to make a suggestion for a ticker function.

Although it is possible to retrive current (real time) value for an asset though historical/recent data, it is a bit unnatural and definitely unexpected (I did not see any reference in the documentation to this option) getting the info in this way. Recent data provides too much info, historical data sort of works (using current day as both limits).

Maybe it is because I am accustomed to crypto market apis (like Bitsatmp's) but a I was expecting a proper ticker function as it is (only current values).

Some aspects that I suggest for this function:

Regards.

typhoon71 commented 4 years ago

I second the addition of a dedicate method for current value retrival.

This morning I found another project that reads current value from investing.com, and then!

Here's a snippet of minimal code that retrieves the current value for bitcon:

import requests
from lxml import html

url = "https://www.investing.com/" + 'crypto' + '/' + 'bitcoin' + '/' + 'btc-eur'  # juicy part 1
user_agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x32; rv:60.0) Gecko/20100101 Firefox/60.0"

response = requests.get(url, headers={"User-Agent": user_agent})

if response.ok:
    page = html.fromstring(response.content)

    if len(page):
        price = str(page.xpath("//span[@id='last_last']/text()")[0])  # juicy part 2
        price = float(price.replace(",", ""))
        print('\n', price)

I suppose this could be a starting point?

ymyke commented 4 years ago

Here's another reason why this could make sense: Today, each call to get_stock_historical_data (with appropriate date filters set) gives me the current price of a stock as the last entry, which can change with each call, as long as the market is open.

This is somewhat misleading, because what the API reports as the Low and Close for that day is not really correct (i.e., subject to change) until the market closes.

Introducing a separate call to get the realtime values could separate the two use cases nicely and allow get_stock_historical_data to only report truly historical data, i.e. data that is correct and won't change any longer.