Exemple #1
0
def download_json_data(companies, mode):
    ts = TimeSeries(key='8T5R3MKIOEJGAL8F', output_format='json', retries=10)
    data, meta_data = {}, {}
    for company in companies:
        if mode == 'daily':
            data[company], meta_data[company] = ts.get_daily_adjusted(
                symbol=company, outputsize='full')
        elif mode == 'monthly':
            data[company], meta_data[company] = ts.get_monthly_adjusted(
                symbol=company)
        elif mode == 'intraday':
            data[company], meta_data[company] = ts.get_intraday(
                symbol=company, interval='1min', outputsize='full')
        elif mode == 'batch':
            data[company], meta_data[company] = ts.get_batch_stock_quotes(
                companies)
            return data
        else:
            print(
                "Only supported modes are 'daily','monthly', 'intraday', 'batch'"
            )
            return data
        print("Finished: " + company)
        time.sleep(12)
    return data
Exemple #2
0
def Time_series(name, time, output, type):
    #ts = TimeSeries(key=API_key, output_format='pandas')
    ts = TimeSeries(key=API_key)
    if (type == 'Intraday'):
        data, meta_data = ts.get_intraday(symbol=name,
                                          interval=time,
                                          outputsize=output)
        #print_time_series(data,name,time,type)
        return data
    elif (type == 'Day Adjusted'):
        data, meta_data = ts.get_daily_adjusted(symbol=name, outputsize=output)
        print_time_series(data, name, time, type)
    elif (type == 'Weekly'):
        data, meta_data = ts.get_weekly(symbol=name)
        print_time_series(data, name, time, type)
    elif (type == 'Weekly Adjusted'):
        data, meta_data = ts.get_weekly_adjusted(symbol=name)
        print_time_series(data, name, time, type)
    elif (type == 'Monthly'):
        data, meta_data = ts.get_monthly(symbol=name)
        #print_time_series(data,name,time,type)
        description(data, name, time, type)
    elif (type == 'Monthly Adjusted'):
        data, meta_data = ts.get_monthly_adjusted(symbol=name)
        print_time_series(data, name, time, type)
Exemple #3
0
    def __init__(self, ticker):
        super().__init__()

        # TODO: don't hardcode this
        self.rF = .001

        # alpha vantage
        key = input('API key: ')
        ts = TimeSeries(key, output_format='pandas')

        # pull spot prices
        tick_data, _ = ts.get_monthly_adjusted(symbol=ticker)
        tick_data = tick_data.sort_index()
        self.S = tick_data.loc[tick_data.index[-3:-1], '5. adjusted close']

        # up/down returns
        rS_up = .1
        rS_dn = -.1

        # TODO: think about other ways to select Su, Sd, K
        self.Su = (1. + rS_up) * self.S[0]
        self.Sd = (1. + rS_dn) * self.S[0]

        # basic info
        self.tick = ticker
        self.date = tick_data.index[-2].isoformat()[:10],
Exemple #4
0
def pull_data(ticker, pullType, interval='0', key='1RJDU8R6RESLVE09'):
    ts = TimeSeries(key=key, output_format='pandas')

    if pullType == 'intraday':
        data1, meta_data1 = ts.get_intraday(symbol=ticker,
                                            interval=interval,
                                            outputsize='full')
        data1.columns = ['Open', 'High', 'Low', 'Close', 'Volume']

        print('Pulled intraday with interval = ' + interval + '\n')
    elif pullType == 'daily':
        data1, meta_data1 = ts.get_daily_adjusted(symbol=ticker,
                                                  outputsize='full')
        data1 = data1.iloc[:, [0, 1, 2, 3, 5, 6, 7]]
        data1.columns = [
            'Open', 'High', 'Low', 'Close', 'Volume', 'Dividend', 'Split Coef'
        ]
        print('Pulled daily\n')
    elif pullType == 'weekly':
        data1, meta_data1 = ts.get_weekly_adjusted(symbol=ticker)
        data1 = data1.iloc[:, [0, 1, 2, 3, 5, 6]]
        data1.columns = ['Open', 'High', 'Low', 'Close', 'Volume', 'Dividend']
        print('Pulled weekly\n')
    elif pullType == 'monthly':
        data1, meta_data1 = ts.get_monthly_adjusted(symbol=ticker)
        data1 = data1.iloc[:, [0, 1, 2, 3, 5, 6]]
        data1.columns = ['Open', 'High', 'Low', 'Close', 'Volume', 'Dividend']
        print('Pulled monthly\n')
    else:
        print('Please enter a valid pull type')

    data1['DT'] = data1.index

    return data1
Exemple #5
0
def get_monthly_closing(ticker):

    # Credentials
    key = "KE59ELWQ1XLDO5ZR"
    ts = TimeSeries(key, output_format="pandas", indexing_type="integer")

    # Query by ticker
    data, meta_data = ts.get_monthly_adjusted(symbol=ticker.upper())

    # Only keep relevant columns
    prices = data[["index", "4. close", "5. adjusted close"]]

    # Rename columns
    prices.rename(
        columns={
            "index": "date",
            "4. close": "monthly_close",
            "5. adjusted close": "monthly_close_adjusted",
        },
        inplace=True,
    )

    # Convert dates from strings to datetime objects
    prices["date"] = prices["date"].apply(lambda x: parse(str(x)))

    # Return ticker and stock data
    return ticker, prices
Exemple #6
0
def processData(ticker):
    ts = TimeSeries('DRLVZJXZ2S4ULOCH', output_format='pandas')
    Monthly_TS_df, meta_data = ts.get_monthly_adjusted(symbol=ticker)
    #Change column names to approprate form,
    for column in Monthly_TS_df.columns:
        Monthly_TS_df.rename({column: column.split('. ')[1]},
                             axis=1,
                             inplace=True)
    return Monthly_TS_df.sort_values('date')
Exemple #7
0
def pull_data(ticker, pullType, interval='0', key='1RJDU8R6RESLVE09'):
    ts = TimeSeries(key=key, output_format='pandas')

    if pullType == 'intraday':
        data1, meta_data1 = ts.get_intraday(symbol=ticker,
                                            interval=interval,
                                            outputsize='full')
        data1.columns = ['Open', 'High', 'Low', 'Close', 'Volume']

        if interval == '1min':
            td = 1
        elif interval == '5min':
            td = 5
        elif interval == '15min':
            td = 15
        elif interval == '30min':
            td = 30
        elif interval == '60min':
            td = 60
        else:
            td = 0

        data1['TimeDelta'] = td * 60 * 1000 * .75

        print('Pulled intraday with interval = ' + interval + '\n')
    elif pullType == 'daily':
        data1, meta_data1 = ts.get_daily_adjusted(symbol=ticker,
                                                  outputsize='full')
        data1 = data1.iloc[:, [0, 1, 2, 3, 5, 6, 7]]
        data1.columns = [
            'Open', 'High', 'Low', 'Close', 'Volume', 'Dividend', 'Split Coef'
        ]
        data1['TimeDelta'] = 24 * 60 * 60 * 1000 * .75
        print('Pulled daily\n')
    elif pullType == 'weekly':
        data1, meta_data1 = ts.get_weekly_adjusted(symbol=ticker)
        data1 = data1.iloc[:, [0, 1, 2, 3, 5, 6]]
        data1.columns = ['Open', 'High', 'Low', 'Close', 'Volume', 'Dividend']
        data1['TimeDelta'] = 7 * 24 * 60 * 60 * 1000 * .75
        print('Pulled weekly\n')
    elif pullType == 'monthly':
        data1, meta_data1 = ts.get_monthly_adjusted(symbol=ticker)
        data1 = data1.iloc[:, [0, 1, 2, 3, 5, 6]]
        data1.columns = ['Open', 'High', 'Low', 'Close', 'Volume', 'Dividend']
        data1['TimeDelta'] = 30 * 24 * 60 * 60 * 1000 * .75
        print('Pulled monthly\n')
    else:
        print('Please enter a valid pull type')

    print('Data collected in dataframe data1\n')
    data1['DT'] = data1.index
    print(type(data1.DT[0]))
    #data1['DT'] = pd.to_datetime(data1.index)
    #data1['DT'] = data1.DT.strftime('%Y/%d/%m/%H/%M')
    return data1
Exemple #8
0
 def pull_tick_monthly(self, tick, adjusted=True):
     self.tracker.wait()
     ts = TimeSeries(key=self.key, output_format='pandas')
     if adjusted:
         df, meta_data = ts.get_monthly_adjusted(tick)
     else:
         df, meta_data = ts.get_monthly(tick)
     df = df.reset_index()
     df['ticker'] = tick
     self.tracker.update(1)
     return df
    def output(self):
        """
        Retrieve stock data
        
        @return TableWrapper of stock data
        """

        stock_symbols = self.ap_paramList[0]()

        timeseries_retriever = TimeSeries(key=DataFetcher.getConfigItem(
            'stocks', 'api_key'),
                                          output_format='pandas',
                                          indexing_type='date')

        data_dict = OrderedDict()
        metadata_dict = OrderedDict()

        for symbol in stock_symbols:

            # Extract data
            if self.data_type == 'daily':
                data, metadata = timeseries_retriever.get_daily(
                    symbol, outputsize='full')
            elif self.data_type == 'daily_adjusted':
                data, metadata = timeseries_retriever.get_daily_adjusted(
                    symbol, outputsize='full')
            elif self.data_type == 'monthly':
                data, metadata = timeseries_retriever.get_monthly(symbol)
            elif self.data_type == 'monthly_adjusted':
                data, metadata = timeseries_retriever.get_monthly_adjusted(
                    symbol)
            elif self.data_type == 'weekly':
                data, metadata = timeseries_retriever.get_weekly(symbol)
            elif self.data_type == 'weekly_adjusted':
                data, metadata = timeseries_retriever.get_weekly_adjusted(
                    symbol)
            elif self.data_type == 'intraday':
                data, metadata = timeseries_retriever.get_weekly_adjusted(
                    symbol, self.interval, outputsize='full')
            # Convert index to pandas datetime
            if self.data_type == 'intraday':
                data.index = pd.to_datetime(data.index).tz_localize(
                    metadata['6. Time Zone'])
            else:
                data.index = pd.to_datetime(data.index)

            data_dict[symbol] = data[self.start_date:self.end_date]
            metadata_dict[symbol] = metadata

        return TableWrapper(data_dict, meta_data=metadata_dict)
Exemple #10
0
def get_monthly(key, symbol):
    ts = TimeSeries(key)
    ts_data, meta = ts.get_monthly_adjusted(symbol)
    dates = []
    print("")

    for i in meta:
        print(i, meta[i])
    print("")

    for date in ts_data:
        dates.append(date)

    for i in dates:
        print("-------------------------------------\n", i, "\n-------------------------------------\n")
        for j in ts_data[i]:
            print("{} : {}".format(j, ts_data[i][j], 3))
        print("")
Exemple #11
0
def get_dividends(key, symbol):
    ts = TimeSeries(key)
    ts_data, meta = ts.get_monthly_adjusted(symbol)
    dividend_dates = []
    print("")

    for i in meta:
        print(i[3::] + ": ", meta[i])
    print("")

    for date in ts_data:
        dividend_dates.append(date)

    for i in dividend_dates:
        print(i, " ",  end="")
        for j in ts_data[i]:
            if j == "7. dividend amount":
                print("{}: {}".format(j[3::].capitalize(), ts_data[i][j], 3))
        print("")
def getPriceHistory():
    sym = request.args.get("sym", default=None, type=str)
    ts = TimeSeries(key='redacted')
    # Get json object with the intraday data and another with  the call's metadata
    try:
        data, meta_data = ts.get_monthly_adjusted(sym)
    except Exception as ex:
        res = current_app.make_response("Could not fetch price data")
        print(traceback.format_exc())
        res.status_code = 404
        return res
    prices = {}
    for k in data.keys():
        #pick closing price for each day
        price = data[k]["4. close"]
        #ignore gaps
        if (float(price) != 0):
            prices[k] = data[k]["4. close"]
    #todo store prices in new db table?
    return json.dumps(prices)
class data_pulling:
    def __init__(self):
        self.api_key = 'MNTZV7K3L1FTVL7C'
        self.ts = TimeSeries(key=self.api_key, output_format='Pandas')
        now = datetime.now()
        yest_day = now + timedelta(days=-1)
        yest_day = yest_day.strftime('%Y-%m-%d')
        self.date1 = yest_day

    def calculation(self):
        for name in [
                'AAPL', 'ABT', 'AMD', 'FB', 'IBM', 'INFY', 'KODK', 'MSFT',
                'SBUX', 'TSLA', 'TTM', 'TWTR'
        ]:
            if name == 'IBM' or name == "SBUX":
                time.sleep(60)
            data, metadata = self.ts.get_monthly_adjusted(symbol=name)
            data.rename(columns={
                '5. adjusted close': 'adjusted_close',
                '1. open': 'open',
                '2. high': 'high',
                '3. low': 'low',
                '4. close': 'close',
                '6. volume': 'volume'
            },
                        inplace=True,
                        errors="raise")
            data['date'] = data.index
            data = data[data['date'] >= self.date1]
            data['date'] = pd.to_datetime(data['date'])
            df1 = pd.read_excel(
                f"D:\\python_files\\stock\\datasets\\{name}.xlsx")
            df1.sort_values(by=['date'], ascending=[True], inplace=True)
            df1 = df1.append(data)
            df1.sort_values(by=['date'], ascending=[True], inplace=True)
            df1['date'] = df1['date'].dt.date
            df1.to_excel(f"D:\\python_files\\stock\\datasets\\{name}.xlsx",
                         index=False)
            print(df1)
Exemple #14
0
def get_ticker_df_alpha_vantage(ticker, mode='day'):
    API_KEY = str(keyring.get_password('av', 'u1'))
    # print type(API_KEY)
    ts = TimeSeries(key=API_KEY)
    if mode == 'month':
        data, meta_data = ts.get_monthly_adjusted(ticker)
    elif mode == 'day':
        data, meta_data = ts.get_daily_adjusted(ticker)
    elif mode == 'intraday':
        data, meta_data = ts.get_intraday(ticker)
    df = pd.DataFrame(data)
    # pdb.set_trace()
    df['name'] = df.index
    df['name'] = df['name'].apply(lambda x: x.split('.')[1].strip())
    df.index = df['name']
    df.pop('name')
    df = df.stack()
    df = pd.to_numeric(df)
    df = df.unstack(0)

    # print ndf.cov()
    # print ndf.describe()
    # print ndf
    return df
Exemple #15
0
class DataCenter:
    def __init__(self, symbol):
        self.symbol = symbol
        self.iex_base_url = "https://api.iextrading.com/1.0/"
        self.ts = TimeSeries(key=API_KEY, retries=100, output_format='pandas')

    def get_intraday(self, interval='15min', outputsize='compact'):
        df, meta_data = self.ts.get_intraday(symbol=self.symbol,
                                             interval=interval,
                                             outputsize=outputsize)
        df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
        df['Date'] = pd.to_datetime(df.index.values)
        return df

    def get_daily(self, outputsize='compact'):
        df, meta_data = self.ts.get_daily_adjusted(symbol=self.symbol,
                                                   outputsize=outputsize)
        return alter_data(df)

    def get_weekly(self):
        df, meta_data = self.ts.get_weekly_adjusted(symbol=self.symbol)
        return alter_data(df)

    def get_monthly(self):
        df, meta_data = self.ts.get_monthly_adjusted(symbol=self.symbol)
        return alter_data(df)

    def get_company(self):
        try:
            r = requests.get("{}stock/{}/company".format(
                self.iex_base_url, self.symbol))
            data = r.json()
            data = pd.DataFrame.from_dict(data, orient='columns').head(1)
            return data
        except Exception as err:
            return None
Exemple #16
0
from alpha_vantage.timeseries import TimeSeries
key = 'M4JP31006H3PKZ8T'
outputsize = 'compact'
symbol = input('Ticker : ')
typ = input('Data type- "daily", "weekly", "monthly", "interval" : ')
ts = TimeSeries(key, output_format='pandas')
if typ == 'daily':
    state = ts.get_daily_adjusted(symbol, outputsize=outputsize)[0]
elif typ == 'weekly':
    state = ts.get_weekly_adjusted(symbol)[0]
elif typ == 'monthly':
    state = ts.get_monthly_adjusted(symbol)[0]
elif typ == 'interval':
    interval = input('Interval-1min, 5min, 15min, 30min, 60min : ')
    state = ts.get_intraday(symbol, interval=interval,
                            outputsize=outputsize)[0]
else:
    print('Wrong input given by the user')
print(state)
Exemple #17
0
    def __init__(self, key, tickers, rF=0., h=5, to_csv=True):
        """
        Initializes Portfolio object

        Parameters
        ----------

        x : array like object
            Weights
        tickers : list
            A list of tickers
        dow : boolean
            If True, loads tickers from the DIJA
        rF : float
            Risk-free rate
        hor : int
            Horizon

        Examples
        --------
        """

        # rates
        self.rF = rF

        # load tickers and add SPY
        self.tickers = tickers
        tickers.append('SPY')
        tickers.sort()

        # load data
        ts = TimeSeries(key, output_format="pandas")
        X = None

        # loop over tickers
        for ticker in tickers:

            # load stock data from Alpha Vantage
            tick_dat, _ = ts.get_monthly_adjusted(symbol=ticker)

            # old and new columns
            old_cols = ['5. adjusted close', '7. dividend amount']
            new_cols = [ticker + '_PRC', ticker + '_DIV']

            # select tick data
            tick_dat = tick_dat[old_cols]
            col_dict = dict(zip(old_cols, new_cols))
            tick_dat = tick_dat.rename(columns=col_dict)

            # reformat date
            tick_dat.index = 100 * tick_dat.index.year + tick_dat.index.month

            # meger to X list
            opts = {'how': 'outer', 'left_index': True, 'right_index': True}
            if X is None:
                X = tick_dat.iloc[::-1]
            else:
                X = X.merge(tick_dat, **opts)

        # drop
        X.to_csv('_'.join(tickers) + '.csv')

        # compute returns
        for t in tickers:
            X[t + '_RET'] = (X[t + '_PRC'] +
                             X[t + '_DIV']) / X[t + '_PRC'].shift() - 1.
            X[t + '_DY'] = X[t + '_DIV'] / X[t + '_PRC'].shift()
            X[t + '_CG'] = X[t + '_PRC'] / X[t + '_PRC'].shift() - 1.

        # kill the first row (with the NAs)
        X = X.loc[X.index[1:], ]

        # store data frame
        self.X = X

        # column names
        self.columns = [ticker + '_RET' for ticker in self.tickers]

        # subset
        idx = self.X.index[-h - 2:-2]
        self.X = self.X.loc[idx]

        # drop
        if to_csv:
            self.X.to_csv('_'.join(self.tickers) + '_JORDAN.csv')

        # statistics
        self.m = self.X[self.columns].mean().to_numpy()
        self.S = self.X[self.columns].cov().to_numpy()

        # matrix and its lu decomposition
        self.N = len(self.tickers)
        lu, piv = lu_factor(self.S)
        self.Si1 = lu_solve((lu, piv), np.ones(self.N))
        self.Sim = lu_solve((lu, piv), self.m)
        a11 = np.ones(self.N) @ self.Si1
        a21 = self.m @ self.Si1
        a22 = self.m @ self.Sim
        self.A = np.array([[a11, a21], [a21, a22]])
Exemple #18
0
from alpha_vantage.timeseries import TimeSeries
import pandas as pd

# IEF

Ticker = ['VFINX', 'VINEX', 'VUSTX']

ts = TimeSeries(key='XAU63L4PGEZMXFRE', output_format='pandas')

for i in range(0, len(Ticker), 1):

    ts = TimeSeries(key='XAU63L4PGEZMXFRE', output_format='pandas')
    # data, meta_data = ts.get_daily(symbol=Ticker[i], outputsize='full')
    data, meta_data = ts.get_monthly_adjusted(symbol=Ticker[i])

    res = data.iloc[:,4]
    res = res.to_frame()
    res.columns = [Ticker[i]]

    if i==0:
        out = res
    else:
        out = out.join(res)

#data['close'].plot()
#plt.title('Intraday Times Series for the MSFT stock (1 min)')
#plt.show()

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('/Users/xudongsong/FinQuant/Indices.xlsx', engine='xlsxwriter')
Exemple #19
0
#Pull types can be 'intraday', 'daily', 'weekly', or 'monthly'
symbol = 'SPY'
pullType = 'intraday'
inter = '5min'

#Open a new timeseries object with the alpha vantage key code
ts = TimeSeries(key='1RJDU8R6RESLVE09', output_format='pandas')

#use the get_ function of the timeseries object with the appropriate period
if pullType == 'intraday':
    data1, meta_data1 = ts.get_intraday(symbol=symbol,
                                        interval=inter,
                                        outputsize='full')
elif pullType == 'daily':
    data1, meta_data1 = ts.get_daily_adjusted(symbol=symbol, outputsize='full')
elif pullType == 'weekly':
    data1, meta_data1 = ts.get_weekly_adjusted(symbol=symbol,
                                               outputsize='full')
elif pullType == 'monthly':
    data1, meta_data1 = ts.get_monthly_adjusted(symbol=symbol,
                                                outputsize='full')
else:
    print('Please enter a valid pull type')

data1.columns = ['Open', 'High', 'Low', 'Close', 'Volume']

#Save the dataframe in csv format
data1.to_csv(directory + symbol + '.csv')

#Notify the user when complete
print('Saved csv')
import pandas as pd
import numpy as np
from alpha_vantage.timeseries import TimeSeries
from scipy import stats

api_key = 'URRP3SWEDY8WA3M4'

ts = TimeSeries(key=api_key, output_format='pandas')

print("Enter a ticker symbol: ")
stock = input()

print("Pick a Time frame in months (recommended = 60)")
time_frame = int(input())

gspc_data, gspc_meta_data = ts.get_monthly_adjusted(symbol='^GSPC')
data, meta_data = ts.get_monthly_adjusted(symbol=stock)

stock_df = pd.DataFrame(data['5. adjusted close'][:time_frame])
gspc_df = pd.DataFrame(gspc_data['5. adjusted close'][:time_frame])

# Calculates beta using the slope of a simple linear regression
def calc_beta(dependent_var, independent_var):
	monthly_prices = pd.concat([dependent_var, independent_var], axis=1)
	monthly_prices.columns = ['stock', '^GSPC']
	monthly_returns = monthly_prices.pct_change(1)
	clean_monthly_returns = monthly_returns.dropna(axis=0)

	gspc = clean_monthly_returns['^GSPC']
	stock = clean_monthly_returns['stock']
Exemple #21
0
def timeSeries(API_key, ticker):
  from alpha_vantage.timeseries import TimeSeries
  import matplotlib.pyplot as plt
  import mplfinance as mpf
  ts=TimeSeries(key=API_key, output_format='pandas')
  option=input('1. Intraday\n2. Daily\n3. Weekly\n4. Monthly\n5. Quote Endpoint\n6. Search Endpoint\n').lower()

  if option=='intraday' or option=='1':
    interval=int(input("Enter the interval:\n\t1. 1 minute\n\t2. 5 minutes\n\t3. 15 minutes\n\t4. 30 minutes\n\t5. 60 minutes\n"))
    inter=['','1min','5min','15min','30min','60min']
    data=ts.get_intraday(symbol=ticker, interval=inter[interval])[0]
    data.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
    data.index.name = "Date"
    mpf.plot(data,
             type='candle', 
             title=f"Intraday Time series for the {ticker} stock {inter[interval]}",
             mav=(20), 
             volume=True, 
             tight_layout=True,
             style='yahoo')
   
    return data

  elif option=='daily' or option=='2':
    data=ts.get_daily_adjusted(symbol=ticker)[0]
    data.columns = ['Open', 'High', 'Low', 'Close', 'Adjusted_close','Volume','Dividend Amount','Split Coefficient']
    data.index.name = "Date"
    mpf.plot(data,
             type='candle', 
             title=f'Daily Time series for the {ticker} stock',
             mav=(20), 
             volume=True, 
             tight_layout=True,
             style='yahoo')
    return data

  elif option=='weekly' or option=='3':
    data=ts.get_weekly_adjusted(symbol=ticker)[0]
    data.columns = ['Open', 'High', 'Low', 'Close', 'Adjusted_close','Volume','Dividend Amount']
    data.index.name = "Date"
    mpf.plot(data,
             type='candle', 
             title=f'Weekly Time series for the {ticker} stock',
             mav=(20), 
             volume=True, 
             tight_layout=True,
             style='yahoo')
    return data

  elif option=='monthly' or option=='4':
    data=ts.get_monthly_adjusted(symbol=ticker)[0]
    data.columns = ['Open', 'High', 'Low', 'Close', 'Adjusted_close','Volume','Dividend Amount']
    data.index.name = "Date"
    mpf.plot(data,
             type='candle', 
             title=f'Monthly Time series for the {ticker} stock',
             mav=(20), 
             volume=True, 
             tight_layout=True,
             style='yahoo')
    return data

  elif option=='quote endpoint' or option=='5':
    data=ts.get_quote_endpoint(symbol=ticker)[0]
    return data

  elif option=='search endpoint' or option=='6':
    keywords=input('Enter the Name of the Stock:\n')
    data=ts.get_symbol_search(keywords=keywords)[0]
    return data
    
  else:
    print("CANNOT RECOGNIZE")
Exemple #22
0
class Datasource(AbcDatasource):
    """alphavantage.com datasource provider.

    Load data from website alphavantage.

    Attributes:
        api_key (str): alphavantage API key

    Usage::

        >>> from Mercury_contrib.datasources import AlphaVantage
        >>> datasource = AlphaVantage('api_key')
        >>> dataframe = datasource.get_timeseries(
                from_date=datetime(2019, 12, 1, 9, 00, 00),
                to_date=datetime(2019, 12, 15, 23, 00, 00),
                instrument="MSFT",
                timeframe=Timeframe.M5,
            )
    """
    def __init__(self, api_key: str) -> None:
        """Class initialization.

        Args:
            api_key: alphavantage api key
        """
        self.api_key = api_key
        self.ts = AvTimeseries(key=api_key,
                               output_format="pandas",
                               indexing_type="date")

    @property
    def colsmap(self) -> Dict[str, str]:
        """Columns mapping dictionary.

        Provide a mapping to translate raw time series columns name
        to standardized naming convention.

        Expect standard names like "open", "high", "low", "close", "adj_close"
        and "volume".
        """
        return {
            "1. open": "open",
            "2. high": "high",
            "3. low": "low",
            "4. close": "close",
            "5. volume": "volume",
        }

    def get_timeseries(self, from_date: datetime, to_date: datetime,
                       instrument: str, timeframe: Timeframe) -> Timeseries:
        """Retrieve a given timeseries from the datasource.

        Args:
            from_date: timeseries starting date.
            to_date: timeseries last date.
            instrument: target instrument.
            timeframe: target timeframe.

        Returns:
            An Mercury Timeseries.

        Raises:
            IndexError: The requested time range cannot be satisfied.
        """
        if timeframe is Timeframe.H4:
            raise ValueError("H4 interval not supported")

        interval = int(timeframe.value / 60)
        if interval <= 60:
            data, meta_data = self.ts.get_intraday(symbol=instrument,
                                                   interval=f"{interval}min",
                                                   outputsize="full")
        if timeframe is Timeframe.D1:
            data, meta_data = self.ts.get_daily_adjusted(symbol=instrument,
                                                         outputsize="full")
        if timeframe is Timeframe.W1:
            data, meta_data = self.ts.get_weekly_adjusted(symbol=instrument)
        if timeframe is Timeframe.MN:
            data, meta_data = self.ts.get_monthly_adjusted(symbol=instrument)

        return Timeseries(instrument, timeframe, data)
Exemple #23
0
import numpy as np
import time

sp = pd.DataFrame(pd.read_csv('./constituents_csv.csv'))

key = '9AD6SV02MT4Z7G8W'

ts = TimeSeries(key, output_format='pandas')

# aapl, meta = ts.get_monthly_adjusted(symbol='AAPL')
# print(aapl[aapl.index >= '2015'])
results = []
for ticker in sp['Symbol'][:100]:
    print(ticker)
    try:
        res, meta = ts.get_monthly_adjusted(symbol=ticker)
        res['ticker'] = ticker
        for i in range(2015, 2021):  # up to 2021
            try:
                year = res[res.index.year == i]
                open_ = year[year.index.month == 1]['1. open'].values
                close = year[year.index.month ==
                             12]['5. adjusted close'].values
                yearly_adj_close = close - open_
                yearly_change = yearly_adj_close / open_
                results.append({
                    'year': i,
                    'ticker': ticker,
                    'yearly adjsuted close': yearly_adj_close[0],
                    'yearly percent change': yearly_change[0],
                    'year open': open_[0],
Exemple #24
0
class AlphaVantageClient:
    def __init__(self, api_key=None):
        if api_key is None:
            default_api_key = utils.get_api_key('AlphaVantage')
            if default_api_key is None:
                raise ValueError('No AlphaVantage API Key found.')
            else:
                self.ts = TimeSeries(key=default_api_key,
                                     output_format='pandas')
        else:
            self.ts = TimeSeries(key=api_key, output_format='pandas')

    def get_data(self,
                 symbol,
                 freq='daily',
                 adjusted=True,
                 interval='15min',
                 outputsize='full'):
        """ Return time series in pandas formet.
        Keyword Arguments:
            symbol:  the symbol for the equity we want to get its data
            freq: frequency of data, supported values are 'daily', 'weekly', 'monthly' (default 'daily'). Currently not support for 'intraday'
            adjusted: adjust the OHLC value (default True)
            interval:  time interval between two conscutive values, used when freq is intraday
                supported values are '1min', '5min', '15min', '30min', '60min'
                (default '15min')
            outputsize:  The size of the call, supported values are
                'compact' and 'full; the first returns the last 100 points in the
                data series, and 'full' returns the full-length intraday times
                series, commonly above 1MB (default 'compact')
        """
        key = '{}-{}'.format(freq, adjusted)

        # elif key == 'intraday-True':
        #     data, _ = self.ts.get_intraday(symbol=symbol, interval=interval, outputsize=outputsize)
        # elif key == 'intraday-False':
        #     data, _ = self.ts.get_intraday(symbol=symbol, interval=interval, outputsize=outputsize)

        if key == 'daily-True':
            data, _ = self.ts.get_daily_adjusted(symbol=symbol,
                                                 outputsize=outputsize)
        elif key == 'daily-False':
            data, _ = self.ts.get_daily(symbol=symbol, outputsize=outputsize)
        elif key == 'weekly-True':
            data, _ = self.ts.get_weekly_adjusted(symbol=symbol)
        elif key == 'weekly-False':
            data, _ = self.ts.get_weekly(symbol=symbol)
        elif key == 'monthly-True':
            data, _ = self.ts.get_monthly_adjusted(symbol=symbol)
        elif key == 'monthly-False':
            data, _ = self.ts.get_monthly(symbol=symbol)
        else:
            raise Warning(
                'Freq: {} or Adjusted: {} is not valid. Default to Daily Adjusted.'
            )
            data, _ = self.ts.get_daily_adjusted(symbol=symbol,
                                                 outputsize=outputsize)

        if freq == 'intraday':
            data.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
        else:
            columns_name = [
                'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume',
                'Dividends', 'Stock Splits'
            ]
            data.columns = columns_name[:len(data.columns)]

        data = data.rename(index={'date': 'Date'})

        return data.sort_index(ascending=True)

    def get_latest_data(self, symbol):
        """ Return the latest price and volume information for a security of your choice 
        Keyword Arguments:
            symbol:  the symbol for the equity we want to get its data
        """
        data, _ = self.ts.get_quote_endpoint(symbol=symbol)

        columns_name = [
            'Open', 'High', 'Low', 'Price', 'Volume', 'Date', 'Previous Close',
            'Change', 'Change Pct'
        ]
        data = data.iloc[:, 1:]
        data.columns = columns_name
        data = data.set_index(['Date'])

        return data
Exemple #25
0
class AlphaVantageHandle(DataAPIBase):
    RAPIDAPI_KEY = "de7dba73d8msh6207eff68e3f134p10e7d1jsn87fd4c7d47de"
    ALPHA_VANTAGE_KEY = "EL89QWXFIKG17CEH"
    ALPHA_VANTAGE_PRIMIUM_KEY = "BO5YJYOVZPVLML38"
    API_TIME_LIMIT = 70  # The limit unit for trading in seconds
    API_CALL_LIMIT = 30  # The limit of number of calls per limit unit

    def __init__(self,
                 interval=None,
                 symbol=None,
                 use_rapid=False,
                 skip_data_init=False,
                 outputsize="full"):
        super(AlphaVantageHandle, self).__init__()
        self.log = logging.getLogger("Alpha Vantage API")

        self._outputsize = outputsize
        self.api_time_queue = Queue()
        # Initialize alpha vantage time series
        if use_rapid:
            self.time_series = TimeSeries(key=self.RAPIDAPI_KEY,
                                          output_format="pandas",
                                          rapidapi=True)
        else:
            self.time_series = TimeSeries(key=self.ALPHA_VANTAGE_PRIMIUM_KEY,
                                          output_format="pandas")

        self.tech_indicators = TechIndicators(
            key=self.ALPHA_VANTAGE_PRIMIUM_KEY, output_format='pandas')

        if not skip_data_init:
            # Set the symbol and interval for the handle
            self.update_symbol_interval(symbol=symbol, interval=interval)
            # Fetch time series data
            self.log.info(
                "Downloading time series and technical indicator data from alpha vantage:\n "
                "   symbol: %s\n"
                "   interval: %s\n", self._symbol, self._interval)
            self.update_all_data()
            self.log.info("Data downloaded")

    def update_symbol_interval(self,
                               symbol=None,
                               interval=None,
                               force_update=False):
        """
        Update the symbol and interval for the handle
        """
        if symbol == None or (symbol != "keep" and len(symbol) < 1):
            self.log.error("Invalid symbol: %s", symbol)
            raise Exception("Invalid symbol!")
        if self._symbol is not None:
            force_update = True
        if symbol != "keep":
            self._symbol = symbol

        if interval not in INTERVAL_TYPE and interval != "keep":
            self.log.error("Interval type %s is invalid. You must provide a type:\n  " + \
                           "".join(iv + "\n  " for iv in INTERVAL_TYPE), interval)
            raise Exception("Invalid interval type!")

        if self._interval is not None:
            force_update = True
        if interval != "keep":
            self._interval = interval

        if force_update:
            self.update_all_data()

    def update_all_data(self):
        self.log.info("Updating time series and tech indicators data")
        self.fetch_ts_data()
        self.fetch_ti_data()

    def _limited_api_wrapper(self, func):
        time_queue = self.api_time_queue
        time_limit = self.API_TIME_LIMIT
        call_limit = self.API_CALL_LIMIT

        def limited_api(*args, **kwargs):
            if time_queue.empty():
                time_queue.put(time.time())
            else:
                cur_time = time.time()
                while not time_queue.empty():
                    if time_queue.queue[0] < cur_time - time_limit:
                        time_queue.get()
                    else:
                        break
                if time_queue.qsize() >= call_limit:
                    time_delta = time.time() - time_queue.queue[0]
                    time.sleep(time_delta)
                    time_queue.get()
            time_queue.put(time.time())
            return func(*args, **kwargs)

        return limited_api

    def fetch_ts_data(self):
        """
        Fetch time series data from alpha vantage based on the current data type
        """
        if self._interval not in INTERVAL_TYPE:
            self.log.error("Interval type %s is not supported!",
                           self._interval)
            raise Exception("Interval type not supported!")

        if self._interval in INTRA_DAY_TYPE:
            self.ts_data, metadata = self.time_series.get_intraday(
                symbol=self._symbol,
                interval=self._interval,
                outputsize=self._outputsize)
        elif self._interval == "daily":
            self.ts_data, metadata = self.time_series.get_daily_adjusted(
                symbol=self._symbol, outputsize=self._outputsize)
        elif self._interval == "weekly":
            self.ts_data, metadata = self.time_series.get_weekly_adjusted(
                symbol=self._symbol, outputsize=self._outputsize)
        elif self._interval == "monthly":
            self.ts_data, metadata = self.time_series.get_monthly_adjusted(
                symbol=self._symbol, outputsize=self._outputsize)
        else:
            self.log.error("Interval type %s not implemented!", self._interval)
            raise NotImplementedError

        self.log.info("Fetched data with:\n"
                      "  information: %(1. Information)s\n"
                      "  symbol: %(2. Symbol)s\n"
                      "  last refreshed: %(3. Last Refreshed)s" % metadata)

    def fetch_ti_data(self):
        """
        Fetch time indicator data from alpha vantage based on the current data type
        """
        if self._interval not in INTERVAL_TYPE:
            self.log.error("Interval type %s is not supported!",
                           self._interval)
            raise Exception("Interval type not supported!")
        for ti_type in TI_TYPE:
            self.log.info("Fetching ti data for %s", ti_type)
            ti_data = self._fetch_ti_data_helper(ti_type)
            if not ti_data.empty:
                self.ti_data = pd.concat([self.ti_data, ti_data], axis=1)

    def _fetch_ti_data_helper(self, ti_type):
        """
        Helper function to get data for a specific technical indicator
        We are currently getting high and low for each interval
        """
        ti_data = pd.DataFrame()
        if ti_type == "SMA":
            get_sma = self._limited_api_wrapper(self.tech_indicators.get_sma)
            data_high, metadata = get_sma(symbol=self._symbol,
                                          interval=self._interval,
                                          series_type="high")
            data_low, metadata = get_sma(symbol=self._symbol,
                                         interval=self._interval,
                                         series_type="low")
            ti_data = pd.concat([data_high, data_low], axis=1)
            ti_data.columns = ["SMA_H", "SMA_L"]
        elif ti_type == "EMA":
            get_ema = self._limited_api_wrapper(self.tech_indicators.get_ema)
            data_high, metadata = get_ema(symbol=self._symbol,
                                          interval=self._interval,
                                          series_type="high")
            data_low, metadata = get_ema(symbol=self._symbol,
                                         interval=self._interval,
                                         series_type="low")
            ti_data = pd.concat([data_high, data_low], axis=1)
            ti_data.columns = ["EMA_H", "EMA_L"]
        elif ti_type == "VWAP":
            if self._interval not in INTRA_DAY_TYPE:
                self.log.error(
                    "VWAP only support intra day data. No data gathered")
            else:
                get_vwap = self._limited_api_wrapper(
                    self.tech_indicators.get_vwap)
                data, metadata = get_vwap(symbol=self._symbol,
                                          interval=self._interval)
                ti_data = data.copy()
        elif ti_type == "MACD":
            get_macd = self._limited_api_wrapper(self.tech_indicators.get_macd)
            data_high, metadata = get_macd(symbol=self._symbol,
                                           interval=self._interval,
                                           series_type="high")
            data_low, metadata = get_macd(symbol=self._symbol,
                                          interval=self._interval,
                                          series_type="low")

            ti_data = pd.concat([
                data_high[["MACD_Signal", "MACD"]],
                data_low[["MACD_Signal", "MACD"]]
            ],
                                axis=1)
            ti_data.columns = [
                "MACD_Signal_H", "MACD_H", "MACD_Signal_L", "MACD_L"
            ]
        elif ti_type == "STOCH":
            get_stoch = self._limited_api_wrapper(
                self.tech_indicators.get_stoch)
            data, metadata = get_stoch(symbol=self._symbol,
                                       interval=self._interval)
            ti_data = data.copy()
        elif ti_type == "RSI":
            get_rsi = self._limited_api_wrapper(self.tech_indicators.get_rsi)
            data_high, metadata = get_rsi(symbol=self._symbol,
                                          interval=self._interval,
                                          series_type="high")
            data_low, metadata = get_rsi(symbol=self._symbol,
                                         interval=self._interval,
                                         series_type="low")
            ti_data = pd.concat([data_high, data_low], axis=1)
            ti_data.columns = ["RSI_H", "RSI_L"]
        elif ti_type == "ADX":
            get_adx = self._limited_api_wrapper(self.tech_indicators.get_adx)
            data, metadata = get_adx(symbol=self._symbol,
                                     interval=self._interval)
            ti_data = data.copy()
        elif ti_type == "CCI":
            get_cci = self._limited_api_wrapper(self.tech_indicators.get_cci)
            data, metadata = get_cci(symbol=self._symbol,
                                     interval=self._interval)
            ti_data = data.copy()
        elif ti_type == "AROON":
            get_aroon = self._limited_api_wrapper(
                self.tech_indicators.get_aroon)
            data_high, metadata = get_aroon(symbol=self._symbol,
                                            interval=self._interval,
                                            series_type="high")
            data_low, metadata = get_aroon(symbol=self._symbol,
                                           interval=self._interval,
                                           series_type="low")
            ti_data = pd.concat([data_high, data_low], axis=1)
            ti_data.columns = [
                "Aroon_Down_H", "Aroon_Up_H", "Aroon_Down_L", "Aroon_Up_L"
            ]
        elif ti_type == "BBANDS":
            get_bbands = self._limited_api_wrapper(
                self.tech_indicators.get_bbands)
            # We want both SMA and EMA features
            data_high_sma, metadata = get_bbands(symbol=self._symbol,
                                                 interval=self._interval,
                                                 series_type="high")
            data_low_sma, metadata = get_bbands(symbol=self._symbol,
                                                interval=self._interval,
                                                series_type="low")

            data_high_ema, metadata = get_bbands(symbol=self._symbol,
                                                 interval=self._interval,
                                                 series_type="high",
                                                 matype=1)
            data_low_ema, metadata = get_bbands(symbol=self._symbol,
                                                interval=self._interval,
                                                series_type="low",
                                                matype=1)
            ti_data = pd.concat(
                [data_high_sma, data_high_ema, data_low_sma, data_low_ema],
                axis=1)
            ti_data.columns = [
                "BBANDS_Upper_SMA_H", "BBANDS_Lower_SMA_H",
                "BBANDS_Middle_SMA_H", "BBANDS_Upper_EMA_H",
                "BBANDS_Lower_EMA_H", "BBANDS_Middle_EMA_H",
                "BBANDS_Upper_SMA_L", "BBANDS_Lower_SMA_L",
                "BBANDS_Middle_SMA_L", "BBANDS_Upper_EMA_L",
                "BBANDS_Lower_EMA_L", "BBANDS_Middle_EMA_L"
            ]
        elif ti_type == "AD":
            get_ad = self._limited_api_wrapper(self.tech_indicators.get_ad)
            data, metadata = get_ad(symbol=self._symbol,
                                    interval=self._interval)
            ti_data = data.copy()
        elif ti_type == "OBV":
            get_obv = self._limited_api_wrapper(self.tech_indicators.get_obv)
            data, metadata = get_obv(symbol=self._symbol,
                                     interval=self._interval)
            ti_data = data.copy()
        else:
            self.log.error("Technical indicator type %s not implemented!",
                           ti_type)
            raise NotImplementedError

        return ti_data
Exemple #26
0
class DataFetcher:
    """
        Provides abstractions for:
            1. fetch data from AlphaVantage API
            2. saves data to ../data/api_data

        Momentarily only compatible with the AlphaVantage API.
    """
    def __init__(self, api_key):
        self._api_key = api_key
        self.ts = TimeSeries(key=self._api_key, output_format="pandas")

    def fetch_data(self, tickers, time_window):

        # format tickers as list
        tickers = [tickers] if not isinstance(tickers, list) else tickers

        try:
            if len(tickers) <= 5:

                for ticker in tickers:
                    ticker = ticker.lower()

                    if time_window == "daily":
                        data, meta_data = self.ts.get_daily_adjusted(
                            symbol=ticker, outputsize="full")
                    elif time_window == "weekly":
                        data, meta_data = self.ts.get_weekly_adjusted(
                            symbol=ticker, outputsize="full")
                    elif time_window == "monthly":
                        data, meta_data = self.ts.get_monthly_adjusted(
                            symbol=ticker, outputsize="full")
                    else:
                        # time_window parameter not supported, raise exception
                        raise TypeError(
                            f"The following time_window: '{time_window}' is not supported momentarily"
                        )

                    # format and convert index to datetime
                    data.index = pd.to_datetime(
                        data.index.strftime("%Y-%m-%d"))
                    data = data.sort_index(ascending=True)

                    # remove numbers from column names
                    data = data.rename(
                        columns={
                            col: re.sub(r"([0-9]\. )", "", col)
                            for col in data.columns
                        })
                    data = data.rename(
                        columns={col: col.lower()
                                 for col in data.columns})

                    # create api_data directory
                    if not path.exists(path.join("..", "data", "api_data")):
                        makedirs(path.join("..", "data", "api_data"))

                    # save data to ./data/api_data
                    save_path = path.join(path.dirname(path.abspath(__file__)),
                                          "..", "data", "api_data",
                                          f"{ticker}_{time_window}.csv")
                    data.to_csv(save_path)
                return True
            else:
                # no more than 5 API calls (tickers) per minute, raise exception
                raise ValueError(
                    "No more than 5 API calls/minute supported! Please provide a ticker list <= 5"
                )
        except (ValueError, TypeError) as e:
            traceback.print_exc()
            print(e)
            sys.exit(0)
Exemple #27
0
class REST(object):

    def __init__(self, api_key):
        self._api_key = get_alpha_vantage_credentials(api_key)
        self._session = requests.Session()
        self._timeseries = TimeSeries(key=self._api_key)
        self._cryptocurrencies = CryptoCurrencies(key=self._api_key)
        self._foreignexchange = ForeignExchange(key=self._api_key)
        self._sectorperformance = SectorPerformances(key=self._api_key)
        self._techindicators = TechIndicators(key=self._api_key)

    def _request(self, method, params=None):
        url = 'https://www.alphavantage.co/query?'
        params = params or {}
        params['apikey'] = self._api_key
        resp = self._session.request(method, url, params=params)
        resp.raise_for_status()
        return resp.json()

    def get(self, params=None):
        ''' Customizable endpoint, where you can pass all 
        keywords/paramters from the documentation:
        https://www.alphavantage.co/documentation/#

        Returns:
            pandas, csv, or json
        '''
        return self._request('GET', params=params)

    def historic_quotes(self, symbol, adjusted=False, outputsize='full', cadence='daily', output_format=None):
        ''' Returns the one of the TIME_SERIES_* endpoints of the Alpha Vantage API.

        Params:
            symbol: The ticker to return
            adjusted: Return the adjusted prices
            cadence: Choose between ['daily', 'weekly', 'monthly'], to return the cadence
            output_format: Choose between['json', 'csv', 'pandas']

        Returns:
            pandas, csv, or json
        '''
        if output_format:
            self._timeseries.output_format = output_format
        if cadence == 'daily':
            data, _ = self._timeseries.get_daily_adjusted(
                symbol=symbol, outputsize=outputsize) if adjusted else self._timeseries.get_daily(symbol=symbol, outputsize=outputsize)
        if cadence == 'weekly':
            data, _ = self._timeseries.get_weekly_adjusted(
                symbol=symbol) if adjusted else self._timeseries.get_weekly(symbol=symbol)
        if cadence == 'monthly':
            data, _ = self._timeseries.get_monthly_adjusted(
                symbol=symbol) if adjusted else self._timeseries.get_monthly(symbol=symbol)
        return data

    def intraday_quotes(self, symbol, interval='5min', outputsize='full', output_format=None):
        ''' Returns the TIME_SERIES_INTRADAY endpoint of the Alpha Vantage API.

        Params:
            symbol: The ticker to return
            interval: Choose between['1min', '5min', '15min', '30min', '60min']
            output_format: Choose between['json', 'csv', 'pandas']

        Returns:
            pandas, csv, or json
        '''
        if output_format:
            self._timeseries.output_format = output_format
        data, _ = self._timeseries.get_intraday(
            symbol=symbol, interval=interval, outputsize=outputsize)
        return data

    def current_quote(self, symbol):
        ''' Returns the GLOBAL_QUOTE endpoint
        of the Alpha Vantage API.

        Params:
            symbol: The ticker to return
            output_format: Choose between['json', 'csv', 'pandas']

        Returns:
            pandas, csv, or json
        '''
        data, _ = self._timeseries.get_quote_endpoint(symbol=symbol)
        return data

    def last_quote(self, symbol):
        return self.current_quote(symbol)

    def company(self, symbol, datatype='json'):
        return self.search_endpoint(symbol, datatype=datatype)

    def search_endpoint(self, keywords, datatype='json'):
        '''Search endpoint returns a list of possible companies
        that correspond to keywords

        Params:
            datatype: csv, json, or pandas
            keywords: ex. keywords=microsoft

        Returns:
            pandas, csv, or json
        '''
        params = {'function': 'SYMBOL_SEARCH',
                  'keywords': keywords, 'datatype': datatype}
        return self.get(params)

    def historic_fx_quotes(self, from_symbol, to_symbol, outputsize='full', cadence='daily', output_format=None):
        ''' Returns the one of the FX_* endpoints of the Alpha Vantage API.

        Params:
            from_currency: The symbol to convert
            to_currency: The symbol to convert to
            cadence: Choose between ['daily', 'weekly', 'monthly'], to return the cadence
            output_format: Choose between['json', 'csv', 'pandas']

        Returns:
            pandas, csv, or json
        '''
        if output_format:
            self._foreignexchange.output_format = output_format
        if cadence == 'daily':
            data, _ = self._foreignexchange.get_currency_exchange_daily(
                from_symbol=from_symbol, to_symbol=to_symbol, outputsize=outputsize)
        if cadence == 'weekly':
            data, _ = self._foreignexchange.get_currency_exchange_weekly(
                from_symbol=from_symbol, to_symbol=to_symbol, outputsize=outputsize)
        if cadence == 'monthly':
            data, _ = self._foreignexchange.get_currency_exchange_monthly(
                from_symbol=from_symbol, to_symbol=to_symbol, utputsize=outputsize)
        return data

    def intraday_fx_quotes(self, from_symbol, to_symbol, interval='5min', outputsize='full', output_format=None):
        ''' Returns the FX_INTRADAY endpoint of the Alpha Vantage API.

        Params:
            from_currency: The symbol to convert
            to_currency: The symbol to convert to
            interval: Choose between['1min', '5min', '15min', '30min', '60min']
            output_format: Choose between['json', 'csv', 'pandas']

        Returns:
            pandas, csv, or json
        '''
        if output_format:
            self._foreignexchange.output_format = output_format
        data, _ = self._foreignexchange.get_currency_exchange_intraday(
            from_symbol=from_symbol, to_symbol=to_symbol, interval=interval, outputsize=outputsize)
        return data

    def exchange_rate(self, from_currency, to_currency):
        ''' Returns the exchange rate of two currencies, digital or physical.
        CURRENCY_EXCHANGE_RATE endpoint of the Alpha Vantage API

        Params:
            from_currency: The symbol to convert
            to_currency: The symbol to convert to

        Returns:
            json
        '''
        params = {'function': "CURRENCY_EXCHANGE_RATE",
                  'from_currency': from_currency, 'to_currency': to_currency}
        data = self.get(params)
        return data

    def historic_cryptocurrency_quotes(self, symbol, market, cadence='daily', output_format=None):
        ''' Returns the one of the DIGITAL_CURRENCY_* endpoints of the Alpha Vantage API.

        Params:
            symbol: The cryptocurrency to return
            market: The market it's being sold on
            cadence: Choose between ['daily', 'weekly', 'monthly'], to return the cadence
            output_format: Choose between['json', 'csv', 'pandas']

        Returns:
            pandas, csv, or json
        '''
        if output_format:
            self._cryptocurrencies.output_format = output_format
        if cadence == 'daily':
            data, _ = self._cryptocurrencies.get_digital_currency_daily(
                symbol=symbol, market=market)
        if cadence == 'weekly':
            data, _ = self._cryptocurrencies.get_digital_currency_weekly(
                symbol=symbol, market=market)
        if cadence == 'monthly':
            data, _ = self._cryptocurrencies.get_digital_currency_monthly(
                symbol=symbol, market=market)
        return data

    def techindicators(self, techindicator='SMA', output_format='json', **kwargs):
        ''' Returns the one of the technical indicator endpoints of the Alpha Vantage API.

        Params:
            techindicator: The technical indicator of choice
            params: Each technical indicator has additional optional parameters

        Returns:
            pandas, csv, or json
        '''
        if output_format:
            self._techindicators.output_format = output_format
        params = {'function': techindicator}
        for key, value in kwargs.items():
            params[key] = value
        data = self.get(params)
        return data

    def sector(self):
        ''' Returns the sector performances

        Returns:
            pandas, csv, or json
        '''
        data, _ = self._sectorperformance.get_sector()
        return data
Exemple #28
0
    def get_raw_prices(self):
        if (self.fetch_data == 'pdr'):
            self.attrs = [
                'Adj Close', 'Close', 'High', 'Open', 'Low', 'Volume'
            ]
            self.symbs = self.tickers_names.index
            self.midx = pd.MultiIndex.from_product([self.attrs, self.symbs],
                                                   names=('Attributes',
                                                          'Symbols'))

            if (self.new_db_raw_prices):
                dt_raw_prices = pdr.DataReader(self.tickers_names, 'yahoo',
                                               self.start_date, self.end_date)
                dt_raw_prices['Dates'] = pd.to_datetime(dt_raw_prices.index,
                                                        format='%Y-%m-%d')

                engine = create_engine("sqlite:///" + self.output_directory +
                                       "/dt_raw_prices_pdr.db",
                                       echo=False)
                dt_raw_prices.to_sql('dt_raw_prices',
                                     engine,
                                     if_exists='replace',
                                     index=False)
            else:
                engine = create_engine("sqlite:///" + self.output_directory +
                                       "/dt_raw_prices_pdr.db",
                                       echo=False)

            self.dt_raw_prices = pd.read_sql_table(
                'dt_raw_prices',
                con=engine,
                parse_dates={'Dates': {
                    'format': '%Y-%m-%d'
                }})

            self.dt_raw_prices.rename(columns={"('Dates', '')": 'Dates'},
                                      inplace=True)
            self.dt_raw_prices.set_index('Dates', inplace=True)
            self.dt_raw_prices.columns = self.midx
            self.dt_select = self.dt_raw_prices[[self.ohlc, 'Volume']]

            # print(self.dt_raw_prices)
            # print(self.dt_select)

        elif (self.fetch_data == 'alphav'):
            self.symbs = [
                'NVDA', 'AMZN', 'TSLA', 'MRNA', 'AAPL'
            ]  # Helper.nasdaq100_tickers(self.url_nasdaq).index.to_list()

            if (self.freq == 'monthly'):
                self.attrs = [
                    'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume',
                    'Dividend'
                ]
            elif (self.freq == 'daily'):
                self.attrs = [
                    'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume',
                    'Dividend', 'Split coeff'
                ]
            elif (self.freq == 'intraday'):
                self.attrs = ['Open', 'High', 'Low', 'Close', 'Volume']

            self.midx = pd.MultiIndex.from_product([self.symbs, self.attrs],
                                                   names=('Symbols',
                                                          'Attributes'))

            if (self.new_db_alphav_prices):
                api_key = os.getenv('ALPHAVANTAGE_API_KEY')
                ts = TimeSeries(key=api_key, output_format='pandas')

                data_all = []
                meta_data_all = []

                if (self.freq == 'monthly'):
                    for i, el in enumerate(self.symbs):
                        data, meta_data = ts.get_monthly_adjusted(symbol=el)
                        data.columns = self.attrs
                        data_all.append(data)
                        meta_data_all.append(meta_data)
                elif (self.freq == 'daily'):
                    for i, el in enumerate(self.symbs):
                        data, meta_data = ts.get_daily_adjusted(symbol=el)
                        data.columns = self.attrs
                        data_all.append(data)
                        meta_data_all.append(meta_data)
                elif (self.freq == 'intraday'):
                    for i, el in enumerate(self.symbs):
                        data, meta_data = ts.get_intraday(symbol=el,
                                                          interval='60min',
                                                          outputsize='compact')
                        data.columns = self.attrs
                        data_all.append(data)
                        meta_data_all.append(meta_data)

                self.dt_raw_prices = pd.concat(data_all, axis=1)
                self.dt_raw_prices.columns = self.midx
                self.dt_raw_prices = self.dt_raw_prices[::-1]
                self.dt_raw_prices['Dates'] = pd.to_datetime(
                    self.dt_raw_prices.index, format='%Y-%m-%d')

                engine = create_engine("sqlite:///" + self.output_directory +
                                       "/dt_raw_prices_alv.db",
                                       echo=False)
                self.dt_raw_prices.to_sql('dt_raw_prices',
                                          engine,
                                          if_exists='replace',
                                          index=False)
            else:
                engine = create_engine("sqlite:///" + self.output_directory +
                                       "/dt_raw_prices_alv.db",
                                       echo=False)

            self.dt_raw_prices = pd.read_sql_table(
                'dt_raw_prices',
                con=engine,
                parse_dates={'Dates': {
                    'format': '%Y-%m-%d'
                }})

            self.dt_raw_prices.rename(columns={"('Dates', '')": 'Dates'},
                                      inplace=True)
            self.dt_raw_prices.set_index('Dates', inplace=True)
            self.dt_raw_prices.columns = self.midx
            self.dt_raw_prices.index = pd.to_datetime(self.dt_raw_prices.index)
            self.dt_raw_prices = self.dt_raw_prices.swaplevel(0, 1,
                                                              1).sort_index(1)

            # select the tickers based on ohlc parameters
            if (self.freq == 'monthly' or self.freq == 'daily'):
                self.dt_select = self.dt_raw_prices.loc[:,
                                                        self.dt_raw_prices.
                                                        columns.
                                                        get_level_values(0).
                                                        isin([self.ohlc] +
                                                             ['Volume'])]
            elif (self.freq == 'intraday'):
                self.dt_select = self.dt_raw_prices.loc[:,
                                                        self.dt_raw_prices.
                                                        columns.
                                                        get_level_values(0).
                                                        isin([
                                                            'Close', 'Volume'
                                                        ])]
Exemple #29
0
    def show_frontier_simple(self):
        symbs = [self.rr_symb1,self.rr_symb2]

        if(self.freq == 'monthly'):
            attrs = ['Open','High','Low','Close','Adj Close','Volume','Dividend']
        elif(self.freq == 'daily'):
            attrs = ['Open','High','Low','Close','Adj Close','Volume','Dividend','Split coeff']
            
        midx = pd.MultiIndex.from_product([symbs,attrs],names=('Symbols','Attributes'))

        if(self.new_db_alphav_prices):
            api_key = os.getenv('ALPHAVANTAGE_API_KEY')
            ts = TimeSeries(key=api_key, output_format='pandas')

            if(self.freq == 'monthly'):
                data1, meta_data1 = ts.get_monthly_adjusted(symbol=self.rr_symb1)
                data2, meta_data2 = ts.get_monthly_adjusted(symbol=self.rr_symb2)
            elif(self.freq == 'daily'):
                data1, meta_data1 = ts.get_daily_adjusted(symbol=self.rr_symb1)
                data2, meta_data2 = ts.get_daily_adjusted(symbol=self.rr_symb2)
                
            data1.columns = attrs
            data2.columns = attrs

            dt_alphav_prices = pd.concat([data1,data2],axis=1)
            dt_alphav_prices.columns = midx
            dt_alphav_prices = dt_alphav_prices[::-1]
            dt_alphav_prices['Dates'] = pd.to_datetime(dt_alphav_prices.index,format='%Y-%m-%d')
            
            engine = create_engine("sqlite:///" + self.output_directory + "/dt_alphav_prices.db", echo=False)
            dt_alphav_prices.to_sql(
                'dt_alphav_prices',
                engine,
                if_exists='replace',
                index=False
                )
        else:
            engine = create_engine("sqlite:///" + self.output_directory + "/dt_alphav_prices.db", echo=False)

        self.dt_alphav_prices = pd.read_sql_table(
            'dt_alphav_prices',
            con=engine,
            parse_dates={'Dates': {'format': '%Y-%m-%d'}}
            )

        self.dt_alphav_prices.rename(columns={"('Dates', '')":'Dates'},inplace=True)
        self.dt_alphav_prices.set_index('Dates',inplace=True)
        self.dt_alphav_prices.columns = midx

        returns1 = self.dt_alphav_prices[(self.rr_symb1,'Adj Close')].pct_change().fillna(0)
        returns2 = self.dt_alphav_prices[(self.rr_symb2,'Adj Close')].pct_change().fillna(0)

        if(len(returns1) > len(returns2)):
            returns1 = returns1[-len(returns2):]

        if(len(returns2) > len(returns1)):
            returns2 = returns2[-len(returns1):]

        mean_returns1 = np.mean(returns1)
        variance1 = np.var(returns1)
        standard_deviation1 = np.sqrt(variance1)

        mean_returns2 = np.mean(returns2)
        variance2 = np.var(returns2)
        standard_deviation2 = np.sqrt(variance2)

        correlation = np.corrcoef(returns1, returns2)[0][1]
        print(f'Correlation = {correlation}')

        weights = []
        for n in range(0, 101):
            weights.append((1 - 0.01 * n, 0 + 0.01 * n))
            
        returns = []
        standard_deviations = []

        portfolio_50_50_standard_deviation = None
        portfolio_50_50_returns = None

        plt.figure(figsize=(32,20))
        for w1, w2 in weights:
            returns.append(w1 * mean_returns1 + w2 * mean_returns2)

            variance = w1**2 * standard_deviation1**2 + w2**2 * standard_deviation2**2 + \
                2 * w1 * w2 * standard_deviation1 * standard_deviation2 * correlation

            standard_deviation = np.sqrt(variance)
            standard_deviations.append(standard_deviation)

            plt.scatter(standard_deviations[-1], returns[-1], color='#007bff')

            if w1 == 0.5 and w2 == 0.5:
                portfolio_50_50_standard_deviation = standard_deviations[-1]
                portfolio_50_50_returns = returns[-1]

        plt.scatter(portfolio_50_50_standard_deviation,
                    portfolio_50_50_returns, marker='x', color='red', alpha=1, s=320)

        x_padding = np.average(standard_deviations) / 25
        plt.xlim(min(standard_deviations) - x_padding,
                 max(standard_deviations) + x_padding)

        y_padding = np.average(returns) / 25
        plt.ylim(min(returns) - y_padding, max(returns) + y_padding)

        plt.gca().set_xticks(plt.gca().get_xticks().tolist()) # remove in the future - placed to avoid warning - it is a bug from matplotlib 3.3.1
        plt.gca().set_xticklabels(['{:.2f}%'.format(x*100) for x in plt.gca().get_xticks()])
        plt.gca().set_yticks(plt.gca().get_yticks().tolist()) # remove in the future - placed to avoid warning - it is a bug from matplotlib 3.3.1
        plt.gca().set_yticklabels(['{:.2f}%'.format(y*100) for y in plt.gca().get_yticks()])

        plt.title(f'Efficient Frontier ({self.rr_symb1.upper()} and {self.rr_symb2.upper()})')

        plt.xlabel('Risk (' + self.freq.capitalize() + ')')
        plt.ylabel('Return (' + self.freq.capitalize() + ')')
        plt.show()
Exemple #30
0
            symbol=symbol, outputsize='compact')

    elif choice == 4:
        data, meta_data = ts.get_weekly(
            symbol=symbol)

    elif choice == 5:
        data, meta_data = ts.get_weekly_adjusted(
            symbol=symbol)

    elif choice == 6:
        data, meta_data = ts.get_monthly(
            symbol=symbol)

    elif choice == 7:
        data, meta_data = ts.get_monthly_adjusted(
            symbol=symbol)

    print(f"\n\n{symbol}\n")
    pprint(data.head(n))
    graph = input("\nDo you want to plot a graph? (y/n) ")
    if graph == 'y':
        data['4. close'].plot()
        if choice == 1:
            plt.title(
                f'{choices[choice]} Time Series for the {symbol} stock ({interval})')
        else:
            plt.title(
                f'{choices[choice]} Time Series for the {symbol} stock')
        plt.show()