vendredi 24 juillet 2020

How can I condition a column's row within a DataFrame within a function?

I am trying to loop an if condition over a DataFrame column within the tracker() function. Fundamentally I want to be able to place an order through an api if the condition is triggered (highlighted in bold). I am struggling to find a way of doing so. Help would be much appreciated.

Currently the DataFrames are produced, but no action is occurring with regards to my 'if' statement, and no error message is produced. Thanks!

#Initialising empty list
p = []
df = pd.DataFrame()
#Function turns the sequential values into a list then into a df
def tracker(p, df):
    url = 'https://finance.yahoo.com/...'
    response=requests.get(url)
    soup = BeautifulSoup(response.text, 'lxml')
    price= float(soup.find('div', {'class':'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text)

    p.append(price)
    df = pd.DataFrame(p)
    df.columns = ['Price']
    df['Difference'] = df['Price'].diff()
    #waiting period
    time.sleep(5)
    return df
    **if df.loc[df['Difference']] == 0.01:
        order = api.submit_order(...)
        print(order)**

while True:
    print(tracker(p,df))

Aucun commentaire:

Enregistrer un commentaire