-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathubah.py
More file actions
65 lines (62 loc) · 2.52 KB
/
ubah.py
File metadata and controls
65 lines (62 loc) · 2.52 KB
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
# This is an implementation of uniform buy and hold.
from datetime import date
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from stockMarketReader import readDataSet
def getDatesVec(data):
"""
Get the vector of dates that spans a given stock market data set - specifically done for CORN algorithm but not exclusive to it
note that this requires the pandas dataframe of data
NOTE pandas dataframe for data
"""
startDate = data.Date.min()
startDate = data[data['Date'] == startDate]
startDate = startDate.Ticker.to_numpy()
tick = np.unique(startDate)[0]
tickerDates = data[data['Ticker'] == tick]
tickerDates = np.unique(data.Date.to_numpy())
return tickerDates
def ubah(data, startAt, stopAt):
"""
Given a data set, preform a uniform buy and hold strategy.
Thus divide the initial capital (1) amongst all assets equally and track the return.
This will return the total return at each day, sequentially for a universal portfolio.
"""
dates = getDatesVec(data)
startDate = dates[startAt]
endDate = dates[stopAt]
dates = dates[startAt:stopAt]
startPrices = data[data['Date'] == startDate]
numStocks = len(np.unique(startPrices.Ticker.to_numpy()))
tick = np.unique(startPrices.Ticker.to_numpy())[0]
# print("Ticker ")
# print(tick)
data = data[data['Date'] >= startDate]
data = data[data['Date'] < endDate]
dates = data[data['Ticker'] == tick]
print(dates)
dates = dates.Date.to_numpy()
# dates = dates[startDate:endDate]
propPort = 1 / numStocks
# getting the init prop of a stock we can own
# this will assume that this will come out in the right order - which it should
# Prop owned start acts like the uniform portfolio - in the original capital is divided equally amongst all assets -> 1/numStock
# But obviously different percentages of each asset can be bought for this amount, therefore the propOwned will have different percentages
propOwnedStart = propPort / startPrices.Close.to_numpy()
returns = np.array(())
for i in dates:
currDay = data[data['Date'] == i]
prices = currDay.Close.to_numpy()
try:
dayVal = np.sum(propOwnedStart * prices)
returns = np.append(returns, dayVal)
except:
returns = np.append(returns, returns[-1])
# plt.ylabel("Multiple of Increase")
# plt.xlabel("Number of Days Passed")
# plt.plot(returns)
# plt.show()
return returns
# data = readDataSet()
# vals = ubah(data)