Ejemplo n.º 1
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the Alpha Vantage sensor."""
    from alpha_vantage.timeseries import TimeSeries
    from alpha_vantage.foreignexchange import ForeignExchange

    api_key = config.get(CONF_API_KEY)
    symbols = config.get(CONF_SYMBOLS)

    timeseries = TimeSeries(key=api_key)

    dev = []
    for symbol in symbols:
        try:
            timeseries.get_intraday(symbol[CONF_SYMBOL])
        except ValueError:
            _LOGGER.error(
                "API Key is not valid or symbol '%s' not known", symbol)
        dev.append(AlphaVantageSensor(timeseries, symbol))

    forex = ForeignExchange(key=api_key)
    for conversion in config.get(CONF_FOREIGN_EXCHANGE):
        from_cur = conversion.get(CONF_FROM)
        to_cur = conversion.get(CONF_TO)
        try:
            forex.get_currency_exchange_rate(
                from_currency=from_cur, to_currency=to_cur)
        except ValueError as error:
            _LOGGER.error(
                "API Key is not valid or currencies '%s'/'%s' not known",
                from_cur, to_cur)
            _LOGGER.debug(str(error))
        dev.append(AlphaVantageForeignExchange(forex, conversion))

    add_devices(dev, True)
Ejemplo n.º 2
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the Alpha Vantage sensor."""
    from alpha_vantage.timeseries import TimeSeries
    from alpha_vantage.foreignexchange import ForeignExchange

    api_key = config.get(CONF_API_KEY)
    symbols = config.get(CONF_SYMBOLS, [])
    conversions = config.get(CONF_FOREIGN_EXCHANGE, [])

    if not symbols and not conversions:
        msg = 'Warning: No symbols or currencies configured.'
        hass.components.persistent_notification.create(
            msg, 'Sensor alpha_vantage')
        _LOGGER.warning(msg)
        return

    timeseries = TimeSeries(key=api_key)

    dev = []
    for symbol in symbols:
        try:
            _LOGGER.debug("Configuring timeseries for symbols: %s",
                          symbol[CONF_SYMBOL])
            timeseries.get_intraday(symbol[CONF_SYMBOL])
        except ValueError:
            _LOGGER.error(
                "API Key is not valid or symbol '%s' not known", symbol)
        dev.append(AlphaVantageSensor(timeseries, symbol))

    forex = ForeignExchange(key=api_key)
    for conversion in conversions:
        from_cur = conversion.get(CONF_FROM)
        to_cur = conversion.get(CONF_TO)
        try:
            _LOGGER.debug("Configuring forex %s - %s", from_cur, to_cur)
            forex.get_currency_exchange_rate(
                from_currency=from_cur, to_currency=to_cur)
        except ValueError as error:
            _LOGGER.error(
                "API Key is not valid or currencies '%s'/'%s' not known",
                from_cur, to_cur)
            _LOGGER.debug(str(error))
        dev.append(AlphaVantageForeignExchange(forex, conversion))

    add_devices(dev, True)
    _LOGGER.debug("Setup completed")
Ejemplo n.º 3
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the Alpha Vantage sensor."""
    from alpha_vantage.timeseries import TimeSeries

    api_key = config.get(CONF_API_KEY)
    symbols = config.get(CONF_SYMBOLS)

    timeseries = TimeSeries(key=api_key)

    dev = []
    for symbol in symbols:
        try:
            timeseries.get_intraday(symbol)
        except ValueError:
            _LOGGER.error(
                "API Key is not valid or symbol '%s' not known", symbol)
            return
        dev.append(AlphaVantageSensor(timeseries, symbol))

    add_devices(dev, True)
Ejemplo n.º 4
0
    df["cum_roll_max"] = df["cum_return"].cummax()
    df["drawdown"] = df["cum_roll_max"] - df["cum_return"]
    df["drawdown_pct"] = df["drawdown"] / df["cum_roll_max"]
    max_dd = df["drawdown_pct"].max()
    return max_dd


# Download historical data for DJI constituent stocks

tickers = [
    "MSFT", "AAPL", "FB", "AMZN", "INTC", "CSCO", "VZ", "IBM", "QCOM", "LYFT"
]

ohlc_intraday = {}  # directory with ohlc value for each stock
key_path = "D:\\Udemy\\Quantitative Investing Using Python\\1_Getting Data\\AlphaVantage\\key.txt"
ts = TimeSeries(key='GAVN79RJETRO047U', output_format='pandas')

attempt = 0  # initializing passthrough variable
drop = [
]  # initializing list to store tickers whose close price was successfully extracted
while len(tickers) != 0 and attempt <= 5:
    tickers = [j for j in tickers if j not in drop]
    for i in range(len(tickers)):
        try:
            ohlc_intraday[tickers[i]] = ts.get_intraday(symbol=tickers[i],
                                                        interval='5min',
                                                        outputsize='full')[0]
            ohlc_intraday[tickers[i]].columns = [
                "Open", "High", "Low", "Adj Close", "Volume"
            ]
            drop.append(tickers[i])
Ejemplo n.º 5
0
# input file (relative path)
input_file = 'companies.csv'

# output file (relative path)
output_dir = 'stock_data'
output_file = output_dir + '/' + 'stock_' + str(start) + '_' + str(
    end) + '.csv'

##############
#### CODE ####

# create output dir
os.makedirs(output_dir, exist_ok=True)

# load api key
ts = TimeSeries(key=api_key.KEY, output_format='pandas')

# create output file with header
with open(output_file, 'w') as f:
    f.write('Date,Open,High,Low,Close,Volume,Company\n')

# process google
with open(input_file, 'r') as comp:
    reader = csv.reader(comp)

    # for each company
    for row in reader:
        name = str(row[0])
        ticker = str(row[1]).strip()
        print('\n' + name)
Ejemplo n.º 6
0
def main():
    """
    Gamestonk Terminal is an awesome stock market terminal that has been developed for fun,
    while I saw my GME shares tanking. But hey, I like the stock.
    """
    # Enable VT100 Escape Sequence for WINDOWS 10 Ver. 1607
    if sys.platform == "win32":
        os.system("")

    s_ticker = ""
    s_start = ""
    df_stock = pd.DataFrame()
    s_interval = "1440min"

    # Set stock by default to speed up testing
    # s_ticker = "BB"
    # ts = TimeSeries(key=cfg.API_KEY_ALPHAVANTAGE, output_format='pandas')
    # df_stock, d_stock_metadata = ts.get_daily_adjusted(symbol=s_ticker, outputsize='full')
    # df_stock.sort_index(ascending=True, inplace=True)
    # s_start = datetime.strptime("2020-06-04", "%Y-%m-%d")
    # df_stock = df_stock[s_start:]

    # Add list of arguments that the main parser accepts
    menu_parser = argparse.ArgumentParser(add_help=False, prog="gamestonk_terminal")
    choices = [
        "help",
        "quit",
        "q",
        "clear",
        "load",
        "candle",
        "view",
        "export",
        "disc",
        "mill",
        "ba",
        "res",
        "fa",
        "ta",
        "dd",
        "eda",
        "pred",
        "ca",
        "op",
        "fred",
        "pa",
        "crypto",
        "ra",
    ]
    menu_parser.add_argument("opt", choices=choices)
    completer = NestedCompleter.from_nested_dict({c: None for c in choices})

    # Print first welcome message and help
    print("\nWelcome to Gamestonk Terminal 🚀\n")
    should_print_help = True
    parsed_stdin = False

    if gtff.ENABLE_THOUGHTS_DAY:
        print("-------------------")
        try:
            thought.get_thought_of_the_day()
        except Exception as e:
            print(e)
        print("")

    # Loop forever and ever
    while True:
        main_cmd = False
        if should_print_help:
            print_help(s_ticker, s_start, s_interval, b_is_stock_market_open())
            should_print_help = False

        # Get input command from stdin or user
        if not parsed_stdin and len(sys.argv) > 1:
            as_input = " ".join(sys.argv[1:])
            parsed_stdin = True
            print(f"{get_flair()}> {as_input}")
        elif session and gtff.USE_PROMPT_TOOLKIT:
            as_input = session.prompt(f"{get_flair()}> ", completer=completer)
        else:
            as_input = input(f"{get_flair()}> ")

        # Is command empty
        if not as_input:
            print("")
            continue

        # Parse main command of the list of possible commands
        try:
            (ns_known_args, l_args) = menu_parser.parse_known_args(as_input.split())

        except SystemExit:
            print("The command selected doesn't exist\n")
            continue

        b_quit = False
        if ns_known_args.opt == "help":
            should_print_help = True

        elif (ns_known_args.opt == "quit") or (ns_known_args.opt == "q"):
            break

        elif ns_known_args.opt == "clear":
            s_ticker, s_start, s_interval, df_stock = clear(
                l_args, s_ticker, s_start, s_interval, df_stock
            )
            main_cmd = True

        elif ns_known_args.opt == "load":
            s_ticker, s_start, s_interval, df_stock = load(
                l_args, s_ticker, s_start, s_interval, df_stock
            )
            main_cmd = True

        elif ns_known_args.opt == "candle":

            if s_ticker:
                candle(
                    s_ticker,
                    (datetime.now() - timedelta(days=180)).strftime("%Y-%m-%d"),
                )

            else:
                print(
                    "No ticker selected. Use 'load ticker' to load the ticker you want to look at.",
                    "\n",
                )

            main_cmd = True

        elif ns_known_args.opt == "view":

            if s_ticker:
                view(l_args, s_ticker, s_start, s_interval, df_stock)

            else:
                print(
                    "No ticker selected. Use 'load ticker' to load the ticker you want to look at."
                )
            main_cmd = True

        elif ns_known_args.opt == "export":
            export(l_args, df_stock)
            main_cmd = True

        elif ns_known_args.opt == "disc":
            b_quit = disc_controller.menu()

        elif ns_known_args.opt == "mill":
            b_quit = mill.papermill_menu()

        elif ns_known_args.opt == "ba":
            b_quit = ba_controller.menu(s_ticker, s_start)

        elif ns_known_args.opt == "res":
            b_quit = rm.res_menu(s_ticker, s_start, s_interval)

        elif ns_known_args.opt == "ca":
            b_quit = ca_controller.menu(df_stock, s_ticker, s_start, s_interval)

        elif ns_known_args.opt == "fa":
            b_quit = fam.fa_menu(s_ticker, s_start, s_interval)

        elif ns_known_args.opt == "ta":
            b_quit = ta_controller.menu(df_stock, s_ticker, s_start, s_interval)

        elif ns_known_args.opt == "dd":
            b_quit = ddm.dd_menu(df_stock, s_ticker, s_start, s_interval)

        elif ns_known_args.opt == "eda":
            if s_interval == "1440min":
                b_quit = eda_controller.menu(df_stock, s_ticker, s_start, s_interval)
            else:
                df_stock = yf.download(s_ticker, start=s_start, progress=False)
                df_stock = df_stock.rename(
                    columns={
                        "Open": "1. open",
                        "High": "2. high",
                        "Low": "3. low",
                        "Close": "4. close",
                        "Adj Close": "5. adjusted close",
                        "Volume": "6. volume",
                    }
                )
                df_stock.index.name = "date"
                s_interval = "1440min"

                b_quit = eda_controller.menu(df_stock, s_ticker, s_start, s_interval)

        elif ns_known_args.opt == "op":
            b_quit = op_controller.menu(s_ticker)

        elif ns_known_args.opt == "fred":
            b_quit = fm.fred_menu()

        elif ns_known_args.opt == "pa":
            b_quit = port_controller.menu()

        elif ns_known_args.opt == "crypto":
            b_quit = crypto_controller.menu()

        elif ns_known_args.opt == "pred":

            if not gtff.ENABLE_PREDICT:
                print("Predict is not enabled in feature_flags.py")
                print("Prediction menu is disabled")
                print("")
                continue

            try:
                # pylint: disable=import-outside-toplevel
                from gamestonk_terminal.prediction_techniques import pred_menu as pm
            except ModuleNotFoundError as e:
                print("One of the optional packages seems to be missing")
                print("Optional packages need to be installed")
                print(e)
                print("")
                continue
            except Exception as e:
                print(e)
                print("")
                continue

            if s_interval == "1440min":
                b_quit = pm.pred_menu(df_stock, s_ticker, s_start, s_interval)
            # If stock data is intradaily, we need to get data again as prediction
            # techniques work on daily adjusted data. By default we load data from
            # Alpha Vantage because the historical data loaded gives a larger
            # dataset than the one provided by quandl
            else:
                try:
                    ts = TimeSeries(
                        key=cfg.API_KEY_ALPHAVANTAGE, output_format="pandas"
                    )
                    # pylint: disable=unbalanced-tuple-unpacking
                    df_stock_pred, _ = ts.get_daily_adjusted(
                        symbol=s_ticker, outputsize="full"
                    )
                    # pylint: disable=no-member
                    df_stock_pred = df_stock_pred.sort_index(ascending=True)
                    df_stock_pred = df_stock_pred[s_start:]
                    b_quit = pm.pred_menu(
                        df_stock_pred, s_ticker, s_start, s_interval="1440min"
                    )
                except Exception as e:
                    print(e)
                    print("Either the ticker or the API_KEY are invalids. Try again!")
                    return

        elif ns_known_args.opt == "ra":
            b_quit = ra_controller.menu(df_stock, s_ticker, s_start, s_interval)

        else:
            print("Shouldn't see this command!")
            continue

        if b_quit:
            break
        else:
            if not main_cmd:
                should_print_help = True

    print(
        "Hope you enjoyed the terminal. Remember that stonks only go up. Diamond hands.\n"
    )
Ejemplo n.º 7
0
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import sklearn.linear_model
import scipy.stats as stats
import pandas_market_calendars as mcal
from alpha_vantage.timeseries import TimeSeries
api_key = '8FIYTT49ZEZT2GV5'

ts = TimeSeries(key=api_key, output_format='pandas')
data, meta_data = ts.get_daily(symbol='SPY', outputsize='full')

data = data.reset_index()
data['date'] = data['date'].dt.strftime('%Y%m%d')
data['date'] = data['date'].values.astype(int)

X = np.c_[data['date']]
Y = np.c_[data['4. close']]
X = [i[0] for i in X]
Y = [i[0] for i in Y]

X = X[::-1]  #REVERSING ORDER
Y = Y[::-1]  #REVERSING ORDER
last_day = len(X) - 1
th_day = list(range(0, last_day + 1))


def YYYY_MM_DD_to_th_day(YMD):
    early = nyse.schedule(start_date='1999-11-01', end_date=YMD)
    return len(early) - 1
Ejemplo n.º 8
0
import pandas as pd
from alpha_vantage.timeseries import TimeSeries
import time

api_key = 'R7SX528FT8JX6F7O'
ts = TimeSeries(key =api_key, output_format = 'pandas')
data, meta_data = ts.get_intraday(symbol = 'MSFT', interval = '1min', outputsize = 'full')
data.
print(data)

i = 1
while i ==1 :
    data.to_excel("stock_quote.xlsx")
    time.sleep(60)
Ejemplo n.º 9
0
def update_metrics(n):
    lon, lat, alt = satellite.get_lonlatalt(datetime.datetime.now())
    ts = TimeSeries(key=api_key, output_format='pandas')
    ti = TechIndicators(key=api_key, output_format='pandas')
    period = 100
    GOOGLdata_ts, GOOGLmeta_data_ts = ts.get_intraday(symbol='EBAY', interval='5min', outputsize='compact')
    GOOGLdata_ti, GOOGLmeta_data_ti = ti.get_sma(symbol='EBAY', interval='5min', time_period=period, series_type='close')


    def prepareData(series, lag_start, lag_end, test_size, target_encoding=False):
        """
            series: pd.DataFrame
                dataframe with timeseries

            lag_start: int
                initial step back in time to slice target variable
                example - lag_start = 1 means that the model
                          will see yesterday's values to predict today

            lag_end: int
                final step back in time to slice target variable
                example - lag_end = 4 means that the model
                          will see up to 4 days back in time to predict today

            test_size: float
                size of the test dataset after train/test split as percentage of dataset

            target_encoding: boolean
                if True - add target averages to the dataset

        """

        # copy of the initial dataset
        data = pd.DataFrame(series.copy())
        data.columns = ["y"]

        # lags of series
        for i in range(lag_start, lag_end):
            data["lag_{}".format(i)] = data.y.shift(-i)

        # datetime features
        # train-test split
        y = data.dropna().y
        X = data.dropna().drop(['y'], axis=1)

        return X, y

    X, y = prepareData(GOOGLdata_ts['4. close'], lag_start=0, lag_end=19, test_size=0.3, target_encoding=True)

    X_train_scaled = scaler.fit_transform(X)
    X_test_scaled = scaler.transform(X)

    pred = model.predict(X_test_scaled)
    closep = GOOGLdata_ts.iloc[0]['4. close']
    smav = GOOGLdata_ti.iloc[0]['SMA']
    predikt = pred[0]
    if smav > predikt:
        ton = 'Trading stategy: Bear Strategy'
    else:
        ton = 'Trading stategy: Bull Strategy'
    style = {'padding': '5px', 'fontSize': '16px'}
    return [
        html.Span('Close Price Prediction: {0:.2f}'.format(predikt), style=style),
        html.Span('SMA: {0:.2f}'.format(smav), style=style),
        html.Span(ton.format(alt), style=style)
    ]
Ejemplo n.º 10
0
def grabData(key, input_ticker, delta):
    tS = TimeSeries(key=key, output_format='pandas', indexing_type='date')
    tI = TechIndicators(key=key, output_format='pandas')

    data, meta_data = tS.get_daily_adjusted(
        symbol=input_ticker,
        outputsize='full')  #compact for last 100 or full for everything
    macd, macd_meta = tI.get_macd(symbol=input_ticker,
                                  interval='daily',
                                  series_type='close')
    rsi, rsi_meta = tI.get_rsi(symbol=input_ticker,
                               interval='daily',
                               time_period=14,
                               series_type='close')
    willr, willr_meta = tI.get_willr(symbol=input_ticker,
                                     interval='daily',
                                     time_period=14)
    adx, adx_meta = tI.get_adx(symbol=input_ticker,
                               interval='daily',
                               time_period=14)
    mom, mom_meta = tI.get_mom(symbol=input_ticker,
                               interval='daily',
                               time_period=10,
                               series_type='close')

    all_vals = [data, macd, rsi, willr, adx, mom]

    final_df = pd.concat(
        all_vals, axis=1, sort=True
    )  # Sort arg may need to be False, leaving it blank raises Pandas error
    final_df = final_df.dropna()
    df = final_df.iloc[::-1]
    df = df.reset_index()
    df = df.drop(['6. volume', '7. dividend amount'], axis=1)

    # *************************************************************************

    labels = []
    priceDiffernces = []

    for index, row in df.iterrows():
        if 0 <= index < delta:
            pass
        else:
            initPrice = row[5]
            deltaPrice = df.iloc[index - delta][5]
            priceDiffernces.append(round((deltaPrice - initPrice), 2))

            if deltaPrice > initPrice:
                labels.append(1)
            else:
                labels.append(0)

    df = df.drop(list(range(delta)))

    labelsPD = pd.Series(labels)
    priceDiffsPD = pd.Series(priceDiffernces)
    df['Labels'] = labelsPD.values
    df['Price Diffs'] = priceDiffsPD.values

    df.to_csv(input_ticker + '.csv')
Ejemplo n.º 11
0
def get_alphavantage_compact_data(symbol=None):
    if symbol is None:
        symbol = 'MSFT'
    ts = TimeSeries(output_format='pandas', indexing_type='date')
    return ts.get_daily_adjusted(symbol=symbol, outputsize='compact')
Ejemplo n.º 12
0
    df = DF.copy()
    df["cum_return"] = (1 + df["ret"]).cumprod()
    df["cum_roll_max"] = df["cum_return"].cummax()
    df["drawdown"] = df["cum_roll_max"] - df["cum_return"]
    df["drawdown_pct"] = df["drawdown"]/df["cum_roll_max"]
    max_dd = df["drawdown_pct"].max()
    return max_dd

# Download historical data for DJI constituent stocks

tickers = ["MSFT","AAPL","FB","AMZN","INTC", "CSCO","VZ","IBM","QCOM","LYFT"]


ohlc_intraday = {} # directory with ohlc value for each stock            
key_path = "D:\\Udemy\\Quantitative Investing Using Python\\1_Getting Data\\AlphaVantage\\key.txt"
ts = TimeSeries(key=open(key_path,'r').read(), output_format='pandas')

attempt = 0 # initializing passthrough variable
drop = [] # initializing list to store tickerList whose close price was successfully extracted
while len(tickers) != 0 and attempt <=5:
    tickers = [j for j in tickers if j not in drop]
    for i in range(len(tickers)):
        try:
            ohlc_intraday[tickers[i]] = ts.get_intraday(symbol=tickers[i],interval='5min', outputsize='full')[0]
            ohlc_intraday[tickers[i]].columns = ["Open","High","Low","Adj Close","Volume"]
            drop.append(tickers[i])      
        except:
            print(tickers[i]," :failed to fetch data...retrying")
            continue
    attempt+=1
def fetch_graph_results(strategy_name, investment_per_strategy, stock_symbol_array):
    stock_details = []
    five_days_history = []
    investment_per_company = investment_per_strategy / 3

    for stock_symbol in stock_symbol_array:

        ts = TimeSeries(key='L7LPZFOTDXED8KS0')
        data, meta_data = ts.get_daily_adjusted(stock_symbol)

        if meta_data:

            count = 0
            for each_entry in data:
                if count < 5:
                    stock_details.append(
                        [strategy_name, stock_symbol, each_entry, data[each_entry]['5. adjusted close']])
                    five_days_history.append(each_entry)
                    count = count + 1
                else:
                    break

    first_day = []

    first_day_history = []
    second_day_history = []
    third_day_history = []
    fourth_day_history = []
    fifth_day_history = []

    first_day_investment = 0
    second_day_investment = 0
    third_day_investment = 0
    forth_day_investment = 0
    fifth_day_investment = 0

    graph_results = []
    graph_results_detailed = []

    for entry in stock_details:
        if entry[2] == sorted(set(five_days_history))[0]:
            first_day.append([entry[1], entry[3]])
            no_of_stocks_per_company = math.floor(investment_per_company / float(entry[3]))
            first_day_history.append([entry[1], round(float(entry[3]), 2), no_of_stocks_per_company])
            first_day_investment += no_of_stocks_per_company * float(entry[3])

    graph_results.append([sorted(set(five_days_history))[0], round(first_day_investment, 2)])

    for entry in stock_details:

        if entry[2] == sorted(set(five_days_history))[1]:
            for company in first_day_history:
                if company[0] == entry[1]:
                    second_day_history.append([entry[1], round(float(entry[3]), 2), company[2]])
                    second_day_investment += (float(entry[3]) * company[2])

        elif entry[2] == sorted(set(five_days_history))[2]:
            for company in first_day_history:
                if company[0] == entry[1]:
                    third_day_history.append([entry[1], round(float(entry[3]), 2), company[2]])
                    third_day_investment += (float(entry[3]) * company[2])

        elif entry[2] == sorted(set(five_days_history))[3]:
            for company in first_day_history:
                if company[0] == entry[1]:
                    fourth_day_history.append([entry[1], round(float(entry[3]), 2), company[2]])
                    forth_day_investment += (float(entry[3]) * company[2])

        elif entry[2] == sorted(set(five_days_history))[4]:
            for company in first_day_history:
                if company[0] == entry[1]:
                    fifth_day_history.append([entry[1], round(float(entry[3]), 2), company[2]])
                    fifth_day_investment += (float(entry[3]) * company[2])

    graph_results.append([sorted(set(five_days_history))[1], round(second_day_investment, 2)])
    graph_results.append([sorted(set(five_days_history))[2], round(third_day_investment, 2)])
    graph_results.append([sorted(set(five_days_history))[3], round(forth_day_investment, 2)])
    graph_results.append([sorted(set(five_days_history))[4], round(fifth_day_investment, 2)])

    graph_results_detailed.append([sorted(set(five_days_history))[0], first_day_history])
    graph_results_detailed.append([sorted(set(five_days_history))[1], second_day_history])
    graph_results_detailed.append([sorted(set(five_days_history))[2], third_day_history])
    graph_results_detailed.append([sorted(set(five_days_history))[3], fourth_day_history])
    graph_results_detailed.append([sorted(set(five_days_history))[4], fifth_day_history])

    return graph_results, graph_results_detailed
Ejemplo n.º 14
0
# In[273]:

import pandas as pd
from alpha_vantage.timeseries import TimeSeries

import os

import pprint
import datetime as dt

from sklearn.preprocessing import StandardScaler

# In[15]:

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

# In[22]:

df = pd.read_csv('fund_comp.csv', header=0).set_index('company_id')
df1 = pd.read_csv('fund_by_comp.csv', header=0).set_index('company_id')

# In[17]:

#data from http://rankandfiled.com/#/data/hedgefunds
df2 = pd.read_csv('cik_ticker_map.csv', header=0,
                  delimiter='|').set_index('CIK')

# In[ ]:

df2 = pd.read_csv('cik_ticker_map.csv', header=0,
Ejemplo n.º 15
0

def get_alpha_key():
    key_path = Path() / "key"
    if key_path.exists():
        with open(key_path, 'r') as f:
            key = f.read()
        return key
    else:
        raise FileExistsError(
            "There is no file named 'key' in the project directory. Please add it with your Alpha Vantage API key."
        )


KEY = get_alpha_key()
ts = TimeSeries(KEY, output_format='pandas')
ti = TechIndicators(KEY)

PATH_TO_SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
PATH_TO_ROOT_DIR = os.path.normpath(os.path.join(PATH_TO_SCRIPT_DIR, '..'))

REQ_TYPES = [
    "symbol", "last", "daily", "daily_adj", "intraday", "market_times"
]
INTERVALS = ["1min", "5min", "15min", "30min", "60min"]


def get_data(symbol,
             mode="daily",
             adjusted=False,
             interval='15min',
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input
import dash_bootstrap_components as dbc

#in this section the User will type in the ticker they want displayed in the DASH application

# ATTENTION: Please enter your stock in the ticker string below

ticker = 'TSLA'

# Once ticker is entered, save the file and the dash app will automatically update
# In this section we make the API call and select which columns to pull data from. We'll use an interval of every 5 minutes

key = 'RLVN7TWQW8MB1ZI3'
ts = TimeSeries(key, output_format='pandas')
ttm_data, ttm_meta_data = ts.get_intraday(symbol=ticker,
                                          interval='5min',
                                          outputsize='compact')
df = ttm_data.copy()
df = df.transpose()
df.rename(index={
    "1. open": "open",
    "2. high": "high",
    "3. low": "low",
    "4. close": "close",
    "5. volume": "volume"
},
          inplace=True)
df = df.reset_index().rename(columns={'index': 'indicator'})
df = pd.melt(df, id_vars=['indicator'], var_name='date', value_name='rate')
Ejemplo n.º 17
0
    def test_av_access(self):

        ts = TimeSeries(config.AV_KEY, output_format="pandas")
        ts.get_daily(symbol="MSFT", outputsize="compact")
        ts.get_intraday(symbol="MSFT", interval="5min", outputsize="compact")
Ejemplo n.º 18
0
from alpha_vantage.timeseries import TimeSeries
import json
import sys
JSON_INDENT = 4
print(type(sys.argv[1]))
print(type(sys.argv[2]))
ts = TimeSeries(key='H0WG63BW6PGFSYEB')
# Get json object with the intraday data and another with the call's metadata
data, meta_data = ts.get_intraday(sys.argv[1], interval=sys.argv[2])
dic = {}
for i, j in data.items():
    dic[i] = {}
    for x, y in j.items():
        dic[i][x.split(".")[1].strip()] = y
data = dic
#print(data)
with open("minute.json", 'w', encoding='utf-8') as output_file:
    output_file.write(
        str(
            json.dumps(
                data,
                indent=JSON_INDENT,
                # Commented out because str is python3 and accounts for encoding specifications
                #                     ensure_ascii=False,
                #                     encoding='utf-8'
            )))
Ejemplo n.º 19
0
def get_vantage_client():
    VANTAGE_API_KEY = 'R4RZ079WZET1M0JB'
    return TimeSeries(key=VANTAGE_API_KEY, output_format='pandas')
Ejemplo n.º 20
0
def update_graph_live(n):
    satellite = Orbital('TERRA')
    ts = TimeSeries(key=api_key, output_format='pandas')
    ti = TechIndicators(key=api_key, output_format='pandas')
    period = 100
    GOOGLdata_ts, GOOGLmeta_data_ts = ts.get_intraday(symbol='EBAY', interval='5min', outputsize='compact')
    GOOGLdata_ti, GOOGLmeta_data_ti = ti.get_sma(symbol='EBAY', interval='5min', time_period=period, series_type='close')

    data = {
        'time': [],
        'close price': [],
        'SMA(simple moving average)': [],
        'ML prediction of close price': []
    }

    def prepareData(series, lag_start, lag_end, test_size, target_encoding=False):
        """
            series: pd.DataFrame
                dataframe with timeseries

            lag_start: int
                initial step back in time to slice target variable
                example - lag_start = 1 means that the model
                          will see yesterday's values to predict today

            lag_end: int
                final step back in time to slice target variable
                example - lag_end = 4 means that the model
                          will see up to 4 days back in time to predict today

            test_size: float
                size of the test dataset after train/test split as percentage of dataset

            target_encoding: boolean
                if True - add target averages to the dataset

        """

        # copy of the initial dataset
        data = pd.DataFrame(series.copy())
        data.columns = ["y"]

        # lags of series
        for i in range(lag_start, lag_end):
            data["lag_{}".format(i)] = data.y.shift(-i)

        # datetime features
        # train-test split
        y = data.dropna().y
        X = data.dropna().drop(['y'], axis=1)

        return X, y

    X, y = prepareData(GOOGLdata_ts['4. close'], lag_start=0, lag_end=19, test_size=0.3, target_encoding=True)

    X_train_scaled = scaler.fit_transform(X)
    X_test_scaled = scaler.transform(X)

    pred = model.predict(X_test_scaled)

    for j in range(70):
        tme = GOOGLdata_ts.index[j]
        data['time'].append(tme)
        cp = GOOGLdata_ts.iloc[j]['4. close']
        data['close price'].append(cp)
        predplot = pred[j] + 0.1
        data['ML prediction of close price'].append(predplot)
        sma = GOOGLdata_ti.iloc[j]['SMA']
        data['SMA(simple moving average)'].append(sma)

    print(data)
    # Create the graph with subplots
    fig = plotly.tools.make_subplots(rows=2, cols=1, vertical_spacing=0.2)
    fig['layout']['margin'] = {
        'l': 30, 'r': 10, 'b': 30, 't': 10
    }
    fig['layout']['legend'] = {'x': 0, 'y': 1, 'xanchor': 'left'}

    fig.append_trace({
        'x': data['time'],
        'y': data['SMA(simple moving average)'],
        'name': 'SMA',
        'mode': 'lines+markers',
        'type': 'scatter'
    }, 1, 1)
    fig.append_trace({
        'x': data['time'],
        'y': data['ML prediction of close price'],
        'name': 'Prediction',
        'mode': 'lines+markers',
        'type': 'scatter'
    }, 1, 1)
    '''fig.append_trace({
        'x': data['time'],
        'y': data['Latitude'],
        'text': data['time'],
        'name': 'Longitude vs Latitude',
        'mode': 'lines+markers',
        'type': 'scatter'
    }, 2, 1)'''

    return fig
def results():
    # build a request object
    req = request.get_json(force=True)
    print('input request', req)
    # fetch action from json
    action = req.get('queryResult').get('action')
    #print('action',action)
    if action == 'read_excel':
        parameters = req['queryResult']['parameters']
        print('action', action)
        print('parameters', parameters)
        exl = pd.read_excel('Portfolio.xlsx')

        fname = parameters['given-name']
        lname = parameters['last-name']
        folio = parameters['number']

        exl['Name'] = exl['Name'].astype('category')

        exl = exl[(exl['Name'].str.contains(fname, case=False))
                  & (exl['Folio No'] == int(folio))]
        filtered_exl = exl[[
            'Companies', 'Stocks', 'Purchase Date', 'Purchase Price(USD)',
            'Purchase Investment (USD)'
        ]]
        total_investment = filtered_exl['Purchase Investment (USD)'].sum()
        filtered_exl_dict = filtered_exl.to_dict(orient='records')
        #print(filtered_exl_dict)
        #print(type(filtered_exl_dict))
        filtered_exl_dict.append(
            {'Total Purchase Investments (USD)': round(total_investment, 2)})
        print(filtered_exl_dict)
        # return a fulfillment response
        if len(filtered_exl) != 0:
            return {'Here, are your details': filtered_exl_dict}
        else:
            return {
                'fulfillmentText':
                'User Authetication Error.We are not able to authenticate your details. Please  enter correct Name & Folio Number. Thanks.'
            }

    elif action == 'marketvalueaction':

        parameters = req['queryResult']['parameters']
        print('action', action)
        print('parameters', parameters)

        apikey = 'JL9GIGT0KVN581XQ'
        invst = pd.read_excel('Portfolio.xlsx')

        ts1 = TimeSeries(key=apikey,
                         output_format='pandas',
                         indexing_type='integer')
        fname = parameters['given-name']
        currentvalue = parameters['currentmarketvalue'].lower()
        folio = parameters['number']

        if (
            (currentvalue == 'current market value')
        ):  #or (currentvalue == 'sure') or (currentvalue == 'ok') or (currentvalue == 'okay')) :

            invst['Name'] = invst['Name'].astype('category')
            invst = invst[(invst['Name'].str.contains(fname, case=False))
                          & (invst['Folio No'] == int(folio))]
            tckr = invst['Ticker'].tolist()
            start = time.time()
            data, meta_data = ts1.get_batch_stock_quotes(symbols=tckr)
            data.rename({'2. price': 'Current Market Price (USD)'},
                        axis=1,
                        inplace=True)
            data.rename({'1. symbol': 'Ticker'}, axis=1, inplace=True)
            data.rename({'4. timestamp': 'Current Date'}, axis=1, inplace=True)

            current = pd.merge(invst, data, on='Ticker',
                               how='inner').drop(['date', '3. volume'], axis=1)
            current['Current Market Price (USD)'] = current[
                'Current Market Price (USD)'].astype('float')
            current['Current Investment (USD)'] = current[
                'Current Market Price (USD)'] * current['Stocks']

            filtered_current = current[[
                'Companies', 'Stocks', 'Purchase Price(USD)',
                'Purchase Investment (USD)', 'Current Market Price (USD)',
                'Current Investment (USD)'
            ]]
            current_total = filtered_current['Current Investment (USD)'].sum()

            filtered_current_dict = filtered_current.to_dict(orient='records')
            filtered_current_dict.append({
                'Current Market Value of All Investments (USD)':
                round(current_total, 2)
            })
            print(filtered_current_dict)
            end = time.time()
            print('API time ', end - start)
            # return a fulfillment response
            if len(filtered_current) != 0:
                return {'Here, are your details': filtered_current_dict}
            else:
                return {
                    'fulfillmentText':
                    'Please enter whether you would like to see current market value of your investments or not.Thanks.'
                }
        elif ((currentvalue == 'no') or (currentvalue == 'nope')
              or (currentvalue == 'not')):
            return {'fulfillmentText': 'Alright.'}

    elif action == 'performeractions':

        parameters = req['queryResult']['parameters']
        print('action', action)
        print('parameters', parameters)

        apikey = 'K9HSCD76XIMHD8VJ'
        invst = pd.read_excel('Portfolio.xlsx')

        ts1 = TimeSeries(key=apikey,
                         output_format='pandas',
                         indexing_type='integer')
        fname = parameters['given-name']
        performer = parameters['performerentity'].lower()
        folio = parameters['number']

        invst['Name'] = invst['Name'].astype('category')
        invst = invst[(invst['Name'].str.contains(fname, case=False))
                      & (invst['Folio No'] == int(folio))]
        tckr = invst['Ticker'].tolist()
        start = time.time()
        data, meta_data = ts1.get_batch_stock_quotes(symbols=tckr)
        data.rename({'2. price': 'Current Market Price (USD)'},
                    axis=1,
                    inplace=True)
        data.rename({'1. symbol': 'Ticker'}, axis=1, inplace=True)
        data.rename({'4. timestamp': 'Current Date'}, axis=1, inplace=True)

        current = pd.merge(invst, data, on='Ticker',
                           how='inner').drop(['date', '3. volume'], axis=1)
        current['Current Market Price (USD)'] = current[
            'Current Market Price (USD)'].astype('float')
        current['Current Investment (USD)'] = current[
            'Current Market Price (USD)'] * current['Stocks']

        filtered_current = current[[
            'Companies', 'Stocks', 'Purchase Price(USD)',
            'Purchase Investment (USD)', 'Current Market Price (USD)',
            'Current Investment (USD)'
        ]]
        current_total = filtered_current['Current Investment (USD)'].sum()

        filtered_current['Stock Profit/Loss (USD)'] = filtered_current[
            'Current Investment (USD)'] - filtered_current[
                'Purchase Investment (USD)']
        filtered_current['Stock Profit/Loss (USD)'] = round(
            filtered_current['Stock Profit/Loss (USD)'], 2)
        best = filtered_current[
            filtered_current['Stock Profit/Loss (USD)'] ==
            filtered_current['Stock Profit/Loss (USD)'].max()]
        worst = filtered_current[
            filtered_current['Stock Profit/Loss (USD)'] ==
            filtered_current['Stock Profit/Loss (USD)'].min()]
        end = time.time()
        print('API time ', end - start)

        if performer == 'good':
            best = best[['Companies', 'Stocks', 'Stock Profit/Loss (USD)']]
            filtered_current_dict = best.to_dict(orient='records')
            print(filtered_current_dict)
            return {
                'Here is your best performing stock': filtered_current_dict
            }
        elif performer == 'bad':
            worst = worst[['Companies', 'Stocks', 'Stock Profit/Loss (USD)']]
            filtered_current_dict = worst.to_dict(orient='records')
            print(filtered_current_dict)
            return {
                'Here is your worst performing stock': filtered_current_dict
            }

        #print(filtered_current_dict)
        #print(type(filtered_current_dict))
        #filtered_current_dict.append({'Current Market Value of All Investments (USD)':round(current_total,2)})

        else:
            return {
                'fulfillmentText':
                'Please provide input for either a best or worst performing stock.Thanks.'
            }

    elif action == 'goodbyeaction':

        parameters = req['queryResult']['parameters']
        print('action', action)
        print('parameters', parameters)
        goodbye = parameters['goodbyeentity'].lower()
        if (
            (goodbye == 'yes')
        ):  #or (currentvalue == 'sure') or (currentvalue == 'ok') or (currentvalue == 'okay')) :
            return {
                'fulfillmentText':
                'Alright sure, I can help you with your investment history, current market value of your investments or your best/worst performing stocks.'
            }
        elif ((goodbye == 'no'
               )):  #or (currentvalue == 'nope') or (currentvalue == 'not')):
            return {
                'fulfillmentText':
                'Thanks for using Portfolio Bot.Have a good day!.'
            }

    else:
        return {'fulfillmentText': 'Sorry,i am not able to understand this.'}
def get_daily(stock):
    ts = TimeSeries(key=APIkey, output_format='pandas')
    data, meta_data = ts.get_daily(symbol=stock, outputsize='full')
    return data, meta_data
Ejemplo n.º 23
0
 def __init__(self):
     self.ts = TimeSeries('P9HHDBDFZ3RZYTSW', output_format='pandas')
     self.stockDict = {'GOOGL':'Google', 'MSFT':'Microsoft','AAPL':'Apple','FB':'Facebook','TWTR':'Twitter'}
Ejemplo n.º 24
0
from alpha_vantage.timeseries import TimeSeries
import os
# API KEY---------------------------------------------------------
key = "LOK9IWR45QLHXH88"
ts = TimeSeries(key)
#---------------------------------------------------------

# Handling Historical Input------------------------------------------

market = ["DJI", "MSFT"]
marketstart = '09:31:00'
marketstop = '16:00:00'
#USA eastern time by default (GMT -5), 9:30AM opens, 4PM closes, Weekend closes
#if historical is true, it will fetch ALL data past 20 years for every stock in the market
#---------------------------
historical = True
#---------------------------
if historical == True:
    print("fetching historical data")
    print("will fetch the following stocks: {}".format(str(market)))
    for stocks in market:
        data, meta_data = ts.get_daily_adjusted(stocks, outputsize='full')
        f = open(
            "{}\\Input_CSV\\Historical\\{}.txt".format(
                os.path.dirname(__file__), str(stocks)), 'w')

        for days in data:
            currentday = data[days]

            # Use this if viewer friendly text is needed
            #currentline = "0. day: {}\t1. open: {}\t2. high: {}\t3. low: {}\t4. close: {}\t5. adjusted close: {}\t6. volume: {}\t7. dividend amount: {}\t8. split coefficient: {}\n"
Ejemplo n.º 25
0
import os

import stocktrends
import pandas as pd
from alpha_vantage.timeseries import TimeSeries

key = os.environ.get('ALPHAVANTAGE_KEY')
print(key)

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

symbol = 'NSE:RELIANCE'

df, meta_data = ts.get_daily_adjusted(symbol=symbol, outputsize='compact')
renames = {
    '1. open': 'open',
    '2. high': 'high',
    '3. low': 'low',
    '4. close': 'close',
    '5. adjusted close': 'adjusted close',
    '6. volume': 'volume',
    '7. dividend amount': 'dividend amount',
    '8. split coefficient': 'split coefficient'
}
df.rename(columns=renames, inplace=True)

df.to_csv('tmp.csv')
df = pd.read_csv('tmp.csv')
renko = stocktrends.Renko(df)
renko.brick_size = 9.5
r = renko.get_bricks()
Ejemplo n.º 26
0
#print(stocks)
stocks = stocks.to_numpy()
row, column = stocks.shape

#current allowed loss and required profit
loss = np.array(stocks[1:row, 4])
buy = np.array(stocks[1:row, 2])  #price at which bought
profit = np.array(stocks[1:row, 3])

#array for current prices of the stocks
cur = np.array([])

#use alpha_vantage to find current price
api = 'ENTER_YOUR_API'  #to get your API go to alphavantage website create a free account and copy paste the key between quotes
#example: api = 'ASDGWRIGVANVFBGB'
ts = TimeSeries(key=api, output_format='pandas')

i = 1
while i < row:  #find current stock price of each stock in the list
    print(str(stocks[i, 0]))
    data, meta_data = ts.get_intraday(symbol=str(stocks[i, 0]),
                                      interval='1min',
                                      outputsize='full')
    cur = np.append(cur, float(data.iat[0, 3]))
    print(cur)
    i = i + 1
    time.sleep(
        12)  #to ensure 5 calls per minute as max calls are 5 on AlphaVantage

#compare current prices with max allowed loss
i = 0
Ejemplo n.º 27
0
class intradayCrawler(Crawler):


    def __init__(self, config):
        super().__init__(config)
        self.config = config
        self.interval_capabilities = ['1min', '5min', '15min', '30min', '60min']
        self.default_interval = '5min'
        # Check that we have an api key
        if 'api_key' in self.config.keys():
            init_api_key(self.config['api_key'])
        else:
            init_api_key()

        # Verify that it works
        verify_api_key()

        # Check the configuration
        if 'interval' not in self.config:
            self.config['interval'] = '1min'
        if self.config['interval'] not in self.interval_capabilities:
            warnings.warn(f'An interval of {self.config["interval"]} is not supported. Using {self.default_interval}')
            self.config['interval'] = self.default_interval

        if 'symbol' not in self.config:
            raise Exception('No symbol specified for the Crawler')


        self.ts = TimeSeries(output_format='json')

        self.base_url = config['base_url'] if 'base_url' in config else 'http://localhost:8080'

    def __getitem__(self, index):

        data, meta_data = self.ts.get_intraday(symbol= self.config['symbol'],
                                             interval=self.config['interval'],
                                             outputsize='full')

        last_refreshed = time.mktime(datetime.strptime(meta_data['3.Last Refreshed'], "%Y-%m-%d %H:%M:%S").timetuple())

        # Todo: check with date time from db and stop iteration?
        dt_object = datetime.fromtimestamp(last_refreshed)


        new_data = EconDataListRequest(self.compile_data(data))
        return new_data

    def post(self, data):
        if data is None:
            return

        try:
            response = req.post(url = self.base_url + '/api/alphavantage_crawler/insert', data=data.serialize())
            if response.status_code != 200:
                # bad request? 404? duplicates? -- only thing that matters is that it is not OK
                # connectivity is not bad because exception was not raised, so no reschedule
                # that should be a dev-only error. Thus, just notify discord
                raise Exception(f'Got not-OK status code {response.status_code}.')
            # success
        except BaseException as e:
            print(str(e))
            notify('crawler', human_required_update(str(e), 'alphavantage'))
            return

    def __str__(self):
        return "Alpha Vantage"


    def datetime_to_timestamp(self, datetime):
        return time.mktime(datetime.strptime(datetime, "%Y-%m-%d %H:%M:%S").timetuple())

    def compile_data(self, data):
        EconData_list = []
        for timestamp_key, values in data.items():
            EconData_list.append(
                EconData(
                    timestamp= self.datetime_to_timestamp(timestamp_key),
                    equity= self.config['symbol'],
                    open_price= values['open'],
                    close_price= values['close'],
                    high_price= values['high'],
                    low_price= values['low'],
                    volume= values['volume'],
                )
            )
        return EconData_list
Ejemplo n.º 28
0
import pandas as pd
import chart_studio.plotly as py
import plotly.graph_objs as go
import datetime
from alpha_vantage.timeseries import TimeSeries


# TS data acessor
ts = TimeSeries(key='Y9AHX32YU1JIRCEJ',output_format='pandas')
stock_code = 'SQ'

def read_stock(stock_code, interval = '5min'):

    df, _ = ts.get_intraday(symbol= stock_code,

                                    interval=interval,

                                    outputsize='full')

    # trim the df 

    df.rename(index= pd.to_datetime,columns = lambda x : x.split(' ')[-1],inplace = True) 
    return df


# 5-minutes interval
df_5min = read_stock(stock_code)


# 1-minute interval
df_1min = read_stock(stock_code, interval= '1min')
Ejemplo n.º 29
0
def dataimport():
    ###Main program of this python file that imports data from alphavantage,
    ###and exports a csv file with historical data of the listed stocks

    #Import the list of DJIA companies during the 17year span
    #This creates a matrix of dates on top, and then 30 companies in DJIA
    djiacompanies = pd.read_csv(
        '/Users/takuyawakayama/Desktop/Columbia/APMA4903 Seminar/djia-components.csv'
    )

    #Create list of dates that there was an index change
    #type=DatetimeIndex, list of dates of form datetime64[ns]
    switchdates = pd.to_datetime(list(djiacompanies))

    #Lists all companies that we might ever use. type = LIST
    allcompanies = pd.Series(djiacompanies.values.ravel()).unique().tolist()

    #API key for Alphavantage
    apikey = 'AU6S38KFU04HJB8S'
    outputsize = 'full'

    #Initialize dataframe using AAPL as sample company
    ts = TimeSeries(key=apikey, output_format='pandas')
    totaldatadf, meta_data = ts.get_daily_adjusted(symbol='AAPL',
                                                   outputsize=outputsize)
    columns = [
        'low', 'open', 'high', 'close', 'volume', 'split coefficient',
        'dividend amount'
    ]
    totaldatadf.drop(columns, inplace=True, axis=1)
    totaldatadf = totaldatadf.rename(columns={'adjusted close': 'nan'})

    #Create dataframe of historical daily prices for each company ever in DJIA
    for company in allcompanies:

        #Sometimes gives Error 503, so force program to complete
        connected = False
        while not connected:
            try:
                companydatadf, meta_data = ts.get_daily_adjusted(
                    symbol=company, outputsize=outputsize)
                columns = [
                    'low', 'open', 'high', 'close', 'volume',
                    'split coefficient', 'dividend amount'
                ]
                companydatadf.drop(columns, inplace=True, axis=1)
                companydatadf = companydatadf.rename(
                    columns={'adjusted close': company})
                totaldatadf = pd.concat([totaldatadf, companydatadf], axis=1)
                connected = True
            except:
                time.sleep(10)
                pass

    #Delete sample column
    totaldatadf.drop('nan', axis=1, inplace=True)

    #Reorder
    totaldatadf = totaldatadf.iloc[::-1]

    #Export to CSV
    totaldatadf.to_csv("totaldata.csv")
    return
Ejemplo n.º 30
0
class OptimizePortfolio():

    def __init__(self):
        self.ts = TimeSeries('P9HHDBDFZ3RZYTSW', output_format='pandas')
        self.stockDict = {'GOOGL':'Google', 'MSFT':'Microsoft','AAPL':'Apple','FB':'Facebook','TWTR':'Twitter'}

    def loadCurrentPortfolio(self, accountDetails):
        stocks = ['GOOGL','MSFT','TWTR','FB','AAPL']
        # currentEvaluation = 0
        # for s in stocks:
        #     price = self.getLiveStockRate(s)
        #     alloc = accountDetails[s][0]
        #     currentEvaluation += alloc * 10 * price
        currentEvaluation = "Thank you! The current portfolio allocations are as follows:"
        for s in stocks:
            if accountDetails[s][0] > 0.0:
                currentEvaluation += " " + str(accountDetails[s][0] * 100) + "% stocks of " + self.stockDict.get(s) + ","
        currentEvaluation = currentEvaluation.strip(',')
        return currentEvaluation

    def getLiveStockRate(self, stockCode):
        df = self.ts.get_intraday(stockCode, interval='1min', outputsize='compact')
        df = df[0]['4. close']
        return df[len(df)-1]

    def getDailyStockRate(self, stockCode):
        # df,_ = self.ts.get_daily(stockCode, outputsize='full')
        df = pd.read_csv(os.getcwd() + "/Data/daily_"+stockCode+".csv", index_col='date')
        df = df.rename(columns={'1. open': 'Open', '2. high':'High', '3. low':'Low', '4. close':'Close', '5. volume':'Volume'})
        return df

    def optimizePortfolio(self, accountNumber, loadPortfolioDataObject):
        print(accountNumber)
        accountDetails = loadPortfolioDataObject.getAccountDetails(accountNumber)
        stocks = ['GOOGL','MSFT','TWTR','FB','AAPL']
        stockAllocations = []
        dfList = []
        for s in stocks:
            dfList.append(self.getDailyStockRate(s))
            stockAllocations.append((s, accountDetails[s][0]))
        
        companyAllocations = OrderedDict(stockAllocations)
        startDate = '2019-01-01'
        endDate = datetime.today().strftime('%Y-%m-%d')
        dates = pd.date_range(startDate, endDate)
        newdf = pd.DataFrame(index = dates)
        for id, df in enumerate(dfList):
            df.drop(['Open', 'High', 'Low', 'Volume'], axis=1, inplace=True)
            df = df.rename(columns={'Close':stocks[id]})
            newdf = newdf.join(df)
        
        newdf = newdf.dropna()
        afterdf = newdf
        norm = newdf/newdf.iloc[0]
        norm = norm.astype(float)
        for s in stocks:
            norm[s] = norm[s].astype(float)
            norm[s] *= float(companyAllocations.get(s)*accountDetails['TOTAL'][0])
        
        evaluation = norm.sum(axis=1)
        before, beforeSharpeRatio, beforePortValue = self.printStats(evaluation)

        baseAllocation = []
        for _, value in companyAllocations.items():
            baseAllocation.append(value)
        baseAllocation = np.array(baseAllocation)
        self.currentValue = accountDetails['TOTAL'][0]
        alloc = minimize(self.calculateSharpeRatio, baseAllocation, args=(afterdf,),method='SLSQP',
        bounds=((0,1),(0,1),(0,1),(0,1),(0,1)),constraints=({'type':'eq','fun':lambda inputs: 1.0-np.sum(inputs)}))

        dates = pd.date_range(startDate, endDate)
        newdf = pd.DataFrame(index = dates)
        for id, df in enumerate(dfList):
            df = df.rename(columns={'Close':stocks[id]})
            newdf = newdf.join(df)
        newdf = newdf.dropna()
        norm = newdf/newdf.iloc[0]
        norm = norm.astype(float)
        for id, s in enumerate(stocks):
            norm[s] = norm[s].astype(float)
            norm[s] *= float(alloc.x[id]*accountDetails['TOTAL'][0])
        evaluationNew = norm.sum(axis=1)
        after, afterSharpeRatio, afterPortValue = self.printStats(evaluationNew)

        s1 = "The current portfolio allocations are as follows:"
        for s in stocks:
            if accountDetails[s][0] > 0.0:
                s1 += " " + str(accountDetails[s][0] * 100) + "% stocks of " + self.stockDict.get(s) + ","
        s1 = s1.strip(',')
        s1 += ". Based on these allocations, the current portfolio returns are as follows. " + before
        s1 +=  ". However, on optimizing it, the new portfolio returns are as follows. " + after
        s1 += ". These new returns can be achieved if portfolio allocations change to the following:"
        for id,s in enumerate(stocks):
            if alloc.x[id] > 0.0:
                s1 += " " + str(alloc.x[id] * 100) + "% stocks of " + self.stockDict.get(s) + ","
        s1 = s1.strip(',')
        if afterPortValue > beforePortValue:
            s1 += ". The optimized portfolio is $" + str(afterPortValue - beforePortValue) + " higher than original portfolio."
        else:
            s1 += ". The optimized portfolio is $" + str(beforePortValue - afterPortValue) + " less than original portfolio as original portfolio had higher risks and the optimization process tries to balance the risk involved."
        s1 += " Kindly drop by our office, if you want to update your portfolio allocations. Kindly type quit to exit or type Hello to continue chatting with me."
        return s1

    def printStats(self, evaluation):
        cumulativeReturn = (evaluation[-1]/evaluation[0])-1

        dailyReturn = evaluation.copy()
        dailyReturn[1:] = (evaluation[1:]/evaluation[:-1].values)-1
        dailyReturn = dailyReturn[1:]

        dailyReturnAverage = dailyReturn.mean()

        dailyReturnStd = dailyReturn.std()

        sharpeRatio = dailyReturnAverage/dailyReturnStd
        sharpeRatio = np.sqrt(len(evaluation))*sharpeRatio

        portValue = evaluation.iloc[len(evaluation)-1]
        
        s1 = "Cumulative Return is $" + str(cumulativeReturn)
        s1 = s1 + ". Daily Return mean is $" + str(dailyReturnAverage)
        s1 = s1 + ". Volatility or Standard Deviation is " + str(dailyReturnStd)
        s1 = s1 + ". Sharpe Ratio is " + str(sharpeRatio)
        s1 = s1 + ". Portfolio evaluation at the end of the term is $" + str(portValue) + "."
        
        return s1, sharpeRatio, portValue

    def calculateSharpeRatio(self, allocs, afterdf):
        norm = afterdf/afterdf.iloc[0]
        allocated = norm * allocs
        newValue = allocated * self.currentValue
        evaluationNew = newValue.sum(axis=1)
        
        dailyReturn = evaluationNew.copy()
        dailyReturn[1:] = (evaluationNew[1:]/evaluationNew[:-1].values)-1
        dailyReturn = dailyReturn[1:]
        sharpeRatio = dailyReturn.mean()/dailyReturn.std()
        sharpeRatio = np.sqrt(len(evaluationNew))*sharpeRatio
        return -1*sharpeRatio
Ejemplo n.º 31
0
# to load needed modules
import quandl  # financial data source
import pandas as pd  # for dataframe setup and csv loading
from datetime import datetime  # timse series: date convention
import matplotlib.pyplot as plt  # module for plotting
import os  # for input/output of files/directories
import plotly.offline as pyo
import plotly.graph_objs as go
from alpha_vantage.timeseries import TimeSeries

TICKER = 'EOD/AMZN'  # use either EOD or XNAS(datasets) / then ticker
TICKER2 = TICKER[-4:]  # change according to number of ticker characters
symbol = TICKER2
os.chdir(r'D:\Users\jong\PyProjects\Stocks\US Stocks')
key = open('alphavantage.txt', 'r').read()
ts = TimeSeries(key=key, output_format='pandas')
data = data, meta_data = ts.get_daily_adjusted(symbol=symbol,
                                               outputsize='full')
data = data.reset_index()
data['date'] = pd.to_datetime(data['date'])
data.set_index('date', inplace=True)
print(data.tail())

x = data.index
dfA = data['4. close']
y1 = dfA

Product_A = [
    go.Scatter(
        x=x,
        y=y1,
Ejemplo n.º 32
0
from alpha_vantage.timeseries import TimeSeries
import datetime,time
ts = TimeSeries(key='25JBS8HQYNAQ0EIU', output_format='pandas')


def get_stock_data_1min(symbol):
    print(datetime.datetime.now())
    data, meta_data = ts.get_intraday(symbol=symbol,interval='1min')
    print(datetime.datetime.now())
    print(data)

if __name__ == "__main__":
    symbol="7974.T"
    time.sleep(60*60*3)

    i=0
    while True:
        now = datetime.datetime.now()
        if now.second==0 or now.second==30 :
            get_stock_data_1min(symbol)
            time.sleep(2)
        i += 1
        time.sleep(0.9)
        # print(i)

        if i==100000:
            break

    
    
Ejemplo n.º 33
0
User = '******'
PassWord = '******'
Host = '127.0.0.1'
Port = '3306'
Database = 'SEProject'
api_key = 'EQ6GGWD5D4ME4283'

# define database engines
sqlite_engine = create_engine('sqlite:///database_stock.db',
                              convert_unicode=True,
                              echo=True)
# MYSQL_engine = create_engine(
#     'mysql+mysqlconnector://' + User + ':' + PassWord +
#     '@' + Host + ':' + Port + '/' + Database, echo=False)
# get TimeSeries/TechIndicator object of Alpha Vantage API
ts = TimeSeries(key=api_key, output_format='pandas', retries=20)
ti = TechIndicators(key=api_key, output_format='pandas', retries=20)

# using alpha vantage finance api to save data into a pandas dataframe
stocks = [
    'AAPL', 'GOOGL', 'NVDA', 'AABA', 'AMZN', 'MSFT', 'BAC', 'NKE', 'NFLX', 'FB'
]


def init_db():
    # initialize database and create schema if using mysql, if using sqlite it's not necessary
    try:
        cnx = mysql.connector.connect(
            user=User, password=PassWord,
            host=Host)  # using configuration of sever
    except mysql.connector.Error:
Ejemplo n.º 34
0
# https://github.com/RomelTorres/alpha_vantage
from alpha_vantage.timeseries import TimeSeries
from pprint import pprint
ts = TimeSeries(key='Y3086XYRBVO5KMFD', output_format='pandas')
data, meta_data = ts.get_intraday('AAPL', interval='1min', outputsize='full')
pprint(data.head(5))
Ejemplo n.º 35
0
from alpha_vantage.timeseries import TimeSeries
from download_stock_av import api_key


ts = TimeSeries(key=api_key.KEY, output_format='pandas')
df, meta_data = ts.get_daily(symbol='VIG.VI', outputsize='compact')

# print(data)


print(df.loc[['2017-11-28']])