<jupyter_start><jupyter_text>WRDSWharton Research Data Services (WRDS) provides rich amount of data and fruitful tutorials. We will count on WRDS to pull security price, fundamental, and analyst data.After finishing [initial setup](https://wrds-www.wharton.upenn.edu/pages/support/programming-wrds/programming-python/python-from-your-computer/initial-setup-the-pgpass-file), you can create a new connection by using the following code:<jupyter_code># Now that we've installed Python and the wrds module, the next step is to setup a pgpass file on your workstation. The pgpass file includes your WRDS username and password so that you do not need to enter them each time you wish to connect to WRDS within Python. With the wrds module, creating this file is easy!

# Where wrds_username is your own WRDS username (the same as your login to the WRDS website). You will be prompted once for your WRDS username and password on your first login, at the Connection() step, but then, after running create_pgpass_file() once, you should be able to connect from then on without needing to do so.

import wrds
db = wrds.Connection(wrds_username='joe')
db.create_pgpass_file()
#Test this by disconnecting and reconnecting, using the following:

db.close()
db = wrds.Connection(wrds_username='joe')
# you can create a new connection by using the following code:

db = wrds.Connection(wrds_username='fanjia')
# You can use this connection object (`db`) to query WRDS database. You can find company specific code using [WRDS company code lookup](https://wrds-web.wharton.upenn.edu/wrds/code_search/). Note that the most standard identifiers, such as companies’ tickers and CUSIPs, tend to change over time. Therefore, using GVKEY (Global Company Key) which is a unique number assigned to each company in the Compustat-Capital IQ database is always prefered. To pull the company information of BMW, you can query against [Compustat Global Daily](https://wrds-www.wharton.upenn.edu/data-dictionary/comp_global_daily/) library:

db.raw_sql(
    """
    SELECT *
    FROM comp_global_daily.g_company
    WHERE gvkey = '100022'
    """)
# The following code queries against the daily prices of BMW since 1/1/2001. Note that the adjusted price is PRICE/AJEXDI and you need to adjust that manually: 

db.raw_sql(
    """
    SELECT datadate as date, prchd as high, 
            prcld as low, prcod as open, prccd as close, 
            cshtrd as volume, ajexdi 
    FROM comp_global_daily.g_sec_dprc
    WHERE datadate >= '2001-01-01'::date
    AND gvkey = '100022'
    AND iid = '01W'
    ORDER BY date
    """,
    date_cols=["datadate"])
##################### Close the Connection #######################
# You should always disconnect using `close()` when you exit your Python environment or complete your data query download step and want to move onto another:

db.close() 

# Or you can use `with` statement which automatically closes the connection when the query is finished:

def get_data_wrds():
    with wrds.Connection(wrds_username="iewaij") as db:
        data = db.raw_sql(
            """
            SELECT datadate as date, prchd as high, prcld as low, prcod as open, prccd as close, cshtrd as volume, ajexdi 
            FROM comp_global_daily.g_sec_dprc
            WHERE datadate >= '2001-01-01'::date
            AND gvkey = '100022'
            AND iid = '01W'
            ORDER BY date
            """,
            date_cols=["datadate"],
        )
    return data<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0869316557048899, 'fraction_numerical': 0.012582213325707749, 'mean_word_length': 3.6953020134228187, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '165928', 'license': 'MIT', 'n_tokens_mistral': 1056, 'n_tokens_neox': 989, 'n_words': 419, 'path': 'notebooks/preparation/1.Data Import.ipynb', 'repo_name': 'Blokas23/TradingStrategies', 'size': '3497'}