-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathinstrument.py
352 lines (321 loc) · 13.7 KB
/
instrument.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#-*- coding:utf-8 -*-
import datetime
import pyktlib
import dbaccess
import copy
from misc import *
class ProductType:
Future, Stock, Option = range(3)
class VolGrid(object):
def __init__(self, name, accrual = 'COM', tday = datetime.date.today(), is_spot = False, ccy = 'CNY'):
self.name = name
self.accrual = accrual
self.ccy = ccy
self.df = {}
self.fwd = {}
self.last_update = {}
self.volnode = {}
self.volparam = {}
self.underlier = {}
self.t2expiry = {}
self.main_cont = ''
self.option_insts = {}
self.spot_model = is_spot
def copy_volgrid(vg):
volgrid = VolGrid(vg.name, accrual = vg.accrual, is_spot = vg.spot_model, ccy = vg.ccy)
volgrid.main_cont = vg.main_cont
for expiry in vg.option_insts:
volgrid.df[expiry] = vg.df[expiry]
volgrid.fwd[expiry] = vg.fwd[expiry]
volgrid.last_update[expiry] = vg.last_update[expiry]
volgrid.volnode[expiry] = pyktlib.Delta5VolNode(vg.t2expiry[expiry]/BDAYS_PER_YEAR,
vg.fwd[expiry],
vg.volparam[expiry][0],
vg.volparam[expiry][1],
vg.volparam[expiry][2],
vg.volparam[expiry][3],
vg.volparam[expiry][4],
vg.accrual)
volgrid.volparam[expiry] = copy.copy(vg.volparam[expiry])
volgrid.underlier[expiry] = copy.copy(vg.underlier[expiry])
volgrid.t2expiry[expiry] = vg.t2expiry[expiry]
volgrid.option_insts[expiry] = copy.copy(vg.option_insts[expiry])
return volgrid
class Instrument(object):
def __init__(self,name):
self.name = name
self.exchange = 'CFFEX'
self.ptype = ProductType.Future
self.product = 'IF'
self.ccy = 'CNY'
self.broker_fee = 0.0
self.marginrate = (0,0)
self.multiple = 0
self.pos = 1
self.tick_base = 0
self.start_tick_id = 0
self.last_tick_id = 0
# market snapshot
self.price = 0.0
self.prev_close = 0.0
self.volume = 0
self.open_interest = 0
self.last_update = 0
self.ask_price1 = 0.0
self.ask_vol1 = 0
self.bid_price1 = 0.0
self.bid_vol1 = 0
self.ask_price2 = 0.0
self.ask_vol2 = 0
self.bid_price2 = 0.0
self.bid_vol2 = 0
self.ask_price3 = 0.0
self.ask_vol3 = 0
self.bid_price3 = 0.0
self.bid_vol3 = 0
self.ask_price4 = 0.0
self.ask_vol4 = 0
self.bid_price4 = 0.0
self.bid_vol4 = 0
self.ask_price5 = 0.0
self.ask_vol5 = 0
self.bid_price5 = 0.0
self.bid_vol5 = 0
self.up_limit = 1e10
self.down_limit = -1e10
self.last_traded = 0
self.max_holding = (500, 500)
self.mid_price = 0.0
self.cont_mth = 205012 # only used by option and future
self.expiry = datetime.datetime(2050,12,31, 15, 0, 0)
self.day_finalized = False
def shift_price(self, direction, tick_num = 0, price_level = '1'):
price_str = 'bid_price' + str(price_level) if direction > 0 else 'ask_price' + str(price_level)
base_price = getattr(self, price_str)
if direction > 0:
return min(base_price + tick_num * self.tick_base, self.up_limit)
else:
return max(base_price - tick_num * self.tick_base, self.down_limit)
def check_price_limit(self, num_tick = 0):
tick_base = self.tick_base
if (self.ask_price1 >= self.up_limit - num_tick * tick_base) or (self.bid_price1 <= self.down_limit + num_tick * tick_base):
return True
else:
return False
def fair_price(self):
self.mid_price = (self.ask_price1 + self.bid_price1)/2.0
return self.mid_price
def initialize(self):
pass
def update_param(self, tday):
pass
def calc_margin_amount(self, direction, price = 0.0):
my_marginrate = self.marginrate[0] if direction == ORDER_BUY else self.marginrate[1]
return self.price * self.multiple * my_marginrate
class SpreadInst(object):
def __init__(self, inst_data, instIDs, weights, multiple = None):
self.instIDs = instIDs
self.name = '_'.join([str(s) for s in instIDs + weights])
self.inst_objs = [inst_data[inst] for inst in instIDs]
self.weights = weights
self.conv_factor = [ inst_obj.multiple for inst_obj in self.inst_objs ]
self.tick_base = [inst_obj.tick_base for inst_obj in self.inst_objs]
self.multiple = multiple if multiple != None else self.conv_factor[-1]
self.last_update = [inst_obj.last_update for inst_obj in self.inst_objs]
self.ask_price1 = 0.0
self.ask_vol1 = 0
self.bid_price1 = 0.0
self.bid_vol1 = 0
self.mid_price = 0
def update(self):
self.bid_price1 = self.price('bid')
self.ask_price1 = self.price('ask')
self.mid_price = (self.ask_price1 + self.bid_price1)/2.0
self.bid_vol1 = min([inst_obj.bid_vol1 if w > 0 else inst_obj.ask_vol1 for inst_obj, w in zip(self.inst_objs, self.weights)])
self.ask_vol1 = min([inst_obj.ask_vol1 if w > 0 else inst_obj.bid_vol1 for inst_obj, w in zip(self.inst_objs, self.weights)])
def shift_price(self, direction, tick_num = 0, price_level = '1'):
price_str = 'bid_price' + str(price_level) if direction > 0 else 'ask_price' + str(price_level)
base_price = getattr(self, price_str)
return base_price + sign(direction) * tick_num * sum([abs(tb) for tb in self.tick_base])
def price(self, direction = 'mid', prices = None):
if prices == None:
if direction == 'bid':
fields = ['bid_price1', 'ask_price1']
elif direction == 'ask':
fields = ['ask_price1', 'bid_price1']
else:
fields = ['mid_price', 'mid_price']
prices = [getattr(inst_obj, fields[0]) if w>0 else getattr(inst_obj, fields[1]) for inst_obj, w in zip(self.inst_objs, self.weights)]
return sum([ p * w * cf for (p, w, cf) in zip(prices, self.weights, self.conv_factor)])/self.multiple
class Stock(Instrument):
def __init__(self,name):
Instrument.__init__(self, name)
self.initialize()
def initialize(self):
self.product = self.name
self.ptype = ProductType.Stock
self.start_tick_id = 1530000
self.last_tick_id = 2130000
self.multiple = 1
self.tick_base = 0.01
self.broker_fee = 0
self.marginrate = (1,0)
if self.name in CHN_Stock_Exch['SZE']:
self.exchange = 'SZE'
else:
self.exchange = 'SSE'
return
class Future(Instrument):
def __init__(self,name):
Instrument.__init__(self, name)
self.initialize()
def initialize(self):
self.ptype = ProductType.Future
self.product = inst2product(self.name)
prod_info = dbaccess.load_product_info(self.product)
self.exchange = prod_info['exch']
if self.exchange == 'CZCE':
self.cont_mth = int(self.name[-3:]) + 201000
else:
self.cont_mth = int(self.name[-4:]) + 200000
self.start_tick_id = prod_info['start_min'] * 1000
if self.product in night_session_markets:
self.start_tick_id = 300000
self.last_tick_id = prod_info['end_min'] * 1000
self.multiple = prod_info['lot_size']
self.tick_base = prod_info['tick_size']
self.broker_fee = prod_info['broker_fee']
return
def update_param(self, tday):
self.marginrate = dbaccess.load_inst_marginrate(self.name)
class OptionInst(Instrument):
Greek_Map = {'pv': 'price', 'delta': 'delta', 'gamma': 'gamma', \
'theta': 'theta', 'vega': 'vega'}
def __init__(self, name):
self.strike = 0.0 # only used by option
self.otype = 'C' # only used by option
self.underlying = '' # only used by option
Instrument.__init__(self, name)
self.pricer = None
self.pricer_func = pyktlib.BlackPricer
self.pricer_param = []
self.pv = 0.0
self.delta = 1
self.theta = 0.0
self.gamma = 0.0
self.vega = 0.0
self.risk_price = self.price
self.risk_updated = 0.0
self.margin_param = [0.15, 0.1]
self.initialize()
def approx_pv(self, curr_price):
dp = (curr_price - self.risk_price)
return self.pv + self.delta * dp + self.gamma * dp * dp / 2.0
def approx_delta(self, curr_price):
return self.delta + (curr_price - self.risk_price) * self.gamma
def initialize(self):
pass
def update_param(self, tday):
pass
def set_pricer(self, vg, irate):
expiry = self.expiry
t2exp = vg.t2expiry[expiry]/BDAYS_PER_YEAR
param = [t2exp, vg.fwd[expiry], vg.volnode[expiry], self.strike, irate, self.otype] + self.pricer_param
self.pricer = self.pricer_func(*param)
def update_greeks(self, last_updated, greeks = ['pv', 'delta', 'gamma', 'vega']):
if self.pricer == None:
return None
for attr in greeks:
setattr(self, attr, getattr(self.pricer, self.Greek_Map[attr])())
self.risk_price = self.pricer.fwd_()
self.risk_updated = last_updated
def calc_margin_amount(self, direction, price = 0.0):
my_margin = self.price
if direction == ORDER_SELL:
a = self.margin_param[0]
b = self.margin_param[1]
if price == 0.0:
price = self.strike
if self.otype == 'C':
my_margin += max(price * a - max(self.strike-price, 0), price * b)
else:
my_margin += max(price * a - max(price - self.strike, 0), self.strike * b)
return my_margin * self.multiple
class StockOptionInst(OptionInst):
def __init__(self,name):
OptionInst.__init__(self, name)
self.margin_param = [0.12, 0.07]
self.initialize()
def initialize(self):
self.ptype = ProductType.Option
prod_info = dbaccess.load_stockopt_info(self.name)
self.exchange = prod_info['exch']
self.multiple = prod_info['lot_size']
self.tick_base = prod_info['tick_size']
self.strike = prod_info['strike']
self.otype = prod_info['otype']
self.underlying = prod_info['underlying']
self.product = self.underlying
self.cont_mth = prod_info['cont_mth']
self.expiry = get_opt_expiry(self.underlying, self.cont_mth)
class FutOptionInst(OptionInst):
def __init__(self,name):
OptionInst.__init__(self, name)
if self.exchange not in ['CFFEX', 'SHFE']:
self.pricer_func = pyktlib.AmericanFutPricer
self.pricer_param = [AMERICAN_OPTION_STEPS]
self.margin_param = [0.15, 0.1]
else:
self.pricer_func = pyktlib.BlackPricer
self.pricer_param = []
self.margin_param = [0.15, 0.1]
self.initialize()
def initialize(self):
self.ptype = ProductType.Option
self.product = inst2product(self.name)
if (self.product[-3:] == 'Opt') and ((self.product in product_code['CZCE']) \
or (self.product in product_code['SHFE'])):
if (self.product in product_code['CZCE']):
idx = 5
else:
idx = 6
self.underlying = str(self.name[:idx])
self.otype = str(self.name[idx])
if idx == 5:
cmth = int(self.underlying[-3:])
if cmth < 800:
cmth = cmth + 202000
else:
cmth = cmth + 201000
else:
cmth = int(self.underlying[-4:]) + 200000
self.cont_mth = cmth
self.strike = float(self.name[(idx+1):])
self.product = str(self.name[:2])
self.expiry = get_opt_expiry(self.underlying, self.cont_mth)
else:
sep_name = self.name.split('-')
if self.product == 'IO_Opt':
self.underlying = str(sep_name[0].replace('IO','IF'))
self.strike = float(sep_name[2])
self.otype = str(sep_name[1])
self.cont_mth = int(self.underlying[-4:]) + 200000
self.expiry = get_opt_expiry(self.underlying, self.cont_mth)
self.product = 'IO'
elif '_Opt' in self.product:
self.underlying = str(sep_name[0])
self.strike = float(sep_name[2])
self.otype = str(sep_name[1])
self.cont_mth = int(self.underlying[-4:]) + 200000
self.expiry = get_opt_expiry(self.underlying, self.cont_mth)
self.product = str(self.product[:-4])
prod_info = dbaccess.load_product_info(self.product)
self.exchange = prod_info['exch']
self.start_tick_id = prod_info['start_min'] * 1000
if self.product in night_session_markets:
self.start_tick_id = 300000
self.last_tick_id = prod_info['end_min'] * 1000
self.multiple = prod_info['lot_size']
self.tick_base = prod_info['tick_size']
self.broker_fee = prod_info['broker_fee']
return