-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarketsimcode.py
More file actions
66 lines (59 loc) · 2.53 KB
/
Copy pathmarketsimcode.py
File metadata and controls
66 lines (59 loc) · 2.53 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
66
import pandas as pd
import numpy as np
import datetime as dt
import os
from util import get_data, plot_data
def compute_portvals(orders, start_val = 1000000, commission=9.95, impact=0.005):
og_orders = orders
#print og_orders
#sort og_order by index date
order = og_orders.sort_index();
#print order
index = order.index;
start_date, end_date = index.min(), index.max()
#print start_date, end_date
symbols = list(order.Symbol.unique())
#print symbols
price = get_data(symbols, pd.date_range(start_date, end_date), addSPY=True, colname='Adj Close');
price["CASH"] = 1.0
del price["SPY"]
price.fillna(method="ffill", inplace=True)
price.fillna(method="bfill", inplace=True)
#print price
tradeZero = np.zeros(price.shape);
trade = pd.DataFrame(tradeZero, price.index, price.columns)
#print trade
for index, row in order.iterrows():
if row["Shares"] <= 0:
print "negative shares!"
if row["Order"] == "SELL":
#print trade.loc[index, "CASH"]
trade.loc[index, "CASH"] = trade.loc[index, "CASH"] + price.loc[index, row["Symbol"]]*(1-impact) * row["Shares"] - commission
# print trade.loc[index, "CASH"]
#print trade.loc[index, row["Symbol"]], row["Shares"]
trade.loc[index, row["Symbol"]] = trade.loc[index, row["Symbol"]] - row["Shares"]
#print trade.loc[index, row["Symbol"]]
elif row["Order"] == "BUY":
# print trade.loc[index, "CASH"]
trade.loc[index, "CASH"] = trade.loc[index, "CASH"] - price.loc[index, row["Symbol"]] * (1 + impact) * row[
"Shares"] - commission
# print trade.loc[index, "CASH"]
# print trade.loc[index, row["Symbol"]], row["Shares"]
trade.loc[index, row["Symbol"]] = trade.loc[index, row["Symbol"]] + row["Shares"]
# print trade.loc[index, row["Symbol"]]
else:
print "Wrong order type!"
#print trade
holding = pd.DataFrame(tradeZero, price.index, price.columns)
for k in range(len(holding)):
if k !=0 :
holding.ix[k] = holding.ix[k] + holding.ix[k-1]
elif k == 0 :
holding.ix[0, -1] = holding.ix[0, -1] + start_val
holding.ix[0, :-1] = trade.ix[0, :-1]
#print holding
value = price * holding
#print value
return pd.DataFrame(value.sum(axis=1), value.index, ["Portfolio_Value"])
if __name__ == "__main__":
print "marketsim"