beginner – Python yahoo finance option chain scraper

This is my 1st python program I’ve actually written and I have little to no background in the language I just figured I could learn and do something that interested me at the same time.

So, I’m not a book learner. I can’t just sit down and read a bunch of technical documents and stuff like that so I’m sure a lot of this code is unconventional at best and just plain bad at worst. I learn best by doing, so now that I got this program to work I want to learn how to make it better.

The goal of the program is to load the yahoo finance options page for each ticker in a file that I created and then pull all of the call options data for each expiration date and then load all of that data into a sql database to be queried later

I added the multi-processing to try and make it faster but it seems to have just slowed it down so I gotta figure that part out

import logging
import pyodbc
import config
import yahoo_fin as yfin
import asyncio
import multiprocessing
import time
from yahoo_fin import options
from datetime import datetime, date
from selenium import webdriver


def main():
    read_ticker_file()


def init_selenium():
    driver = webdriver.Chrome(config.CHROME_DRIVER)
    return driver


def yfin_options(symbol):
    logging.basicConfig(filename='yfin.log', level=logging.INFO)
    logging.basicConfig(filename='no_options.log', level=logging.ERROR)

    try:
        # get all options dates (in epoch) from dropdown on yahoo finance options page
        dates = get_exp_dates(symbol)

        # iterate each date to get all calls and insert into sql db
        for date in dates:
            arr = yfin.options.get_calls(symbol, date)

            arr_length = len(arr.values)

            i = 0

            for x in range(0, arr_length):
                strike: str = str(arr.values(i)(2))
                volume = str(arr.values(i)(8))
                open_interest = str(arr.values(i)(9))
                convert_epoch = datetime.fromtimestamp(int(date))
                try:
                    sql_insert(symbol, strike, volume, open_interest, convert_epoch)
                    i += 1
                except Exception as insert_fail:
                    print("I failed at sqlinsert {0}".format(insert_fail))
            file_name_dir = "C:\temp\rh\options{0}{1}.xlsx".format(symbol, date)
            logging.info(arr.to_excel(file_name_dir))

    except Exception as e:
        bad_tickers_file_dir = config.BAD_TICKERS
        f = open(bad_tickers_file_dir, "a")
        f.write(symbol)
        f.write('n')


def sql_insert(symbol, strike, volume, open_interest, exp_date):
    conn_string = ('Driver={SQL Server};'
                   'Server=DESKTOP-7ONNV8L;'
                   'Database=optionsdb;'
                   'Trusted_Connection=yes;')

    conn = pyodbc.connect(conn_string)
    cursor = conn.cursor()

    insert_string = """INSERT INTO dbo.options (Ticker, Strike, Volume, OpenInterest, expDate)
                    VALUES
                    (?, ?, ?, ?, ?)"""

    cursor.execute(insert_string, symbol, strike, volume, open_interest, str(exp_date))

    conn.commit()


def get_exp_dates(symbol):
    url = "https://finance.yahoo.com/quote/" + symbol + "/options?p=" + symbol
    chromedriver = init_selenium()
    chromedriver.get(url)
    # Yahoo Finance options dropdown class name (find better way to do this)
    select_dropdown = chromedriver.find_element_by_css_selector("div(class='Fl(start) Pend(18px)') > select")
    options_list = (x for x in select_dropdown.find_elements_by_tag_name("option"))
    dates = ()
    for element in options_list:
        dates.append(element.get_attribute("value"))

    return dates


def read_ticker_file():
    file1 = open(config.TICKER_FILE, 'r')
    lines = file1.readlines()

    count = 0

    ticker_arr = ()
    # loop to read each ticker in file
    for line in lines:
        count += 1
        line = line.strip('n')
        line = line.strip()
        ticker_arr.append(line)

    return ticker_arr


if __name__ == "__main__":
    pool = multiprocessing.Pool()

    # input list
    inputs = read_ticker_file()
    # pool object with number of element
    pool = multiprocessing.Pool(processes=4)

    pool.map(yfin_options, inputs)

    pool.close()
    pool.join()

How much does it cost to advertise on Yahoo Answers?

Advertising

y u no do it?

Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

Starts at just $1 per CPM or $0.10 per CPC.

yahoo – A email was sent using my ip address but not from my device


Your privacy


By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.




python 3.x – ¿Ya no es posible acceder a Yahoo Finanzas?

Desde hace unas semana, no puedo acceder a Yahoo Finanzas, de diferentes maneras, con script que siempre me funcionaron bien. Todas las opciones me devuelven errores, muy largos para transcribirlos en esta consulta.
Opción 1.

import pandas
from pandas_datareader import data as pdr
import yfinance as yfin
yfin.pdr_override()

df = pdr.get_data_yahoo("TSLA", start="2017-01-01", end="2021-04-30")
print(df)

Opción 2

import datetime as dt
import pandas_datareader.data as pdr

# Descarga datos de ejemplo
AIG = pdr.DataReader('AIG', 'yahoo', start = dt.datetime(2011,1,1),
end = dt.datetime(2011,12,31))

Opción 3:

from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override() 

start = "2000-1-4"
end = "2020-7-19"
iberdrola = pdr.DataReader("IBE.MC", start, end)
iberdrola = iberdrola(("Close"))
iberdrola(:3)

¿Han dejado de permitir las descargas?.

I cannot send a long email through either chrome or yahoo. Might the problem be that the email is too long?

I cannot send a long email through either chrome or yahoo. Might the problem be that the email is too long? – Web Applications Stack Exchange

Here is a free list of good free advertising sites I got from yahoo answers.

Advertising

y u no do it?

Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

Starts at just $1 per CPM or $0.10 per CPC.

cannot copy and paste in yahoo

When I highlight bcc from the email that was sent earlier – copy – paste some of the emails addresses turn pink, and the email is not being sent to that recipient.

Any suggestions?

yahoo finance – TA-Lib Indicators in Backtrader at Python

I am beginner in Python, I Got Error: ” TypeError: Invalid parameter value for nbdevup (expected float, got int) ”
when Using BolingerBands Indicator of talib in backtrader strategy. Here is my strategy Calss:

How Can I fix It?

import backtrader as bt
import talib

class TestStrategy(bt.Strategy):

def log(self, txt, dt=None):
    # breakpoint()
    ''' Logging function fot this strategy'''
    dt = dt or self.datas(0).datetime.date(0)
    dt2 = self.datas(0).datetime.time(0)
    # print('%s, %s' % (dt.isoformat(), txt))
    print('%s %s, %s' % (dt.isoformat(),dt2, txt))
     

def __init__(self):  ## initialize function
    # Keep a reference to the "close" line in the data(0) dataseries
    breakpoint()
    self.dataclose = self.datas(0).close
    self.order = None ## because at first we have no orders
    breakpoint()
    upper_band, mov_ave, lower_band = bt.talib.BBANDS(self, timeperiod = 14)

    # Ema_fast = bt.ind.EMA(period=5)
    # ma_slow = bt.ind.SMA(period=8)
    # Keep a reference to the "close" line in the data(0) dataseries
    self.dataclose = self.datas(0).close
    self.order = None ## because at first we have no orders
    self.crossover = bt.ind.CrossOver(lower_band,self.close)
    

def notify_order(self,order):
    if order.status in (order.Submitted,order.Accepted):
        return
    if order.status in (order.Completed): 
        if order.isbuy:
            self.log('BUY EXECUTED {}'.format(order.executed.price))
        elif order.issell():
            self.log('SELL EXECUTED {}'.format(order.executed.price))
        self.bar_executed = len(self)
        
    self.order = None ## reset to no order
    
     
def next(self):
    # Simply log the closing price of the series from the reference
    self.log('Close, %.2f' % self.dataclose(0))
    if self.order:
        return
    if not self.position:
    
    # print(len(self))
    # print(self.order)
    # print(self.position)
        if  self.crossover > 0:    
            # BUY, BUY, BUY!!!
            self.log('BUY CREATE, %.2f' % self.dataclose(0))
            self.order = self.buy() ## track num of sell amd buy
                
    else:  ## when we start to sell
        if self.crossover < 0: ## 5 days after I Placed Buy
            self.log('SELL CREATED {}'.format(self.dataclose(0)))
            self.order = self.sell()    ## track num of sell amd buy

yahoo mail – Can I forward an email without a recipient?

I seem to have somehow forwarded an email to myself, but there’s no recipient!

I don’t understand what I did, and I can’t replicate the behavior.

Does anyone know what’s going on?

This is not a case of BCC. With forwarding via BCC, in the body of the message, you get

—– Forwarded Message —–
From: “myname” xxx@yahoo.com
To:

Where xxx is my email address, and To: field is blank.

In this case, the body contains

—– Forwarded Message —–
From: “sender” sender@gmail.com
To: “differentname” xxx@yahoo.com

Where ‘sender’ is the original sender, and ‘differentname’ is my name as it appears in the sender’s contact list. (It is not the same as ‘myname’ from the BCC, which is my name as it appears in my contacts.)
enter image description here

Where did Yahoo! go wrong?

Yahoo! is one of the first-ever websites, ever.
It has been around for almost as long as I have been, but for some reason, at some point, it lost its popularity.
Now you have these big giants, the social media sites, google, all these sites that are now the most well-known sites in the world, while not as many even know about the existence of Yahoo!.

Where did they go wrong?
At one point they were the biggest, but then they lost it, and now, as far as I know, are not even one of the top websites in the world.
SEMrush

 

DreamProxies - Cheapest USA Elite Private Proxies 100 Cheap USA Private Proxies Buy 200 Cheap USA Private Proxies 400 Best Private Proxies Cheap 1000 USA Private Proxies 2000 USA Private Proxies 5000 Cheap USA Private Proxies ExtraProxies.com - Buy Cheap Private Proxies Buy 50 Private Proxies Buy 100 Private Proxies Buy 200 Private Proxies Buy 500 Private Proxies Buy 1000 Private Proxies Buy 2000 Private Proxies ProxiesLive.com Proxies-free.com New Proxy Lists Every Day Proxies123.com Best Quality USA Private Proxies