Threading with yfinance?

Viewed 54

So I was wondering how I could use threading or more specifically the concurrent.futures.ThreadPoolExecutor() to get a faster runtime of this script? as it heavily depends on downloads from yahoo finance.

Thankful for any help or suggestions. also any suggestions for cleaner/better code-style are more than welcomed. Thanks.

import yfinance as yf
import pandas as pd
import statistics
import matplotlib.pyplot as plt
import time
import concurrent.futures #!

correlation_snp = []
correlation_gold = []
correlation_bonds = []

def callculate_range():

    starting_point = 9

    for i in range(starting_point, 52):

        time_range = f'{i}wk'

        BTC = yf.download(tickers='BTC-USD', period=time_range, interval='1d')
        BTC.rename(columns={'Adj Close': 'BTC Closing'}, inplace=True)

        SaP = yf.download('SPY', period=time_range, interval="1d")
        SaP.rename(columns={'Adj Close': 'S&P Closing'}, inplace=True)

        Gold = yf.download('GC=F', period=time_range, interval="1d")
        Gold.rename(columns={'Adj Close': 'Gold Closing'}, inplace=True)

        Bonds = yf.download('MXBIX', period=time_range, interval="1d")
        Bonds.rename(columns={'Adj Close': 'Bonds Closing'}, inplace=True)


        btc_closing = BTC['BTC Closing']
        btc_closing = pd.Series(btc_closing, index=SaP.index)
        gold_closing = Gold['Gold Closing']
        gold_closing = pd.Series(gold_closing, index=SaP.index)
        sap_closing = SaP['S&P Closing']
        bond_closing = Bonds['Bonds Closing']
        bond_closing = pd.Series(bond_closing, index=SaP.index)

        correlation_snp.append(btc_closing.corr(sap_closing))
        correlation_gold.append(btc_closing.corr(gold_closing))
        correlation_bonds.append(btc_closing.corr(bond_closing))
0 Answers
Related