def gather_data(): """call financial web scraping API with user defined parameters""" dir_path file_path = os.path.split(os.path.abspath(__file__)) stocks_path = dir_path + '../data/stocks.json' with open(stocks_path, 'r') as f: data = json.load(f) # nyse100, nasdaq100, snp500 = data['NYSE100'], data['NASDAQ100'], data['SNP500'] # other_stocks = ['AAPL', 'GOOG', 'GPRO', 'TSLA', 'APRN', 'FB', 'NVDA', 'SNAP', 'SPY', 'NFLX', 'AMZN', 'AMD'] # tickers = nyse100 + nasdaq100 + snp500 + other_stocks # sources = valid_sources() # used for testing stocks = ['AAPL', 'TSLA'] # sources = ['seekingalpha', 'bloomberg', 'techcrunch', 'marketwatch', 'yahoofinance'] sources = ['seekingalpha', 'bloomberg'] csv_path = dir_path + '../data/examples.csv' json_path = dir_path + '../data/links.json' stats_path = dir_path + '../data/stocker_stats.json' dm = Stocker(stocks, sources, csv_path, json_path, stats_path=stats_path) flags = { 'date_checker': True, 'depth': 1, 'validate_url': False, 'length_check': True, 'min_length': 100, } dm.stock(shuffle=True, flags=flags)
def listenToClient(client): data = bytearray() while True: datachunk = client.recv(BUFFER) if not datachunk: break data += datachunk client.close() data = data.decode().strip('\n').replace(" ", "").split(",") requestID = data[0] request_symbol = data[1] days_into_future = data[2] s3_client = boto3.client('s3') obj = s3_client.get_object( Bucket='elasticbeanstalk-us-west-1-643247086707', Key=request_symbol + '.csv') payload = str(obj['Body'].read()).replace(r'\n', '\n') with open(request_symbol + ".csv", 'w') as f: f.write(payload) stock = Stocker(request_symbol, 'CSV', '') predicted_price = stock.create_prophet_model( days=int(days_into_future), resample=False, symbol=request_symbol, requestID=requestID) graph = open(request_symbol + "_" + requestID + ".png", "rb").read() os.remove(request_symbol + '.csv') r.set(request_symbol + "_" + requestID, graph) os.remove(request_symbol + "_" + requestID + ".png") r.set(request_symbol + "_price_" + requestID, predicted_price)
def stockValueInFuture(): print( "enter the code for the company you want to check the predictions of:") companyCode = input() print("Enter the No. of Days you want to predict the stocks for:") n = int(input()) st = Stocker(companyCode) st.create_prophet_model(n)
def cname_test(): passed, failed = Test('cname_test', 1), Test('cname_test', 0) tickers = ['AAPL', 'GOOG', 'GPRO', 'TSLA', 'NFLX', 'AMZN', 'AMD'] sources = ['seekingalpha'] dm = Stocker(tickers, sources, csv_path=None, json_path=None) dm.build_queries(depth=2) q2s = dm.queries[:: 2] #starting at index 2 (to get the cname query), get every second query for q in q2s: print(q)
def stockValueOverTime(): print( "enter the code for the company you want to check the predictions of:") companyCode = input() print("Enter the start Date(SYNTAX: YYYY-MM-DD)") start_date = input(datetime.date) print("Enter the end Date(SYNTAX: YYYY-MM-DD)") end_date = input(datetime.date) print("Enter number of shares") nshares = int(input()) st = Stocker(companyCode) st.buy_and_hold(start_date, end_date, nshares)
def sytra_follow_fixture(sytra_file_fixture): # prepare Stocker as stck stck = Stocker(sytra_file_fixture) # follow, force: already exists summary.csv stck.follow_interface(codes=[], force=True) # set analyconf.toml (testroot/'4382'/'analyconf.toml').write_text(ac4382) (testroot/'6890'/'analyconf.toml').write_text(ac6890) # given style yield stck
def get_analytics(query_params): incoming_date_format = "%d-%m-%Y-%H:%M" new_date_format = "%Y-%m-%d" start_date = datetime.strptime(query_params["start_date"], incoming_date_format).strftime(new_date_format) end_date = datetime.strptime(query_params["end_date"], incoming_date_format).strftime(new_date_format) symbol = query_params["symbol"] stock = Stocker(symbol) statistics = microsoft.get_stats(start_date = start_date, end_date = end_date, stats = ['Open', 'High','Low', 'Daily Change', 'Volume']) profit = microsoft.buy_and_hold(start_date='2012-03-01', end_date='2012-03-31') predict = microsoft.create_prophet_model() response = { "statusCode" : status, "body" : { "open": statistics[0], "high": statistics[1], "low": statistics[2], "daily_change": statistics[3], "volume": statistics[4], "profit": profit, "predict": predict } } return json.dumps(response)
def __init__(self, dt=0.25): """Initialize model object. Parameters ---------- dt : float Time step in days. """ self._string_test = "Hello there" self.dt = dt self.numSusceptible = 0 self.numInfected = 0 self.numRecovered = 0 self.INIT_CATTLE_PROBABILITY = 0.02 self.list_cattle = [] self.list_environ = [] self.list_environ.append(RoadEast()) self.list_environ.append(RoadWest()) self.list_environ.append(SaleBarn()) self.list_environ.append(Stocker()) self.list_environ.append(Feedlot()) self.list_environ.append(Abattoir()) self.num_farms = 6 self.farm_cattle_init()
def test_follow(sytra_file_fixture): stck = Stocker(sytra_file_fixture) # NO ERROR # follow 4382, 6890 in prepare stck.follow_interface(codes=[], force=True) # create and print sbase stck.create_sbase() # save to sytraconf.toml stck.dump()
def run(root_parser): namespace = root_parser.parse_args() if namespace.subcom=='init': Stocker.stocker_init(**vars(namespace)) return stck = Stocker(namespace.rootpath) if namespace.debug: print(stck.get_follows_tuple(), stck.get_lateststr()) print(namespace) return if hasattr(namespace, 'func'): namespace.func( stck, namespace) if namespace.create: stck.create_sbase() stck.dump()
def listenToClient(client): data = bytearray() while True: datachunk = client.recv(BUFFER) if not datachunk: break data += datachunk client.close() data = data.decode().strip('\n').replace(" ", "").split(",") requestID = data[0] request_symbol = data[1] days_into_future = data[2] s3_client = boto3.client('s3') resource = boto3.resource('s3') my_bucket = resource.Bucket('elasticbeanstalk-us-west-1-643247086707') objs = list(my_bucket.objects.filter(Prefix='')) files = list() for obj in objs: _, filename = os.path.split(obj.key) files.append(filename) if request_symbol + ".csv" in files: obj = s3_client.get_object( Bucket='elasticbeanstalk-us-west-1-643247086707', Key=request_symbol + '.csv') lastUpdated = obj['LastModified'] if datetime.now() - lastUpdated.replace(tzinfo=None) >= timedelta( days=7): Stocker(ticker=request_symbol).stock.to_csv(request_symbol + '.csv') my_bucket.upload_file(request_symbol + '.csv', Key=request_symbol + '.csv') os.remove(request_symbol + '.csv') else: Stocker(ticker=request_symbol).stock.to_csv(request_symbol + '.csv') my_bucket.upload_file(request_symbol + '.csv', Key=request_symbol + '.csv') os.remove(request_symbol + '.csv') sock = socket.socket() sock.connect((DM_SERVICE, DM_PORT)) msg = requestID + "," + request_symbol + "," + days_into_future sock.send(msg.encode('ascii')) sock.close()
def stock_info(): ## Get historical data on stock ##print("Data provided for free by IEX. \nView IEX’s Terms of Use at https://iextrading.com/api-exhibit-a/") ticker = get_stock() try: stock = Stocker(ticker) except: print("Ticker not found.") print("\n\nGeneral info on historical data of stock and movement in the past\n\n") time.sleep(5) stock.plot_stock() ## Object to return for predictive model #stock_obj = return stock
def sytra_file_fixture(): if testroot.exists(): shutil.rmtree(str(testroot)) # create directory and sytraconf.toml Stocker.stocker_init(rootdir=str(testroot), daystr='2020-07-10') # directory for holidays (testroot/'holidays'/'2020.csv').write_text(h2020) (testroot/'holidays'/'2021.csv').write_text(h2021) # directory for prepare (testroot/'prepare'/'4382.csv').write_text(s4382) (testroot/'prepare'/'6890.csv').write_text(s6890) # sbase.csv (testroot/'summary.csv').write_text(sum0713) # load style yield str(testroot)
def __init__(self, dt=0.25, init_extra_weight=0.0): """Initialize model object. Parameters ---------- dt : float Time step in days. init_extra_weight : float Adds this many pounds to the initialization of each cattle in order to speed-up the simulation. """ self._string_test = "Hello there" self.dt = dt self.numSusceptible = 0 self.numInfected = 0 self.numRecovered = 0 self.cumulativeInfected = 0 self.INIT_CATTLE_PROBABILITY = 0.02 self._init_extra_weight = init_extra_weight self.plot_figure = None self.plot_axes = None self.plot_image = None self.list_cattle = [] self.list_farms = [] self.salebarn = SaleBarn() self.stocker = Stocker() self.feedlot = Feedlot() self.abattoir = Abattoir() self.roadeast = RoadEast(adjacent_salebarn=self.salebarn) self.roadwest = RoadWest(adjacent_salebarn=self.salebarn) #- Linkages for multi-way movement between certain regions: self.salebarn.adjacent_stocker = self.stocker self.salebarn.adjacent_feedlot = self.feedlot self.stocker.adjacent_salebarn = self.salebarn self.feedlot.adjacent_salebarn = self.salebarn self.feedlot.adjacent_abattoir = self.abattoir self.num_farms = 6 self.farm_cattle_init()
from stocker import Stocker import matplotlib.pyplot as plt microsoft = Stocker(ticker='MSFT') stock_history = microsoft.stock # microsoft.plot_stock(start_date='2001-01-05', end_date='2002-02-05', stats=["Adj. Close"], plot_type="basic") # microsoft.buy_and_hold(start_date='2001-01-05', end_date='2002-02-05', nshares=200) model, model_data = microsoft.create_prophet_model() model.plot_components(model_data) plt.show() print(microsoft.weekly_seasonality) microsoft.weekly_seasonality = True print(microsoft.weekly_seasonality) model, model_data = microsoft.create_prophet_model(days=0) model.plot_components(model_data) plt.show() microsoft.weekly_seasonality = False microsoft.changepoint_date_analysis() model, future = microsoft.create_prophet_model(days=180)
import stocker from stocker import Stocker stock_name = 'AAPL' s = Stocker(stock_name) #s.plot_stock() #stock_data = stocker.get_data.main(stock_name, years=1) #stock_history = s.stock #print(stock_history.tail()) prediction, a, model = stocker.predict.tomorrow('GOOG', plot=True) #E = [17.95, 40.85, 1.88, 11.92, 177.70, 24.11] #p = model.predict(E) #print(prediction) #stock_names = ['GAZP.ME', 'TSLA', 'BP', 'AAPL', 'GOOG', 'SBER.ME'] #E = [17.95, 40.85, 1.88, 11.92, 177.70, 24.11]
!git clone 'https://github.com/WillKoehrsen/Data-Analysis.git' os.chdir('./Data-Analysis/stocker') !pip install -U quandl numpy pandas fbprophet matplotlib pytrends pystan import stocker from stocker import Stocker data = pd.DataFrame() stocks = ['AAPL', 'ADBE', 'SYMC', 'EBAY', 'MSFT', 'QCOM', 'HPQ', 'JNPR', 'AMD', 'IBM'] apple = Stocker('AAPL') df = apple.make_df('1990-12-12', '2016-12-12') df = df.set_index(['Date']) apple_closes = df['Adj. Close'] df.head() apple_closes.head() for ticker in stocks: name = str(ticker) print(name) s = Stocker(name) df = s.make_df('2000-12-12', '2016-12-12') df = df.set_index(['Date']) data[name] = df['Adj. Close']
from stocker import Stocker amazon = Stocker('AMZN') amazon.plot_stock() # predict days into the future model, model_data = amazon.create_prophet_model(days=90) amazon.evaluate_prediction()
def GetStockPrediction(csvfile,ticker,inifile,litemode=False): ### MAKE HISTORY GRAPH. hstg = 30 # number of days # Define files and filetypes ext = ".pdf" ModZ =[str(stocksfolder+ticker+".HighLowClose" + ext), #0 str(stocksfolder+ticker+".ChangeVolume"+ext), #1 str(stocksfolder+ticker+".ChangePoint"+ext), #2 # str(stocksfolder+ticker+".ChangePointTrends"+ext),#3 str(stocksfolder+ticker+".evaluate_prediction"+ext),#4 str(stocksfolder+ticker+".TrendsA"+ext),#5 str(stocksfolder+ticker+".Naive" + str(hstg) + ext)] #6 StockFiles=[str(stocksfolder+ticker+".HighLowClose" + ext), #0 str(stocksfolder+ticker+".ChangeVolume"+ext), #1 str(stocksfolder+ticker+".ChangePoint"+ext), #2 str(stocksfolder+ticker+".ChangePointTrends"+ext),#3 str(stocksfolder+ticker+".evaluate_prediction"+ext),#4 str(stocksfolder+ticker+".TrendsA"+ext),#5 str(stocksfolder+ticker+".Naive" + str(hstg) + ext),#6 # ACTUAL MACHINE LEARNING BEGINS str(stocksfolder+ticker+".linear_regression_01"+ext)]#7 # print(csvfile) # df = pd.read_csv(csvfile) ## START ANALSYS. Months = str((today - datetime.timedelta(days=hstg)).date()) ### Stocker is initialized and will retrieve the entire stock's data as it is read in the file. stock = Stocker(ticker=ticker, exchange='CSV', csv_repository='stocks') print(stock.stock.head()) plt.clf() ### plot_stock will plot the stock's (High, Low, Close) recent history. see 'hstg' for number of days stock.plot_stock(start_date = str(Months), stats = ['high', 'low', 'close'], plot_type='hlc', filename=StockFiles[0]) plt.clf() ### plot_stock will plot the Stock's (Daily Change, Volume) recent history. see 'hstg' for number of days stock.plot_stock(start_date = str(Months) , stats = ['Daily Change', 'volume'], plot_type='pct', filename=StockFiles[1]) plt.clf() stock.changepoint_date_analysis(filename=StockFiles[2]) plt.clf() # stock.buy_and_hold(start_date=str(Months),nshares=1,filename=str(stocksfolder+ticker+".PredictedBuyHold"+ext)) # This function is broken. # plt.clf() stock.evaluate_prediction(start_date=str(Months),nshares=1,filename=StockFiles[4]) plt.clf() ## START BASIC PREDICTIONS BASED ON ANALYSIS model, model_data = stock.create_prophet_model() model.plot_components(model_data) plt.savefig(StockFiles[5]) plt.clf() stock.predict_future(days=hstg,filename=StockFiles[6]) plt.clf() # START MORE ADVANCED PREDICTONS BASED ON DEEP LEARNING. if litemode: pass else: ### changepoint_date_analysis is looking through historical data of the past 3 years to find the optimal time to buy or sell a stock. print(inifile) search_terms = inifile["STOCK PROFILE"]["search_terms"] # print(search_terms) bountylist = search_terms.split(",") # print(bountylist) stock.changepoint_date_analysis(search=bountylist,filename=StockFiles[3]) plt.clf() # LINEAR REGRESSION 01 stock.Stocker_linear_regression(days=hstg, filename=StockFiles[7]) plt.clf() ## Merge files into a pdf. if litemode: ModZ = ModZ; print(ModZ) else: ModZ = StockFiles[:]; print(ModZ) # thelimit = len(ModZ) Merge_PDFS(ModZ,reportsfolder+ticker+"._REPORT.PDF")
import pdb import mymodule #Stocker Library Code Example from stocker import Stocker microsoft = Stocker('MSFT') stock_history = microsoft.stock stock_history.head() microsoft.plot_stock() microsoft.plot_stock(start_date = '2000-01-03', end_date = '2018-01-16', stats ['Daily Change', 'Adj.Volume'], plot_type='pct') microsoft.buy_and_hold(start_date = '1986-03-13', end_date='2018-01-16', nshares=100) model, model_data = microsoft.create_prophet_model() model.plot_components(model_data) plt.show() print(micorsoft.weekly_seasonality) microsoft.weekly_seasonality = True print(microsoft.weekly_seasonality) #only shows data in the first 80% of data microsoft.changepoint_date_analysis() #This will need to add a library of search terms related to the stocks information microsoft.changepoint_date_analysis(search = 'Microsoft profit') microsoft.changepoint_date_analysis(search = 'Microsoft Office') #Future predictions
def get_stocker(names): stocker_data = [] for x in names: stock = Stocker(x) stocker_data.append(stock) return stocker_data
# -*- coding: utf-8 -*- # @Author : Ryan # @Time : 2021/9/29 12:39 # @Software : PyCharm # @Description : import pandas as pd from prophet import Prophet from stocker import Stocker microsoft = Stocker(ticker='MSFT') print(microsoft)
from stocker import Stocker hp = Stocker("HPQ") stock_history = hp.stock print(stock_history.head()) model, model_data = hp.create_prophet_model() model.plot_components(model_data) plt.show()
# -*- coding: utf-8 -*- """ Created on Fri Jun 7 22:57:31 2019 @author: Huntrer """ from stocker import Stocker microsoft = Stocker('MSFT') # A method (function) requires parentheses microsoft.plot_stock()
#!/usr/bin/env python import sys from stocker import Stocker if __name__ == "__main__": args = sys.argv[1:] symbol = args[0] stocker = Stocker() price, time_of_price = stocker.get_stock_price(symbol) print "{symbol}: ${price}, Time of price: {time_of_price}".format(symbol=symbol, price=price, time_of_price=time_of_price)
# -*- coding: utf-8 -*- """ Created on Sun Aug 29 15:26:09 2021 @author: jeti8 """ from stocker import Stocker amazon = Stocker('NKE') model, model_data = amazon.create_prophet_model(days=90) amazon.evaluate_prediction(nshares=1000) amazon.predict_future(days=10) amazon.predict_future(days=100)
# coding: utf-8 # In[23]: import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # In[24]: from stocker import Stocker # In[25]: microsoft = Stocker('MSFT') # In[26]: stock_history = microsoft.stock stock_history.head() # In[27]: f = open('msft1.txt', 'w') f.write(str(stock_history.head())) f.close() # In[28]: microsoft.plot_stock() # In[29]:
import numpy as np import matplotlib.pyplot as plt import matplotlib.style import matplotlib as mpl from matplotlib.pylab import rcParams rcParams['figure.figsize'] = 20, 10 from sklearn.preprocessing import MinMaxScaler from sklearn.linear_model import LinearRegression from fastai.structured import add_datepart import tensorflow as tf from tensorflow.keras import layers from sklearn import neighbors from sklearn.model_selection import GridSearchCV from pandas.util.testing import assert_frame_equal goog = Stocker('GOOGL') goog.plot_stock() # Create model model, model_data = goog.create_prophet_model(days=90) goog.evaluate_prediction() # Optimize the model goog.changepoint_prior_analysis(changepoint_priors=[0.001, 0.05, 0.1, 0.2]) goog.changepoint_prior_validation(start_date='2016-01-04', end_date='2017-01-03', changepoint_priors=[0.001, 0.05, 0.1, 0.2]) # Evaluate the new model goog.evaluate_prediction() print(goog.evaluate_prediction(nshares=1000))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Sep 1 20:43:37 2018 @author: https://towardsdatascience.com/stock-prediction-in-python-b66555171a2 """ #%% from stocker import Stocker teradyne = Stocker('TER') #%% model, model_data = teradyne.create_prophet_model(days=180) #%% teradyne.stock.head() #%% teradyne.evaluate_prediction(nshares=100) #------------- #%% twtr = Stocker('TWTR') #%% model, model_data = twtr.create_prophet_model(days=200) #%% twtr.stock.tail()
sids = ['2816'] for sid in sids: start = '2015-01-01' #end=str(today).split('-')[0]+'-'+str(today).split('-')[1] end = '2018-12-26' df = st3.GetSidDataFrame(sid, start, end) df['date'] = pd.to_datetime(df['date']) df.set_index("date", inplace=True) df = df['close'] price = df.squeeze() #print(price.head()) #print(price.index) Target = Stocker(price, name=sid) #Target.changepoint_prior_validation(start_date ='2015-12-03',end_date ='2018-12-21',changepoint_priors = [0.3,0.4,0.45,0.5,0.6]) #Target.plot_stock() #原始參數預測 #model, model_data = Target.create_prophet_model(days=10) #原始參數回測 #Target.evaluate_prediction() #Target.changepoint_prior_analysis(changepoint_priors=[0.001, 0.05, 0.1, 0.2]) #Target.changepoint_prior_validation(start_date='2015-01-07', end_date='2018-12-21', changepoint_priors=[0.05,0.1,0.2,0.3,0.4]) #調整參數為0.4