Giter Site home page Giter Site logo

quant's Introduction

Hello, today I will tell you the details of the stock market analysis project with python.

Stock Market Analysis with Pandas Python

First, let's start our project by importing the libraries.

import

import pandas_datareader.data as web 
import datetime
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

After defining our libraries, we determine our start and end dates for our stock market analysis.

start = datetime.datetime(2021,1,1)
end = datetime.datetime(2022,2,25)

What is Pandas Datareader in Python?

Pandas Datareader is a Python package that allows us to create a pandas DataFrame by using some popular data sources available on the internet including:

1.Yahoo Finance 2.Google Finance 3.Morningstar 4.IEX 5.Robinhood 6.Engima 7.Quandl 8.FRED 9.World Bank 10.OECD and many more.

Yes, we learned what datareader is, now let's use datareader to pull google stock market data from yahoo source.

google = web.DataReader("GOOGL", "yahoo", start, end)
google.head()

155878863-6bb6dfab-5d67-4e74-bfde-7ae57b740942

google.tail()

tail

Yes, we have extracted the change table of the google stock market data within the first and last 5 days in the date ranges we defined above.

google['Open'].plot(label = 'GOOGL Open price', figsize= (16,8))
google['Close'].plot(label = 'GOOGL Close price')
google['High'].plot(label = 'GOOGL High price')
google['Low'].plot(label = 'GOOGL Low price')
plt.legend()
plt.title('Google Stock Prices')
plt.ylabel('Stock Price')
plt.show()

Thanks to the code blocks above, we showed Google's data such as opening, closing, volume in certain time intervals in the stock market on the plot chart.

Now three big automobile and engine companies; Let's examine the stock market data of TESLA, FORD AND GENERAL MOTORS.

tesla = web.DataReader("TSLA",'yahoo', start, end)
ford  = web.DataReader("F", 'yahoo', start, end)
gm = web.DataReader("GM", 'yahoo', start,end)

Just like we got Google data, we got the data of three companies from yahoo using pandas datareader.

tesla.to_csv('Tesla_Stock.csv')
ford.to_csv('Ford_Stock.csv')
gm.to_csv('GM_Stock.csv')

We will use the following lines of code to convert the captured data into a csv file.

tesla.head()

asdadad

tesla['Open'].plot(label = 'Tesla', figsize= (16,8))
gm['Open'].plot(label = 'GM')
ford['Open'].plot(label = 'Ford')
plt.legend()
plt.title('Stock Prices of Tesla, GM, Ford')
plt.ylabel('Stock Price')
plt.show()

Thanks to the code blocks we wrote above, you can see our chart below, which compares the opening data of three companies.

2

tesla['Total Traded']= tesla['Open'] * tesla['Volume']
gm['Total Traded']= gm['Open'] * gm['Volume']
ford['Total Traded']= ford['Open'] * ford['Volume']
tesla['Total Traded'].plot(label = 'Tesla', figsize= (16,8))
gm['Total Traded'].plot(label = 'GM')
ford['Total Traded'].plot(label = 'Ford')
plt.legend()
plt.ylabel('Total Traded')

3

Thanks to the lines of code we wrote above, we compared the total trade data of 3 companies.

Moving average

In statistics, a moving average (rolling average or running average) is a calculation to analyze data points by creating a series of averages of different subsets of the full data set. It is also called a moving mean (MM)[1] or rolling mean and is a type of finite impulse response filter. Variations include: simple, cumulative, or weighted forms. Given a series of numbers and a fixed subset size, the first element of the moving average is obtained by taking the average of the initial fixed subset of the number series. Then the subset is modified by "shifting forward"; that is, excluding the first number of the series and including the next value in the subset.

tesla['Open'].plot(label = 'No Moving Average', figsize= (16,8))
tesla['MA50']=tesla['Open'].rolling(50).mean()
tesla['MA50'].plot(label='MA50')
tesla['MA200']= tesla['Open'].rolling(200).mean()
tesla['MA200'].plot(label='MA200')
plt.legend()

4

In the chart we have seen above, we have drawn the moving average chart of the tesla company.

What is a Scatter Matrix?

A scatter matrix (pairs plot) compactly plots all the numeric variables we have in a dataset against each other one. In Python, this data visualization technique can be carried out with many libraries but if we are using Pandas to load the data, we can use the base scatter_matrix method to visualize the dataset. Now let's create a scatter matrix together and display our data on the scatter map.

from pandas.plotting import scatter_matrix
import pandas as pd
car_comp = pd.concat([tesla['Open'],gm['Open'],ford['Open']],axis = 1)
car_comp.columns = ['Tesla Open', 'GM Open', 'Ford Open']
scatter_matrix(car_comp,figsize = (12,12))

5

quant's People

Contributors

ujjwalagni avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.