示例#1
0
def stock_report(ticker):
    try:
        long_period = stock_sources.long_period_df(ticker)

        short_period = stock_sources.short_period_df(ticker)

        # Find the current price

        current_price = stock_sources.only_close(short_period)[ticker][-1:]
    except Exception as e:
        print(f"Error: {e}")
    # print(current_price)

    # # Find the 200 day moving average.

    two_hundred_day_ma = stock_sources.only_close(
        long_period
    ).rolling(window=200).mean().iloc[-1:, :]

    # If a stock's 200ma > it's current price, then print true
    for stock, price in two_hundred_day_ma.items():
        report = pd.DataFrame(
            {
                'Current': [1],
                '200 ma': [1]
            }, index=ticker
        )
        # Print statements test to make sure the values are correct
        # print(price.values)
        # print(current_price[stock].values)

        # If the 200 ma is greater than the current price add it to the list to email.
        if price.values > current_price[stock].values:
            # print(f'{stock}\'s Current Price is BELOW its\'s 200 ma')
            report.loc[stock, 'Current'] = round(
                current_price[stock].values[0], 2)
            report.loc[stock, '200 ma'] = round(price.values[0], 2)
            # Give me only the stocks that have a lower current value than the 200ma
            mask = report['Current'] != 1

            report_to_email = report[mask]
            report_to_email.sort_index(inplace=True)
            report_to_email = report_to_email.to_html(
                notebook=True)
            return report_to_email

        else:
            pass
示例#2
0
def main():
    ####################### IMPORTS ##########################################

    # Personal Modules
    from read_config import export_variables
    from Email.email_test import send_email
    # from data_analysis import stock_report
    from crypto_data_collection import get_close_data
    from arima_test import arima_prediction
    import stock_sources
    import read_config
    from random_walk import random_walk_benchmark
    from default_analysis import default_analysis, plot_dataframe, plot_dataframe_zoomed

    # Personal information to be used in the program
    email, password, crypto_api = read_config.export_variables(
        env_location)

    ####################### Stock Data ######################################
    stock_df = stock_sources.long_period_df(stock_ticker)
    stock_close_df = stock_sources.only_close(stock_df)

    ####################### Stock Predictions ###############################
    print("Training the Model")
    stock_train_prediction_mse, stock_prediction_df = arima_prediction(
        stock_close_df)

    ###################### Stock Benchmark #################################

    stock_prediction_benchmark_mse = random_walk_benchmark(stock_close_df)

    # stock_report = stock_report(stock_ticker)
    ####################### Crypto Data ####################################

    # end = datetime.date.today()
    # 2 Years according to stock data
    # start = end - datetime.timedelta(days=505)
    # crypto_close_df = get_close_data(crypto_ticker, start, end, crypto_api)

    ####################### Crypto Predictions ###############################

    # crypto_train_prediction_mse, crypto_prediction = arima_prediction(
    #     crypto_close_df
    # )
    # crypto_prediction_mse, crypto_prediction = arima_prediction(
    #     crypto_close_df)

    ###################### Crypto Prediction Benchmark #######################

    # crypto_prediction_benchmark_mse = random_walk_benchmark(crypto_close_df)

    # good_stock_prediction = False
    # good_crypto_prediction = False

    # good_stock_prediction = True if stock_train_prediction_mse < stock_prediction_benchmark_mse else False
    # good_crypto_prediction = True if crypto_train_prediction_mse < crypto_prediction_benchmark_mse else False

    # print('\n\n\n')
    # print(f"Good Stock Prediction: {good_stock_prediction}")

    # print(f"Good Crypto Prediction: {good_crypto_prediction}")

    # If stock_train_prediction_mse IS LESS than random_walk_benchmark, then use it.
    # Else Don't and only email the user moving averages graph

    ####################### Default Analysis ################################

    current_stock_price = stock_sources.current_price(stock_ticker)

    # DEFAULT
    stock_analysis_df = default_analysis(
        stock_prediction_df, current_stock_price).copy()

#     crypto_default_analysis = default_analysis(
#         crypto_prediction, current_crypto_price)

    # Crypto Data probably will only show daily results
    # current_crypto_price = pd.DataFrame(crypto_close_df[-1])
    # print(current_crypto_price)
    # print(crypto_close_df)
    # crypto_default_analysis = default_analysis(
    #     crypto_close_df['Close'], current_crypto_price)
    # print(f"Default Crypto Analysis Says: {crypto_default_analysis}")

    ####################### STRATEGY #######################################

    # If this is True, email them everything

    ####################### EMAIL ##########################################

    # Should contain: MSEs, Predictions, Current Price, Moving Average Chart
    # If no move to be made, moving averaage chart should be sent regardless along with a different title
    # If the predicted price is close to the 50 day or 100 ma, then email.
    # conditions = [
    # good_stock_prediction == True and stock_default_analysis_response == True
    # crypto_default_analysis == True
    # ]

    # Stock Graphs
    plot_dataframe(stock_ticker, stock_analysis_df)
    plot_dataframe_zoomed(stock_ticker, stock_analysis_df)

    # Crypto Graphs
    # plot_dataframe(crypto_ticker, crypto_prediction)
    # plot_dataframe_zoomed(crypto_ticker, crypto_prediction)

    # SENDING THE ACTUAL EMAIL
    # send_email(email, password)

    print(f'\nPredicted Prices and Moving Averages for {stock_ticker}\n')
    print(stock_analysis_df.tail(10))
    print("---------------------------------------------------------")
    print(f"\nRandom Walk RMSE: {stock_prediction_benchmark_mse}")
    print(f"ARIMA Train RMSE: {stock_train_prediction_mse}")
示例#3
0
    actual_50 = df['50ma'][-50:-4]
    actual_100 = df['100ma'][-50:-4]

    predicted_close = df['Close'][-5:]
    predicted_50 = df['50ma'][-5:]
    predicted_100 = df['100ma'][-5:]

    actual_close.plot(title=f"{ticker} from {start} to {end} + 5",
                      c="b",
                      legend=True,
                      grid=True)
    actual_50.plot(c="yellow", legend=True)
    actual_100.plot(c="orange", legend=True)

    predicted_close.plot(c="red", legend=True)
    predicted_50.plot(c="green", legend=True)
    predicted_100.plot(c="black", legend=True)
    # plt.show()
    plt.savefig("./Functions/Email/stock_image2.png", orientation="landscape")


if __name__ == "__main__":
    ticker = "AAPL"
    long_df = stock_sources.long_period_df(ticker)
    current_price_df = stock_sources.current_price(ticker)
    df = default_analysis(long_df, current_price_df, sensitivity=0.01)
    da = default_analysis(long_df, current_price_df)
    print(da)
    plot_dataframe(ticker, long_df)
    plot_dataframe_zoomed(ticker, long_df)
示例#4
0
        test_model = ARIMA(test, order=second_best_order)
        test_results = test_model.fit()

    test_forecast = pd.DataFrame(
        test_results.forecast(5)[0],
        columns=['Close']
    )
    test_predictions = test.append(test_forecast)
    return train_mse, test_predictions


if __name__ == '__main__':

    print('\n\n\n\nSTOCK DATA')
    ticker = 'AAPL'
    df = long_period_df(ticker)

    # Chopping that dataframe down to Close only information
    close_df = only_close(df)

    # Finding the percent change of the closing data
    standard_returns = close_df.pct_change().dropna()

    arima_prediction(close_df)

    # # print('\n\n\n\nCrypto Data')
    # ticker = 'BTC'

    # end = datetime.date.today()
    # start = end - datetime.timedelta(days=505)