Skip to content

Position

Positions

The feature of list_positions is used to query unrealized gain or loss of account and you need to login first.

In

api.list_positions?

Out

Signature:
    api.list_positions(
    account: shioaji.account.Account = None,
    timeout: int = 5000,
    cb: Callable[[List[Union[shioaji.position.StockPosition, shioaji.position.FuturePosition]]], NoneType] = None,
    ) -> List[Union[shioaji.position.StockPosition, shioaji.position.FuturePosition]]
Docstring:
    query account of unrealized gain or loss
    Args:
        account (:obj:Account):
            choice the account from listing account (Default: stock account)

Stocks

In

api.list_positions(api.stock_account)

Out

stock_positions = [StockPosition(code='2890', direction=<Action.Buy: 'Buy'>, quantity=10, price=10.1, pnl=1234.0, yd_quantity=10,margin_purchase_amount=0, collateral=0, short_sale_margin=0, interest=0)]

Stock to dataframe

In

df = pd.DataFrame(s.__dict__ for s in stock_positions)
df

Out

id code cond direction pnl price last_price quantity yd_quantity margin_purchase_amount collateral short_sale_margin interest
0 2890 StockOrderCond.Cash Action.Buy -1234.0 10.1 12.0 10 10 0 0 0 0

Attributes:

code (str): contract id.
direction (Action): action.
    {Buy, Sell}
quantity (int): quantity.
price (float): the average price.
last_price (float): last price.
pnl (float): unrealized profit.
yd_quantity (int): yesterday
cond (StockOrderCond): Default Cash.
    {Cash(現股), Netting(餘額交割), MarginTrading(融資), ShortSelling(融券), Emerging(興櫃)}
margin_purchase_amount (int): margin_purchase_amount.
collateral (int): collateral.
short_sale_margin (int): short_sale_margin.
interest (int): interest.
Shares

In

api.list_positions(api.stock_account, unit=sj.constant.Unit.Share)

Out

[StockPosition(id=0, code='2890', direction=<Action.Buy: 'Buy'>, quantity=10000, price=10.1, last_price=12.0, pnl=1234.0, yd_quantity=10000, margin_purchase_amount=0, collateral=0, short_sale_margin=0, interest=0)]

Futures

In

api.list_positions(api.futopt_account)

Out

[FuturePosition(code='TX201370J2', direction=<Action.Buy: 'Buy'>, quantity=3, price=131.0000, last_price=126.0, pnl=-750.00)]

Future to dataframe

In

positions = api.list_positions(api.futopt_account)
df = pd.DataFrame(p.__dict__ for p in positions)
df

Out

id code direction quantity price last_price pnl
0 TX201370J2 Action.Buy 3 131.0000 126.0 -750.00

Attributes:

code (str): contract id.
direction (Action): action.
    {Buy, Sell}
quantity (int): quantity.
price (float): the average price.
last_price (float): last price.
pnl (float): unrealized profit.