Skip to main content

A Fast and Efficient way to get the current Oil Price from Yahoo Finance API in Python.

Project description

bmdOilPriceFetch

A Fast and Efficient way to get the current Oil Price from Yahoo Finance API in Python. Data is returned as a dictionary.

I've found a lack of good simple ways to get the oil price from a free online API, so I designed one myself based upon Yahoo Finance.

It simply returns a dictionary with the current market data for Oil (WTI Front Month).

It also accepts a parameter ticker which will get the market data for that ticker on Yahoo Finance. i.e bmdPriceFetch()

Example Usage

An example usage file is included called main.py. It contains the basics for running this code. It is in essense:

from bmdOilPriceFetch import bmdPriceFetch

data = bmdPriceFetch()
if data is not None:
    outputString = f"The price of WTI is ${data['regularMarketPrice']:.2f}"
    print(outputString)

Example Output Data

The output is a dictionary:

    {   'close': 53.650001525878906, 
        'high': 53.650001525878906, 
        'lastClose': 52.98, 
        'low': 53.060001373291016,
        'open': 53.130001068115234,
        'regularMarketPrice': 53.65,
        'volume': 2664,
        'metadata': {

            'chartPreviousClose': 52.98,
            'currency': 'USD',
            'currentTradingPeriod': {

                'post': {

                    'end': 1611205140,
                    'gmtoffset': -18000,
                    'start': 1611205140,
                    'timezone': 'EST'},

                    'pre': {

                        'end': 1611118800,
                        'gmtoffset': -18000,
                        'start': 1611118800,
                        'timezone': 'EST'},

                    'regular': {

                        'end': 1611205140,
                        'gmtoffset': -18000,
                        'start': 1611118800,
                        'timezone': 'EST'}},

            'dataGranularity': '1d',
            'exchangeName': 'NYM',
            'exchangeTimezoneName': 'America/New_York',
            'firstTradeDate': 967003200,
            'gmtoffset': -18000,
            'instrumentType': 'FUTURE',
            'priceHint': 2,
            'range': '',
            'regularMarketPrice': 53.65,
            'regularMarketTime': 1611142219,
            'symbol': 'CL=F',
            'timezone': 'EST',
            'validRanges': [

                '1d',
                '5d',
                '1mo',
                '3mo',
                '6mo',
                '1y',
                '2y',
                '5y',
                '10y',
                'ytd',
                'max']}}

Changelog

  • v0.6.2 2022-01-03 Revise procedure to include simple example main.py file. No core change
  • v0.6.1 2021-11-30 Readme Fix.
  • v0.6 2021-11-30 Error Catching & Minor Readability Improvements.
  • v0.5 2021-07-11 Yahoo! Finance API decided they want a user agent. Added.
  • v0.4 2021-06-21 Bugfix. ^GSPC Lookup didnt work on weekends for Close. Made more fault tolerant.
  • v0.3 2021-02-06 Released as a package.
  • v0.2 2021-01-22 Minor tidy up, allows input of any Stock. Execution Time around 500ms. Code= 45 Lines
  • v0.1 2021-01-20 Initial Revision. Execution Time around 500ms. Code= 47 Lines

Notes for uploading to pypi

Steps:

  1. pip install twine
  2. python setup.py sdist bdist_wheel
  3. Twine upload dist/*
  4. Enter username d... and password: ....

Credits

designed in 2021 by bmd.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bmdOilPriceFetch-0.6.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bmdOilPriceFetch-0.6.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file bmdOilPriceFetch-0.6.2.tar.gz.

File metadata

  • Download URL: bmdOilPriceFetch-0.6.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.26.0 setuptools/57.1.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.6

File hashes

Hashes for bmdOilPriceFetch-0.6.2.tar.gz
Algorithm Hash digest
SHA256 f5182fb6df2e2309e92a5c664a4bf85bbd016d154af0f2b4589cd87cc95a4c64
MD5 9b25c09bf45455f40b0ef24f794db787
BLAKE2b-256 4d594d6536bb4492dcbff351cbf18627d16044b2a93624ced19a363980dcd01b

See more details on using hashes here.

File details

Details for the file bmdOilPriceFetch-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: bmdOilPriceFetch-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.26.0 setuptools/57.1.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.6

File hashes

Hashes for bmdOilPriceFetch-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 be572b3cbd6d766b937d7de1bf37dc14672304069f810058b486e642ec4b5a1b
MD5 13a347968205583dc5d1dcf2c4c00cb3
BLAKE2b-256 0f67fd5aaaf252d2684c4915144e2ab1876952deb07b35308c7e756e98deda88

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page