0

我有 Python 中的 TD API,我试图从多引号 api 门户获取 423 个引号,但它并不总是返回 423。这听起来像异步情况吗?这是我实现异步的尝试,但我确定我没有做对。任何指针?

n = symbols_list
payload = {'symbol':n}

content = requests.get(url = endpoint, params = payload, headers = headers)
data = content.json()
time.sleep(1)

async def get(
    session: aiohttp.ClientSession,
    n: str,
    **kwargs
) -> dict:
    url = endpoint
    headers = headers
    print(f"Requesting {url}")
    resp = await session.request('GET', url=url, **kwargs)
    # Note that this may raise an exception for non-2xx responses
    # You can either handle that here, or pass the exception through
    data = await resp.json()
    print('Received data for {url}')
    return data


async def main(n, **kwargs):
    # Asynchronous context manager.  Prefer this rather
    # than using a different session for each GET request
    async with aiohttp.ClientSession() as session:
        tasks = []
        for symbols in n:
            try:
                tasks.append(get(session=session, n=symbols, **kwargs))
                payload = {'symbol':symbols}
                a = data
                b = a[symbols]['symbol']
                c = a[symbols]['lastPrice']
                d = a[symbols]['netChange']
                e = a[symbols]['totalVolume']
                f = a[symbols]['regularMarketLastPrice']
                g = a[symbols]['highPrice']
                h = a[symbols]['lowPrice']
                quotes = pd.DataFrame({'symbol' : [symbols], 'last' : [c], 'change' : [d], 'volume' : [e]
                              ,'OOCLast' : [f], 'high' : [g], 'low' : [h]})
                quotes.to_sql(name='quotes', con=engine, if_exists='append')
            except:
                pass
                
        # asyncio.gather() will wait on the entire task set to be
        # completed.  If you want to process results greedily as they come in,
        # loop over asyncio.as_com#pleted()
                htmls = await asyncio.gather(*tasks, return_exceptions=True)
                return htmls
               
            


if __name__ == '__main__':
    n = symbols_list
    # ...
    # Either take colors from stdin or make some default here
    await main(n)


4

2 回答 2

0

乔纳森。您是否认为将我的 api 密钥放在 requests.get 括号的标题部分中,而不是放在上面的有效负载括号中与它有什么关系?

 ##Quotes

from sqlalchemy import create_engine
import pandas as pd
import requests
import sqlite3


engine = create_engine('sqlite:///iterist.db', echo=False)
con = sqlite3.connect("iterist.db")
cur=con.cursor()
cur.execute('drop table if exists quotes2')

distinct_symbols="""select distinct symbol from index_symbols order by symbol asc"""
main_list=pd.read_sql(distinct_symbols, engine)
df = pd.DataFrame(main_list, columns=['symbol'])
symbols_list=df['symbol'].values.tolist()

n = symbols_list

sym_list = n[0:401] # take a slice of the symbols from earlier

chunk_size = 400 # each call has this many symbols returned; pretty sure the max symbols per call is 500, but you can try any number here and it'll just loop the sym_list
num_calls = int(len(sym_list) / chunk_size) + 1

df_hold_list = [] #collect intermidiary calls
for i in range(0,num_calls):
    sym_string = ','.join(sym_list[chunk_size*i:chunk_size*(i+1)]) #make the list into a string
    #print(i, sym_string)
    endpoint = 'https://api.tdameritrade.com/v1/marketdata/quotes'
    payload ={
    'symbol': sym_string,
    }
    response = requests.get(endpoint,params=payload, headers=headers)
    # print(response.url)
    data = response.json()
    # print(data)
    df = pd.DataFrame(data).T #transpose dataframe
    #print(df)
    df_hold_list.append(df)

dff = pd.concat(df_hold_list) # final dataframe; from here you can do whatever you    wish with the data
dff.to_sql(name='quote2', con=engine, if_exists='replace')
于 2021-04-14T02:28:29.020 回答
0

这可能比您想要的要多一些,但您可以根据需要对其进行修改。我使用第一个调用来获取符号数据集,这样我就可以使用大量符号并测试 api 端点。

见内联评论:

import pandas as pd
import requests
apikey = 'you api'
sym_list = '[A-Z].*' # this will get ALL symbols tda supports at the time; takes a few seconds to run

endpoint = 'https://api.tdameritrade.com/v1/instruments'
payload ={
'apikey': apikey, #your api key goes here
'symbol': sym_list,
'projection' : 'symbol-regex'
}
response = requests.get(endpoint,params=payload)
print(response.url)
data = response.json()
# print(data)

df = pd.DataFrame(data).T #transpose dataframe 
df = df[df['exchange']=='NYSE'] # filter for just NYSE stock symbols
symbols = df.symbol.unique().tolist() # get symbols to a list for later (over 5,000 symbols)

注意:一些 sybmols 不会返回任何东西,但是下面的代码非常一致并且对我来说效果很好。

sym_list = symbols[0:501] # take a slice of the symbols from earlier

chunk_size = 500 # each call has this many symbols returned; pretty sure the max symbols per call is 500, but you can try any number here and it'll just loop the sym_list
num_calls = int(len(sym_list) / chunk_size) + 1

df_hold_list = [] #collect intermidiary calls
for i in range(0,num_calls):
    sym_string = ','.join(sym_list[chunk_size*i:chunk_size*(i+1)]) #make the list into a string
    # print(i, sym_string)
    endpoint = 'https://api.tdameritrade.com/v1/marketdata/quotes'
    payload ={
    'apikey': apikey, #your api key goes here
    'symbol': sym_string,
    }
    response = requests.get(endpoint,params=payload)
    # print(response.url)
    data = response.json()
    # print(data)

    df = pd.DataFrame(data).T #transpose dataframe
    df_hold_list.append(df)

dff = pd.concat(df_hold_list) # final dataframe; from here you can do whatever you wish with the data

输出:

         assetType assetMainType      cusip    symbol                                        description bidPrice  ... markChangeInDouble markPercentChangeInDouble regularMarketPercentChangeInDouble delayed realtimeEntitled assetSubType
A           EQUITY        EQUITY  00846U101         A            Agilent Technologies, Inc. Common Stock   120.80  ...               1.22                    0.9335                             0.9335    True            False          NaN
B           EQUITY        EQUITY  067806109         B                    Barnes Group, Inc. Common Stock    40.47  ...               0.50                    0.9878                             0.9878    True            False          NaN
C           EQUITY        EQUITY  172967424         C                       Citigroup, Inc. Common Stock    72.42  ...               0.09                    0.1244                             0.1244    True            False          NaN
D           EQUITY        EQUITY  25746U109         D                 Dominion Energy, Inc. Common Stock    76.20  ...               0.09                    0.1181                             0.1181    True            False          NaN
E           EQUITY        EQUITY  26874R108         E                            ENI S.p.A. Common Stock    24.00  ...              -0.21                   -0.8564                            -0.8564    True            False          ADR
...            ...           ...        ...       ...                                                ...      ...  ...                ...                       ...                                ...     ...              ...          ...
ENBA        EQUITY        EQUITY  29250N477      ENBA  Enbridge Inc 6.375% Fixed-to-Floating Rate Sub...    16.49  ...               0.00                    0.0000                             0.0000    True            False          NaN
ENBL        EQUITY        EQUITY  292480100      ENBL  Enable Midstream Partners, LP Common Units rep...     6.83  ...               0.09                    1.3216                             1.3216    True            False          NaN
CVII+       EQUITY        EQUITY  17144M110     CVII+  Churchill Capital Corp VII Warrants, each exer...     1.42  ...              -0.07                   -4.6980                            -6.0403    True            False          NaN
SOV-C.CL    EQUITY        EQUITY             SOV-C.CL  Santander Holdings USA, Inc. Dep Shs repstg 1/...    24.99  ...              25.01                    0.0000                             0.0000    True            False          NaN
ENIA        EQUITY        EQUITY  29274F104      ENIA      Enel Americas S.A. American Depositary Shares     8.62  ...              -0.06                   -0.6865                            -0.6865    True            False          ADR

[492 rows x 49 columns]
于 2021-04-10T02:35:38.433 回答