-1

I have the following code:

import lxml
from urllib.request import urlopen
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
from pandas_datareader import data as web
import datetime 
import seaborn as sn

df1 = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
tickers = df1['Symbol'].values.tolist()
tickers = [x.replace('.', '-') for x in tickers] 
for ticker in tickers:
    df = web.DataReader(ticker, 'yahoo')
    if df['Close'][-2] > df['Open'][-2]:
        fifty_pct = (df['Open'].iloc[-2] + df['Close'].iloc[-2])/2
        if df['Open'][-1] > df['Close'][-1] and df['Close'] < fifty_pct:
            print(ticker)

When I run this code for some reason i am getting the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Why am i getting this error?

AMC
  • 2,642
  • 7
  • 13
  • 35
Slartibartfast
  • 1,058
  • 4
  • 26
  • 60

1 Answers1

0

Fix your code by adding any

if df['Open'][-1] > df['Close'][-1] and (df['Close'] < fifty_pct).any():
BENY
  • 317,841
  • 20
  • 164
  • 234