@pattmax/

StockTrace

Python

No description

fork
loading
Files
  • main.py
  • amzn_2018-1-1_2018-9-30.csv
  • amzn_2018-1-1_2018-9-30.csv_GRAPH.png
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import pandas_datareader as web
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import datetime

def help():
	#Explains commands to user
	print("'quit' - Closes the program.")
	print("'csv' - Generates historical stock data and saves as a csv.")
	print("'plot' - Opens a csv file and plots closing prices on a graph.")
	print("'ma' - Opens a csv file and plots moving average of closing prices on a graph.")
	print("'si' - Calculates simple interest (interest = principal * rate * time).")

def csv():
	#Exports csv of selected stock
	#User input
	symbol = input("Symbol>>")
	start_year = int(input("Start year>>"))
	start_month = int(input("Start month>>"))
	start_day = int(input("Start day>>"))
	end_year = int(input("End year>>"))
	end_month = int(input("End month>>"))
	end_day = int(input("End day>>"))
	print("Processing...")

	#datetime.datetime is a data type within the datetime module
	start = datetime.datetime(start_year, start_month, start_day)
	end = datetime.datetime(end_year, end_month, end_day)
		
	#DataReader method name is case sensitive
	df = web.DataReader(symbol, 'yahoo', start, end)
		
	#invoke to_csv for df dataframe object from 
	#DataReader method in the pandas_datareader library
		
	#.csv must not be open in another app, such as Excel
		
	export = str(str(symbol) + '_' + str(start_year) + '-' + str(start_month) + '-' + str(start_day) + '_' + str(end_year) + '-' + str(end_month) + '-' + str(end_day) + '.csv')

	df.to_csv(export)

	print("Exported as: " "'" + export + "'")
	print("Finished")

def plot():
	#Plots selected csv file on graph
	filename = input("csv filename>> ")
	ma = input("Moving average 1>>")
	ma2 = input("Moving average 2>>")

	df = pd.read_csv(filename, parse_dates=True, index_col=0)
	df[ma + 'ma'] = df['Adj Close'].rolling(window=100).mean()
	df[ma2 + 'ma'] = df['Adj Close'].rolling(window=100).mean()
	ax1 = plt.subplot2grid((6,1), (0,0), rowspan=10, colspan=2)
	ax2 = plt.subplot2grid((6,1), (0,0), rowspan=10, colspan=2)
	ax3 = plt.subplot2grid((6,1), (0,0), rowspan=10, colspan=2)
	ax1.plot(df.index, df['Adj Close'])
	ax2.plot(df.index, df[ma + 'ma'])
	ax3.plot(df.index, df[ma2 + 'ma'])

	plt.savefig(filename + "_GRAPH.png")
	
	print("Exported as: " + filename + "_GRAPH.png")
	print("Finished")

def si():
	principal = input("Current amount: $")
	rate = input("Interest rate: ")
	time = input("Duration: ")
	interest=float(principal)*float(rate)*float(time)
	print(interest)

print("   _____ _             _    ")
print("  / ____| |           | |   ")
print(" | (___ | |_ ___   ___| | __")
print("  \___ \| __/ _ \ / __| |/ /")
print("  ____) | || (_) | (__|   < ")
print(" |_______\\__\\___/ \\___|_|\\_\\")
print(" |__   __|                  ")
print("    | |_ __ __ _  ___ ___   ")
print("    | | '__/ _` |/ __/ _ \  ")
print("    | | | | (_| | (_|  __/  ")
print("    |_|_|  \__,_|\___\___|  ")
print("Type 'help' for options")

while True:
	tool = input(">>")

	if tool == "help":
		help()
	
	if tool == "quit":
		exit()

	elif tool == "csv":
		csv()
	
	elif tool == "plot":
		plot()
	
	elif tool == "si":
		si()