Loop elements in List, Boolean, if condition accepts, create a new list (python)

Asked

Viewed 41 times

-1

can you help me? I need to find the companies on the list business1 which satisfy the following condition:

the value shown in the last row of the multiple column is greater than 1 at the specific date

this search should go through for each company in the list loan 1, that is, I will add more to these

import numpy as np
import pandas as pd
import yfinance as yf

empresas1 = ['BRKM5.SA']#,'ITSA4.SA','PETR4.SA','WEGE3.SA','BBAS3.SA'] #> adicionar loop para as empresas nesta lista (remover "]#" para ter a lista completa

# historico de volume das empresas em colunas
hist_vol = yf.download(empresas1, start="2019-01-01", end="2021-08-19")[['Volume']]

# adicionando coluna média móvel do volume
periodo1 = 20
hist_vol[f'MM{periodo1}'] = hist_vol["Volume"].rolling(periodo1).mean()

# adicionando coluna múltiplo entre volume / média móvel
hist_vol['Multiplos'] = hist_vol["Volume"] / hist_vol[f'MM{periodo1}']

# exibir apenas a coluna de múltiplos
hist_vol = hist_vol.drop(columns=['Volume',f'MM{periodo1}'])

# saida (resultado)
valor = hist_vol['Multiplos'].tail(1).item()
if valor >= 1:
  print("ok") #se sim, incluir o nome da empresa em uma nova lista [] e após finalizar o loop imprimir a nova lista
else:
  print("Não faz nada") #continua o loop e avaliar as outras empresas

1 answer

3


You need to implement a loop, for this I recommend using the for, syntax is something like: for each item in a list,

    lista = ['BRKM5.SA', 'ITSA4.SA','PETR4.SA','WEGE3.SA','BBAS3.SA']
    for item in lista:
        print(item)
        # você utiliza o item  da lista para fazer suas buscas agora.

  • thanks @Gorn, it worked, I got the result!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.