For you stock, we will label each week as ’green” or ”red”.
a ”green” week means that is was a good week to be invested for that week (from the first to last trading day of this week, typically from Monday to Friday).
a ”red” week means that it was not a good week to be invested but to keep money in cash (e.g. prices fell or there was too much volatility in the price).
Take your stock and assign colors for two years: year 1 and year 2.
For other years, put color ”undefined”. To help you with this task, we included a sample script to display daily prices for a stock (”MSFT”) between two dates:
stock data plot.py
This script displays and saves a plot of prices between two dates. You will need to modify the names of input files and considering 10 days at a time, examine (visually) the behavior of your stock and assign a color label. The important point is to be consistent with your rules on assigning color labels. For example, you may decide that if a price this friday is higher than last friday, then it is green, otherwise red. But try to come up with more interesting rules. Add a column ”label” to your stock data file with this label. Please try to do this as dilligently as you can. In many subsequent assignments, we will build machine learning classifiers that will ”train” on your labels in year 1 and predict labels for year 2. We will analyze such classifiers in terms of their accuracy. And you will implement trading strategies based on your label predictions. In this course, you will implement a number of machine learning classifiers to predict labels. In addition to colors, we will define a set of numeric features for each week (e.g. average of daily returns and average volatility). Typically, you will train your classifier using these features and labels from year 1 and then, using features for year 2, you will predict labels for year 2. The accuracy of your classifier is the percentage of weeks in year 2 predicted correctly by your classifier. A correct color is the one that you manually assigned for that week. For these classifiers, you will be asked to compute the performance of a trading strategy based on your colors.
To help you with this, you need to write a ”generic” script that, given a set of labels for each week for a particular time period (e.g. a year), computes the performance of your trading strategy. We assume that for each week, you know the opening price of the first trading day and the (adjusted) close price of the last trading day in a week. In the strategy based on labels, you will invest in your stock during ”green” weeks and keep your money in cash during ”red” weeks. There are no short positions. You start with $100 prior to week 1. The strategy is summarized as follows:
1. for the very first ”green” week, you invest $100 by buying (possibly fractional number of) shares of your stock at the opening price of thefirst trading day of that week. Strictly speaking, you should be buying at the (adjusted) closing price of the last trading day of the previous week but our approach is simpler to implement
2. if the next week is ”red” (you want to be out of market next week):
(a) if you have a position this week (this week is ”green”), you sell your shares at the (adjusted) closing price of last trading day of this week
(b) if you have no position this week (this week is ”red”), you do nothing and remain in cash for next week
3. if the next week is ”green” (you want to be invested next week):
(a) if you have a position this week (i.e. this week is ”green”), you do nothing (continue to be invested in your stock for next week)
(b) if you have no position this week (this week is ”red”), you buy (possibly fractional number of) shares of your stock at the opening price of next week - you invest all the money you have accumulated so far
4. ignore trading costs and assume that you are able to buy or sell at open or (adjusted) closing prices
Questions:
1. implement a ”buy-and-hold” strategy for year 2 - you invest $100 on the first trading day (at the opening price) and sell at the last trading day at the (adjusted) closing price. 2. implement a trading strategy based on your labels for year 2 and compare the performance with the ”buy-and-hold” strategy. Which strategy results in a larger amount at the end of the year?
stock data plot.py
# -*- coding: utf-8 -*
import os
import pandas as pd
import matplotlib.pyplot as plt
ticker='MSFT'
input_dir = r'C:\Users\epinsky\bu\python\data_science_with_Python\datasets'
ticker_file = os.path.join(input_dir, ticker + '.csv')
plot_dir = r'C:\Users\epinsky\bu\python\data_science_with_Python\plots\plots_677_O2'
try:
df = pd.read_csv(ticker_file)
start_date='2015-01-09';
end_date='2015-01-20'
df = df[df['Date'] >= start_date]
df = df[df['Date'] <= end_date]
fig = plt.figure()
ax = plt.gca()
df = df[['Date','Week_Number','Weekday', 'Day', 'Adj Close']]
weekday_list = df['Weekday'].tolist()
ticks_list = df['Date'].tolist()
plt.plot(df['Date'], df['Adj Close'])
plt.xticks(ticks_list, weekday_list, rotation='vertical')
plt.grid(True)
plt.title('daily prices for ' + ticker + ' from ' + start_date + ' to ' + end_date)
plt.legend()
output_file = os.path.join(plot_dir, ticker + '_prices_' + start_date + '_to_' + end_date + '.pdf')
plt.show()
fig.savefig(output_file)
except Exception as e:
print(e)
print('failed to read stock data for ticker: ', ticker)
If you need any help in python programming help, python assignment help, python homework help or need any help related to python data science then you can contact us and get instant help with an affordable price.
Send your requirement details at here:
realcode4you@gmail.com
Comments