skip to Main Content

I have the following function, which does extracts some data and then tries to store each data in its respective location in the dictionary tickerdata. Each value of the dictionary is another dictionary. However, the values for the first inner dictionary(key = "MMM") are being set as the values for the rest of the dictionary. I am uncertain on how to fix this.

def scandata(volumelimita, volumelimitb, lastchangelimita,lastchangelimitb, ivlimita, ivlimitb):
    tickers = save_sp500_tickers()
    tickerdata = {key: {} for key in tickers}
    keydefine = {"price":0, "volume":0, "netchange":0, "iv":0}
    tickerdata = {key: keydefine for key in tickers}
    for i in range(100):
        #print("increment is "+str(i))
        stock = tickers[i]
        try:
            if((fetchstock(stock)['totalVolume']>volumelimita) and (fetchstock(stock)['totalVolume']< volumelimitb) and (fetchstock(stock)['netChange']>lastchangelimita) and (fetchstock(stock)['netChange']< lastchangelimitb) and((fetchstock(stock)['volatility']*100)>ivlimita)and((fetchstock(stock)['volatility']*100)<ivlimitb)):
                tickerdata[stock]['price'] = fetchstock(stock)[stock]['lastPrice']
                tickerdata[stock]['volume'] = fetchstock(stock)[stock]['totalVolume']
                tickerdata[stock]['netchange']= fetchstock(stock)[stock]['netChange']
                tickerdata[stock]['iv'] = fetchstock(stock)[stock]['volatility']*100
        except KeyError:
            print('here was a key error at '+ stock)
            pass
    return tickerdata       

Output:

here was a key error at AKAM
here was a key error at ALK
here was a key error at ALB
here was a key error at ARE
here was a key error at ALXN
here was a key error at ALGN
here was a key error at ALLE
here was a key error at LNT
here was a key error at ALL
here was a key error at GOOGL
here was a key error at GOOG
here was a key error at MO
here was a key error at AMZN
here was a key error at AMCR
here was a key error at AEE
here was a key error at AAL
here was a key error at AEP
here was a key error at AXP
here was a key error at AIG
here was a key error at AMT
here was a key error at AWK
here was a key error at AMP
here was a key error at ABC
here was a key error at AME
here was a key error at AMGN
here was a key error at APH
here was a key error at ADI
here was a key error at ANSS
here was a key error at ANTM
here was a key error at AON
here was a key error at AOS
here was a key error at APA
here was a key error at AIV
here was a key error at AAPL
here was a key error at AMAT
here was a key error at APTV
here was a key error at ADM
here was a key error at ANET
here was a key error at AJG
here was a key error at AIZ
here was a key error at T
here was a key error at ATO
here was a key error at ADSK
here was a key error at ADP
here was a key error at AZO
here was a key error at AVB
here was a key error at AVY
here was a key error at BKR
here was a key error at BLL
here was a key error at BAC
here was a key error at BK
here was a key error at BAX
here was a key error at BDX
here was a key error at BRK.B
here was a key error at BBY
here was a key error at BIO
here was a key error at BIIB
here was a key error at BLK
here was a key error at BA
here was a key error at BKNG
here was a key error at BWA
here was a key error at BXP
here was a key error at BSX
here was a key error at BMY
here was a key error at AVGO
here was a key error at BR
here was a key error at CBRE
here was a key error at CDW
here was a key error at CE
here was a key error at CNC
here was a key error at CNP
here was a key error at CERN
here was a key error at CF
here was a key error at SCHW
{'MMM': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ABT': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ABBV': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ABMD': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ACN': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ATVI': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ADBE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AMD': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AAP': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AES': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AFL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'A': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'APD': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AKAM': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ALK': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ALB': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ARE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ALXN': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ALGN': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ALLE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'LNT': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ALL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'GOOGL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'GOOG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'MO': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AMZN': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AMCR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AEE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AAL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AEP': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AXP': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AIG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AMT': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AWK': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AMP': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ABC': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AME': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AMGN': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'APH': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ADI': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ANSS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ANTM': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AON': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AOS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'APA': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AIV': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AAPL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AMAT': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'APTV': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ADM': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ANET': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AJG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AIZ': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'T': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ATO': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ADSK': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ADP': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AZO': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AVB': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AVY': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BKR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BLL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BAC': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BK': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BAX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BDX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BRK.B': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BBY': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BIO': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BIIB': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BLK': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BA': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BKNG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BWA': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BXP': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BSX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BMY': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'AVGO': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'BF.B': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CHRW': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'COG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CDNS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CPB': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'COF': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CAH': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'KMX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CCL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CARR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CTLT': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CAT': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CBOE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CBRE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CDW': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CNC': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CNP': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CERN': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CF': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'SCHW': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CHTR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CVX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CMG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CB': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CHD': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CI': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CINF': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CTAS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CSCO': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'C': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CFG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CTXS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CLX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CME': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CMS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'KO': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CTSH': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CMCSA': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CMA': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CAG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CXO': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'COP': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ED': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'STZ': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'COO': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CPRT': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'GLW': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CTVA': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'COST': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CCI': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CSX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CMI': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'CVS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DHI': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DHR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DRI': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DVA': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DAL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'XRAY': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DVN': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DXCM': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FANG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DLR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DFS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DISCA': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DISCK': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DISH': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DLTR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'D': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DPZ': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DOV': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DOW': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DTE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DUK': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DRE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DD': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'DXC': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EMN': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ETN': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EBAY': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ECL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EIX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EW': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EA': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EMR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ETR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EOG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EFX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EQIX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EQR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ESS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EL': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ETSY': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EVRG': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'ES': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'RE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EXC': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EXPE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EXPD': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'EXR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'XOM': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FFIV': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FB': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FAST': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FRT': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FDX': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FIS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FITB': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FE': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FRC': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FISV': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FLT': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FLIR': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FLS': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FMC': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'F': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FTNT': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FTV': {'price': 85.97, 'volume': 990589, 'netchange': 0.0, 'iv': 4.78}, 'FBHS': {'price': 85.97, 'volume': 990589..

2

Answers


  1. keydefine = {"price":0, "volume":0, "netchange":0, "iv":0}
    tickerdata = {key: keydefine for key in tickers}
    

    This uses a reference to the same dict object for every value in tickerdata. See with id:

    print([id(val) for val in tickerdata.values()])
    # [13068360, 13068360]
    

    Either use keydefine.copy() or create a new dict directly:

    tickerdata = {key: keydefine.copy() for key in tickers}
    

    or

    tickerdata = {key: {"price": 0, "volume": 0, "netchange": 0, "iv": 0} for key in tickers}
    

    Or as @roganjosh noted in the comments, you don’t even have to initialize the inner dict with values:

    tickerdata = {key: {} for key in tickers}
    

    At this point you can simply do

    tickerdata  = dict.fromkeys(tickers, {})
    
    Login or Signup to reply.
  2. The issue seems to be fetchstock(stock)[stock]. In if statements you just use fetchstock(stock) to be a dict. Recommend the following changes so you can see what key is failing?

    def scandata(volumelimita, volumelimitb, lastchangelimita,lastchangelimitb, ivlimita, ivlimitb):
        tickers = save_sp500_tickers()
        keydefine = {"price":0, "volume":0, "netchange":0, "iv":0}
        tickerdata = {key: keydefine for key in tickers}
        for stock in tickers[0:100]
            try:
                k = None
                s = fetchstock(stock)
                k = 'totalVolume'
                if not (s[k]>volumelimita): continue 
                if not (s[k]< volumelimitb): continue
    
                k = 'netChange'
                if not (s[k]>lastchangelimita): continue
                if not (s[k]< lastchangelimitb): continue
    
                k = 'volatility'
                volatility = s[k]*100
                if not (volatility>ivlimita): continue
                if not ((volatility<ivlimitb): continue
    
                t = tickerdata[stock]
    
                t['iv']       = volatility
    
                k = 'lastPrice'
                t['price']    = s[k]
    
                k = 'totalVolume'
                t['volume']   = s[k]
    
                k = 'netChange'
                t['netchange']= s[]
    
    
            except KeyError:
                print('here was a key error at '+ stock, k)
                pass
        return tickerdata  
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search