Pandas Dataframe Yahoo Finance检查交易量是否符合标准

时间:2018-08-24 16:42:42

标签: python pandas dataframe

下面的程序将.CSV文件中的数千个股票行情自动收录器导入到列表中,并将行情自动收录器作为参数传递给函数,该函数将拉出该特定股票的“已调整平仓”列,并将票据设置为列名。这是我有一个包含数千种股票的数据框,仅通过使用df ['EnterTickerNameHere']就可以获取调整后的收盘价。来自yahoo的数据包含“交易量”字段,如果交易量大于100,000,我只想向其df添加股票。我不确定如何在保持数据框与现在相同的格式的同时执行此操作。谢谢您的阅读!

import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
from pandas_datareader import data as pdr
import datetime
import fix_yahoo_finance as yf
yf.pdr_override()
import time

def getTickers():
    today = str(datetime.date.today())
    fourty_days_prior = str(datetime.date.today() - datetime.timedelta(days=40))

    # import the tickers from csv to a python list
    tickers = pd.read_csv('tickers.csv', sep='\n',dtype={'Tickers' : str})
    tickers = tickers.Tickers.tolist()

    data = pdr.get_data_yahoo(tickers, start= fourty_days_prior, end=today)['Adj Close']

2 个答案:

答案 0 :(得分:0)

首先在df中加载['Adj Close']:

data = pdr.get_data_yahoo(tickers, start= fourty_days_prior, end=today)['Adj Close']

现在为卷创建单独的df:

volume = pdr.get_data_yahoo(tickers, start= fourty_days_prior, end=today)['Volume']

(假设这会在df中获得列名称为“ Volume”的卷)

现在您可以这样做:

data['volume'] = [value if value > 100000 else 0 for value in volume['Volume']

(如果您不希望零,则可以使用“无”)

现在您在df中有一个“音量”列,该列仅包含大于100000的音量值

答案 1 :(得分:0)

您不必两次下载所有数据。

1)为每个股票下载最新的交易量(仅需要1天的数据)

2)将条件应用于本系列以更新您的股票清单

3)下载调整。关闭以查看此减少的行情清单

def getTickers(min_volume=100000):
    today = str(datetime.date.today())
    fourty_days_prior = str(datetime.date.today() - datetime.timedelta(days=40))

    # import the tickers from csv to a python list
    tickers = pd.read_csv('tickers.csv', sep='\n',dtype={'Tickers' : str})
    tickers = tickers.Tickers.tolist()

    # first import the latest volumes and apply your condition
    volumes = pdr.get_data_yahoo(tickers, start=today, end=today)['Volume'].T
    filtered_tickers = volumes[volumes.iloc[:,0] > min_volume].index.tolist()

    data = pdr.get_data_yahoo(filtered_tickers, start= fourty_days_prior, end=today)['Adj Close']
    return data