def main(): API_key="" quandl.save_key(API_key) #API KEY #print(quandl.ApiConfig.api_key) start_date_str= "2001-12-31" end_date_str= "2018-12-31" start_date= datetime.date.fromisoformat(start_date_str) end_date= datetime.date.fromisoformat(end_date_str) register_matplotlib_converters() data1= [] data1.append(get_data(start_date, end_date, "EOD/GOOGL")) #Google data1.append(get_data(start_date, end_date, "EOD/AAPL")) #Apple data1.append(get_data(start_date, end_date, "EOD/MSFT")) #Microsoft keys1= inter_3keys(data1[0].index.tolist(),data1[1].index.tolist(),data1[2].index.tolist()) av1= average(data1, keys1) data2= [] data2.append(get_data(start_date, end_date, "EOD/TM")) data2.append(get_data(start_date, end_date, "EOD/GM")) data2.append(get_data(start_date, end_date, "EOD/F")) keys2= inter_3keys(data2[0].index.tolist(),data2[1].index.tolist(),data2[2].index.tolist()) av2= average(data2, keys2) inter= {} interkeys= [] with open('INTDSRUSM193N.csv', 'r') as csvFile: reader = csv.reader(csvFile) for row in reader: if row[0]!="DATE": key= pd.Timestamp(row[0]) inter[key]= float(row[1]) interkeys.append(key) keyx1= inter_2keys(interkeys, keys1) keyx2= inter_2keys(interkeys, keys2) interest1= [ (av1["Close"][key] - av1["Open"][keyx1[0]])/av1["Open"][keyx1[0]] for key in keyx1] interest2= [ (av2["Close"][key] - av2["Open"][keyx2[0]])/av2["Open"][keyx2[0]] for key in keyx2] # plt.plot(keys1, interest1, keys2, interest2) # plt.show() # print(keyx) df1= pd.DataFrame({"x": interest1, "y": [ inter[key] for key in keyx1]}) mod1 = smf.ols('y ~ x', data=df1) res1 = mod1.fit() print(res1.summary()) df2= pd.DataFrame({"x": interest2, "y": [ inter[key] for key in keyx2]}) mod2 = smf.ols('y ~ x', data=df2) res2 = mod2.fit() print(res2.summary())
def check_api_keys(): """Check api keys and if they are supplied""" key_dict = {} if cfg.API_KEY_ALPHAVANTAGE == "REPLACE_ME": # pragma: allowlist secret key_dict["ALPHA_VANTAGE"] = "Not defined" else: df = TimeSeries( key=cfg.API_KEY_ALPHAVANTAGE, output_format="pandas" ).get_intraday(symbol="AAPL") if df[0].empty: key_dict["ALPHA_VANTAGE"] = "defined, test failed" else: key_dict["ALPHA_VANTAGE"] = "defined, test passed" if cfg.API_KEY_FINANCIALMODELINGPREP == "REPLACE_ME": # pragma: allowlist secret key_dict["FINANCIAL_MODELING_PREP"] = "Not defined" else: r = requests.get( f"https://financialmodelingprep.com/api/v3/profile/AAPL?apikey={cfg.API_KEY_FINANCIALMODELINGPREP}" ) if r.status_code in [403, 401]: key_dict["FINANCIAL_MODELING_PREP"] = "defined, test failed" elif r.status_code == 200: key_dict["FINANCIAL_MODELING_PREP"] = "defined, test passed" else: key_dict["FINANCIAL_MODELING_PREP"] = "defined, test inconclusive" if cfg.API_KEY_QUANDL == "REPLACE_ME": # pragma: allowlist secret key_dict["QUANDL"] = "Not defined" else: try: quandl.save_key(cfg.API_KEY_QUANDL) quandl.get_table( "ZACKS/FC", paginate=True, ticker=["AAPL", "MSFT"], per_end_date={"gte": "2015-01-01"}, qopts={"columns": ["ticker", "per_end_date"]}, ) key_dict["QUANDL"] = "defined, test passed" except quandl.errors.quandl_error.ForbiddenError: key_dict["QUANDL"] = "defined, test failed" if cfg.API_POLYGON_KEY == "REPLACE_ME": key_dict["POLYGON"] = "Not defined" else: r = requests.get( f"https://api.polygon.io/v2/aggs/ticker/AAPL/range/1/day/2020-06-01/2020-06-17?apiKey={cfg.API_POLYGON_KEY}" ) if r.status_code in [403, 401]: key_dict["POLYGON"] = "defined, test failed" elif r.status_code == 200: key_dict["POLYGON"] = "defined, test passed" else: key_dict["POLYGON"] = "defined, test inconclusive" if cfg.API_FRED_KEY == "REPLACE_ME": key_dict["FRED"] = "Not defined" else: r = requests.get( f"https://api.stlouisfed.org/fred/series?series_id=GNPCA&api_key={cfg.API_FRED_KEY}" ) if r.status_code in [403, 401, 400]: key_dict["FRED"] = "defined, test failed" elif r.status_code == 200: key_dict["FRED"] = "defined, test passed" else: key_dict["FRED"] = "defined, test inconclusive" if cfg.API_NEWS_TOKEN == "REPLACE_ME": key_dict["NEWSAPI"] = "Not defined" else: r = requests.get( f"https://newsapi.org/v2/everything?q=keyword&apiKey={cfg.API_NEWS_TOKEN}" ) if r.status_code in [401, 403]: key_dict["NEWSAPI"] = "defined, test failed" elif r.status_code == 200: key_dict["NEWSAPI"] = "defined, test passed" else: key_dict["NEWSAPI"] = "defined, test inconclusive" if cfg.TRADIER_TOKEN == "REPLACE_ME": key_dict["TRADIER"] = "Not defined" else: r = requests.get( "https://sandbox.tradier.com/v1/markets/quotes", params={"symbols": "AAPL"}, headers={ "Authorization": f"Bearer {cfg.TRADIER_TOKEN}", "Accept": "application/json", }, ) if r.status_code in [401, 403]: key_dict["TRADIER"] = "defined, test failed" elif r.status_code == 200: key_dict["TRADIER"] = "defined, test passed" else: key_dict["TRADIER"] = "defined, test inconclusive" if cfg.API_CMC_KEY == "REPLACE_ME": key_dict["COINMARKETCAP"] = "Not defined" else: cmc = CoinMarketCapAPI(cfg.API_CMC_KEY) try: cmc.exchange_info() key_dict["COINMARKETCAP"] = "defined, test passed" except CoinMarketCapAPIError: key_dict["COINMARKETCAP"] = "defined, test failed" if cfg.API_FINNHUB_KEY == "REPLACE_ME": key_dict["FINNHUB"] = "Not defined" else: r = r = requests.get( f"https://finnhub.io/api/v1/quote?symbol=AAPL&token={cfg.API_FINNHUB_KEY}" ) if r.status_code in [403, 401, 400]: key_dict["FINNHUB"] = "defined, test failed" elif r.status_code == 200: key_dict["FINNHUB"] = "defined, test passed" else: key_dict["FINNHUB"] = "defined, test inconclusive" if cfg.API_IEX_TOKEN == "REPLACE_ME": key_dict["IEXCLOUD"] = "Not defined" else: try: pyEX.Client(api_token=cfg.API_IEX_TOKEN, version="v1") key_dict["IEXCLOUD"] = "defined, test passed" except PyEXception: key_dict["IEXCLOUD"] = "defined, test failed" # Reddit reddit_keys = [ cfg.API_REDDIT_CLIENT_ID, cfg.API_REDDIT_CLIENT_SECRET, cfg.API_REDDIT_USERNAME, cfg.API_REDDIT_PASSWORD, cfg.API_REDDIT_USER_AGENT, ] if "REPLACE_ME" in reddit_keys: key_dict["REDDIT"] = "Not defined" else: praw_api = praw.Reddit( client_id=cfg.API_REDDIT_CLIENT_ID, client_secret=cfg.API_REDDIT_CLIENT_SECRET, username=cfg.API_REDDIT_USERNAME, user_agent=cfg.API_REDDIT_USER_AGENT, password=cfg.API_REDDIT_PASSWORD, ) try: praw_api.user.me() key_dict["REDDIT"] = "defined, test passed" except ResponseException: key_dict["REDDIT"] = "defined, test failed" # Twitter keys twitter_keys = [ cfg.API_TWITTER_KEY, cfg.API_TWITTER_SECRET_KEY, cfg.API_TWITTER_BEARER_TOKEN, ] if "REPLACE_ME" in twitter_keys: key_dict["TWITTER"] = "Not defined" else: params = { "query": "(\\$AAPL) (lang:en)", "max_results": "10", "tweet.fields": "created_at,lang", } r = requests.get( "https://api.twitter.com/2/tweets/search/recent", params=params, # type: ignore headers={"authorization": "Bearer " + cfg.API_TWITTER_BEARER_TOKEN}, ) if r.status_code == 200: key_dict["TWITTER"] = "defined, test passed" elif r.status_code in [401, 403]: key_dict["TWITTER"] = "defined, test failed" else: key_dict["TWITTER"] = "defined, test inconclusive" # Robinhood keys rh_keys = [cfg.RH_USERNAME, cfg.RH_PASSWORD] if "REPLACE_ME" in rh_keys: key_dict["ROBINHOOD"] = "Not defined" else: key_dict["ROBINHOOD"] = "defined, not tested" # Degiro keys dg_keys = [cfg.DG_USERNAME, cfg.DG_PASSWORD, cfg.DG_TOTP_SECRET] if "REPLACE_ME" in dg_keys: key_dict["DEGIRO"] = "Not defined" else: key_dict["DEGIRO"] = "defined, not tested" # OANDA keys oanda_keys = [cfg.OANDA_TOKEN, cfg.OANDA_ACCOUNT] if "REPLACE_ME" in oanda_keys: key_dict["OANDA"] = "Not defined" else: key_dict["OANDA"] = "defined, not tested" # Binance keys bn_keys = [cfg.API_BINANCE_KEY, cfg.API_BINANCE_SECRET] if "REPLACE_ME" in bn_keys: key_dict["BINANCE"] = "Not defined" else: key_dict["BINANCE"] = "defined, not tested" # SentimentInvestor keys si_keys = [cfg.API_SENTIMENTINVESTOR_KEY, cfg.API_SENTIMENTINVESTOR_TOKEN] if "REPLACE_ME" in si_keys: key_dict["SENTIMENT_INVESTOR"] = "Not defined" else: account = requests.get( f"https://api.sentimentinvestor.com/v4/account" f"?token={cfg.API_SENTIMENTINVESTOR_TOKEN}&key={cfg.API_SENTIMENTINVESTOR_KEY}" ) if account.ok and account.json().get("success", False): key_dict["SENTIMENT_INVESTOR"] = "Defined, test passed" else: key_dict["SENTIMENT_INVESTOR"] = "Defined, test unsuccessful" print( tabulate( pd.DataFrame(key_dict.items()), showindex=False, headers=[], tablefmt="fancy_grid", ), "\n", )
import argparse parser = argparse.ArgumentParser(description='Saving Key to ARGS Parser') parser.add_argument('--key', dest='key', help='Enter you key here') args = parser.parse_args() import quandl import os home_path = os.getcwd() quandl.save_key(args.key, filename=home_path+"/key/userkey") print(quandl.ApiConfig.api_key)
import quandl import pickle import time import pandas as pd # quandl.ApiConfig.api_key = 'j2QGEgPyPhe-dGsNNtzE' quandl.ApiConfig.api_version = '2015-04-09' quandl.save_key("j2QGEgPyPhe-dGsNNtzE") print(quandl.ApiConfig.api_key) metadata = pd.read_csv("data/futures/CHRIS_metadata.csv") codes = metadata['code'].tolist() print(len(codes)) counter = 0 for code in codes: if counter % 300 == 0 and counter != 0: time.sleep(60) futures = quandl.get("CHRIS/" + code) futures.to_csv("data/futures/" + code + ".csv") print("finished ", code) counter += 1 # print("CHRIS/" + codes[2]) # futures = quandl.get("CHRIS/" + codes[2]) # futures = quandl.get("CHRIS/" + "CME_EM1") # print(futures) # pickle.dump(futures, open("data/fx_empirical_asset_pricing_via_ml/futures.pkl", "wb"))
## ## ## ## update=1 os.makedirs('Qian',exist_ok=True) if update: quandl.save_key('w8WdHsCDd6zBPE-G2Pba') QuandlGet() QuandlProcess() YahooGet() YahooProcess() DataMerge() # MergeAll([]) # ['cpi','gold','misery','brent','wti','silver','spx','usd','unemployment','inflation']