import pandas as pd
import datetime

%matplotlib inline 

"""
Explanation: Determining the best return on investiment (60 day periods)
Using historical closing prices of MSFT when was a good time to buy and sell based on the return on investment metric.
Return on investment: 
(sold_price – purchase_price) / purchase_price

For example, if you buy 20 shares of Joe's Pizza for $10 a share, your investment cost is $200. 
If you sell those shares for $250, then your ROI is ($250-200)/$200 for a total of 0.25 or 25%.
End of explanation
"""


ls *.csv

stock = pd.read_csv('msft.csv', parse_dates=['Date']).set_index('Date').sort()
stock.tail()

stock["Open"].plot(figsize=(15, 5));

def ROI(series):
    """Return on investiment"""
    return (series[-1] - series[0]) / series[0]

days = 60 
stock["ROI"] = pd.rolling_apply(stock["Close"], days, ROI)
stock.tail()

good_times = stock[stock.ROI > 0.25]
good_times.tail()

ax = stock["Close"].plot(figsize=(15, 5));
ax.plot(good_times.index, good_times["Close"], 'or', alpha=0.3);

"""
Explanation: Q. Using all the historical closing prices of MSFT shares calculate which 60 day periods yielded a return on investment greater than 25%.
End of explanation
"""


