Search code examples
pythonpython-3.xlistfinance

Trouble with creating list


I made a stock screener that gives the technicals of stocks that meat my criteria, at the end, I want it to print a concise list of stocks that do meet my criteria. I tried to use the append function, but it only prints one stock, instead, I want it to print the ticker of each stock that prints, for example 'mmm',...

The current output looks like this. MMM Trailing P/E: 17.61 Return on Equity: 54.34% Revenue: 32.35B Quarterly Revenue Growth: -5.00%


def scrape(stock_list, interested, technicals):
    for each_stock in stock_list:
        technicals = scrape_yahoo(each_stock)
        condition_1 = float(technicals.get('Return on Equity',0).replace('%','').replace('N/A','-100')) > 25
        condition_2 = float(technicals.get('Trailing P/E',0).replace('N/A','')) > 15
        condition_3 = float(technicals.get('Price/Book (mrq)',0)) <15
        if (condition_1 and condition_2)==True:
            print(each_stock)
            SuggestedStocks = []
            SuggestedStocks.append(each_stock)  
            for ind in interested: 

                print(ind + ": "+ technicals[ind])         
            print("------")
            time.sleep(1)                                                    # Use delay to avoid getting flagged as bot
    #return technicals
    print(SuggestedStocks)


def main():

    stock_list = ['MMM', 'ABT', 'ABBV', 'ABMD', 'ACN', 'ATVI', 'ADBE', 'AMD']
    interested = ['Trailing P/E', 'Return on Equity', 'Revenue', 'Quarterly Revenue Growth']
    technicals = {}

    tech = scrape(stock_list, interested, technicals)
    print(tech)
main()

It only prints the ticker of the last stock that meets my criteria:ADBE.


Solution

  • It is because you reset suggested stocks in each loop SuggestedStocks = [] you should initialize it outside of the loop.