Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
51bitquant committed Jul 16, 2022
1 parent 12cf4dd commit 9c050b5
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/howtrader文档说明.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

5. [如何对接tradingview等三方交易信号](./如何对接tradingview等三方交易信号.md)

6. [linux下如何安装anaconda](./linux下安装anaconda.md)
6. [linux下如何安装anaconda](./linux下安装anaconda.md)

7. [如何订阅仓位数据](./如何订阅仓位数据.md)
109 changes: 109 additions & 0 deletions docs/如何订阅仓位数据.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# 如何订阅仓位数据


## 合约

订阅合约的仓位数据,可以看源码中如何推送事件的。可以看howtrader.trader.gateway文件中的on_position方法

```
def on_position(self, position: PositionData) -> None:
"""
Position event push.
Position event of a specific vt_symbol is also pushed.
"""
self.on_event(EVENT_POSITION, position)
self.on_event(EVENT_POSITION + position.vt_symbol, position)
```

其中PositionData定义而如下:

```
@dataclass
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
liquidation_price: float = 0
leverage: int = 1
pnl: float = 0
yd_volume: float = 0
def __post_init__(self) -> None:
""""""
self.vt_symbol: str = f"{self.symbol}.{self.exchange.value}"
self.vt_positionid: str = f"{self.vt_symbol}.{self.direction.value}"
```

这里面,它推送两个事件,一个是EVENT_POSITION,
也就是只要是仓位的数据它都通过这个来推送,如果你想订阅所有的仓位数据,可以订阅这个事件,还有一个就是订阅EVENT_POSITION
\+ position.vt_symbol,
比如你想订阅合约中的BTCUSDT.BINANCE的仓位,可以通过self.cta.event_engine.register(EVENT_POSITION
\+ BTCUSDT.BINANCE, self.my_on_position)

核心就是通过event_engine来订阅仓位数据,然后有更新会推送给你。

或者在CTA策略中,可以通过self.get_position(vt_positionid)来获取,改vt_positionid是:
f"{self.vt_symbol}.{self.direction.value}",对于单向持仓而言,self.direction.value是NET
也就是vt_position是vt_symbol.NET.

## 现货

对于现货,它就没有仓位的概念,要通过资产的方式来获取,同样在howtrader.trader.gateway中,可以看on_account的事件推送:

```
def on_account(self, account: AccountData) -> None:
"""
Account event push.
Account event of a specific vt_accountid is also pushed.
"""
self.on_event(EVENT_ACCOUNT, account)
self.on_event(EVENT_ACCOUNT + account.vt_accountid, account)
```

如果是订阅所有的账户信息,就通过注册EVENT_ACCOUNT这个事件来处理。 如果想订阅某一个特定的资产可以通过

```
EVENT_ACCOUNT+account.vt_accountid
```
来订阅,那么account.vt_accountid是什么呢?可以看看howtrader.trader.object中看AccountData类的定义:

```
@dataclass
class AccountData(BaseData):
"""
Account data contains information about balance, frozen and
available.
"""
accountid: str
balance: float = 0
frozen: float = 0
def __post_init__(self) -> None:
""""""
self.available: float = self.balance - self.frozen
self.vt_accountid: str = f"{self.gateway_name}.{self.accountid}"
```

可以看到vt_accountid 是gateway_name + accountid,
gateway_name对于usdt合约是BINANCE_USDT, 现货是BINANCE_SPOT,
币币合约是BINANCE_INVERSE, 所以比如订阅USDT资产,该事件就是
BINANCE_SPOT.USDT,BTC资产就是BINANCE_SPOT.BTC, ETH就是BINANCE_SPOT.ETH,
以此类推。

0 comments on commit 9c050b5

Please sign in to comment.