Skip to content

Commit

Permalink
add position get get and fix init in looper
Browse files Browse the repository at this point in the history
  • Loading branch information
somewheve committed Mar 8, 2021
1 parent ab6e71e commit 242339a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 41 deletions.
11 changes: 9 additions & 2 deletions ctpbee/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,17 @@ def _start_looper(self):
self.trader.account.basic_info = self.basic_info
""" trader初始化参数"""
self.trader.init_params(params=self.config)

flag = False
while True:
try:
p = next(d)
self.trader(p)
if flag:
p = next(d)
self.trader(p)
else:
from ctpbee.constant import EVENT_INIT_FINISHED
self.app_signal.init_signal.send(Event(type=EVENT_INIT_FINISHED, data=None))
flag = True
except StopIteration:
self.logger.info("回测结束,正在生成结果")
break
Expand Down
112 changes: 73 additions & 39 deletions ctpbee/data_handle/local_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,49 +522,83 @@ def clear_frozen(self):
x.active_orders.clear()
x.calculate_frozen()

def get_all_positions(self):
""" 返回所有的持仓信息 """
position_list = []
def get_all_position_objects(self):
""" 返回PositionData格式的持仓数据 """
pos = []

for x in self.values():
if x.local_symbol == "":
continue
if x.long_pos != 0:
temp = {}
temp['exchange'] = x.exchange
temp['direction'] = "long"
temp['position_profit'] = x.long_pnl
temp['symbol'] = x.local_symbol
temp['local_symbol'] = x.local_symbol
temp['price'] = x.long_price
temp['volume'] = x.long_pos
temp['yd_volume'] = x.long_yd
temp["frozen"] = x.long_pos_frozen
temp["available"] = x.long_pos - x.long_pos_frozen
temp['stare_position_profit'] = x.long_stare_pnl
if x.long_pos == x.long_yd:
temp['position_date'] = 2
elif x.long_pos != x.long_yd:
temp['position_date'] = 1
position_list.append(temp)
if x.short_pos != 0:
temp = {}
temp['exchange'] = x.exchange
temp['direction'] = "short"
temp['position_profit'] = x.short_pnl
temp['symbol'] = x.local_symbol
temp['local_symbol'] = x.local_symbol
temp['price'] = x.short_price
temp['volume'] = x.short_pos
temp['yd_volume'] = x.short_yd
temp["frozen"] = x.short_pos_frozen
temp["available"] = x.short_pos - x.short_pos_frozen
temp['stare_position_profit'] = x.short_stare_pnl
if x.short_pos == x.short_yd:
temp['position_date'] = 2
elif x.short_pos != x.short_yd:
temp['position_date'] = 1
position_list.append(temp)
return position_list
pos.append(PositionData(
symbol=x.symbol,
exchange=x.exchange,
direction=Direction.LONG,
volume=x.long_volume,
frozen=x.long_pos_frozen,
price=x.long_price,
pnl=long_pnl,
yd_volume=long_yd
))
elif x.short_pos != 0:
pos.append(PositionData(
symbol=x.symbol,
exchange=x.exchange,
direction=Direction.SHORT,
volume=x.short_volume,
frozen=x.short_pos_frozen,
price=x.short_price,
pnl=short_pnl,
yd_volume=short_yd
))
return pos

def get_all_positions(self, obj=False):
""" 返回所有的持仓信息 """
if obj:
return self.get_all_position_objects()
else:
position_list = []
for x in self.values():
if x.local_symbol == "":
continue
if x.long_pos != 0:
temp = {}
temp['exchange'] = x.exchange
temp['direction'] = "long"
temp['position_profit'] = x.long_pnl
temp['symbol'] = x.local_symbol
temp['local_symbol'] = x.local_symbol
temp['price'] = x.long_price
temp['volume'] = x.long_pos
temp['yd_volume'] = x.long_yd
temp["frozen"] = x.long_pos_frozen
temp["available"] = x.long_pos - x.long_pos_frozen
temp['stare_position_profit'] = x.long_stare_pnl
if x.long_pos == x.long_yd:
temp['position_date'] = 2
elif x.long_pos != x.long_yd:
temp['position_date'] = 1
position_list.append(temp)
if x.short_pos != 0:
temp = {}
temp['exchange'] = x.exchange
temp['direction'] = "short"
temp['position_profit'] = x.short_pnl
temp['symbol'] = x.local_symbol
temp['local_symbol'] = x.local_symbol
temp['price'] = x.short_price
temp['volume'] = x.short_pos
temp['yd_volume'] = x.short_yd
temp["frozen"] = x.short_pos_frozen
temp["available"] = x.short_pos - x.short_pos_frozen
temp['stare_position_profit'] = x.short_stare_pnl
if x.short_pos == x.short_yd:
temp['position_date'] = 2
elif x.short_pos != x.short_yd:
temp['position_date'] = 1
position_list.append(temp)
return position_list

@property
def length(self):
Expand Down
4 changes: 4 additions & 0 deletions ctpbee/interface/looper/md_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ def subscribe(self, code):

def connect(self, info):
print("虚拟行情已经载入,请使用外部行情")

def unsubscribe(self, local_symbol):
import warnings
warnings.warn("正在回测接口中调用无效的取消订阅接口")

0 comments on commit 242339a

Please sign in to comment.