diff --git a/ft-app/app/models.py b/ft-app/app/models.py index 7052cfb..99dffb0 100644 --- a/ft-app/app/models.py +++ b/ft-app/app/models.py @@ -156,6 +156,7 @@ class OptionTrade(Base): product_code: Mapped[str] = mapped_column(String(10)) contract_code: Mapped[str] = mapped_column(String(10), index=True) option_type: Mapped[str] = mapped_column(String(4)) + direction: Mapped[str] = mapped_column(String(4)) strike_price: Mapped[float] = mapped_column(Float) open_date: Mapped[date] = mapped_column(Date) open_price: Mapped[float] = mapped_column(Float) @@ -170,5 +171,8 @@ class OptionTrade(Base): if self.close_price is None: return None mul = 20 # glass futures point value - result = (self.close_price - self.open_price) * mul - (self.open_fee or 0) - (self.close_fee or 0) + if self.direction == "sell": + result = (self.open_price - self.close_price) * mul - (self.open_fee or 0) - (self.close_fee or 0) + else: + result = (self.close_price - self.open_price) * mul - (self.open_fee or 0) - (self.close_fee or 0) return round(result, 2) \ No newline at end of file diff --git a/ft-app/app/routers/option_trades.py b/ft-app/app/routers/option_trades.py index 3bfc158..7b7b96d 100644 --- a/ft-app/app/routers/option_trades.py +++ b/ft-app/app/routers/option_trades.py @@ -8,6 +8,7 @@ from app.models import OptionTrade, Contract, Product router = APIRouter(prefix="/options", tags=["options"]) OPTION_TYPES = [("C", "C 看涨"), ("P", "P 看跌")] +OPTION_DIRECTIONS = [("buy", "买"), ("sell", "卖")] def get_product_contracts(db: Session) -> dict: @@ -43,6 +44,7 @@ def option_page(request: Request, db: Session = Depends(get_db)): active_nav="options", product_contracts=get_product_contracts(db), option_types=OPTION_TYPES, + option_directions=OPTION_DIRECTIONS, open_trades=open_trades, closed_trades=closed_trades, today=today_str(), @@ -55,6 +57,7 @@ def open_trade( request: Request, contract_code: str = Form(...), option_type: str = Form(...), + direction: str = Form(...), strike_price: float = Form(...), open_date: str = Form(...), open_price: float = Form(...), @@ -69,6 +72,7 @@ def open_trade( product_code=product_code, contract_code=code, option_type=option_type, + direction=direction, strike_price=strike_price, open_date=date.fromisoformat(open_date), open_price=open_price, diff --git a/ft-app/app/templates/options.html b/ft-app/app/templates/options.html index 2cd85eb..d3d7653 100644 --- a/ft-app/app/templates/options.html +++ b/ft-app/app/templates/options.html @@ -49,7 +49,7 @@ -
+
+
+ + +
@@ -84,16 +92,23 @@ {% if open_trades %}
- + {% for t in open_trades %} + @@ -148,16 +163,23 @@
已平仓 · {{ closed_trades|length }} 笔
品种合约类型行权价权利金开仓日期手续费操作
品种合约类型买卖行权价权利金开仓日期手续费操作
{{ t.product_code }} {{ t.contract_code.replace(t.product_code, '', 1) }} {% if t.option_type == 'C' %} - C 看涨 + C {% else %} - P 看跌 + P + {% endif %} + + {% if t.direction == 'sell' %} + + {% else %} + {% endif %} {{ t.strike_price }}
- + {% for t in closed_trades %} + @@ -173,6 +195,8 @@ {% if p > 0 %}+{% endif %}{{ p }} + {% endif %}
品种合约类型行权价开仓平仓权利金平仓价开仓费平仓费盈亏操作
品种合约类型买卖行权价开仓平仓权利金平仓价开仓费平仓费盈亏操作
{{ t.product_code }} {{ t.contract_code.replace(t.product_code, '', 1) }} {% if t.option_type == 'C' %} - C 看涨 + C {% else %} - P 看跌 + P + {% endif %} + + {% if t.direction == 'sell' %} + + {% else %} + {% endif %} {{ t.strike_price }} @@ -233,4 +257,37 @@ function hideCloseForm() { } {% endif %} + + + + + {% endblock %} diff --git a/ft-app/app/templates/trades.html b/ft-app/app/templates/trades.html index 31115df..c1bc0c2 100644 --- a/ft-app/app/templates/trades.html +++ b/ft-app/app/templates/trades.html @@ -167,6 +167,8 @@ {% if p > 0 %}+{% endif %}{{ p }} + {% endif %} @@ -227,4 +229,37 @@ function hideCloseForm() { } {% endif %} + + + + + {% endblock %}