Skip to content

Instantly share code, notes, and snippets.

View quantra-go-algo's full-sized avatar

Algorithmic Trading quantra-go-algo

View GitHub Profile
@quantra-go-algo
quantra-go-algo / import_libraries_and_price_data.py
Last active February 27, 2020 12:50
We are importing the libraries and price data of General Motors to implement Random Walk
# Import the necessary libraries
import yfinance as yf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Import the price data of General Motors
df = yf.download('GM','2008-01-02', '2020-2-27')
@quantra-go-algo
quantra-go-algo / simulation_of_random_walk.py
Last active April 28, 2020 20:42
We are creating the simulation of the random walk for General Motors price data from 2019 February onwards
# Calculate the daily percentage change, mean and sigma
df['daily_pct_change'] = df['Adj Close'].pct_change()
mu = df['daily_pct_change'].iloc[:-252].mean()
sigma = df['daily_pct_change'].iloc[:-252].std()
# Creating the random walk simulation of the probable price path
simulation = {}
simulation['Actual'] = list(df['Adj Close'].iloc[-252:].values)
for sim in range(1,5): # Taking 5 paths
@quantra-go-algo
quantra-go-algo / random_walk_with_5_trials.py
Last active February 28, 2020 14:00
We will plot the random walk for General Motors for 50 simulations
# Plotting the actual Adjusted Close price of General Motors
df['Adj Close'][-252:].plot(figsize=(10,7),grid=True,legend=False)
plt.xlabel('Year')
plt.ylabel('Price')
# Plotting the simulation of random walk
simulation=pd.DataFrame(simulation)
simulation.index=df[-252:].index
simulation.plot(figsize=(10,7),grid=True,legend=False)
@quantra-go-algo
quantra-go-algo / five_number_summary.py
Created March 2, 2020 20:17
To list the minimum, first Quartile, second quartile, third quartile and maximum of a group
# import 11 day Tesla data
import yfinance as yf
tesla = yf.download('TSLA','2020-01-27', '2020-02-11')
tesla['Close']
# Calculating the 5 number summary
from numpy import percentile
# calculate quartiles
All_quartiles = percentile(tesla['Close'], [25, 50, 75])
@quantra-go-algo
quantra-go-algo / variance_standard_deviation.py
Created March 2, 2020 20:20
To compute the variance and the standard deviation
import statistics as st
print("Variance of the Closing price is % s"
%(st.variance(tesla['Close'])))
print("Standard Deviation of Closing Price is % s "
% (st.stdev(tesla['Close'])))
@quantra-go-algo
quantra-go-algo / Plot_closing_price.py
Created March 2, 2020 20:21
To plot the closing price of a stock
import matplotlib.pyplot as plt
tesla['Close'].plot(figsize=(10,7))
plt.legend()
plt.grid()
plt.show()
# Importing the modules
import yfinance as yf
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import confusion_matrix
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
# Downloading GOOGLE OHLCV using yfinance with adjusted prices
@quantra-go-algo
quantra-go-algo / Sample_Stand_alone_HTML.py
Created March 11, 2020 08:11
This creates an html file of the output
import plotly
import plotly.graph_objs as go
plotly.offline.plot({
"data": [go.Scatter(x=[11, 15, 30, 37, 42], y=[15, 18, 71, 42, 42])],
"layout": go.Layout(title="HTML file")}, auto_open=True)
@quantra-go-algo
quantra-go-algo / Sample_Plotly_In_Notebook.py
Created March 11, 2020 08:13
This code creates a sample plot in the Jupyter Notebook itself
import plotly
import plotly.graph_objs as go
plotly.offline.iplot({
"data": [go.Scatter(x=[11, 15, 30, 37, 42], y=[15, 18, 71, 42, 42])],
"layout": go.Layout(title="Line plot")})
@quantra-go-algo
quantra-go-algo / Import_OHLC_data.py
Last active March 11, 2020 12:35
This code saves the OHLC data in a csvfile
# Importing libraries
import yfinance as yf
import plotly as py
import plotly.graph_objects as go
import pandas as pd
# Import Tesla data
tesla = yf.download('TSLA','2020-02-01', '2020-03-03')