Skip to content

Instantly share code, notes, and snippets.

@accessnash
Created May 17, 2020 13:03
Show Gist options
  • Select an option

  • Save accessnash/a50d4b30b07b96c52874095824c9bfe4 to your computer and use it in GitHub Desktop.

Select an option

Save accessnash/a50d4b30b07b96c52874095824c9bfe4 to your computer and use it in GitHub Desktop.
Stock returns for Tesla for a 5 yr period
# -*- coding: utf-8 -*-
"""
Created on Tue May 12 20:26:35 2020
@author: User
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
tesla = pd.read_csv('C:\\Users\\User\\Dropbox\\My PC (DESKTOP-78513LN)\\Desktop\\Python programming\\Python for Financial analysis & Algo trading\\Python-for-Finance-Repo-master\\07-Stock-Market-Analysis-Capstone-Project\\Tesla_Stock.csv')
tesla.head()
tesla['Open'].plot(label='Tesla', title = 'Opening prices')
tesla['Volume'].plot(label='Tesla', title = 'Volumes Traded')
plt.legend()
tesla['Volume'].argmax()
tesla['Total Traded'] = tesla['Open']*tesla['Volume']
tesla['Total Traded'].plot(label='Tesla')
tesla['MA50']= tesla['Open'].rolling(50).mean()
tesla['MA200']= tesla['Open'].rolling(200).mean()
tesla[['Open', 'MA50', 'MA200']].plot()
#from mpl_finance import candlestick_ohlc
#from matplotlib.dates import DateFormatter,date2num, WeekdayLocator, DayLocator, MONDAY
#tesla_reset = tesla.loc['2012-01'].reset_index()
tesla['returns'] = tesla['Close'].pct_change()
tesla['returns'].hist(bins=100, label= 'Tesla')
tesla['Cumulative Returns'] = (1+ tesla['returns']).cumprod()
tesla['Cumulative Returns'].plot(label='Tesla')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment