未加星标

Predicting Stock Prices with Python

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Investing in the stock market used to require a ton of capital and a broker that would take a cut from your earnings. Then Robinhood disrupted the industry allowing you to invest as little as $1 and avoid a broker altogether. Robinhood and apps like it have opened up investing to anyone with a connected device and gave non-investors the opportunity to profit from the newest tech start-up.


Predicting Stock Prices with Python
“space gray iPhone X turned on” by rawpixel on Unsplash

However, giving those of us who are not economists or accountants the freedom to invest our money in the “hottest” or “trending” stocks is not always the best financial decision.

Thousands of companies use software to predict the movement in the stock market in order to aid their investing decisions. The average Robinhood user does not have this available to them. Primitive predicting algorithms such as a time-sereis linear regression can be done with a time series prediction by leveraging python packages like scikit-learn and iexfinnance .

This program will scrape a given amount of stocks from the web, predict their price in a set number of days and send an SMS message to the user informing them of stocks that might be good to check out and invest in.

Setup

In order to create a program that predicts the value of a stock in a set amount of days, we need to use some very useful python packages. You will need to install the following packages:

nump y selenium sklearn i exfinance

If you do not already have some of these packages you can install them through pip install PACKAGE or by cloning the git repository.

Here is an example of installing numpy with pip

pip install numpy

and with git

git clone https://github.com/numpy/numpy
cd numpy
python setup.py install

Now open up your favorite text editor and create a new python file. Start by importing the following packages

import numpy as np from datetime import datetime import smtplib import time from selenium import webdriver #For Prediction from sklearn.linear_model import LinearRegression from sklearn import preprocessing, cross_validation, svm #For Stock Data from iexfinance import Stock from iexfinance import get_historical_data

Note: the datetime, time and smtplib packages come with python

In order to scrape the Yahoo stock screener, you will also need to install the Chromedriver in order to properly use Selenium. That can be found here

Getting theStocks

Using the Selenium package we can scrape Yahoo stock screeners for stock’s ticker abbreviations.

First, make a function getStocks that takes a parameter of n , where n is the number of stocks we wish to retrieve.

def getStocks(n):

In the function create your chrome driver then use driver.get(url) to retrieve the desired webpage. We will be navigating to https://finance.yahoo.com/screener/predefined/aggressive_small_caps?offset=0&count=202 which will display 200 stocks listed in the category “aggressive small caps”. If you go to https://finance.yahoo.com/screener you will see a list of all screener categories that Yahoo provides. You can then change the URL to your liking.

#Navigating to the Yahoo stock screener driver = webdriver.Chrome( ‘<strong>PATH TO CHROME DRIVER</strong>’) url = “https://finance.yahoo.com/screener/predefined/aggressive_small_caps?offset=0&count=202" driver.get(url)

Make sure to add the path to where you downloaded the chromedriver to where the bolded code is.

You will now need to create a list to hold the ticker values stock_list = [] .

Next, we need to find the XPath for the ticker elements so that we can scrape them. Go to the screener URL and open up developer tools in your web browser ( Command+Option+i / Control+Shift+I or F12 for windows).

Click the “Select Element” button


Predicting Stock Prices with Python

Click on the ticker and inspect its attributes


Predicting Stock Prices with Python

Finally, copy the XPath of the first ticker the HTML element should look something like this

<a href=”/quote/RAD?p=RAD” title=”Rite Aid Corporation” class=”Fw(b)” data-reactid=”79">RAD</a>
Predicting Stock Prices with Python

The XPath should look something like this

//*[<a href="http://twitter.com/id" data-href="http://twitter.com/id" title="Twitter profile for @id" rel="noopener" target="_blank">@id</a>=”scr-res-table”]/div[2]/table/tbody/tr[<strong>1</strong>]/td[1]/a

If you inspect the ticker attributes below the first one you will notice that the XPath is exactly the same except the bolded 1 in the code above increments by 1 for each ticker. So the 57th ticker XPath value is

//*[<a href="http://twitter.com/id" data-href="http://twitter.com/id" title="Twitter profile for @id" rel="noopener" target="_blank">@id</a>=”scr-res-table”]/div[2]/table/tbody/tr[<strong>57</strong>]/td[1]/a

This greatly helps us. We can simply make a for loop that increments that value every time it runs and stores the value of the ticker to our stock_list .

stock_list = [] n += 1 for i in range(1, n): ticker = driver.find_element_by_xpath( ‘//*[@id = “scr-res-table”]/div[2]/table/tbody/tr[‘ + str(i) + ‘]/td[1]/a’) stock_list.append(ticker.text)

n is the number of stocks that our function, getStocks(n) , will retrieve. We have to increment by 1 since Python is 0-indexed. Then we use the value i to modify our XPath for each ticker attribute.

Use driver.quit() to exit the web browser. We now have all ticker values and are ready to predict the stocks.

We are going to create a function to predict the stocks in th

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

分页:12
转载请注明
本文标题:Predicting Stock Prices with Python
本站链接:https://www.codesec.net/view/611839.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(11)