-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPortfolio.py
134 lines (103 loc) · 4.33 KB
/
Portfolio.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
"""
Created on Fri May 18 11:08:52 2018
@author: mai1346
"""
from Tradelog import Tradelog
class Portfolio(object):
'''
本类用于记录整个Portfolio的各个资产的仓位和市值。
本类接收Fill事件,并根据事件调整持仓信息。
'''
def __init__(self, data_handler, events, start_date, initial_capital=100000):
'''
data_handler - 数据来源
start_date - Portfolio 开始时间
initial_capital - 初始本金.
'''
self.data_handler = data_handler
self.events = events
self.symbol_list = self.data_handler.symbol_list
self.start_date = start_date
self.initial_capital = initial_capital
self.all_positions = self._construct_all_positions()
self.current_positions = dict((k, v) for k, v in [(s, 0) for s in self.symbol_list])
self.all_holdings = self._construct_all_holdings()
self.current_holdings = self._construct_current_holdings()
self.Tradelog = Tradelog()
def _construct_all_positions(self):
d = dict((k, v) for k, v in [(s, 0) for s in self.symbol_list])
d['datetime'] = self.start_date
return [d]
def _construct_all_holdings(self):
d = dict((k, v) for k, v in [(s, 0.0) for s in self.symbol_list])
d['datetime'] = self.start_date
d['cash'] = self.initial_capital
d['commission'] = 0.0
d['total'] = self.initial_capital
return [d]
def _construct_current_holdings(self):
d = dict((k, v) for k, v in [(s, 0.0) for s in self.symbol_list])
d['cash'] = self.initial_capital
d['commission'] = 0.0
d['total'] = self.initial_capital
return d
def _update_positions_from_fill(self, fill):
'''
根据FILL事件更新资产的持仓信息
'''
# Check whether the fill is a buy or sell
fill_dir = 0
if fill.direction == 'BUY':
fill_dir = 1
if fill.direction == 'SELL':
fill_dir = -1
# Update positions list with new quantities
self.current_positions[fill.symbol] += fill_dir * fill.quantity
def _update_holdings_from_fill(self, fill):
'''
根据FILL事件更新portfolio的资金信息
'''
# Check whether the fill is a buy or sell
fill_dir = 0
if fill.direction == 'BUY':
fill_dir = 1
if fill.direction == 'SELL':
fill_dir = -1
# Update holdings list with new quantities
fill_cost = fill.fill_cost
cost = fill_dir * fill_cost * fill.quantity
self.current_holdings[fill.symbol] += cost
self.current_holdings['commission'] += fill.commission
self.current_holdings['cash'] -= (cost + fill.commission)
self.current_holdings['total'] -= (cost + fill.commission)
def _update_tradelog_from_fill(self, fill):
'''
更新Tradelog
'''
self.Tradelog.update(fill)
def get_current_position(self,symbol):
return self.current_positions[symbol]
def update_fill(self, event):
if event.type == 'FILL':
self._update_positions_from_fill(event)
self._update_holdings_from_fill(event)
self._update_tradelog_from_fill(event)
def update_timeindex(self):
latest_datetime = self.data_handler.get_latest_bar_datetime(self.symbol_list[0])
positions = dict((k, v) for k, v in [(s, 0) for s in self.symbol_list])
positions['datetime'] = latest_datetime
for s in self.symbol_list:
positions[s] = self.current_positions[s]
self.all_positions.append(positions)
holdings = dict((k, v) for k, v in [(s, 0) for s in self.symbol_list])
holdings['datetime'] = latest_datetime
holdings['cash'] = self.current_holdings['cash']
holdings['commission'] = self.current_holdings['commission']
holdings['total'] = self.current_holdings['cash']
for s in self.symbol_list:
market_value = self.current_positions[s] * \
self.data_handler.get_latest_bars_values(s, "close")[0] # [0] because it's a np array
holdings[s] = market_value
holdings['total'] += market_value
self.all_holdings.append(holdings)