@@ -45,7 +45,20 @@ def arg_to_type(self, arg):
4545 return self .tdict [arg .upper ()]
4646 else :
4747 return arg
48-
48+
49+
50+ class Captial (object ):
51+ """ 账号资金 """
52+ def __init__ (self , dt , contract , type_ , side , direction , price , quantity ):
53+ self .broker_id = None
54+ self .account_id = None
55+ self .margin = None
56+ self .position_profit = None
57+ self .close_profit = None
58+ self .trading_day = None
59+ self .cash = None
60+ self .equity = None
61+
4962
5063class PriceType (object ):
5164 """ 下单类型
@@ -78,23 +91,34 @@ class Direction(object):
7891 """
7992 LONG = 1
8093 SHORT = 2
81- tdict = {'LONG' : LONG ,
94+ arg2type = {'LONG' : LONG ,
8295 'SHORT' : SHORT
8396 }
8497
98+ type2str = { LONG : 'LONG' ,
99+ SHORT : 'SHORT' ,
100+ }
101+
85102 @classmethod
86103 def arg_to_type (self , arg ):
87104 """
88105 把用户输入参数转化为系统类型。
89106 """
90107 if type (arg ) == str :
91- return self .tdict [arg .upper ()]
108+ return self .arg2type [arg .upper ()]
92109 else :
93110 return arg
94111
112+ @classmethod
113+ def type_to_str (self , type ):
114+ return self .type2str [type ]
115+
95116
96117class Transaction (object ):
97118 """ 成交记录。
119+
120+ :ivar ref: 本地编号
121+ :vartype ref: int
98122
99123 :ivar id: 成交编号
100124 :vartype id: :class:`OrderID`
@@ -123,12 +147,13 @@ class Transaction(object):
123147 :ivar commission: 佣金百分比
124148 :vartype commission: float
125149
126- :ivar assure_ratio : 保证金比例。
127- :vartype assure_ratio : float
150+ :ivar margin_ratio : 保证金比例。
151+ :vartype margin_ratio : float
128152 """
129153 def __init__ (self , order = None ):
130154 if order :
131155 self .id = order .id
156+ self .ref = order .ref
132157 self .contract = order .contract
133158 self .direction = order .direction
134159 self .price = order .price
@@ -137,7 +162,7 @@ def __init__(self, order=None):
137162 self .datetime = order .datetime
138163 self .price_type = order .price_type
139164 self .commission = 0
140- self .assure_ratio = 1
165+ self .margin_ratio = 1
141166
142167 def __hash__ (self ):
143168 if hasattr (self , '_hash' ):
@@ -160,14 +185,15 @@ def profit(self, new_price):
160185 profit += (self .price - new_price ) * self .quantity
161186 return profit
162187
163- def deposit (self , new_price ):
188+ def margin (self , new_price ):
164189 """ 根据当前价格计算这笔交易的保证金。
165190
166191 :param float new_price: 当前价格。
167192 :return: 保证金。
168193 :rtype: float
169194 """
170- return self .price * self .quantity * self .assure_ratio
195+ price = self .price if self .contract .is_stock else new_price
196+ return price * self .quantity * self .margin_ratio
171197
172198
173199class OrderID (object ):
@@ -208,7 +234,10 @@ def __ge__(self, other):
208234class Order (object ):
209235 """ 订单
210236
211- :ivar id: 成交编号
237+ :ivar ref: 本地编号
238+ :vartype ref: int
239+
240+ :ivar id: 报单编号
212241 :vartype id: :class:`OrderID`
213242
214243 :ivar contract: 合约。
@@ -233,6 +262,7 @@ class Order(object):
233262 :vartype price_type: :class:`PriceType`
234263 """
235264 def __init__ (self , dt , contract , type_ , side , direction , price , quantity ):
265+ self .ref = OrderID .next_order_id ()
236266 self .id = OrderID .next_order_id ()
237267 self .contract = contract
238268 self .direction = direction
@@ -274,6 +304,7 @@ def __init__(self, str_contract):
274304 assert False
275305 self .exch_type = exchange # 用'stock'表示中国股市
276306 self .code = code
307+ self ._is_stock = True if exchange == 'stock' else False
277308
278309 def __str__ (self ):
279310 """"""
@@ -286,19 +317,22 @@ def __hash__(self):
286317 self ._hash = hash (self .__str__ ())
287318 return self ._hash
288319
320+ @property
321+ def is_stock (self ):
322+ """ 是否是股票"""
323+ return self ._is_stock
324+
289325 @classmethod
290326 def get_trading_interval (self , contract ):
291327 """ 获取合约的交易时段。"""
292328 pass
293329
294330 @classmethod
295- def get_assure_ratio (self , contract ):
331+ def get_margin_ratio (self , contract ):
296332 """ 获取合约的交易时段。"""
297333 pass
298334
299335
300-
301-
302336class Period (object ):
303337 """ 周期 """
304338 #class Type(Enum):
@@ -387,22 +421,51 @@ def __init__(self, order, transaction):
387421
388422 @property
389423 def datetime (self ):
424+ """ 持仓日期 """
390425 return self .transaction .datetime
391426
392427 @property
393428 def price (self ):
394- """ 成交价格"""
429+ """ 成交价格 """
395430 return self .transaction .price
396431
397432 @property
398- def assure_ratio (self ):
433+ def margin_ratio (self ):
399434 """ 保证金比例 """
400- return self .transaction .assure_ratio
435+ return self .transaction .margin_ratio
401436
402437 @property
403438 def direction (self ):
404439 return self .transaction .direction
405440
441+ @property
442+ def margin (self , new_price ):
443+ """ 持仓的保证金占用 """
444+ return self .transaction .margin (new_price );
445+
446+ def profit (self , new_price ):
447+ """ 持仓盈亏 """
448+ return self .profit (new_price )
449+
450+ @property
451+ def close_profit (self ):
452+ """ 平仓盈亏 """
453+ pass
454+
455+ @property
456+ def pre_margin (self , new_price ):
457+ """ 昨日保证金占用 """
458+ pass
459+
460+ @property
461+ def cost (self ):
462+ """ 持仓成本 """
463+ return self .price * self .quantity
464+
465+
466+
467+
468+
406469 #def order_time(self):
407470 #return self.order.datetime
408471
0 commit comments