【问题标题】:How do I iterate through combinations of two lists and perform a function each time?如何遍历两个列表的组合并每次执行一个功能?
【发布时间】:2019-12-13 13:32:57
【问题描述】:

为历史股票数据执行 Alphavantage API 拉取。我正在拉他们的指标之一。我不想编写 36 个单独的函数并手动拉取,而是想遍历 36 种可能的组合,并每次使用不同的变量(变量是每个组合)进行拉取。下面是我的代码。它当前返回“NONE”。我做错了什么?

另外,有没有办法将这两个功能合二为一?

谢谢!

def get_ppo_series(matype, series_type):
    pull_parameters = {
        'function': 'PPO',
        'symbol': stock,
        'interval': interval,
        'series_type': series_type,
        'fastperiod': 12,
        'slowperiod': 26,
        'matype': matype,
        'datatype': 'json',
        'apikey': key
    }
    column = 0
    pull = rq.get(url, params=pull_parameters)
    data = pull.json()
    df = pd.DataFrame.from_dict(data['Technical Analysis: PPO'], orient='index', dtype=float)
    df.reset_index(level=0, inplace=True)
    df.columns = ['Date', 'PPO Series ' + str(column)]
    df.insert(0, 'Stock', stock)
    column += 1
    return df.tail(past_years * annual_trading_days)

def run_ppo_series():
    matype = list(range(8))
    series_type = ['open', 'high', 'low', 'close']
    combinations = product(matype, series_type)
    for matype, series_type in combinations:
        get_ppo_series(matype, series_type)

print(run_ppo_series())

我还尝试了以下方法。这个版本至少运行了一次迭代并返回了数据。但它停在那里???

def get_ppo_series():
    column = 0
    matype = list(range(8))
    series_type = ['open', 'high', 'low', 'close']
    combinations = product(matype, series_type)
    for matype, series_type in combinations:
        pull_parameters = {
            'function': 'PPO',
            'symbol': stock,
            'interval': interval,
            'series_type': series_type,
            'fastperiod': 12,
            'slowperiod': 26,
            'matype': matype,
            'datatype': 'json',
            'apikey': key
        }
        pull = rq.get(url, params=pull_parameters)
        data = pull.json()
        df = pd.DataFrame.from_dict(data['Technical Analysis: PPO'], orient='index', dtype=float)
        df.reset_index(level=0, inplace=True)
        df.columns = ['Date', 'PPO Series ' + str(column)]
        df.insert(0, 'Stock', stock)
        column += 1
        return df.tail(past_years * annual_trading_days)

print(get_ppo_series())

【问题讨论】:

    标签: python-3.x pandas function iteration alpha-vantage


    【解决方案1】:
    import requests as rq
    import itertools
    
    url = 'https://www.alphavantage.co/query?'
    key = 'get your own key'
    
    def get_ppo_series(matype, series_type):
        pull_parameters = {
            'function': 'PPO',
            'symbol': 'msft',
            'interval': '60min',
            'series_type': series_type,
            'fastperiod': 12,
            'slowperiod': 26,
            'matype': matype,
            'datatype': 'json',
            'apikey': key
        }
        column = 0
        pull = rq.get(url, params=pull_parameters)
        data = pull.json()
        print('*' * 50)
        print(f'MAType: {matype}, Series: {series_type}')
        print(data)
    
    def run_ppo_series():
        matype = list(range(8))
        series_type = ['open', 'high', 'low', 'close']
        combinations = itertools.product(matype, series_type)
        for matype, series_type in combinations:
            get_ppo_series(matype, series_type)
    
    run_ppo_series()
    
    1. 上面的代码可以正常工作一次提供symbolinterval值。
    2. 感谢您使用 Alpha Vantage!我们的标准 API 调用频率是每分钟 5 次调用,每天 500 次调用
    3. 我没有理会get_ppo_seriesDataFrame 部分,因为它与接收数据无关
    4. 我会将函数分开,它看起来更简洁,而且我认为函数执行 1 事情是标准的。
    5. 可以在每 5 次迭代后向代码和 time.sleep(60) 添加一个计数器,除非您有不同的 API 调用频率

    每 5 次 api 调用后等待 60 秒的功能

    import time
    
    def run_ppo_series():
        matype = list(range(8))
        series_type = ['open', 'high', 'low', 'close']
        combinations = itertools.product(matype, series_type)
        count = 0
        for matype, series_type in combinations:
            if (count%5 == 0) & (count != 0):
                time.sleep(60)
            get_ppo_series(matype, series_type)
            count+=1
    

    【讨论】:

    • 非常感谢!你是摇滚明星!
    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    相关资源
    最近更新 更多