Created
September 9, 2016 16:32
-
-
Save kylecorry31/b0b827de302fdbe917ae0d901d18135e to your computer and use it in GitHub Desktop.
Stock Analysis using moving averages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
""" | |
moving_average : list[float] int -> list[float] | |
Consumes a list of floats and computes the moving average with a window | |
size of window_size | |
""" | |
def moving_average(a_list, window_size): | |
ma = [] | |
if not len(a_list) % window_size == 0: | |
return ma | |
for i in range(window_size-1, len(a_list)): | |
s = 0 | |
for j in range(0, window_size): | |
s += a_list[i - window_size + 1 + j] | |
ma.append(s/window_size) | |
return ma | |
def intersects(y1, y2, y3, y4): | |
return ((y3 < y1) and (y4 > y2)) or ((y3 > y1) and (y4 < y2)) | |
def convert_stock_price(prices): | |
return list(reversed(list(map(lambda x: float(x), prices.split('\n')[:-1])))) | |
def plot_stock_and_average(stock, m=5): | |
prices = convert_stock_price(stock) | |
length = len(prices) | |
prices = prices[length%m:] | |
ma = moving_average(prices, m) | |
prices = prices[m-1:] | |
print(len(ma)) | |
print(len(prices)) | |
plt.plot(prices) | |
plt.plot(ma) | |
plt.show() | |
def intersections(l1, l2): | |
indexes = [] | |
way_intersects = [] | |
for i in range(len(l1) - 1): | |
if intersects(l1[i], l1[i+1], l2[i], l2[i+1]): | |
indexes.append(i) | |
if intersects_above(l1[i], l1[i+1], l2[i], l2[i+1]): | |
way_intersects.append('above') | |
else: | |
way_intersects.append('below') | |
return (indexes, way_intersects) | |
def process_ma_signals(l1, l2): | |
indexes, way_intersects = intersections(l1, l2) | |
last_intersect_day = len(l1) - indexes[-1] | |
last_intersect_way = way_intersects[-1] | |
if last_intersect_way == 'above': | |
return 'Last signal was a buy. It occured between ' + str(last_intersect_day - 1) + \ | |
' and ' + str(last_intersect_day) + ' days ago' | |
else: | |
return 'Last signal was a sell. It occured between ' + str(last_intersect_day - 1) + \ | |
' and ' + str(last_intersect_day) + ' days ago' | |
def intersects_above(y1, y2, y3, y4): | |
return intersects(y1, y2, y3, y4) and y1 < y3 and y2 > y4 | |
def intersects_below(y1, y2, y3, y4): | |
return intersects(y1, y2, y3, y4) and y1 > y3 and y2 < y4 | |
def stock_analysis(prices): | |
stock = convert_stock_price(prices) | |
stock = stock[len(stock)%5:] | |
ma = moving_average(stock, 5) | |
stock = stock[4:] | |
s = process_ma_signals(stock, ma) | |
print(s) | |
plt.plot(stock) | |
plt.plot(ma) | |
plt.show() | |
def tests(): | |
assert moving_average([1, 2, 3, 4, 5], 5) == [3] | |
assert moving_average([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5) == [3, 4, 5, 6, 7, 8] | |
assert moving_average([], 5) == [] | |
assert moving_average([1, 2, 3], 1) == [1, 2, 3] | |
assert intersects(1, 3, 2, 1) | |
assert intersects(1, 3, 0, 4) | |
assert intersects(1, 3, 4, 5) == False | |
print("All tests passed") | |
tests() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment