-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathposition.py
More file actions
141 lines (122 loc) · 5.89 KB
/
Copy pathposition.py
File metadata and controls
141 lines (122 loc) · 5.89 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
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
135
136
137
138
139
140
141
#-*- coding:utf-8 -*-
import logging
from base import *
from misc import *
class Position(object):
def __init__(self, instrument, gateway = 'CTP'):
self.instrument = instrument
self.gateway = gateway
self.tday_pos = [0, 0]
self.tday_avp = [0.0, 0.0]
self.pos_tday = [0]
self.pos_yday = [0] # yday's overnight position
self.curr_pos = [0]
self.locked_pos = [0]
self.orders = [] #元素为Order
def re_calc(self):
tday_opened = [0, 0]
tday_o_locked = [0, 0]
self.orders = [o for o in self.orders if o.volume != 0]
for mo in self.orders:
if mo.direction == ORDER_BUY:
tday_opened[0] += mo.filled_volume
tday_o_locked[0] += mo.volume
elif mo.direction == ORDER_SELL:
tday_opened[1] += mo.filled_volume
tday_o_locked[1] += mo.volume
self.tday_pos = tday_opened
if self.tday_pos[0] > 0:
self.tday_avp[0] = sum([o.filled_price*o.filled_volume for o in self.orders if o.direction == ORDER_BUY])/self.tday_pos[0]
else:
self.tday_avp[0] = 0.0
if self.tday_pos[1] > 0:
self.tday_avp[1] = sum([o.filled_price*o.filled_volume for o in self.orders if o.direction == ORDER_SELL])/self.tday_pos[1]
else:
self.tday_avp[1] = 0.0
def update_pos(self, key, value):
setattr(self, key, int(value[0]))
def __str__(self):
return unicode(self).encode('utf-8')
def __unicode__(self):
return '%s' % (self.instrument.name)
class GrossPosition(Position):
def __init__(self, instrument, gateway = 'CTP', intraday_close_ratio = 1):
super(GrossPosition, self).__init__(instrument, gateway)
self.pos_tday = [0, 0]
self.pos_yday = [0, 0] # yday's overnight position
self.curr_pos = [0, 0]
self.locked_pos = [0, 0]
self.can_yclose = [0, 0]
self.can_close = [0, 0]
self.can_open = [0, 0]
self.intraday_close_ratio = intraday_close_ratio
def update_pos(self, key, value):
setattr(self, key, [int(value[0]), int(value[1])])
def set_intraday_close_ratio(self, ratio):
self.intraday_close_ratio = ratio
def update_can_close(self, tday_opened, tday_c_locked, yday_c_locked):
self.can_yclose = [0, 0]
self.can_close[0] = max(self.pos_yday[1] + int(tday_opened[1] * self.intraday_close_ratio) - tday_c_locked[0], 0)
self.can_close[1] = max(self.pos_yday[0] + int(tday_opened[0] * self.intraday_close_ratio) - tday_c_locked[1], 0)
def re_calc(self):
tday_opened = [0, 0]
tday_o_locked = [0, 0]
tday_closed = [0, 0]
tday_c_locked = [0, 0]
yday_closed = [0, 0]
yday_c_locked = [0, 0]
for mo in self.orders:
logging.debug(str(mo))
if mo.action_type == OF_OPEN:
if mo.direction == ORDER_BUY:
tday_opened[0] += mo.filled_volume
tday_o_locked[0] += mo.volume
else:
tday_opened[1] += mo.filled_volume
tday_o_locked[1] += mo.volume
elif (mo.action_type == OF_CLOSE) or (mo.action_type == OF_CLOSE_TDAY):
if mo.direction == ORDER_BUY:
tday_closed[0] += mo.filled_volume
tday_c_locked[0] += mo.volume
else:
tday_closed[1] += mo.filled_volume
tday_c_locked[1] += mo.volume
elif mo.action_type == OF_CLOSE_YDAY:
if mo.direction == ORDER_BUY:
yday_closed[0] += mo.filled_volume
yday_c_locked[0] += mo.volume
else:
yday_closed[1] += mo.filled_volume
yday_c_locked[1] += mo.volume
self.update_can_close(tday_opened, tday_c_locked, yday_c_locked)
self.tday_pos[0] = tday_opened[0] + tday_closed[0] + yday_closed[0]
self.tday_pos[1] = tday_opened[1] + tday_closed[1] + yday_closed[1]
if self.tday_pos[0] > 0:
self.tday_avp[0] = sum([o.filled_price*o.filled_volume for o in self.orders if o.direction == ORDER_BUY])/self.tday_pos[0]
else:
self.tday_avp[0] = 0.0
if self.tday_pos[1] > 0:
self.tday_avp[1] = sum([o.filled_price*o.filled_volume for o in self.orders if o.direction == ORDER_SELL])/self.tday_pos[1]
else:
self.tday_avp[1] = 0.0
self.curr_pos[0] = tday_opened[0] - tday_closed[1] + self.pos_yday[0] - yday_closed[1]
self.curr_pos[1] = tday_opened[1] - tday_closed[0] + self.pos_yday[1] - yday_closed[0]
self.locked_pos[0] = self.pos_yday[0] - yday_closed[1] + tday_o_locked[0] - tday_closed[1]
self.locked_pos[1] = self.pos_yday[1] - yday_closed[0] + tday_o_locked[1] - tday_closed[0]
self.can_open[0] = max(self.instrument.max_holding[0] - self.locked_pos[0], 0)
self.can_open[1] = max(self.instrument.max_holding[1] - self.locked_pos[1], 0)
def get_open_volume(self):
return self.can_open
def get_close_volume(self):
return self.can_close
def get_yclose_volume(self):
return self.can_yclose
####头寸
class SHFEPosition(GrossPosition):
def __init__(self, instrument, gateway = 'CTP', intraday_close_ratio = 1):
super(SHFEPosition, self).__init__(instrument, gateway, intraday_close_ratio)
def update_can_close(self, tday_opened, tday_c_locked, yday_c_locked):
self.can_yclose[0] = max(self.pos_yday[1] - yday_c_locked[0], 0)
self.can_yclose[1] = max(self.pos_yday[0] - yday_c_locked[1], 0)
self.can_close[0] = max(int(tday_opened[1] * self.intraday_close_ratio) - tday_c_locked[0], 0)
self.can_close[1] = max(int(tday_opened[0] * self.intraday_close_ratio) - tday_c_locked[1], 0)