VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 11
声望: 0

在VN trader中,用什么指令函数,或者取哪个变量,能获取持仓信息?变量的数据结构是怎样的?

Administrator
avatar
加入于:
帖子: 4548
声望: 325

https://www.vnpy.com/docs/cn/index.html

请直接看文档

Member
avatar
加入于:
帖子: 11
声望: 0

在文档里,没看到有关的获取持仓的函数介绍。
请问,在UI界面,用什么函数与变量,能获得制定合约的持仓数量,多仓与空仓都有的情况?

Administrator
avatar
加入于:
帖子: 4548
声望: 325

底层持仓的数据,请通过PositionMonitor来查看,相关代码实现里面都有

Member
avatar
加入于:
帖子: 11
声望: 0

找到了持仓监控的类,没看懂如何读取持仓数据?是放在self.下的哪个变量?用什么机制读取?
class PositionMonitor(BaseMonitor):
"""
Monitor for position data.
"""

event_type = EVENT_POSITION
data_key = "vt_positionid"
sorting = True

headers = {
    "symbol": {"display": "代码", "cell": BaseCell, "update": False},
    "exchange": {"display": "交易所", "cell": EnumCell, "update": False},
    "direction": {"display": "方向", "cell": DirectionCell, "update": False},
    "volume": {"display": "数量", "cell": BaseCell, "update": True},
    "yd_volume": {"display": "昨仓", "cell": BaseCell, "update": True},
    "frozen": {"display": "冻结", "cell": BaseCell, "update": True},
    "price": {"display": "均价", "cell": BaseCell, "update": False},
    "pnl": {"display": "盈亏", "cell": PnlCell, "update": True},
    "gateway_name": {"display": "接口", "cell": BaseCell, "update": False},
}
Administrator
avatar
加入于:
帖子: 4548
声望: 325

CTA策略模块无法访问底层账户的实际持仓,可以通过self.pos查看自己的逻辑持仓

Member
avatar
加入于:
帖子: 46
声望: 1

是不是可以在策略里面调用strategy engine里面的offset_converter.get_position_holding获得

用Python的交易员 wrote:

CTA策略模块无法访问底层账户的实际持仓,可以通过self.pos查看自己的逻辑持仓

Member
avatar
加入于:
帖子: 5055
声望: 305

可以通过main_engine.get_position获取

Member
avatar
加入于:
帖子: 46
声望: 1

strategy里面怎样可以调用main_engine的get_position呢?

xiaohe wrote:

可以通过main_engine.get_position获取

Member
avatar
加入于:
帖子: 5055
声望: 305

可参考template的get_pricetick函数

Member
avatar
加入于:
帖子: 46
声望: 1

感谢

xiaohe wrote:

可参考template的get_pricetick函数

Member
avatar
加入于:
帖子: 13
声望: 1

还是不会,我也需要取到多空各持仓多少 持仓均价多少

Member
avatar
加入于:
帖子: 13
声望: 1

\vnpy-master\build\lib\vnpy\trader\engine.py

def get_leg(self, vt_symbol: str) -> LegData:
""""""
leg = self.legs.get(vt_symbol, None)

    if not leg:
        leg = LegData(vt_symbol)
        self.legs[vt_symbol] = leg

        # Subscribe market data
        contract = self.main_engine.get_contract(vt_symbol)
        if contract:
            leg.update_contract(contract)

            req = SubscribeRequest(
                contract.symbol,
                contract.exchange
            )
            self.main_engine.subscribe(req, contract.gateway_name)

        # Initialize leg position
        for direction in Direction:
            vt_positionid = f"{vt_symbol}.{direction.value}"
            position = self.main_engine.get_position(vt_positionid)

            if position:
                leg.update_position(position)

    return leg
Member
avatar
加入于:
帖子: 13
声望: 1

C:\vnstudio\Lib\site-packages\vnpy_ctastrategy\template.py

def get_pricetick(self):
"""
Return pricetick data of trading contract.
"""
return self.cta_engine.get_pricetick(self)

Member
avatar
加入于:
帖子: 13
声望: 1

\vnpy-master\examples\no_ui\run.py
def run_child():
"""
Running in the child process.
"""
SETTINGS["log.file"] = True

event_engine = EventEngine()
main_engine = MainEngine(event_engine)
main_engine.add_gateway(CtpGateway)
cta_engine = main_engine.add_app(CtaStrategyApp)
main_engine.write_log("主引擎创建成功")

log_engine = main_engine.get_engine("log")
event_engine.register(EVENT_CTA_LOG, log_engine.process_log_event)
main_engine.write_log("注册日志事件监听")

main_engine.connect(ctp_setting, "CTP")
main_engine.write_log("连接CTP接口")

sleep(10)

cta_engine.init_engine()
main_engine.write_log("CTA策略初始化完成")

cta_engine.init_all_strategies()
sleep(60)   # Leave enough time to complete strategy initialization
main_engine.write_log("CTA策略全部初始化")

cta_engine.start_all_strategies()
main_engine.write_log("CTA策略全部启动")
Member
avatar
加入于:
帖子: 13
声望: 1

\vnpy-master\vnpy\trader\engine.py

def get_position(self, vt_positionid: str) -> Optional[PositionData]:
"""
Get latest position data by vt_positionid.
"""
return self.positions.get(vt_positionid, None)

Member
avatar
加入于:
帖子: 13
声望: 1

\vnpy-master\vnpy\trader\object.py
C:\vnstudio\lib\site-packages\vnpy\trader\object.py

class PositionData(BaseData):
"""
Positon data is used for tracking each individual position holding.
"""

symbol: str
exchange: Exchange
direction: Direction

volume: float = 0
frozen: float = 0
price: float = 0
pnl: float = 0
yd_volume: float = 0

def __post_init__(self):
    """"""
    self.vt_symbol = f"{self.symbol}.{self.exchange.value}"
    self.vt_positionid = f"{self.vt_symbol}.{self.direction.value}"
Member
avatar
加入于:
帖子: 13
声望: 1
C:\vnstudio\Lib\site-packages\vnpy_ctastrategy\template.py 文件里添加

def get_position(self,vt_positionid):
"""
返回持仓数据PositionData
"""
return self.cta_engine.get_position(self,vt_positionid)

Member
avatar
加入于:
帖子: 12
声望: 0

没看懂啊,大佬能不能再讲详细一点

Member
avatar
加入于:
帖子: 12
声望: 0

八八八 wrote:

C:\vnstudio\Lib\site-packages\vnpy_ctastrategy\template.py 文件里添加

def get_position(self,vt_positionid):
"""
返回持仓数据PositionData
"""
return self.cta_engine.get_position(self,vt_positionid)
没看懂啊,大佬能不能再讲详细一点

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】