dbutils.library.restartPython()
dbutils.library.list()

# COMMAND ----------

import yfinance as data
import matplotlib.pyplot as plt
import pandas as pd
import datetime

start_date = '2014-08-31'
end_date = '2019-08-31'

from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override(
)  # <== that's all it takes :-) #data = yf.download("SPY AAPL MSFT", start="2018-01-01", end="2018-04-30",group_by="ticker")
# download dataframe and Use pandas_reader.data.DataReader to load the desired data. As simple as that.
appleData = pdr.get_data_yahoo("AAPL", start_date, end_date, group_by="ticker")
#spyData = pdr.get_data_yahoo("SPY",start_date, end_date,group_by="ticker")

# COMMAND ----------

appleData.head(5)

# COMMAND ----------

#appleData.shape[1]
#appleData.shape[1]
#appleData.iloc[0][1]
appleData.head(5)
appleData.describe
Beispiel #2
0
def prediction():
    #db연동
    stock_db = pymysql.connect(
        host='127.0.0.1',
        user='******',
        passwd='',  #자신비밀번호
        db='stock',
        charset='utf8')
    cursor = stock_db.cursor(pymysql.cursors.DictCursor)
    sql = "SELECT * FROM stock_item;"  #select쿼리
    cursor.execute(sql)
    result = cursor.fetchall()  #원래코드
    #result = [cursor.fetchone()]#한개만 가져옴#test용
    print(result)

    for i in result:
        stock_name = i['stock_name']
        tf.reset_default_graph()

        yf.pdr_override()
        tf.set_random_seed(777)

        start_date = '2010-01-01'
        name = i['stock_code']
        stock_code = name
        stock = data.get_data_yahoo(name, start_date)
        stock = stock[:-1]  #stock:주가 데이터들 저장

        # 정규화
        def min_max_scaling(x):
            x_np = np.asarray(x)
            return (x_np - x_np.min()) / (x_np.max() - x_np.min() + 1e-7
                                          )  # 1e-7은 0으로 나누는 오류 예방차

        # 역정규화 : 정규화된 값을 원래의 값으로 되돌림
        def reverse_min_max_scaling(org_x, x):  # 종가 예측값
            org_x_np = np.asarray(org_x)
            x_np = np.asarray(x)
            return (x_np *
                    (org_x_np.max() - org_x_np.min() + 1e-7)) + org_x_np.min()

        input_dcm_cnt = 6  #입력데이터의 컬럼 개수
        output_dcm_cnt = 1  #결과데이터의 컬럼 개수

        seq_length = 28  #1개 시퀸스의 길이(시계열데이터 입력 개수)
        rnn_cell_hidden_dim = 20  #각 셀의 히든 출력 크기
        forget_bias = 1.0  #망각편향(기본값 1.0)
        num_stacked_layers = 1  #Stacked LSTM Layers 개수
        keep_prob = 1.0  #Dropout 할때 Keep할 비율

        epoch_num = 1000  #에포크 횟수 (몇회 반복 학습)
        learning_rate = 0.01  #학습률

        stock_info = stock.values[1:].astype(np.float)

        price = stock_info[:, :-1]  # <- here
        norm_price = min_max_scaling(price)

        volume = stock_info[:, -1:]  # <- here
        norm_volume = min_max_scaling(volume)

        x = np.concatenate((norm_price, norm_volume), axis=1)
        y = x[:, [-2]]
        dataX = []
        dataY = []

        for i in range(0, len(y) - seq_length):
            _x = x[i:i + seq_length]
            _y = y[i + seq_length]
            dataX.append(_x)
            dataY.append(_y)
        train_size = int(len(dataY) * 0.7)
        test_size = len(dataY) - train_size

        train_size = int(len(dataY) * 0.7)
        test_size = len(dataY) - train_size

        trainX = np.array(dataX[0:train_size])
        trainY = np.array(dataY[0:train_size])

        testX = np.array(dataX[train_size:len(dataX)])
        testY = np.array(dataY[train_size:len(dataY)])

        X = tf.placeholder(tf.float32, [None, seq_length, input_dcm_cnt])
        Y = tf.placeholder(tf.float32, [None, 1])

        targets = tf.placeholder(tf.float32, [None, 1])
        predictions = tf.placeholder(tf.float32, [None, 1])

        def lstm_cell():
            cell = tf.contrib.rnn.BasicLSTMCell(num_units=rnn_cell_hidden_dim,
                                                forget_bias=forget_bias,
                                                state_is_tuple=True,
                                                activation=tf.nn.softsign)
            if keep_prob < 1.0:
                cell = tf.contrib.rnn.DropoutWrapper(
                    cell, output_keep_prob=keep_prob)
            return cell

        stackedRNNs = [lstm_cell() for _ in range(num_stacked_layers)
                       ]  #Stacked LSTM Layers 개수 1
        multi_cells = tf.contrib.rnn.MultiRNNCell(
            stackedRNNs,
            state_is_tuple=True) if num_stacked_layers > 1 else lstm_cell()

        hypothesis, _states = tf.nn.dynamic_rnn(multi_cells,
                                                X,
                                                dtype=tf.float32)
        hypothesis = tf.contrib.layers.fully_connected(
            hypothesis[:, -1], output_dcm_cnt, activation_fn=tf.identity)

        loss = tf.reduce_sum(tf.square(hypothesis - Y))
        optimizer = tf.train.AdamOptimizer(learning_rate)
        train = optimizer.minimize(loss)

        rmse = tf.sqrt(
            tf.reduce_mean(tf.squared_difference(targets, predictions)))

        train_error_summary = []
        test_error_summary = []
        test_predict = ''

        sess = tf.Session()
        sess.run(tf.global_variables_initializer())

        #학습
        start_time = datetime.datetime.now()
        print('학습 시작...')

        for epoch in range(epoch_num):
            _, _loss = sess.run([train, loss],
                                feed_dict={
                                    X: trainX,
                                    Y: trainY
                                })
            if ((epoch + 1) % 100 == 0) or (epoch == epoch_num -
                                            1):  # 100번째마다 또는 마지막 epoch인 경우
                # 학습용데이터로 rmse오차를 구한다
                train_predict = sess.run(hypothesis, feed_dict={X: trainX})
                train_error = sess.run(rmse,
                                       feed_dict={
                                           targets: trainY,
                                           predictions: train_predict
                                       })
                train_error_summary.append(train_error)

                # 테스트용데이터로 rmse오차를 구한다
                test_predict = sess.run(hypothesis, feed_dict={X: testX})
                test_error = sess.run(rmse,
                                      feed_dict={
                                          targets: testY,
                                          predictions: test_predict
                                      })
                test_error_summary.append(test_error)
                print(
                    "epoch: {}, train_error(A): {}, test_error(B): {}, B-A: {}"
                    .format(epoch + 1, train_error, test_error,
                            test_error - train_error))

        end_time = datetime.datetime.now()  #종료시간을 기록
        elapsed_time = end_time - start_time  # 경과시간을 구한다

        # 결과 그래프 출력
        plt.figure(1)
        plt.plot(train_error_summary, 'gold')
        plt.plot(test_error_summary, 'b')
        plt.xlabel('Epoch(x100)')
        plt.ylabel('Root Mean Square Error')

        plt.figure(2)
        plt.plot(testY, 'r')
        plt.plot(test_predict, 'b')
        plt.xlabel('Time Period')
        plt.ylabel('Stock Price')
        #plt.show()

        recent_data = np.array([x[len(x) - seq_length:]])

        pre_price = int(price[-1][-1])

        test_predict = sess.run(hypothesis, feed_dict={X: recent_data})

        test_predict = reverse_min_max_scaling(price, test_predict)
        tom_price = int(test_predict)

        stock_percent = (
            (test_predict[0] - price[-1][-1]) / price[-1][-1]) * 100
        stock_percent = round(float(stock_percent), 3)

        #db에 예측결과들을 저장함
        sql = 'REPLACE INTO stock_info (name, code, pre_price, tom_price, percent) VALUES (%s, %s, %s, %s, %s);'  #insert쿼리

        cursor.execute(sql, (stock_name, stock_code, pre_price, tom_price,
                             stock_percent))  #쿼리실행
        stock_db.commit()  #db에 반영

    stock_db.close()
# Imports
from pandas_datareader import data as pdr
from yahoo_fin import stock_info as si
from pandas import ExcelWriter
import yfinance as yf
import pandas as pd
import datetime
import time

yf.pdr_override()

# Variables
tickers = si.tickers_sp500()
tickers = [item.replace(".", "-")
           for item in tickers]  # Yahoo Finance uses dashes instead of dots
index_name = '^GSPC'  # S&P 500
start_date = datetime.datetime.now() - datetime.timedelta(days=365)
end_date = datetime.date.today()
exportList = pd.DataFrame(columns=[
    'Stock', "RS_Rating", "50 Day MA", "150 Day Ma", "200 Day MA",
    "52 Week Low", "52 week High"
])
returns_multiples = []

# Index Returns
index_df = pdr.get_data_yahoo(index_name, start_date, end_date)
index_df['Percent Change'] = index_df['Adj Close'].pct_change()
index_return = (index_df['Percent Change'] + 1).cumprod()[-1]

# Find top 30% performing stocks (relative to the S&P 500)
for ticker in tickers:
Beispiel #4
0
def my_view(request, *args, **kwargs):
    yf.pdr_override()
    utc = pytz.utc
    utc_dt = dt.datetime.now()
    # eastern = pytz.timezone('US/Eastern')
    # loc_dt = utc_dt.astimezone(eastern)
    # fmt = '%Y-%m-%d %H:%M:%S %Z%z'
    # loc_dt.strftime(fmt)
    day = dt.timedelta(400)
    start = utc_dt - day

    url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
    # Create a handle, page, to handle the contents of the website
    page = requests.get(url)
    # Store the contents of the website under doc
    doc = lh.fromstring(page.content)
    # Parse data that are stored between <tr>..</tr> of HTML
    tr_elements = doc.xpath('//tr')
    # Create empty list
    col = []
    i = 0
    # For each row, store each first element (header) and an empty list
    for t in tr_elements[1]:
        i += 1
        name = t.text_content()
        col.append((name, []))
    for j in range(2, len(tr_elements)):
        # T is our j'th row
        T = tr_elements[j]

        # If row is not of size 10, the //tr data is not from our table
        if len(T) != 9:
            break

        # i is the index of our column
        i = 0

        # Iterate through each element of the row
        for t in T.iterchildren():
            data = t.text_content()

            # Check if row is empty
            if i > 0:
                # Convert any numerical value to integers
                try:
                    data = int(data)
                except:
                    pass
            # Append the data to the empty list of the i'th column
            col[i][1].append(data)
            # Increment i for the next column
            i += 1
    [len(C) for (title, C) in col]
    Dict = {title: column for (title, column) in col}
    df = pd.DataFrame(Dict)
    col_one_list = df["Symbol\n"].tolist()
    splist = []

    for element in col_one_list:
        splist.append(element.strip())
    for i in range(len(splist)):
        if '.' in splist[i]:
            splist[i] = splist[i].replace(".", "-")
    count = 0
    count1 = 0
    for i in range(len(splist)):
        stock = splist[i]
        print(stock)
        print(start, utc_dt)
        df = pdr.get_data_yahoo(stock, start, utc_dt)
        isempty = df.empty
        if (isempty != True):
            df['200 dma'] = df['Adj Close'].rolling(200).mean().round(5)
            df['50 dma'] = df['Adj Close'].rolling(50).mean().round(5)
            df['over200'] = df['Adj Close'] > df[
                '200 dma']  # Check if close > 200 day moving avg across all dates
            df['over50'] = df['Adj Close'] > df[
                '50 dma']  # Check if close > 50 day moving avg across all dates
            if (df['over200'].iloc[-1] == True):
                count = count + 1  # check if the last 'over200' value is True. If so, increase count
            if (df['over50'].iloc[-1] == True):
                count1 = count1 + 1  # check if the last 'over50' value is True. If so, increase count

    print(count)
    print(len(splist))
    print(count1)
    SPabove200 = round((float(count) / float(len(splist))) * 100, 1)
    print(count)
    print(float(count) / float(len(splist)))
    print(SPabove200)
    SPabove50 = round((float(count1) / float(len(splist))) * 100, 1)

    stock1 = '^GSPC'
    today = dt.datetime.now()

    yesterday = today - dt.timedelta(days=1)

    df = yf.download(stock1, period="1d", interval="1d")
    fdtest = df.tail(1)
    sp = fdtest.index[0]
    yesterday1 = sp.strftime("%m/%d/%Y")
    sp500_ret = round(fdtest['Adj Close'].iloc[-1], 2)
    #sheetlastdate = sheet200.cell(curentrow200 - 1, 1).value
    # yesterday1 = yesterday.strftime("%m/%d/%Y")
    row200 = [yesterday1, sp500_ret, SPabove200]
    row50 = [yesterday1, sp500_ret, SPabove50]
    date = pd.to_datetime(yesterday1, infer_datetime_format=True)
    #if (yesterday1 != sheetlastdate):
    #SpNasdaqDmaData.objects.create(date=yesterday1, sp500=sp500_ret, dma50=SPabove200)
    #SpNasdaqDmaData.objects.create(date = date, sp500 = sp500_ret, dma50 = SPabove50)

    #sp500 = 336
    #dma50 = 54.02

    # Iterate through all the data items
    #for i in range(len(date)):

    # Insert in the database
    #SpNasdaqDmaData.objects.create(date = date, sp500 = sp500, dma50 = dma50)

    # Getting all the stuff from database
    query_results = SpNasdaqDmaData.objects.all()

    # Creating a dictionary to pass as an argument
    context = {'query_results': query_results}
    print("Database Updated")
    # Returning the rendered html
    return render(request, "home.html", context)
Beispiel #5
0
#!/usr/bin/env python3
import os
import pandas_datareader as pdr
import yfinance as fix
import dataMiner as D

err_arrsy = []

fix.pdr_override()


def loadfile(ticker_type, ticker, datapath, startdate, enddate):
    """
    :param ticker_type:
    :param ticker:
    :param datapath:
    :param startdate:
    :param enddate:
    :return:
    """

    err_arrsy = []

    if ticker_type == 'en':
        print("Load data from Yahoo, ticker:", ticker)
        print("From %s date" % startdate)
        print("Till %s date" % enddate)

        try:
            __raw_data = pdr.get_data_yahoo(ticker, startdate, enddate)
            __write_to_file(ticker, __raw_data)
import yfinance as yf
import pandas as pd
from pandas_datareader import data
from datetime import datetime

yf.pdr_override()  #以pandasreader常用的格式覆寫

target_stock = 'TLSA'  #股票代號變數

start_date = datetime(2010, 1, 1)
end_date = datetime(2020, 6, 30)  #設定資料起訖日期

df = data.get_data_yahoo([target_stock], start_date,
                         end_date)  #將資料放到Dataframe裡面

filename = f'./data/{target_stock}.csv'  #以股票名稱命名檔案,放在data資料夾下面

df.to_csv(filename)  #將df轉成CSV保存
Beispiel #7
0
def loadPrice(tokens):
    import pandas_datareader.data as web
    import yfinance as yf
    yf.pdr_override()
    print('Carregando cotações')
    return web.get_data_yahoo(tokens)['Adj Close'].iloc[-1]
import requests
import bs4
# Importing data management packages:
import pandas as pd
import pandas_datareader as pdr
import yfinance as yf
import datetime
from datetime import timedelta
import time
import numpy as np
# Importing data vizualization packages:
import matplotlib.pyplot as plt
import seaborn as sns

sns.set() # Setting all charts to seaborn style
yf.pdr_override() # overiding pdr with yfinance packages


class Security(object):
    '''
    The Security object contains descriptive variables for each Security instance as well
    as the methods to retrive said variables from the web.
    Data is collected via the pandas_datareader package augmented with the yfinance
    package to allow data to be pulled directly from yahoo finance.
    If the Security is an Exchange Traded fund then holdings data is pulled from
    etfdb.com
    Parameters
    ----------
    ticker : str
        The string variable representing the ticker symbol for the Security. This
        string is the argument that is passed to all the data aggregation methods
Beispiel #9
0
def lstm(symbol, start_date, end_date):
    yf.pdr_override()

    df_X = data.DataReader(symbol,
                           start=start_date,
                           end=end_date,
                           data_source='yahoo')
    df_y = df_X[:1500]['Close']
    df_X.drop(df_X.columns[len(df_X.columns) - 1], axis=1, inplace=True)
    df_X['TradeDate'] = df_X.index
    #fig = df_X.plot(x='TradeDate', y='Close', kind='line', figsize=(20,6), rot=20)
    #st.pyplot(fig)

    # Extracting the closing prices of each day
    FullData = df_X[['Close']].values
    #print(FullData[0:5])

    # Feature Scaling for fast training of neural networks
    from sklearn.preprocessing import StandardScaler, MinMaxScaler

    # Choosing between Standardization or normalization
    #sc = StandardScaler()
    sc = MinMaxScaler()

    DataScaler = sc.fit(FullData)
    X = DataScaler.transform(FullData)

    # split into samples
    X_samples = list()
    y_samples = list()

    NumerOfRows = len(X)
    TimeSteps = 10  # next day's Price Prediction is based on last how many past day's prices

    # Iterate thru the values to create combinations
    for i in range(TimeSteps, NumerOfRows, 1):
        x_sample = X[i - TimeSteps:i]
        y_sample = X[i]
        X_samples.append(x_sample)
        y_samples.append(y_sample)

    ################################################
    # Reshape the Input as a 3D (number of samples, Time Steps, Features)
    X_data = np.array(X_samples)
    X_data = X_data.reshape(X_data.shape[0], X_data.shape[1], 1)
    #print('\n#### Input Data shape ####')
    #print(X_data.shape)

    # We do not reshape y as a 3D data  as it is supposed to be a single column only
    y_data = np.array(y_samples)
    y_data = y_data.reshape(y_data.shape[0], 1)

    # Choosing the number of testing data records
    TestingRecords = 5

    # Splitting the data into train and test
    X_train = X_data[:-TestingRecords]
    X_test = X_data[-TestingRecords:]
    y_train = y_data[:-TestingRecords]
    y_test = y_data[-TestingRecords:]

    # Defining Input shapes for LSTM
    TimeSteps = X_train.shape[1]
    TotalFeatures = X_train.shape[2]

    # Importing the Keras libraries and packages
    from keras.models import Sequential
    from keras.layers import Dense
    from keras.layers import LSTM

    # Initialising the RNN
    regressor = Sequential()

    # Adding the First input hidden layer and the LSTM layer
    # return_sequences = True, means the output of every time step to be shared with hidden next layer
    regressor.add(
        LSTM(units=10,
             activation='relu',
             input_shape=(TimeSteps, TotalFeatures),
             return_sequences=True))

    # Adding the Second Second hidden layer and the LSTM layer
    regressor.add(
        LSTM(units=5,
             activation='relu',
             input_shape=(TimeSteps, TotalFeatures),
             return_sequences=True))

    # Adding the Second Third hidden layer and the LSTM layer
    regressor.add(LSTM(units=5, activation='relu', return_sequences=False))

    # Adding the output layer
    regressor.add(Dense(units=1))

    # Compiling the RNN
    regressor.compile(optimizer='adam', loss='mean_squared_error')

    ##################################################

    import time
    # Measuring the time taken by the model to train
    StartTime = time.time()

    # Fitting the RNN to the Training set
    regressor.fit(X_train, y_train, batch_size=5, epochs=100)

    EndTime = time.time()
    st.write("## Total Time Taken: ", round((EndTime - StartTime) / 60),
             'Minutes ##')

    # Making predictions on test data
    predicted_Price = regressor.predict(X_test)
    predicted_Price = DataScaler.inverse_transform(predicted_Price)

    # Getting the original price values for testing data
    orig = y_test
    orig = DataScaler.inverse_transform(y_test)

    # Accuracy of the predictions
    st.write('Accuracy:',
             100 - (100 * (abs(orig - predicted_Price) / orig)).mean())

    # Visualising the results
    import matplotlib.pyplot as plt

    plt.plot(predicted_Price, color='blue', label='Predicted Volume')
    plt.plot(orig, color='lightblue', label='Original Volume')

    plt.title('Stock Price Predictions')
    plt.xlabel('Trading Date')
    plt.xticks(range(TestingRecords), df_X.tail(TestingRecords)['TradeDate'])
    plt.ylabel('Stock Price')

    plt.legend()
    fig1 = plt.gcf()
    fig1.set_figwidth(20)
    fig1.set_figheight(6)
    st.pyplot(fig1)
    #fig1.savefig("predit5.png")
    #from PIL import Image
    #image = Image.open('predit5.png')

    #st.image(image, caption='Predition on Testing Set')

    # Generating predictions on full data
    TrainPredictions = DataScaler.inverse_transform(regressor.predict(X_train))
    TestPredictions = DataScaler.inverse_transform(regressor.predict(X_test))

    FullDataPredictions = np.append(TrainPredictions, TestPredictions)
    FullDataOrig = FullData[TimeSteps:]

    # plotting the full data
    plt.plot(FullDataPredictions, color='blue', label='Predicted Price')
    plt.plot(FullDataOrig, color='lightblue', label='Original Price')
    plt.title('Stock Price Predictions')
    plt.xlabel('Trading Date')
    plt.ylabel('Stock Price')
    plt.legend()
    fig = plt.gcf()
    fig.set_figwidth(20)
    fig.set_figheight(8)
    st.pyplot(fig)
    #plt.show()
    #fig.savefig("preditfull.png")
    #from PIL import Image
    #image = Image.open('preditfull.png')
    #st.image(image, caption='Predition for the whole period')

    # Last 10 days prices
    Last10Days = df_X['Close'].tail(10).to_numpy()

    # Normalizing the data just like we did for training the model
    Last10Days = DataScaler.transform(Last10Days.reshape(-1, 1))

    # Changing the shape of the data to 3D
    # Choosing TimeSteps as 10 because we have used the same for training
    NumSamples = 1
    TimeSteps = 10
    NumFeatures = 1
    Last10Days = Last10Days.reshape(NumSamples, TimeSteps, NumFeatures)

    #############################

    # Making predictions on data
    import sys
    predicted_Price = regressor.predict(Last10Days)
    predicted_Price = DataScaler.inverse_transform(predicted_Price)
    #final_predit = np.savetxt(sys.stdout, predicted_Price, fmt="%.3f")
    final_predit = "".join(str(x) for x in predicted_Price)
    st.write("The next day price prediction for our LSTM Model is: ",
             final_predit)
Beispiel #10
0
    def get(self, request):
        messages.success(request, 'Please wait')
        yf.pdr_override()
        sp_500 = pd.read_html(
            'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
        )[0].filter(['Symbol', 'Security'], axis=1)
        active_df = pd.read_html('https://finance.yahoo.com/most-active')[0]
        active_tickers = active_df['Symbol'].to_list()
        active_df = active_df.set_index('Symbol')
        active_stats = active_df.to_dict()
        stock_stats = {}
        stock_screen = {}
        for row in sp_500.itertuples():
            stock_stats[row[1]] = {}
        # active_tickers = active_tickers[0:5]
        start = dt.datetime.now() - dt.timedelta(days=20)
        now = dt.datetime.now()
        for sym in active_tickers:
            try:
                print(f"Analysis for {sym}")
                # time.sleep(5)
                stock_screen[sym] = {}
                stock_screen[sym]['Name'] = active_stats['Name'][sym]
                stock_screen[sym]['Ticker'] = sym
                stock_screen[sym]['% Change'] = round(
                    float(active_stats['% Change'][sym].replace(
                        '%', '').replace('+', '')), 2)
                stock_screen[sym]['Price (Intraday)'] = round(
                    float(active_stats['Price (Intraday)'][sym]), 2)
                stock_screen[sym]['Market Cap'] = active_stats['Market Cap'][
                    sym]
                if sym in stock_stats.keys():
                    stock_screen[sym]['SP500'] = 'YES'
                else:
                    stock_screen[sym]['SP500'] = 'NO'

                stock_screen[sym]['TREND'], stock_screen[sym][
                    'VOL_TREND'], stock_screen[sym][
                        'price_per_change'], stock_screen[sym][
                            'vol_per_change'] = get_trend(sym, start, now)

                urls = [
                    'https://sg.finance.yahoo.com/quote/{0}/key-statistics?p={0}',
                    'https://finance.yahoo.com/quote/{0}?p={0}'
                ]
                for url in urls:
                    stock_url = url.format(sym)
                    df_list = pd.read_html(stock_url)
                    for df in df_list:
                        for item in df.to_dict('records'):
                            stock_screen[sym][item[0]] = item[1]

                stock_screen[sym]['Volume'] = int(stock_screen[sym]['Volume'])
                stock_screen[sym]['%50MVA'] = get_per_value(
                    stock_screen[sym]['Price (Intraday)'],
                    stock_screen[sym]['50-day moving average 3'])
                stock_screen[sym]['%200MVA'] = get_per_value(
                    stock_screen[sym]['Price (Intraday)'],
                    stock_screen[sym]['200-day moving average 3'])
                stock_screen[sym]['%52LOW'] = get_per_value(
                    stock_screen[sym]['Price (Intraday)'],
                    stock_screen[sym]['52-week low 3'])
                stock_screen[sym]['%52HIGH'] = get_per_value(
                    stock_screen[sym]['Price (Intraday)'],
                    stock_screen[sym]['52-week high 3'])
            except:
                print(f"exception for {sym}")
                print(
                    f"{sys.exc_info()[0]},{sys.exc_info()[1]},{traceback.format_tb(sys.exc_info()[2])} "
                )
                del stock_screen[sym]
                continue

        return render(
            request, 'stocks/active.html',
            dump_to_db(StockTicker, stock_screen, "stocks_stockticker"))
Beispiel #11
0
import datetime
import pandas as pd
from pandas import DataFrame
import numpy as np
import math
import matplotlib.pyplot as plt  # Biblioteca para gerar os gráficos - https://matplotlib.org/

###################################
# TRACKER
###################################

#Definir data de inicio e fim
start_sp = datetime.datetime(2000, 7, 12)
end_sp = datetime.datetime(2019, 7, 12)

yf.pdr_override()  # arredondando dados para duas casas decimais

# Definindo ativo para gerar base de dados
sp500 = pdr.get_data_yahoo('SPY', start_sp, end_sp)

df = DataFrame(sp500)  # Definindo DataFrame

#########################################
# Calculate Relative Strength Index(RSI) #
#########################################

n = 9
prices = df.iloc[:, 3]

deltas = np.diff(prices.T)
seed = deltas[:n + 1]
Beispiel #12
0
import yfinance as yf
yf.pdr_override()  # Fix for yahoo finance


def deadCat_search(stock):
    rating = 1
    status = False
    up_percentage = 20
    down_percentage = -5

    ticker = yf.Ticker(stock)
    hist = ticker.history(period='7d', interval='1d')

    hist['Percent Change'] = hist['Close'].pct_change()
    # stock_return = hist['Percent Change'].sum() * 100
    # try:
    #     if ( (hist['Percent Change'][-3] > up_percentage or hist['Percent Change'][-2] > up_percentage) and
    #             (hist['Percent Change'][-2] < down_percentage or hist['Percent Change'][-1] < down_percentage)):
    #         status = True
    # except Exception as e:
    #     print(e)
    #     print("deadCat_search - No data on " + stock)

    return stock, status, 'Dead Cat', rating
Beispiel #13
0
def plot():
    from pandas_datareader import data
    import yfinance as yf
    import datetime as dt
    from bokeh.plotting import figure, show, output_file
    from bokeh.embed import components
    from bokeh.resources import CDN, INLINE

    yf.pdr_override()

    start = dt.datetime(2019, 11, 2)
    end = dt.datetime(2020, 8, 11)
    hours_12 = 12 * 60 * 60 * 1000

    df = data.get_data_yahoo(tickers="TSLA", start=start, end=end)

    def status(open, close):
        if open < close:
            value = "Profit"
        elif open > close:
            value = "Loss"
        else:
            value = "Equal"
        return value

    df['Status'] = [
        status(open, close) for open, close in zip(df.Open, df.Close)
    ]
    df['Middle_y'] = (df.Open + df.Close) / 2
    df['Height'] = abs(df.Close - df.Open)
    #df

    p = figure(x_axis_type='datetime',
               width=1000,
               height=400,
               title="CandleStick Chart",
               sizing_mode="scale_width")
    p.grid.grid_line_alpha = 0.3

    p.segment(df.index, df.High, df.index, df.Low, color="black")

    p.rect(x=df.index[df.Status == "Profit"],
           y=df.Middle_y[df.Status == 'Profit'],
           width=hours_12,
           height=df.Height[df.Status == "Profit"],
           fill_color="#99FF99",
           line_color="white")

    p.rect(x=df.index[df.Status == "Loss"],
           y=df.Middle_y[df.Status == 'Loss'],
           width=hours_12,
           height=df.Height[df.Status == "Loss"],
           fill_color="#CC0000",
           line_color="white")

    # output_file("cs.html")
    #show(p)

    script1, div1 = components(p)

    cdn_js = CDN.js_files
    cdn_css = CDN.css_files
    return render_template("plot.html",
                           script1=script1,
                           div1=div1,
                           cdn_js=cdn_js[0],
                           cdn_css=cdn_css)
Beispiel #14
0
def yf(ticker: str, **kwargs):
    """yf - yfinance wrapper

    It retrieves market data (ohlcv) from Yahoo Finance using yfinance.
    To install yfinance. (pip install yfinance) This method can also pull
    additional data using the 'kind' kwarg. By default kind=None and retrieves
    Historical Chart Data.

    Other options of 'kind' include:
    * All: "all"
        - Prints everything below but only returns Chart History to Pandas TA
    * Company Information: "info"
    * Institutional Holders: "institutional_holders" or "ih"
    * Major Holders: "major_holders" or "mh"
    * Mutual Fund Holders: "mutualfund_holders" or "mfh"
    * Recommendations (YTD): "recommendations" or "rec"
    * Earnings Calendar: "calendar" or "cal"
    * Earnings: "earnings" or "earn"
    * Sustainability/ESG Scores: "sustainability", "sus" or "esg"
    * Financials: "financials" or "fin"
        - Returns in order: Income Statement, Balance Sheet and Cash Flow
    * Option Chain: "option_chain" or "oc"
        - Uses the nearest expiration date by default
        - Change the expiration date using kwarg "exp"
        - Show ITM options, set kwarg "itm" to True. Or OTM options, set
        kwarg "itm" to False.
    * Chart History:
        - The only data returned to Pandas TA.

    Args:
        ticker (str): Any string for a ticker you would use with yfinance.
            Default: "SPY"
    Kwargs:
        calls (bool): When True, prints only Option Calls for the Option Chain.
            Default: None
        desc (bool): Will print Company Description when printing Company
            Information. Default: False
        exp (str): Used to print other Option Chains for the given Expiration
            Date. Default: Nearest Expiration Date for the Option Chains
        interval (str): A yfinance argument. Default: "1d"
        itm (bool): When printing Option Chains, shows ITM Options when True.
            When False, it shows OTM Options: Default: None
        kind (str): Options see above. Default: None
        period (str): A yfinance argument. Default: "max"
        proxy (dict): Proxy for yfinance to use. Default: {}
        puts (bool): When True, prints only Option Puts for the Option Chain.
            Default: None
        show (int > 0): How many last rows of Chart History to show.
            Default: None
        snd (int): How many recent Splits and Dividends to show in Company
            Information. Default: 5
        verbose (bool): Prints Company Information "info" and a Chart History
            header to the screen. Default: False

    Returns:
        Exits if the DataFrame is empty or None
        Otherwise it returns a DataFrame of the Chart History
    """
    verbose = kwargs.pop("verbose", False)
    if ticker is not None and isinstance(ticker, str) and len(ticker):
        ticker = ticker.upper()
    else:
        ticker = "SPY"

    kind = kwargs.pop("kind", None)
    if kind is not None and isinstance(kind, str) and len(kind):
        kind = kind.lower()

    period = kwargs.pop("period", "max")
    interval = kwargs.pop("interval", "1d")
    proxy = kwargs.pop("proxy", {})
    show = kwargs.pop("show", None)

    if not Imports["yfinance"]:
        print(
            f"[X] Please install yfinance to use this method. (pip install yfinance)"
        )
        return
    if Imports["yfinance"] and ticker is not None:
        import yfinance as yfra
        yfra.pdr_override()

        # Ticker Info & Chart History
        yfd = yfra.Ticker(ticker)
        df = yfd.history(period=period,
                         interval=interval,
                         proxy=proxy,
                         **kwargs)
        print(f"[X] df[{type(df)}:{df.empty}]\n{df}\n")
        if df.empty: return
        df.name = ticker

        try:
            ticker_info = yfd.info
        except KeyError as ke:
            print(f"[X] Ticker '{ticker}' not found.")
            return
        print(
            f"[X] ticker_info[{type(ticker_info)}:{len(ticker_info.keys())}]\n{ticker_info}\n"
        )

        try:
            infodf = DataFrame.from_dict(ticker_info, orient="index")
        except TypeError as te:
            print(f"[X] TypeError: {te}")
        # else:
        #     infodf = DataFrame(ticker_info)

        print(f"[X] infodf.empty: {infodf.empty}")
        if infodf.empty: return
        print(f"[X] infodf[{type(infodf)}:{len(infodf.keys())}]\n{infodf}\n")
        infodf.name, infodf.columns = ticker, [ticker]

        # Dividends and Splits
        dividends, splits = yfd.splits, yfd.dividends

        _all, div = ["all"], "=" * 53  # Max div width is 80
        if kind in _all + ["info"] or verbose:
            description = kwargs.pop("desc", False)
            snd_length = kwargs.pop("snd", 5)

            [print(f"{_[0]}: {_[1]}") for _ in sorted(ticker_info.items())]
            print()

            print("\n====  Company Information  " + div)
            print(
                f"{ticker_info['longName']} ({ticker_info['shortName']}) [{ticker_info['symbol']}]"
            )
            print(
                f"[i] {type(ticker_info['longBusinessSummary'])}: {ticker_info['longBusinessSummary']}"
            )
            if description:
                print(f"{ticker_info['longBusinessSummary']}\n")
            if "address1" in ticker_info and len(ticker_info["address1"]):
                if "address2" in ticker_info and len(ticker_info["address2"]):
                    print(
                        f"{ticker_info['address1']} {ticker_info['address2']}")
                else:
                    print(f"{ticker_info['address1']}")

                if "city" in ticker_info and len(ticker_info["city"]) and "state" in ticker_info and len(ticker_info["state"]) \
                    and "zip" in ticker_info and len(ticker_info["zip"]) and "country" in ticker_info and len(ticker_info["country"]):
                    print(
                        f"{ticker_info['city']}, {ticker_info['state']} {ticker_info['zip']}, {ticker_info['country']}"
                    )
                else:
                    print(
                        f"{ticker_info['state']} {ticker_info['zip']}, {ticker_info['country']}"
                    )
                print(
                    f"Phone (Fax): {ticker_info['phone']} ({ticker_info['fax'] if 'fax' in ticker_info else 'N/A'})"
                )

            if "website" in ticker_info and len(ticker_info['website']):
                s = f"Website: {ticker_info['website']}".ljust(40)
                if "fullTimeEmployees" in ticker_info:
                    s += f"FT Employees: {ticker_info['fullTimeEmployees']:,}".rjust(
                        40)
                print(s)
            elif "fullTimeEmployees" in ticker_info:
                print(f"FT Employees: {ticker_info['fullTimeEmployees']:,}")

            if "companyOfficers" in ticker_info and len(
                    ticker_info['companyOfficers']):
                print(
                    f"Company Officers: {', '.join(ticker_info['companyOfficers'])}"
                    .ljust(40))
            if "sector" in ticker_info and len(
                    ticker_info["sector"]
            ) and "industry" in ticker_info and len(ticker_info["industry"]):
                # print(f"Sector: {ticker_info['sector']}".ljust(39), f"Industry: {ticker_info['industry']}".rjust(40))
                print(
                    f"Sector | Industry".ljust(29),
                    f"{ticker_info['sector']} | {ticker_info['industry']}".
                    rjust(50))

            print("\n====  Market Information   " + div)
            _category = f" | {ticker_info['category']}" if "category" in ticker_info and ticker_info[
                "category"] is not None else ""
            print(
                f"Market | Exchange | Symbol{' | Category' if 'category' in ticker_info and ticker_info['category'] is not None else ''}"
                .ljust(39),
                f"{ticker_info['market'].split('_')[0].upper()} | {ticker_info['exchange']} | {ticker_info['symbol']}{_category}"
                .rjust(40))

            print()
            if "marketCap" in ticker_info and ticker_info[
                    "marketCap"] is not None:
                print(
                    f"Market Cap.".ljust(39),
                    f"{ticker_info['marketCap']:,} ({ticker_info['marketCap']/1000000:,.2f} MM)"
                    .rjust(40))
            if "navPrice" in ticker_info and ticker_info[
                    "navPrice"] is not None or "yield" in ticker_info and ticker_info[
                        "yield"] is not None:
                print(
                    f"NAV | Yield".ljust(39),
                    f"{ticker_info['navPrice']} | {100 * ticker_info['yield']:.4f}%"
                    .rjust(40))
            if "sharesOutstanding" in ticker_info and ticker_info[
                    "sharesOutstanding"] is not None and "floatShares" in ticker_info and ticker_info[
                        "floatShares"] is not None:
                print(
                    f"Shares Outstanding | Float".ljust(39),
                    f"{ticker_info['sharesOutstanding']:,} | {ticker_info['floatShares']:,}"
                    .rjust(40))
            if "impliedSharesOutstanding" in ticker_info and ticker_info[
                    "impliedSharesOutstanding"] is not None:
                print(f"Implied Shares Outstanding".ljust(39),
                      f"{ticker_info['impliedSharesOutstanding']:,}".rjust(40))
            if "sharesShort" in ticker_info and "shortRatio" in ticker_info and ticker_info[
                    "sharesShort"] is not None and ticker_info[
                        "shortRatio"] is not None:
                print(
                    f"Shares Short | Ratio".ljust(39),
                    f"{ticker_info['sharesShort']:,} | {ticker_info['shortRatio']:,}"
                    .rjust(40))
            if "shortPercentOfFloat" in ticker_info and ticker_info[
                    'shortPercentOfFloat'] is not None and "sharesShortPriorMonth" in ticker_info and ticker_info[
                        'sharesShortPriorMonth'] is not None:
                print(
                    f"Short % of Float | Short prior Month".ljust(39),
                    f"{100 * ticker_info['shortPercentOfFloat']:.4f}% | {ticker_info['sharesShortPriorMonth']:,}"
                    .rjust(40))
            if "heldPercentInstitutions" in ticker_info and ticker_info[
                    'heldPercentInstitutions'] is not None or "heldPercentInsiders" in ticker_info and ticker_info[
                        'heldPercentInsiders'] is not None:
                print(
                    f"Insiders % | Institution %".ljust(39),
                    f"{100 * ticker_info['heldPercentInsiders']:.4f}% | {100 * ticker_info['heldPercentInstitutions']:.4f}"
                    .rjust(40))

            print()
            if "bookValue" in ticker_info and ticker_info[
                    'bookValue'] is not None or "priceToBook" in ticker_info and ticker_info[
                        'priceToBook'] is not None or "pegRatio" in ticker_info and ticker_info[
                            'pegRatio'] is not None:
                print(
                    f"Book Value | Price to Book | Peg Ratio".ljust(39),
                    f"{ticker_info['priceToBook']} | {ticker_info['priceToBook']} | {ticker_info['pegRatio']}"
                    .rjust(40))
            if "forwardPE" in ticker_info and ticker_info[
                    'forwardPE'] is not None:
                print(f"Forward PE".ljust(39),
                      f"{ticker_info['forwardPE']}".rjust(40))
            if "forwardEps" in ticker_info and ticker_info[
                    'forwardEps'] is not None or "trailingEps" in ticker_info and ticker_info[
                        'trailingEps'] is not None:
                print(
                    f"Forward EPS | Trailing EPS".ljust(39),
                    f"{ticker_info['forwardEps']} | {ticker_info['trailingEps']}"
                    .rjust(40))
            if "enterpriseValue" in ticker_info and ticker_info[
                    'enterpriseValue'] is not None:
                print(f"Enterprise Value".ljust(39),
                      f"{ticker_info['enterpriseValue']:,}".rjust(40))
            if "enterpriseToRevenue" in ticker_info and ticker_info[
                    'enterpriseToRevenue'] is not None or "enterpriseToEbitda" in ticker_info and ticker_info[
                        'enterpriseToEbitda'] is not None:
                print(
                    f"Enterprise to Revenue | to EBITDA".ljust(39),
                    f"{ticker_info['enterpriseToRevenue']} | {ticker_info['enterpriseToEbitda']}"
                    .rjust(40))

            print()
            if "netIncomeToCommon" in ticker_info and ticker_info[
                    'netIncomeToCommon'] is not None:
                print(f"Net Income to Common".ljust(39),
                      f"{ticker_info['netIncomeToCommon']:,}".rjust(40))
            if "revenueQuarterlyGrowth" in ticker_info and ticker_info[
                    'revenueQuarterlyGrowth'] is not None:
                print(f"Revenue Quarterly Growth".ljust(39),
                      f"{ticker_info['revenueQuarterlyGrowth']}".rjust(40))
            if "profitMargins" in ticker_info and ticker_info[
                    'profitMargins'] is not None:
                print(f"Profit Margins".ljust(39),
                      f"{100 * ticker_info['profitMargins']:.4f}%".rjust(40))
            if "earningsQuarterlyGrowth" in ticker_info and ticker_info[
                    'earningsQuarterlyGrowth'] is not None:
                print(f"Quarterly Earnings Growth".ljust(39),
                      f"{ticker_info['earningsQuarterlyGrowth']}".rjust(40))
            if "annualReportExpenseRatio" in ticker_info and ticker_info[
                    'annualReportExpenseRatio'] is not None:
                print(f"Annual Expense Ratio".ljust(39),
                      f"{ticker_info['annualReportExpenseRatio']}".rjust(40))

            print("\n====  Price Information    " + div)
            _o, _h, _l, _c, _v = ticker_info['open'], ticker_info[
                'dayHigh'], ticker_info['dayLow'], ticker_info[
                    'regularMarketPrice'], ticker_info['regularMarketVolume']
            print(f"Open High Low Close".ljust(39),
                  f"{_o:.4f}, {_o:.4f}, {_l:.4f}, {_c:.4f}".rjust(40))
            print(
                f"HL2 | HLC3 | OHLC4 | C - OHLC4".ljust(39),
                f"{0.5 * (_h + _l):.4f}, {(_h + _l + _c) / 3.:.4f}, {0.25 * (_o + _h + _l + _c):.4f}, {_c - 0.25 * (_o + _h + _l + _c):.4f}"
                .rjust(40))
            print(
                f"Change (%)".ljust(39),
                f"{_c - ticker_info['previousClose']:.4f} ({100 * ((_c / ticker_info['previousClose']) - 1):.4f}%)"
                .rjust(40))
            print(
                f"Volume | Avg Vol (10Day)".ljust(39),
                f"{ticker_info['volume']:,} | {ticker_info['averageVolume']:,} ({ticker_info['averageDailyVolume10Day']:,})"
                .rjust(40))
            print(
                f"Bid | Ask | Spread".ljust(39),
                f"{ticker_info['bid']} x {ticker_info['bidSize']} | {ticker_info['ask']} x {ticker_info['askSize']} | {ticker_info['ask'] - ticker_info['bid']:.4f}"
                .rjust(40))

            print()
            if "52WeekChange" in ticker_info and ticker_info[
                    '52WeekChange'] is not None:
                print(f"52Wk % Change".ljust(39),
                      f"{100 * ticker_info['52WeekChange']:.4f}%".rjust(40))
            if "SandP52WeekChange" in ticker_info and ticker_info[
                    'SandP52WeekChange'] is not None:
                print(
                    f"52Wk % Change vs S&P500".ljust(39),
                    f"{100 *ticker_info['SandP52WeekChange']:.4f}%".rjust(40))
            if "fiftyTwoWeekHigh" in ticker_info and "fiftyTwoWeekLow" in ticker_info and "previousClose" in ticker_info:  # or 'regularMarketPrice'
                print(
                    f"52Wk Range (% from 52Wk Low)".ljust(39),
                    f"{ticker_info['fiftyTwoWeekLow']} - {ticker_info['fiftyTwoWeekHigh']} : {ticker_info['fiftyTwoWeekHigh'] - ticker_info['fiftyTwoWeekLow']:.4f} ({100 * (ticker_info['regularMarketPrice'] / ticker_info['fiftyTwoWeekLow'] - 1):.4f}%)"
                    .rjust(40))

            avg50 = "fiftyDayAverage" in ticker_info and ticker_info[
                'fiftyDayAverage'] is not None
            avg200 = "twoHundredDayAverage" in ticker_info and ticker_info[
                'twoHundredDayAverage'] is not None
            if avg50 and avg200:
                print(
                    f"SMA 50 | SMA 200".ljust(39),
                    f"{ticker_info['fiftyDayAverage']:.4f} | {ticker_info['twoHundredDayAverage']:.4f}"
                    .rjust(40))
            elif avg50:
                print(f"SMA 50".ljust(39),
                      f"{ticker_info['fiftyDayAverage']:.4f}".rjust(40))
            elif avg200:
                print(f"SMA 200".ljust(39),
                      f"{ticker_info['twoHundredDayAverage']:.4f}".rjust(40))
            if "beta" in ticker_info and ticker_info['beta'] is not None:
                print(
                    f"Beta | 3Yr".ljust(39),
                    f"{ticker_info['beta']} | {ticker_info['beta3Year']}".
                    rjust(40))
            if "threeYearAverageReturn" in ticker_info and ticker_info[
                    'threeYearAverageReturn'] is not None or "fiveYearAverageReturn" in ticker_info and ticker_info[
                        'fiveYearAverageReturn'] is not None:
                print(
                    f"Avg. Return 3Yr | 5Yr".ljust(39),
                    f"{100 * ticker_info['threeYearAverageReturn']:.4f}% | {100 * ticker_info['fiveYearAverageReturn']:.4f}%"
                    .rjust(40))

            # Dividends and Splits
            if not dividends.empty or not splits.empty:
                print("\n====  Dividends / Splits   " + div)
                if "dividendRate" in ticker_info and ticker_info[
                        'dividendRate'] is not None and "dividendYield" in ticker_info and ticker_info[
                            'dividendYield'] is not None and "payoutRatio" in ticker_info and ticker_info[
                                'payoutRatio'] is not None:
                    print(
                        f"Rate | Yield | Payout Ratio".ljust(39),
                        f"{ticker_info['dividendRate']} | {100 * ticker_info['dividendYield']:.4f}% | {ticker_info['payoutRatio']}"
                        .rjust(40))
                if "trailingAnnualDividendRate" in ticker_info and ticker_info[
                        'trailingAnnualDividendRate'] is not None and "trailingAnnualDividendYield" in ticker_info and ticker_info[
                            'trailingAnnualDividendYield'] is not None:
                    print(
                        f"Trailing Annual Dividend Rate | Yield".ljust(40),
                        f"{ticker_info['trailingAnnualDividendRate']} | {100 * ticker_info['trailingAnnualDividendYield']:.4f}%\n"
                        .rjust(40))
            if not dividends.empty:
                dividends.name = "Value"
                total_dividends = dividends.size
                dividendsdf = DataFrame(dividends.tail(snd_length)[::-1]).T
                print(
                    f"Dividends (Last {snd_length} of {total_dividends}):\n{dividendsdf}"
                )

            if not splits.empty:
                splits.name = "Ratio"
                total_splits = splits.size
                splitsdf = DataFrame(splits.tail(snd_length)[::-1]).T
                print(
                    f"\nStock Splits (Last {snd_length} of {total_splits}):\n{splitsdf}"
                )

        if kind in _all + ["institutional_holders", "ih"]:
            ihdf = yfd.institutional_holders
            if ihdf is not None and "Date Reported" in ihdf.columns:
                ihdf.set_index("Date Reported", inplace=True)
                ihdf["Shares"] = ihdf.apply(lambda x: f"{x['Shares']:,}",
                                            axis=1)
                ihdf["Value"] = ihdf.apply(lambda x: f"{x['Value']:,}", axis=1)
                if kind not in _all: print(f"\n{ticker_info['symbol']}")
                print("\n====  Instl. Holders       " + div + f"\n{ihdf}")

        if kind in _all + ["major_holders", "mh"]:
            mhdf = yfd.major_holders
            if mhdf is not None and "Major Holders" in mhdf.columns:
                mhdf.columns = ["Percentage", "Major Holders"]
                mhdf.set_index("Major Holders", inplace=True)
                mhdf["Shares"] = mhdf.apply(lambda x: f"{x['Shares']:,}",
                                            axis=1)
                mhdf["Value"] = mhdf.apply(lambda x: f"{x['Value']:,}", axis=1)
                if kind not in _all: print(f"\n{ticker_info['symbol']}")
                print("\n====  Major Holders       " + div + f"\n{mhdf}")

        if kind in _all + ["mutualfund_holders", "mfh"]:
            mfhdf = yfd.get_mutualfund_holders()
            if mfhdf is not None and "Holder" in mfhdf.columns:
                mfhdf.set_index("Date Reported", inplace=True)
                mfhdf["Shares"] = mfhdf.apply(lambda x: f"{x['Shares']:,}",
                                              axis=1)
                mfhdf["Value"] = mfhdf.apply(lambda x: f"{x['Value']:,}",
                                             axis=1)
                if kind not in _all: print(f"\n{ticker_info['symbol']}")
                print("\n====  Mutual Fund Holders  " + div + f"\n{mfhdf}")

        if kind in _all + ["recommendations", "rec"]:
            recdf = yfd.recommendations
            if recdf is not None:
                recdf = ytd_df(recdf)
                # recdf_grade = recdf["To Grade"].value_counts().T
                # recdf_grade.name = "Grades"
                if kind not in _all: print(f"\n{ticker_info['symbol']}")
                print("\n====  Recommendation(YTD)  " + div + f"\n{recdf}")

        if kind in _all + ["calendar", "cal"]:
            caldf = yfd.calendar
            if caldf is not None and "Earnings Date" in caldf.columns:
                caldf.set_index("Earnings Date", inplace=True)
                if kind not in _all: print(f"\n{ticker_info['symbol']}")
                print("\n====  Earnings Calendar    " + div + f"\n{caldf}")

        if kind in _all + ["earnings", "earn"]:
            earndf = yfd.earnings
            if not earndf.empty:
                earndf["Revenue"] = earndf.apply(lambda x: f"{x['Revenue']:,}",
                                                 axis=1)
                earndf["Earnings"] = earndf.apply(
                    lambda x: f"{x['Earnings']:,}", axis=1)
                if kind not in _all: print(f"\n{ticker_info['symbol']}")
                print("\n====  Earnings             " + div + f"\n{earndf}")

        if kind in _all + ["sustainability", "sus", "esg"]:
            susdf = yfd.sustainability
            if susdf is not None:
                susdf.replace({None: False}, inplace=True)
                susdf.columns = ["Score"]
                susdf.drop(susdf[susdf["Score"] == False].index, inplace=True)
                susdf.rename(index=_camelCase2Title,
                             errors="ignore",
                             inplace=True)
                susdf.index.name = "Source"
                if kind not in _all: print(f"\n{ticker_info['symbol']}")
                print("\n====  Sustainability/ESG   " + div + f"\n{susdf}")

        if kind in _all + ["financials", "fin"]:
            icdf = yfd.financials
            bsdf = yfd.balance_sheet
            cfdf = yfd.cashflow

            if icdf.empty or bsdf.empty or cfdf.empty:
                if yfra.__version__ <= "0.1.54":
                    print(
                        f"[!] Best choice: update yfinance to the latest version."
                    )
                    print(
                        f"[!] Ignore if aleady patched. Some tickers do not have financials."
                    )
                    print(
                        f"[!] Otherwise to enable Company Financials, see yfinance Issue #517 patch."
                    )
                    print(
                        f"[!] https://github.com/ranaroussi/yfinance/pull/517/files"
                    )
            else:
                print("\n====  Company Financials   " + div)
                if not icdf.empty: print(f"Income Statement:\n{icdf}\n")
                if not bsdf.empty: print(f"Balance Sheet:\n{bsdf}\n")
                if not cfdf.empty: print(f"Cash Flow:\n{cfdf}\n")

        if kind in _all + ["option_chain", "oc"]:
            try:
                yfd_options = yfd.options
            except IndexError as ie:
                yfd_options = None

            if yfd_options is not None:
                opt_expirations = list(yfd_options)
                just_calls = kwargs.pop("calls", None)
                just_puts = kwargs.pop("puts", None)
                itm = kwargs.pop("itm", None)
                opt_date = kwargs.pop("exp", opt_expirations[0])
                opt_expirations_str = f"{ticker} Option Expirations:\n\t{', '.join(opt_expirations)}\n"

                if kind not in _all: print(f"\n{ticker_info['symbol']}")
                if isinstance(itm, bool) and itm:
                    print("\n====  ITM Option Chains    " + div)
                elif isinstance(itm, bool) and not itm:
                    print("\n====  OTM Option Chains    " + div)
                else:
                    print("\n====  Option Chains        " + div)
                print(opt_expirations_str)

                if opt_date not in opt_expirations:
                    print(
                        f"[X] No Options for {ticker_info['quoteType']} {ticker_info['symbol']}"
                    )
                else:
                    option_columns = [
                        "Contract", "Last Trade", "Strike", "Price", "Bid",
                        "Ask", "Change", "Percent Change", "Volume", "OI",
                        "IV", "ITM", "Size", "Currency"
                    ]
                    cp_chain = yfd.option_chain(proxy=proxy)
                    calls, puts = cp_chain.calls, cp_chain.puts
                    calls.columns = puts.columns = option_columns
                    calls.set_index("Contract", inplace=True)
                    puts.set_index("Contract", inplace=True)

                    calls.name = f"{ticker} Calls for {opt_date}"
                    puts.name = f"{ticker} Puts for {opt_date}"

                    if isinstance(itm, bool):
                        in_or_out = "ITM" if itm else "OTM"
                        calls.name, puts.name = f"{calls.name} {in_or_out}", f"{puts.name} {in_or_out}"
                        itm_calls = f"{calls.name}\n{calls[calls['ITM'] == itm]}"
                        itm_puts = f"{puts.name}\n{puts[puts['ITM'] == itm]}"

                        if just_calls: print(itm_calls)
                        elif just_puts: print(itm_puts)
                        else: print(f"{itm_calls}\n\n{itm_puts}")
                    else:
                        all_calls, all_puts = f"{calls.name}\n{calls}", f"{puts.name}\n{puts}"
                        if just_calls: print(all_calls)
                        elif just_puts: print(all_puts)
                        else: print(f"{all_calls}\n\n{all_puts}")

        if verbose:
            print("\n====  Chart History        " + div +
                  f"\n[*] Pandas TA v{version} & yfinance v{yfra.__version__}")
            print(
                f"[+] Downloading {ticker}[{interval}:{period}] from Yahoo Finance"
            )
        if show is not None and isinstance(show, int) and show > 0:
            print(f"\n{df.name}\n{df.tail(show)}\n")
        if verbose: print("=" * 80 + "\n")
        else: print()
        return df

    else:
        return DataFrame()
Beispiel #15
0
import yfinance as fyf
from pandas_datareader import data as pdr

fyf.pdr_override()

apple = pdr.get_data_yahoo("AAPL", start='2017-01-01')
stock_apple = apple['Close']

stock_apple.head()

def stock_screener(
    index_tinker_name='S&P500',
    min_vol=5e6,
    min_price=0,
    days=365,
    min_rs_rating=70,
):
    # help(si)
    ## fix for yahoo_fin
    start_date, end_date = period(days)
    yf.pdr_override()

    index_tinker = {'DOW': 'DOW', 'NASDAQ': '^IXIC', "S&P500": '^GSPC'}

    index_list = {
        'DOW': si.tickers_sp500(),
        'NASDAQ': si.tickers_nasdaq(),
        "S&P500": si.tickers_sp500()
    }
    st.header(f'Stock Screener {index_tinker_name}')
    # stocklist = si.tickers_sp500()
    min_volume = min_vol
    # index_name = '^GSPC' # SPY or S&P 500
    stocklist = index_list.get(index_tinker_name)[:]

    index_rs_strange_value = calc_relative_strength(
        get_stock(index_tinker[index_tinker_name], days))

    final = []
    index = []

    exclude_list = []
    all_data = []
    latest_iteration = st.empty()
    having_break = st.empty()
    bar = st.progress(0)
    total = len(stocklist)

    for num, stock_name in enumerate(stocklist):
        print(f"checking {num}:{stock_name}")
        if stock_name in exclude_list:
            continue
            FAILED = False
        df = get_stock(stock_name)
        # print('**',df)
        if df is False:
            print(f'SKIPPED to download {stock_name} {num}')
            continue

        stock_meta = Moving_avg(stock_name, df, index_rs_strange_value,
                                min_rs_rating)
        time.sleep(0.2)

        if stock_meta.all_conditions():
            print(f'Passed conditions: {stock_name}')
            final.append(stock_meta.as_dict())
        else:
            print(f'Failed conditions: {stock_name}')
            # all_data.append(stock_meta.as_dict())

        latest_iteration.text(f'Stocks Processed: {(num+1)}/{total}')
        bar.progress((num + 1) / total)

        if num == 0:
            continue
        if num % 10 == 0:
            for i in list(range(5))[::-1]:
                having_break.text(f'waiting for {i}sec')
                time.sleep(1)
            # having_break = st.empty()
        if num % 100 == 0:
            for i in list(range(3))[::-1]:
                having_break.text(f'waiting for {i}min')
                time.sleep(60)
            # having_break = st.empty()
            # time.sleep(5*60)

    final_df = pd.DataFrame(final)
    # all_data_df = pd.DataFrame(all_data)
    return final_df
Beispiel #17
0
def getData(startDate, endDate, cur):

    yf.pdr_override()
    data = pdr.get_data_yahoo(cur, startDate, endDate)
    curv = pd.DataFrame(data)
    return curv
Beispiel #18
0
def calculate_protfolio(list_of_ticks, img_name, type):
    #Select stocks, start year and end year, stock number has no known limit
    ## Insert Parmater
    start_year = '2009-1-1'
    end_year = '2021-04-01'
    Num_porSimulation = 100000  # Number of Portfolio that will build for simulator

    ## End Insert Parameter
    #Select stocks, start year and end year, stock number has no known limit

    #Building the DataBase
    yf.pdr_override()
    frame = {}
    for stock in list_of_ticks:
        data_var = pdr.get_data_yahoo(stock, start_year, end_year)['Adj Close']
        data_var.to_frame()
        frame.update({stock: data_var})
    # End Insert Adj Close To DataBase

    import pandas as pd
    #Mathematical calculations Return Daily And annualy, creation of Number portfolios,
    table = pd.DataFrame(frame)
    returns_daily = table.pct_change()
    returns_annual = ((1 + returns_daily.mean())**250) - 1

    # get daily and covariance of returns of the stock
    cov_daily = returns_daily.cov()
    cov_annual = cov_daily * 250

    # empty lists to store returns, volatility and weights of imiginary portfolios
    port_returns = []
    port_volatility = []
    sharpe_ratio = []
    stock_weights = []

    # set the number of combinations for imaginary portfolios
    num_assets = len(list_of_ticks)
    num_portfolios = Num_porSimulation  # Change porfolio numbers here

    #set random seed for reproduction's sake
    np.random.seed(101)

    # populate the empty lists with each portfolios returns,risk and weights
    for single_portfolio in range(num_portfolios):
        weights = np.random.random(num_assets)
        weights /= np.sum(weights)
        returns = np.dot(weights, returns_annual)
        # calculation the Standard Deviation of portfolio.
        volatility = np.sqrt(np.dot(weights.T, np.dot(cov_annual, weights)))
        # calculation the sharpe of portfolio.
        sharpe = returns / volatility
        sharpe_ratio.append(sharpe)
        # Percent Conversion
        port_returns.append(returns * 100)
        port_volatility.append(volatility * 100)
        stock_weights.append(weights)

    # a dictionary for Returns and Risk values of each portfolio
    portfolio = {
        'Returns': port_returns,
        'Volatility': port_volatility,
        'Sharpe Ratio': sharpe_ratio
    }

    # extend original dictionary to accomodate each ticker and weight in the portfolio
    for counter, symbol in enumerate(list_of_ticks):
        portfolio[symbol +
                  ' Weight'] = [Weight[counter] for Weight in stock_weights]

    # make a nice dataframe of the extended dictionary
    df = pd.DataFrame(portfolio)

    # get better labels for desired arrangement of columns
    column_order = ['Returns', 'Volatility', 'Sharpe Ratio'
                    ] + [stock + ' Weight' for stock in list_of_ticks]

    # reorder dataframe columns
    df = df[column_order]

    # plot frontier, max sharpe & min Volatility values with a scatterplot
    # find min Volatility & max sharpe values in the dataframe (df)
    min_volatility = df['Volatility'].min()
    #min_volatility1 = df['Volatility'].min()+1
    max_sharpe = df['Sharpe Ratio'].max()
    max_return = df['Returns'].max()
    max_vol = df['Volatility'].max()
    # use the min, max values to locate and create the two special portfolios
    sharpe_portfolio = df.loc[df['Sharpe Ratio'] == max_sharpe]
    min_variance_port = df.loc[df['Volatility'] == min_volatility]
    max_returns = df.loc[df['Returns'] == max_return]
    max_vols = df.loc[df['Volatility'] == max_vol]

    # plot frontier, max sharpe & min Volatility values with a scatterplot
    plt.style.use('seaborn-dark')
    df.plot.scatter(x='Volatility',
                    y='Returns',
                    c='Sharpe Ratio',
                    cmap='RdYlGn',
                    edgecolors='black',
                    figsize=(10, 8),
                    grid=True)
    plt.scatter(x=sharpe_portfolio['Volatility'],
                y=sharpe_portfolio['Returns'],
                c='green',
                marker='D',
                s=200)
    plt.scatter(x=min_variance_port['Volatility'],
                y=min_variance_port['Returns'],
                c='orange',
                marker='D',
                s=200)
    plt.scatter(x=max_vols['Volatility'],
                y=max_returns['Returns'],
                c='red',
                marker='D',
                s=200)
    plt.style.use('seaborn-dark')

    plt.xlabel('Volatility (Std. Deviation) Percentage %')
    plt.ylabel('Expected Returns Percentage %')
    plt.title(type)
    plt.subplots_adjust(bottom=0.4)

    # ------------------ Pritning 3 optimal Protfolios -----------------------
    #Setting max_X, max_Y to act as relative border for window size

    red_num = df.index[df["Returns"] == max_return]
    yellow_num = df.index[df['Volatility'] == min_volatility]
    green_num = df.index[df['Sharpe Ratio'] == max_sharpe]
    multseries = pd.Series([1, 1, 1] + [100 for stock in list_of_ticks],
                           index=['Returns', 'Volatility', 'Sharpe Ratio'] +
                           [stock + ' Weight' for stock in list_of_ticks])
    with pd.option_context('display.float_format', '%{:,.2f}'.format):
        plt.figtext(0.2,
                    0.15,
                    "Max returns Porfolio: \n" +
                    df.loc[red_num[0]].multiply(multseries).to_string(),
                    bbox=dict(facecolor='red', alpha=0.5),
                    fontsize=11,
                    style='oblique',
                    ha='center',
                    va='center',
                    wrap=True)
        plt.figtext(0.45,
                    0.15,
                    "Safest Portfolio: \n" +
                    df.loc[yellow_num[0]].multiply(multseries).to_string(),
                    bbox=dict(facecolor='yellow', alpha=0.5),
                    fontsize=11,
                    style='oblique',
                    ha='center',
                    va='center',
                    wrap=True)
        plt.figtext(0.7,
                    0.15,
                    "Sharpe  Portfolio: \n" +
                    df.loc[green_num[0]].multiply(multseries).to_string(),
                    bbox=dict(facecolor='green', alpha=0.5),
                    fontsize=11,
                    style='oblique',
                    ha='center',
                    va='center',
                    wrap=True)
    plt.savefig(str(img_name) + '.png')
Beispiel #19
0
    def analysis(self):
        # Activating Yahoo finance
        yf.pdr_override()
        # Creating data frame with ticker data
        df = pdr.get_data_yahoo(self.ticker, self.start, self.today)

        # Moving Averages
        mas = [2, 3, 5, 8, 10, 20, 30, 40, 50, 60]
        for x in mas:
            df["MA"+str(x)] = round(df.iloc[:, 5].ewm(span=x, adjust=False).mean(), 2)

        pos = 0
        num = 0
        percentchange = []
        date = []
        # Conducting trades
        for i in df.index:
            cmin = min(df["MA2"][i], df["MA3"][i], df["MA5"][i], df["MA8"][i], df["MA10"][i])
            cmax = max(df["MA20"][i], df["MA30"][i], df["MA40"][i], df["MA50"][i], df["MA60"][i])
            close = df["Adj Close"][i]
            if cmin > cmax:
                # RWB
                if pos == 0:
                    bp = close
                    pos = 1
                    # BUY
            elif cmin < cmax:
                # BWR
                if pos == 1:
                    pos = 0
                    sp = close
                    # SELL
                    pc = (sp / bp - 1) * 100
                    percentchange.append(float(pc))
                    date.append(i)

            if num == df["Adj Close"].count() - 1 and pos == 1:
                pos = 0
                sp = close
                # SELL
                pc = (sp / bp - 1) * 100
                percentchange.append(float(pc))
                date.append(i)

            num += 1

        gains = 0
        ng = 0
        losses = 0
        nl = 0
        total_return = 1

        for i in percentchange:
            if i > 0:
                gains += i
                ng += 1
            else:
                losses += i
                nl += 1
            total_return = total_return * ((i / 100) + 1)

        total_return = round((total_return - 1) * 100, 2)

        if ng > 0:
            avg_gain = round((gains / ng), 2)
            max_return = (round(max(percentchange), 2))
        else:
            avg_gain = 0
            max_return = "undefined"

        if nl > 0:
            avg_loss = round((losses / nl), 2)
            max_loss = (round(min(percentchange), 2))
            ratio = (round((-avg_gain / avg_loss), 2))
        else:
            avg_loss = 0
            max_loss = "undefined"
            ratio = "inf"

        # Results
        x = [ng + nl, ratio, avg_gain, avg_loss, max_return, max_loss,
             total_return]

        returns = list(itertools.repeat(float(1), len(df.index)))
        for i in range(len(df.index)):
            for j in range(len(date)):
                if df.index[i] == date[j]:
                    returns[i] = (percentchange[j]/100)+1
        for i in range(len(df.index)):
            if i == df.index[0]:
                returns[0] = 1
            else:
                temp = returns[i-1]
                returns[i] = temp * returns[i]

        return x, returns
Beispiel #20
0
* **volume** int
    * Number of shares traded during trading hours.  

The primary key of this table is (mkt\_date, ticker).


### Importing libraries

First of all, we have to import all of the necessary libraries. We will be using pandas, pandas datareader, datetime, yfinance, and sqlite3.  

Then, as usual, we connect to the database using sqlite3.

from pandas_datareader import data as pdr
from datetime import datetime, date, timedelta
import yfinance as yf
yf.pdr_override() # overrides yfinance to expect pandas datareader
import pandas as pd
import sqlite3

con = sqlite3.connect("../data/invest_tracker.db")
cur = con.cursor()

### Calling the API  

Now, we will build a simple function to download today's stock market prices from the given ticker. Thankfully, YFinance does all of the hard work for us, so weonly need to make a single API call.  

The get\_data\_yahoo call on pandas datareader automatically queries the API, downloads data on the given ticker and date range, and writes it to a dataframe. Since we are only querying a single day, the returned dataframe will only contain a single row.

today = date.today()
yesterday = today - timedelta(days=1)
tomorrow = today + timedelta(days=1)
Beispiel #21
0
import yfinance
from pandas_datareader import data as pdr

yfinance.pdr_override()


def fetch_from_yahoo(all_symbols):
    symbols_str = " ".join(all_symbols)
    data = pdr.get_data_yahoo(
        symbols_str, period="5d",
        interval="30m").fillna(method='ffill').fillna(method='bfill')
    return data.swaplevel(axis=1) if len(all_symbols) > 1 else data
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
import pandas_datareader.data as web
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
import time

import yfinance as yahoo_finance
yahoo_finance.pdr_override()
from mpl_finance import candlestick2_ohlc

from argparse import ArgumentParser


def createZigZagPoints(dfSeries, minSegSize=0.1, sizeInDevs=0.5):
    minRetrace = minSegSize

    curVal = dfSeries[0]
    curPos = dfSeries.index[0]
    curDir = 1
    dfRes = pd.DataFrame(index=dfSeries.index, columns=["Dir", "Value"])
    for ln in dfSeries.index:
        if ((dfSeries[ln] - curVal) * curDir >= 0):
            curVal = dfSeries[ln]
            curPos = ln
        else:
            retracePrc = abs((dfSeries[ln] - curVal) / curVal * 100)
            if (retracePrc >= minRetrace):
                dfRes.loc[curPos, 'Value'] = curVal
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 10:22:27 2020

@author: TerryLaw
"""

from datetime import datetime, timezone, timedelta

import ffn
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import yfinance as yfin
yfin.pdr_override()


def calculate_ifree_growth_and_income_fund():
    if datetime.now(
            timezone.utc).astimezone().tzinfo.utcoffset(None) == timedelta(
                seconds=28800):
        prices = ffn.get('TLT, GLD, BIL',
                         start='2013-01-01',
                         end=datetime.today())
    else:
        prices = ffn.get('TLT, GLD, BIL',
                         start='2012-12-31',
                         end=datetime.today())

    prices = prices.reset_index()
    prices['portfolio'] = 100
    prices['tlt_rb'] = 0
#Richard Moglen Jan 2020 Backtest of RWB strategies
#import necessary packages/libraries
import pandas as pd
import numpy as np
import yfinance as yf
import datetime as dt
from pandas_datareader import data as pdr
import statistics
import time

yf.pdr_override()  # Activate yahoo finance workaround
now = dt.datetime.now()  #Runs until todays date
print()
stock = input("Enter the stock symbol (enter 'quit' to stop): "
              )  #Query user for stock ticker

while stock != "quit":  #Runs this loop until user enters 'quit' (can do many stocks in a row)
    startString = input(
        "Enter Date to start Backtest (ex: 12/4/2003): ")  #Query user
    startList = list(map(
        int, (startString.split('/'))))  #Split string into an array
    startyear = startList[2]  #assign 3rd element in array to startyear
    startmonth = startList[0]  #assign 1st element in array to startyear
    startday = startList[1]  #assign 2nd element in array to startyear
    start = dt.datetime(startyear, startmonth,
                        startday)  #Set starting time for datasample
    startalt = dt.datetime(startyear - 1, startmonth,
                           startday)  #Set starting time for datasample

    df = pdr.get_data_yahoo(stock, startalt,
                            now)  #create panda dataframe with stock data
Beispiel #25
0
import os

from pandas_datareader import data as pdr
import matplotlib.pyplot as pyplot
import pandas as pd
import numpy as np
import yfinance as yf
from datetime import datetime

today = datetime.today().strftime('%Y-%m-%d')

x = (sys.argv[1])

print("The script has the name %s" % x)

yf.pdr_override()  # <== that's all it takes :-)

# download dataframe using pandas_datareader
data = pdr.get_data_yahoo(x, start="2019-06-01", end="2020-12-18")

df = pd.DataFrame(data)

df = df.reset_index()

print(df.columns)

#df.plot(kind='line',x='Date',y='Close',color='red')
#pyplot.show()

#for i in range(0,df.shape[0]-2):
#    df.loc[df.index[i+2],'SMA_3'] = np.round(((df.iloc[i,1]+ df.iloc[i+1,1] +df.iloc[i+2,1])/3),1)
Beispiel #26
0
def main():
    yf.pdr_override()
    sp500 = pdr.get_data_yahoo('^GSPC', period=('30d'))
    print(sp500.tail())
    mpf.plot(sp500, type='line', title='S&P 500')
    action = 1
    while action != 0:
        print("What do you want to do today\n")
        action = gf.getnumber(
            "\npress 1 to search current and historic stock price \npress 2 to download data about a stock \npress 3 to view downloaded data \npress 4 to show the index\npress 5 to get tickers from an index \npress 6 to get all s&p 500 or nasdaq price data (WARNING: THIS WILL TAKE A WHILE)\npress 7 to show the correlation heatmap between stock (WARNING: THIS WILL TAKE A WHILE)\npress 8 to use the stock researcher \npress 9 to use the calculator \npress 0 to quit "
        )
        #if action == 1:
        #Whattofind = input("What you want to find? ")
        #specificCellletter = (find_specific_cell(Whattofind))
        #value = get_role_value(specificCellletter)
        #newvalue = input("What the new value? ")
        #currentSheet['C' + str(value)] = float(newvalue)
        #theFile.save('stock portfolio.xlsx')
        #print("The stock " + Whattofind + " value has changed to " +newvalue)
        #elif action == 2:
        #Whattofind = input("What you want to find? ")
        #specificCellletter = (find_specific_cell(Whattofind))
        #value = get_role_value(specificCellletter)
        #price = currentSheet['C' + str(value)].value
        #print("\nThe value of the stock "+ Whattofind +" is "+ str(price) +"\n")

        if action == 1:  #A function to show stock price by asking the user to input the stock symbol, also provide the user the ability to choose the time period
            Whattofind = input("Which stock price are you interested? ")
            whattofindprice = yf.Ticker(Whattofind)
            pricehistory = whattofindprice.history(period="10d")
            print("The price of " + Whattofind + " for the last 10 days is\n" +
                  str(pricehistory))
            try:
                mpf.plot(pricehistory,
                         type='candle',
                         volume=True,
                         title=str(Whattofind),
                         ylabel='OHLC Candles',
                         ylabel_lower='Volume',
                         style='charles')
            except:
                print()
            option = input("Would you want more data? y/n")
            while option == "y":
                period = gf.getnumber("How many days of data do you want? ")
                pricehistory = whattofindprice.history(period=(str(period) +
                                                               "d"))
                print("The price of " + Whattofind + " is\n" +
                      str(pricehistory))
                mpf.plot(pricehistory,
                         type='candle',
                         volume=True,
                         title=str(Whattofind),
                         ylabel='OHLC Candles',
                         ylabel_lower='Volume',
                         style='charles')
                option = input("Would you want more data? y/n")

        elif action == 2:  #A function allows the user to download stock data by asking the user to input the stock symbol and the start and end date then save the data into a csv file
            yes = 0
            if not os.path.exists('history/downloadhistory.dat'):
                Whattofind = input(
                    "Which stock/stocks price are you interested? (You can enter one or more stocks just separate the stock symbol with space)"
                )
                startdate = input("Startdate (format: YYYY/MM/DD): ")
                enddate = input("Enddate (format: YYYY/MM/DD): ")
                lastdownload = open('history/downloadhistory.dat', 'wb')
                pickle.dump(str(Whattofind), lastdownload)
                pickle.dump(startdate, lastdownload)
                pickle.dump(enddate, lastdownload)
                lastdownload.close()
            else:
                lastdownload = open('history/downloadhistory.dat', 'rb')
                history = pickle.load(lastdownload)
                hisstart = pickle.load(lastdownload)
                hisend = pickle.load(lastdownload)
                print(
                    "You have previously download data of '{}', would you like to download it again?"
                    .format(history))
                choice = input("y/n\n")
                if choice == 'y':
                    question = input(
                        "Would you like to download the same period? if yes please press 1, if you would like to update your data to today press 2, if you would like to update your data to 60days press 3, if you would like to download the data of specific time period press enter."
                    )
                    Whattofind = history
                    if question == '1':
                        startdate = hisstart
                        enddate = hisend
                    elif question == "2":
                        startdate = hisstart
                        enddate = dt.date.today()
                        enddate = enddate + dt.timedelta(days=1)
                        lastdownload = open('history/downloadhistory.dat',
                                            'wb')
                        pickle.dump(str(Whattofind), lastdownload)
                        pickle.dump(startdate, lastdownload)
                        pickle.dump(enddate, lastdownload)
                        lastdownload.close()
                    elif question == '3':
                        yes = 1
                        pass
                    else:
                        startdate = input("Startdate (format: YYYY/MM/DD): ")
                        enddate = input("Enddate (format: YYYY/MM/DD): ")
                        lastdownload = open('history/downloadhistory.dat',
                                            'wb')
                        pickle.dump(str(Whattofind), lastdownload)
                        pickle.dump(startdate, lastdownload)
                        pickle.dump(enddate, lastdownload)
                        lastdownload.close()
                else:
                    Whattofind = input(
                        "Which stock/stocks price are you interested? (You can enter one or more stocks just separate the stock symbol with space)"
                    )
                    CHOICE = input(
                        "The default option for this download is 60 day period, do you want to proceed?(y/n)"
                    )
                    if CHOICE.lower() == 'y':
                        startdate = "0000/00/00"
                        enddate = "0000/00/00"
                        lastdownload = open('history/downloadhistory.dat',
                                            'wb')
                        pickle.dump(str(Whattofind), lastdownload)
                        pickle.dump(startdate, lastdownload)
                        pickle.dump(enddate, lastdownload)
                        lastdownload.close()
                        yes = 1
                    if CHOICE.lower() == 'n':
                        startdate = input("Startdate (format: YYYY/MM/DD): ")
                        enddate = input("Enddate (format: YYYY/MM/DD): ")
                        lastdownload = open('history/downloadhistory.dat',
                                            'wb')
                        pickle.dump(str(Whattofind), lastdownload)
                        pickle.dump(startdate, lastdownload)
                        pickle.dump(enddate, lastdownload)
                        lastdownload.close()
                # the following three try functions are use to change the date from string to dt type and also choosing which mode to download, the 60 days or the custom date
            try:
                startdate = gf.changestringtodate(startdate)
            except:
                pass
            try:
                enddate = gf.changestringtodate(enddate)
                enddate = enddate + dt.timedelta(days=1)
            except:
                pass
            if yes == 0:
                data = yf.download(
                    str(Whattofind), start=startdate,
                    end=enddate).to_csv(str(Whattofind) + ".csv")
            elif yes == 1:
                data = yf.download(
                    str(Whattofind),
                    period="60d").to_csv(str(Whattofind) + ".csv")
            print("The data have been saved in the directory " +
                  str(os.getcwd()))

        elif action == 3:  #A function to allow user to open csv files
            print(
                "Which csv do you want to open? (Please type the full name): ")
            currentpathnote = os.getcwd()
            os.chdir(str(currentpathnote))
            for file in glob.glob("*.csv"):
                print(file)
            filename = input("")
            try:
                df = pd.read_csv(filename,
                                 header=0,
                                 index_col='Date',
                                 parse_dates=True)
                print(df)
            except ValueError:
                df = pd.read_csv(filename, skiprows=1)
                print(df)

        elif action == 4:  #A function allows the user to search up s&p 500 index history
            choice = 0
            stockidx = financeplayground.get_index()
            sp500 = pdr.get_data_yahoo(stockidx, period=('60d'))
            print(sp500.tail())
            mpf.plot(sp500, type='line', title=stockidx)
            choice = gf.getnumber(
                "What would you like to do?\nPress 1 to show the candle plot\nPress 2 to download data\n"
            )
            if choice == 1:
                mpf.plot(sp500, type='candle', title=stockidx, style='charles')
            if choice == 2:
                lastdownload = open('history/downloadhistory.dat', 'rb')
                history = pickle.load(lastdownload)
                hisstart = pickle.load(lastdownload)
                hisend = pickle.load(lastdownload)
                startdate = gf.changestringtodate(hisstart)
                enddate = gf.changestringtodate(hisend)
                enddate = enddate + dt.timedelta(days=1)
                data = yf.download(str(stockidx), start=startdate,
                                   end=enddate).to_csv(str(stockidx) + ".csv")

        elif action == 5:  #get tickers
            action = gf.getnumber(
                "Which ticket do you want to get?\ns&p press 1\nnasdaq press 2"
            )
            if action == 1:
                financeplayground.save_tickers(
                    'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies',
                    'sp500tickers.pickle', 0)
                with open("ticker/sp500tickers.pickle", "rb") as f:
                    tickers = pickle.load(f)
                    print(tickers)
            if action == 2:
                financeplayground.save_tickers(
                    'https://en.wikipedia.org/wiki/NASDAQ-100',
                    'nasdaqtickers.pickle', 1)
                with open("ticker/nasdaqtickers.pickle", "rb") as f:
                    tickers = pickle.load(f)
                    print(tickers)

        elif action == 6:  #download s&p 500 data and automatically compile them into a separate file contain all the adjusted close
            action = gf.getnumber(
                "Which data do you wanna get?\ns&p press 1\nnasdaq press 2\nget all press 3 "
            )
            if action == 1:
                update.updatesp()
            if action == 2:
                update.updatenasdaq()
            if action == 3:
                update.updateall()

        elif action == 7:
            action = gf.getnumber('s&p 500 press 1\nnasdaq press 2 ')
            if action == 1:
                if not os.path.exists(
                        'joined_closed/sp500tickers.pickle_joined_closed.csv'):
                    financeplayground.compile_data('sp500tickers.pickle',
                                                   'stocks_dfs')
                financeplayground.visualize_data(
                    'sp500tickers.pickle_joined_closed.csv')
            if action == 2:
                if not os.path.exists(
                        'joined_closed/nasdaqtickers.pickle_joined_closed.csv'
                ):
                    financeplayground.compile_data('nasdaqtickers.pickle',
                                                   'stocks_nasdaq')
                financeplayground.visualize_data(
                    'nasdaqtickers.pickle_joined_closed.csv')

        elif action == 8:  ## Research stocks
            researcher.research()

        elif action == 9:  #A function that allow the user to use the calculator
            calc = ""
            while calc != 'exit':
                calc = input("Type Calculation (type exit to exit): \n")
                print("Answer: " + str(eval(calc)))
Beispiel #27
0
from pandas_datareader import data as pdr
import yfinance as yf

yf.pdr_override()  #빠르게 data 다운로드

sec = pdr.get_data_yahoo('005930.KS', start='2018-05-04')
msft = pdr.get_data_yahoo('MSFT', start='2018-05-04')
print(sec.head(10))
tmp_msft = msft.drop(columns='Volume')
print(tmp_msft.tail())
print(sec.index)
print(sec.columns)

#import matplotlib.pyplot as plt
#plt.plot(sec.index,sec.Close,'b',label='Samsung Electronics')
#plt.plot(msft.index,msft.Close,'r--',label='Microsoft')
#plt.legend(loc='best')
#plt.show()
print(sec['Close'])
Beispiel #28
0
class BacktestTool(AbstractTool):
    """
    This class represents a tool for backtesting purposes.

    """
    yf.pdr_override()

    def __init__(self,
                 symbol='AAPL',
                 initial_date="2019-01-01",
                 final_date="2020-01-01"):
        """
        Class constructor.

        @param initial_balance: initial_balance
        @@type initial_balance: float
        @param symbols: symbols that should be used while backtesting
        @@type symbols: list of strings
        @param initial_date: initial date to get data
        @@type initial_date: datetime string in the format "%YYYY-%MM-%DD"
        @param final_date: final date to get data
        @@type final_date: datetime string in the format "%YYYY-%MM-%DD"
        @param take_profit: take profit constant (where stop the operation for profit)
        @@type take_profit: float
        @param stop_loss: stop loss constant (where stop the operation for loss)
        @@type stop_loss
        """

        super().__init__(tool_name="Backtest")

        self.symbol = symbol
        self.data = None
        self.initial_date = initial_date
        self.final_date = final_date

    def execute_agent(self,
                      agent,
                      balance,
                      percentage,
                      take_profit,
                      stop_loss,
                      save_log=True):
        """
        Runs the backtest tool.

        @param agent: the agent the method should be executed on.
        @@type agent: class Agent
        """
        self.initial_balance = balance
        print(f'Running backtest on agent {agent.get_name()}...')

        data = self.get_data()

        total_length = len(data)
        for i in tqdm(range(1, total_length)):
            agent.update(data[0:i])

        active_operation_data = agent.get_active_operation_data(
            data['Close'][-1])
        operation_history = agent.get_history()
        balance = agent.get_balance()

        data = self._create_backtest_log_data(agent, active_operation_data,
                                              balance, operation_history)

        # Save log file
        if save_log:
            self.log.log(data)

    def get_data(self):
        """
        Method to get data online using yahoo finance api.

        @return data: data achieved online
        @@@type data: list of dataframes
        """
        if self.initial_date and self.final_date:
            # Get stock data from yahoo
            self.data = pdr.get_data_yahoo(self.symbol,
                                           start=self.initial_date,
                                           end=self.final_date)

            self.data['Change'] = self.data['Close'].pct_change()

            return self.data

        elif not initial_date:
            raise ValueError("Initial date can't be an empty value.")

        else:
            raise ValueError("Final date can't be an empty value.")

    def _create_backtest_log_data(self, agent, active_operation_data, balance,
                                  operation_history):
        """
        Method to create the data that goes into the log file.

        """
        count_buy_operations, count_buy_success_operations, count_sell_operations, count_sell_success_operations, count_successful_operations, count_failed_operations = 0, 0, 0, 0, 0, 0

        for history in operation_history:
            if history['Entered as'].upper() == 'BUY':
                if history['Result'].upper() == 'SUCCESS':
                    count_buy_success_operations += 1
                count_buy_operations += 1
            else:
                if history['Result'].upper() == 'SUCCESS':
                    count_sell_success_operations += 1
                count_sell_operations += 1
            if history['Result'].upper() == 'SUCCESS':
                count_successful_operations += 1
            else:
                count_failed_operations += 1

        total_balance = balance + active_operation_data[1]
        initial_balance = self.initial_balance
        # Initialize as empty
        data = {}
        # Get model name and tools parameters
        data['Used on'] = agent.get_name()
        data['Symbol'] = self.symbol
        data['Initial date'] = self.initial_date
        data['Final date'] = self.final_date
        data['Balance'] = {
            'Initial (R$)': round(self.initial_balance, 2),
            'Final (R$)': round(total_balance, 2)
        }
        data['Profit'] = {
            'Total profit (R$)':
            round(total_balance - initial_balance, 2),
            'Total profit (%)':
            f'{((total_balance - initial_balance) / initial_balance * 100).round(2)} %'
        }
        data['Active'] = {
            'Total (#)': round(active_operation_data[0], 2),
            'Total (R$)': active_operation_data[1]
        }
        data['Operations'] = {
            'Total':
            count_buy_operations + count_sell_operations +
            active_operation_data[0],
            'Total closed':
            count_buy_operations + count_sell_operations,
            'Total buy closed':
            count_buy_operations,
            'Total buy closed successful':
            count_buy_success_operations,
            'Total sell closed':
            count_sell_operations,
            'Total sell closed successful':
            count_sell_success_operations,
            'Total successful':
            count_successful_operations,
            'Total failed':
            count_failed_operations,
            'History':
            operation_history
        }

        return data
Beispiel #29
0
from pandas_datareader import data as pdr
import yfinance as yf
import matplotlib.pyplot as plt
yf.pdr_override() # pandas data reader를 재정의

sec = pdr.get_data_yahoo('005930.KS', start='2018-05-04')
msft = pdr.get_data_yahoo('MSFT', start='2018-05-04')

sec_dpc = (sec['Close']/sec['Close'].shift(1) - 1)*100
sec_dpc.iloc[0] = 0
sec_dpc_cs = sec_dpc.cumsum()
print(sec_dpc.head())

msft_dpc = (msft['Close']/msft['Close'].shift(1) - 1)*100
msft_dpc.iloc[0] = 0
msft_dpc_cs = msft_dpc.cumsum()

plt.plot(sec_dpc_cs.index, sec_dpc_cs, 'b', label='Samsung')
plt.plot(msft_dpc_cs.index, msft_dpc_cs, 'r--', label='MS')
plt.ylabel('Change %')
plt.grid(True)
plt.legend(loc='best')
plt.show()
Beispiel #30
0
import csv
import os
import time
import pickle

app = dash.Dash(external_stylesheets=[dbc.themes.LUX])

#read some older tweets for start and sort them from the most popular
df = pd.read_csv('data/20200706_102141_Tesla_tweets.csv')
df_pop = df[['retweetcount', 'text']]
df_pop = df_pop.drop_duplicates(subset='text').reset_index(drop=True)
df_pop = df_pop.sort_values(by=['retweetcount'],
                            ascending=False).reset_index(drop=True)
df2 = df_pop[['text']]
df2.columns = ['Tweeter Feed']
yf.pdr_override()  #use pandas_datareader

#app layout using some Boostrap components
app.layout = dbc.Container(
    [
        html.H1('Tesla Stock & Twitter Dashboard'),
        html.Hr(),
        html.Label('Select start and end dates:'),
        html.Br(),
        dbc.Container([
            dcc.DatePickerRange(id='my_date_picker',
                                min_date_allowed=datetime.datetime(2015, 1, 1),
                                max_date_allowed=datetime.date.today(),
                                start_date=datetime.datetime(2020, 1, 1),
                                end_date=datetime.date.today()),
            dbc.Button(id='submit-button',