Ejemplo n.º 1
0
def priceopt():
    path_img = 'static/images'
    if not os.path.exists(path_img):
        os.mkdir(path_img)
    # list_funds_selected = []
    list_etf = []
    list_funds_all = []
    path_master_file = os.getcwd() + '/data/ETFSymbols.csv'

    with open(path_master_file) as fp_master_reader:
        read_fund_list = csv.reader(fp_master_reader, delimiter=',')
        for fund in list(read_fund_list):
            for ticker in fund:
                list_funds_all.append(ticker)

    if request.method == 'POST':
        list_etf = request.form.getlist("option")
        print("In Flask \n")
        print(list_etf)
        daysForProjection = request.form["daysForProjection"]
        dict_fund_model = {}
        dict_r2 = {}
        for etf in list_etf:
            model_etf = trainModel.TrainTestModel(etf)
            dict_r2[etf] = model_etf['r2']
            dict_fund_model[etf] = model_etf
        list_fund_progress_graphs = futureProjection.fundFutureProjection(dict_fund_model,
                                                                          list_etf,
                                                                          int(daysForProjection))
        length = len(list_fund_progress_graphs)
        print(list_fund_progress_graphs)
    return render_template("predictionresult.html",
                           list_fund_progress_graphs=list_fund_progress_graphs,
                           length=length)
Ejemplo n.º 2
0
def calculateModel(img_path, list_etf, future_proj_days):
    if future_proj_days == 0:
        future_proj_days = 750  # Number of days in future for which values to be predicted. In live code this is user provided
    print("calculating for future projection days: " + str(future_proj_days))
    dict_r2 = {}  # Dictionary for storing r2 values
    dict_fund_model = {
    }  # Dictionary to store ETF funds and their corresponding model
    start = dt.datetime(2005, 1, 1)
    end = dt.datetime.today()
    for etf in list_etf:
        model_etf = trainModel.TrainTestModel(etf, start, end)
        dict_r2[etf] = model_etf['r2']
        dict_fund_model[etf] = model_etf

    projection_images = []
    dict_future_projection = {}
    for etf_iterator in range(0, len(list_etf)):
        fund_lr_model = dict_fund_model[list_etf[etf_iterator]]
        r2_value = round(fund_lr_model['r2'], 6) * 100
        #coefficient = fund_lr_model['Coefficient'][0]
        #intercept = fund_lr_model['Intercept'][0]
        coefficient = fund_lr_model['Coefficient'][0]
        intercept = fund_lr_model['Intercept'][0]
        df_fund_model_dataset = pd.DataFrame(fund_lr_model['Test_Pred_data'])
        plt.plot(df_fund_model_dataset['Date'],
                 df_fund_model_dataset['ActualPrice'])
        plt.plot(df_fund_model_dataset['Date'],
                 df_fund_model_dataset['PredictedPrice'])
        plt.title(
            f'Performance Trend for {list_etf[etf_iterator]} with confidence level of {r2_value}% \n'
        )
        #plt.show()
        image_name = 'Projection_' + list_etf[etf_iterator] + str(
            int(time.time()))
        plt.savefig(img_path + image_name + ".png")
        plt.close()
        projection_images.append(image_name)

        # Predicting future values
        df_future_projection = futureDates.createFutureDates(future_proj_days)
        df_future_projection['FutureValue'] = (
            (df_future_projection['DateFloat']).values.reshape(-1, 1) *
            coefficient) + intercept
        dict_future_projection[list_etf[etf_iterator]] = df_future_projection
        #df_future_projection['FutureValue'] = ((df_future_projection['DateFloat']) * coefficient) + intercept
        file_name = 'FutureProjection_' + list_etf[etf_iterator] + str(
            int(time.time())) + '.csv'
        #image_future_projection = 'FutureProjections_' + list_etf[etf_iterator] + '.csv'
        df_future_projection.to_csv(img_path + file_name)
        # This part of code is to plot future value graph. This is irrelevant since with LR model growth/fall will always
        # be in a straight line. To test, you can un-comment these code-lines.
        #plt.plot(df_future_projection['Date'], df_future_projection['FutureValue'])
        #plt.title(f'Future growth pattern for {list_etf[etf_iterator]} with confidence level of {r2_value}% \n')
        #plt.show()
    return projection_images
def priceopt():
    path_img = 'static/images'
    if not os.path.exists(path_img):
        os.mkdir(path_img)
    list_funds_selected = []
    list_funds_all = []
    path_master_file = os.getcwd() + '/data/ETFSymbols.csv'

    with open(path_master_file) as fp_master_reader:
        read_fund_list = csv.reader(fp_master_reader, delimiter=',')
        for fund in list(read_fund_list):
            for ticker in fund:
                list_funds_all.append(ticker)

    if request.method == 'POST':
        list_funds_selected = request.form.getlist('option')
        int_projection_days = int(request.form.get('daysForProjection'))
        dict_performance_images = trainModel.TrainTestModel(
            list_funds_selected, path_img, int_projection_days)
        return render_template('Predictionresult.html',
                               option_list=list_funds_all,
                               item_list=dict_performance_images,
                               sub_img_path='/' + path_img)
def predictprice():
    if request.method == "POST":
        list_etf = request.form["etf_fund"]
        daysForProjection = request.form["daysForProjection"]
        #dict_stock = trainModel.TrainTestModel(stock_name)
        dict_fund_model = {}
        dict_r2 = {}
        for etf in list_etf:
            model_etf = trainModel.TrainTestModel(etf)
            dict_r2[etf] = model_etf['r2']
            dict_fund_model[etf] = model_etf
        #dict_future_projection = {}
        #for etf_iterator in range(0,len(list_etf)):
        #fund_lr_model = dict_fund_model[list_etf[etf_iterator]]
        
        dict_fund_progress_graphs = futureProjection.fundFutureProjection(dict_fund_model,list_etf, int(daysForProjection))
        
        #dict_fund_model[stock_name] = trainModel.TrainTestModel(stock_name)
        #daysForProjection = 500
        #dict_future_projection = futureProjection.fundFutureProjection(dict_fund_model,stock_name,int(daysForProjection))
        #(dict_trained_stock_model, etf_fund, future_proj_days):
        length = len(list_etf)
    return render_template("prediction.html", dict_fund_progress_graphs = dict_fund_progress_graphs, list_etf = list_etf, length = length)
Ejemplo n.º 5
0
from pymongo import MongoClient
import csv
import futureDates
import trainModel
pd.plotting.register_matplotlib_converters(explicit=True)

# ETF funds for testing the code. In live code this is a selection from UI
list_etf = ['VHT', 'VIS', 'VOX', 'VCR', 'VDC']
future_proj_days = 750  # Number of days in future for which values to be predicted. In live code this is user provided
dict_r2 = {}  # Dictionary for storing r2 values
dict_fund_model = {
}  # Dictionary to store ETF funds and their corresponding model
start = dt.datetime(2005, 1, 1)
end = dt.datetime.today()
for etf in list_etf:
    model_etf = trainModel.TrainTestModel(etf)
    dict_r2[etf] = model_etf['r2']
    dict_fund_model[etf] = model_etf

dict_future_projection = {}
for etf_iterator in range(0, len(list_etf)):
    fund_lr_model = dict_fund_model[list_etf[etf_iterator]]
    r2_value = round(fund_lr_model['r2'], 6) * 100
    #coefficient = fund_lr_model['Coefficient'][0]
    #intercept = fund_lr_model['Intercept'][0]
    coefficient = fund_lr_model['Coefficient'][0]
    intercept = fund_lr_model['Intercept'][0]
    df_fund_model_dataset = pd.DataFrame(fund_lr_model['Test_Pred_data'])
    plt.plot(df_fund_model_dataset['Date'],
             df_fund_model_dataset['ActualPrice'])
    plt.plot(df_fund_model_dataset['Date'],