<div class="options-page-wrap" style="grid-column:1/-1" id="options-root"
     data-default-underly="{{ options_default_underly | default('ETH') }}"
     data-budget-buffer="{{ options_budget_buffer | default(0.95) }}"
     data-trade-budget="{{ options_trade_budget | default(10) }}"
     data-compound-full-enabled="{% if options_compound_full_enabled %}1{% else %}0{% endif %}"
     data-compound-cap-enabled="{% if options_compound_full_cap_enabled %}1{% else %}0{% endif %}"
     data-compound-cap-usdc="{{ '%.2f'|format(options_compound_full_cap_usdc|default(300)|float) }}"
     data-ask-liq-filter="{% if options_chain_ask_liq_filter is defined %}{{ '1' if options_chain_ask_liq_filter else '0' }}{% else %}1{% endif %}">
    {% set compound_on = options_compound_full_enabled if options_compound_full_enabled is defined else true %}
    {% if not options_enabled %}
    <div class="flash" style="margin-bottom:12px">期权未启用:请在 <code>crypto_monitor_okx/.env</code> 设置 <code>OKX_OPTIONS_ENABLED=true</code> 及 <code>OKX_API_*</code>(永续与期权共用),然后 <code>pm2 restart crypto_okx --update-env</code>.</div>
    {% endif %}
    {% if options_enabled and options_open_allowed is defined and not options_open_allowed %}
    <div class="flash" style="margin-bottom:12px">当前交易模式为对冲(永期/期期),不可单独开期权;持仓可在此查看/平仓.切换请到 env「交易模式」.</div>
    {% endif %}

    <div class="options-dual-grid">
        <div class="card options-order-card"{% if options_open_allowed is defined and not options_open_allowed %} style="opacity:.72"{% endif %}>
            <h2>期权下单{% if options_open_allowed is defined and not options_open_allowed %} <small class="muted">(对冲模式已禁用开仓)</small>{% endif %}</h2>
            <details class="opt-close-rule opt-open-rule">
                <summary>开仓规则说明</summary>
                <div class="opt-close-rule-body">
                    <p>报价单位为每 1 ETH/BTC;1 张 = 0.01。默认选中<strong>最近一期</strong>到期,可手动改。</p>
                    <ul>
                        <li><strong>列表</strong>含卖一/买一;<strong>T 型</strong>仅卖一(买方开仓),中间为跨式双买测算。</li>
                        <li>环境配置「链上仅显示有卖一」开启时,隐藏无真实卖一或深度不足 1 张的合约(估算价 <strong>~</strong> 亦不显示)。</li>
                        <li><strong>开仓只认真实卖一价且卖一深度≥1</strong>;无深度时面板显示参考标记价并禁用买入。</li>
                        <li>链展示近 <span id="opt-chain-dte">14</span> 日到期;列表与 T 型默认<strong>平值 + 实值3档 + 虚值3档</strong>,勾选「展开全部」看全部行权价(若当前为实值/虚值筛选会自动切回「全部」)。</li>
                        <li>「按可用余额打满」可用额度 = min(交易户可用 USDC, 单笔预算 <strong id="opt-trade-budget">{{ '%.2f'|format(options_trade_budget|default(10)|float) }}</strong>),再 × 预算缓冲 <strong id="opt-budget-buf">{{ '%.2f'|format(options_budget_buffer|default(0.95)|float) }}</strong> 算张数(env 可改)。</li>
                        <li>「全仓复利」用期权交易户<strong>全部可用</strong>×缓冲开仓(不受单笔预算限制);可选开启全仓上限;该模式下仅允许同时 1 笔持仓。</li>
                        <li><strong>币本位</strong>(env <code>OKX_OPTIONS_MARGIN_MODE=coin</code>):按交易户 USDT×缓冲买满 ETH/BTC 再开满期权;平仓后自动卖回 USDT;对冲仍仅 USDC。有仓勿切换本位。</li>
                        <li><strong>翻倍出场</strong>:开仓时可勾选;1倍=盈利等于权利金,买一可回收达标后限价平;持仓卡可改倍数或关闭。</li>
                        <li>平仓仅买一限价,详见说明文档。</li>
                    </ul>
                    <p><a href="/options/guide" target="_blank" rel="noopener">打开《期权开平仓与监控说明》</a></p>
                </div>
            </details>
            <div class="form-row options-chain-toolbar">
                <button type="button" class="btn-secondary opt-uly-btn active" data-uly="ETH">ETH</button>
                <button type="button" class="btn-secondary opt-uly-btn" data-uly="BTC">BTC</button>
                <select id="opt-exp-select"><option value="">选择到期日</option></select>
                <span class="opt-chain-view-group">
                    <button type="button" class="btn-secondary opt-view-btn active" data-view="list">列表</button>
                    <button type="button" class="btn-secondary opt-view-btn" data-view="t">T 型</button>
                </span>
                <span id="opt-type-btn-group" class="opt-type-btn-group">
                    <button type="button" class="btn-secondary opt-type-btn active" data-type="C">看涨 Call</button>
                    <button type="button" class="btn-secondary opt-type-btn" data-type="P">看跌 Put</button>
                </span>
                <button type="button" class="btn-secondary opt-money-btn active" data-money="all">全部</button>
                <button type="button" class="btn-secondary opt-money-btn" data-money="itm">实值</button>
                <button type="button" class="btn-secondary opt-money-btn" data-money="otm">虚值</button>
                <label id="opt-strike-expand-wrap" class="opt-strike-expand-label">
                    <input type="checkbox" id="opt-strike-expand-all"> 展开全部
                </label>
                <button type="button" class="btn-secondary" id="opt-load-chain">刷新链</button>
            </div>
            <div id="opt-index-line" class="muted"></div>
            <div class="options-strike-table-wrap" id="opt-strike-table-wrap">
                <table class="options-strike-table" id="opt-strike-table">
                    <thead>
                        <tr id="opt-strike-head-list">
                            <th>行权价</th>
                            <th>类型</th>
                            <th>合约</th>
                            <th>卖一/张</th>
                            <th title="USDC:指数÷卖一;币本位:1÷卖一(卖一为币报价)">杠杆</th>
                            <th>买一/张</th>
                            <th>到期平衡</th>
                            <th>距平衡</th>
                            <th>操作</th>
                        </tr>
                        <tr id="opt-strike-head-t" class="hidden" hidden>
                            <th colspan="3" class="opt-t-head-call">Call</th>
                            <th colspan="3" class="opt-t-head-mid">跨式</th>
                            <th colspan="3" class="opt-t-head-put">Put</th>
                        </tr>
                        <tr id="opt-strike-head-t-cols" class="hidden" hidden>
                            <th>卖一/张</th>
                            <th>类型</th>
                            <th>操作</th>
                            <th>行权价</th>
                            <th title="Call卖一+Put卖一(每1币)">双买/币</th>
                            <th title="到期测算平衡带">平衡带</th>
                            <th>类型</th>
                            <th>卖一/张</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="opt-strike-tbody">
                        <tr><td colspan="9" class="muted">请选择到期日</td></tr>
                    </tbody>
                </table>
            </div>
            <div id="opt-order-panel-host" class="opt-order-backdrop" hidden aria-hidden="true">
                <div id="opt-order-panel" class="opt-order-dialog" role="dialog" aria-modal="true" aria-labelledby="opt-order-dialog-title" style="display:none">
                    <div class="opt-order-dialog-head">
                        <h3 class="opt-order-title" id="opt-order-dialog-title">下单</h3>
                        <button type="button" class="btn-secondary" id="opt-order-close-btn" style="font-size:.72rem;padding:2px 10px">取消</button>
                    </div>
                    <div class="opt-order-layout">
                        <div class="opt-order-main">
                            <div id="opt-order-inst" class="options-order-inst"></div>
                            <div class="options-order-grid">
                                <div><span class="k">卖一/张</span><span id="opt-order-ask" class="v">—</span></div>
                                <div><span class="k">买一/张</span><span id="opt-order-bid" class="v">—</span></div>
                                <div><span class="k">参考标记价</span><span id="opt-order-ref-ask" class="v muted">—</span></div>
                                <div><span class="k">张数</span><span id="opt-order-sheets" class="v">—</span></div>
                                <div><span class="k" id="opt-order-eth-label">ETH 数量</span><span id="opt-order-eth" class="v">—</span></div>
                                <div><span class="k">预估权利金</span><span id="opt-order-premium" class="v">—</span></div>
                                <div><span class="k">合约杠杆</span><span id="opt-order-leverage" class="v" title="名义价值÷权利金,测算用">—</span></div>
                                <div><span class="k">到期平衡</span><span id="opt-order-expiry-be" class="v">—</span></div>
                                <div><span class="k">距平衡</span><span id="opt-order-dist-be" class="v">—</span></div>
                            </div>
                            <div class="options-estimate-row">
                                <div class="opt-est-main">
                                    <label class="btn-secondary opt-order-chip" for="opt-target-idx" title="仅作到期实值估算参考">目标位(指数)</label>
                                    <input type="number" id="opt-target-idx" class="opt-target-idx" step="0.1" min="0" placeholder="参考指数·到期实值"
                                           autocomplete="off" inputmode="decimal" data-lpignore="true" data-1p-ignore="true" data-form-type="other">
                                    <span class="k">预计价值</span>
                                    <span id="opt-est-value" class="v">—</span>
                                    <span class="k">盈利</span>
                                    <span id="opt-est-profit" class="v">—</span>
                                    <span class="k">盈亏比</span>
                                    <span id="opt-est-rr" class="v" title="盈利金额÷本合约权利金">—</span>
                                </div>
                                <span class="muted opt-est-note">目标位仅参考(按到期实值估);盈亏比=盈利÷权利金;到位后按买一限价平;无止损,到期即止损</span>
                            </div>
                            <div class="options-estimate-row opt-profit-exit-row">
                                <div class="opt-est-main">
                                    <label class="btn-secondary opt-order-chip" for="opt-profit-exit-enabled" title="开启后监控买一可回收;达标按买一限价平">
                                        <input type="checkbox" id="opt-profit-exit-enabled">
                                        <span>翻倍出场</span>
                                    </label>
                                    <label class="k" for="opt-profit-exit-mult">倍数</label>
                                    <input type="number" id="opt-profit-exit-mult" class="opt-profit-exit-mult" min="0.1" step="0.1" value="1"
                                           autocomplete="off" inputmode="decimal" data-lpignore="true" data-1p-ignore="true" data-form-type="other">
                                </div>
                                <span class="muted opt-est-note">1倍=盈利等于权利金(可回收≥2×权利金);可开可关,与目标位并行</span>
                            </div>
                            <div class="form-row options-order-mode-row">
                                <div class="opt-size-mode-bar">
                                    <label class="btn-secondary opt-order-chip opt-size-mode-chip">
                                        <input type="radio" name="opt-size-mode" value="sheets"{% if not compound_on %} checked{% endif %}>
                                        <span>指定张数</span>
                                    </label>
                                    <input type="number" id="opt-sheets-amount" min="1" step="1" value="1" placeholder="张数"
                                           autocomplete="off" inputmode="numeric" data-lpignore="true" data-1p-ignore="true" data-form-type="other">
                                    <label class="btn-secondary opt-order-chip opt-size-mode-chip" id="opt-size-mode-budget-wrap"{% if compound_on %} hidden{% endif %}>
                                        <input type="radio" name="opt-size-mode" value="budget_full"{% if compound_on %} disabled{% endif %}>
                                        <span>按可用余额打满</span>
                                    </label>
                                    <label class="btn-secondary opt-order-chip opt-size-mode-chip" id="opt-size-mode-compound-wrap"{% if not compound_on %} hidden{% endif %}>
                                        <input type="radio" name="opt-size-mode" value="compound_full"{% if compound_on %} checked{% endif %}{% if not compound_on %} disabled{% endif %}>
                                        <span>全仓复利</span>
                                    </label>
                                    <label class="btn-secondary opt-order-chip opt-size-mode-chip">
                                        <input type="radio" name="opt-size-mode" value="eth_amount" id="opt-size-mode-eth">
                                        <span>指定币数量</span>
                                    </label>
                                    <input type="number" id="opt-eth-amount" min="0.01" step="0.01" placeholder="如 0.5" style="display:none"
                                           autocomplete="off" inputmode="decimal" data-lpignore="true" data-1p-ignore="true" data-form-type="other">
                                </div>
                                <p class="muted opt-budget-full-hint" id="opt-budget-full-hint" style="display:none;margin:6px 0 0;font-size:.82rem;line-height:1.4">
                                    余额 &gt; 单笔预算(<span id="opt-budget-full-cap">{{ '%.2f'|format(options_trade_budget|default(10)|float) }}</span>U)时按预算;余额不足时按余额;再乘预算缓冲算张数。
                                </p>
                                <p class="muted opt-compound-full-hint" id="opt-compound-full-hint" style="display:none;margin:6px 0 0;font-size:.82rem;line-height:1.4">
                                    用期权交易户全部可用×缓冲开仓;不受单笔预算限制。<span id="opt-compound-cap-line">全仓上限关闭</span>。仅允许同时持有 1 笔仓位。
                                </p>
                                <input type="text" id="opt-signal-note" name="opt_signal_note" class="opt-signal-note" placeholder="备注(关键位说明)"
                                       autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"
                                       data-lpignore="true" data-1p-ignore="true" data-form-type="other" readonly>
                            </div>
                            <div class="opt-order-dialog-actions">
                                <button type="button" class="btn-primary" id="opt-open-btn">限价买入 @ 卖一</button>
                                <button type="button" class="btn-secondary" id="opt-order-cancel-btn">取消</button>
                            </div>
                            <div id="opt-order-msg" class="muted"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="card options-pos-card-wrap">
            <div class="options-pos-head">
                <h2>持仓</h2>
                <button type="button" class="btn-secondary" id="opt-refresh-positions">刷新</button>
            </div>
            <div class="options-pos-tabs" role="tablist" aria-label="持仓面板">
                <button type="button" class="btn-secondary opt-pos-tab active" data-opt-pos-tab="live" role="tab" aria-selected="true" id="opt-pos-tab-live">当前持仓</button>
                <button type="button" class="btn-secondary opt-pos-tab" data-opt-pos-tab="pending" role="tab" aria-selected="false" id="opt-pos-tab-pending">当前委托</button>
                <button type="button" class="btn-secondary opt-pos-tab" data-opt-pos-tab="stats" role="tab" aria-selected="false" id="opt-pos-tab-stats">数据统计</button>
                <button type="button" class="btn-secondary opt-pos-tab" data-opt-pos-tab="history" role="tab" aria-selected="false" id="opt-pos-tab-history">期权历史</button>
            </div>
            <div class="options-pos-tab-body">
                <div class="options-pos-pane is-active" data-opt-pos-pane="live" role="tabpanel" aria-labelledby="opt-pos-tab-live">
                    <div id="opt-target-monitors" class="opt-target-monitors" hidden>
                        <div class="opt-target-monitors-head">目标监控</div>
                        <div id="opt-target-monitors-list"></div>
                    </div>
                    <div id="opt-pos-live" class="panel-scroll pos-list options-pos-live-pane">
                        <div class="pos-empty" id="opt-pos-empty">暂无持仓</div>
                        <div id="opt-pos-cards"></div>
                    </div>
                    <details class="opt-close-rule">
                        <summary>买一平仓规则说明</summary>
                        <div class="opt-close-rule-body">
                            <p>平仓前重新读盘口并校验有效流动性;市价平仓已禁用。</p>
                            <ul>
                                <li>本轮只锁<strong>买一</strong>:张数 = min(持仓, 买一深度),限价 = 当场买一。</li>
                                <li>买一不够时只平能吃掉的部分,剩余等下次再点「买一平仓」。</li>
                                <li>手动平仓只验有效买一(非残档);目标触达后才平,2×权利金只是门控(到 2× 本身不会自动平)。</li>
                                <li><strong>翻倍出场</strong>:开启后可自选倍数(默认1);1倍=盈利等于权利金,买一可回收达标即限价平;可随时关闭。</li>
                                <li>全程 <code>reduceOnly</code> 限价卖,不吃买二及以下、不走市价。</li>
                            </ul>
                            <p><a href="/options/guide" target="_blank" rel="noopener">打开《期权开平仓与监控说明》</a></p>
                        </div>
                    </details>
                </div>
                <div class="options-pos-pane" data-opt-pos-pane="pending" role="tabpanel" aria-labelledby="opt-pos-tab-pending" hidden>
                    <div class="opt-pos-pending-pane">
                        <div class="opt-order-pending-head">
                            <p class="muted opt-pending-ttl-hint" id="opt-pending-ttl-hint" style="margin:0;flex:1">平仓限价超 10 分未成交将自动撤销</p>
                            <button type="button" class="btn-secondary" id="opt-pending-refresh">刷新</button>
                        </div>
                        <div id="opt-pending-list" class="opt-pending-list opt-pending-list--tab">
                            <div class="muted opt-pending-empty">暂无未成交委托</div>
                        </div>
                    </div>
                </div>
                <div class="options-pos-pane" data-opt-pos-pane="stats" role="tabpanel" aria-labelledby="opt-pos-tab-stats" hidden>
                    <div class="options-stats-panel">
                        <div class="options-stats-pnl-summary" id="opt-stats-pnl-summary">
                            <div class="options-stat-item opt-stats-net-item">
                                <span class="k">合计盈亏</span>
                                <span class="v" id="opt-stats-total-pnl">—</span>
                            </div>
                            <div class="options-stat-item">
                                <span class="k">已平净盈亏</span>
                                <span class="v" id="opt-stats-net-realized">—</span>
                            </div>
                            <div class="options-stat-item">
                                <span class="k">持仓浮盈</span>
                                <span class="v" id="opt-stats-open-float">—</span>
                            </div>
                        </div>
                        <div class="options-stats-charts">
                            <div class="opt-stats-chart opt-stats-chart--ring">
                                <div class="opt-stats-ring" id="opt-stats-ring" style="--win-pct: 0">
                                    <span class="opt-stats-ring-label" id="opt-stats-ring-label">—</span>
                                </div>
                                <span class="opt-stats-chart-caption">胜率</span>
                            </div>
                            <div class="opt-stats-chart opt-stats-chart--pnl">
                                <div class="opt-stats-bar-row">
                                    <span class="k">平均盈利</span>
                                    <div class="opt-stats-bar-track">
                                        <div class="opt-stats-bar-fill opt-stats-bar-fill--profit" id="opt-stats-bar-profit"></div>
                                    </div>
                                    <span class="v pos-pnl-profit" id="opt-stats-bar-profit-label">—</span>
                                </div>
                                <div class="opt-stats-bar-row">
                                    <span class="k">平均亏损</span>
                                    <div class="opt-stats-bar-track">
                                        <div class="opt-stats-bar-fill opt-stats-bar-fill--loss" id="opt-stats-bar-loss"></div>
                                    </div>
                                    <span class="v pos-pnl-loss" id="opt-stats-bar-loss-label">—</span>
                                </div>
                            </div>
                        </div>
                        <div class="opt-stats-chart opt-stats-chart--hold">
                            <div class="opt-stats-chart-title">持仓时长对比</div>
                            <div class="opt-stats-bar-row">
                                <span class="k">盈单</span>
                                <div class="opt-stats-bar-track">
                                    <div class="opt-stats-bar-fill opt-stats-bar-fill--profit" id="opt-stats-bar-win-hold"></div>
                                </div>
                                <span class="v" id="opt-stats-win-hold-label">—</span>
                            </div>
                            <div class="opt-stats-bar-row">
                                <span class="k">亏单</span>
                                <div class="opt-stats-bar-track">
                                    <div class="opt-stats-bar-fill opt-stats-bar-fill--loss" id="opt-stats-bar-loss-hold"></div>
                                </div>
                                <span class="v" id="opt-stats-loss-hold-label">—</span>
                            </div>
                        </div>
                        <div class="options-stats-grid">
                            <div class="options-stat-item">
                                <span class="k">胜率</span>
                                <span class="v" id="opt-stats-winrate">—</span>
                            </div>
                            <div class="options-stat-item">
                                <span class="k">盈亏比</span>
                                <span class="v" id="opt-stats-plr">—</span>
                            </div>
                            <div class="options-stat-item">
                                <span class="k">已平笔数</span>
                                <span class="v" id="opt-stats-closed">—</span>
                            </div>
                            <div class="options-stat-item">
                                <span class="k">平均盈利</span>
                                <span class="v pos-pnl-profit" id="opt-stats-profit">—</span>
                            </div>
                            <div class="options-stat-item">
                                <span class="k">平均亏损</span>
                                <span class="v pos-pnl-loss" id="opt-stats-loss">—</span>
                            </div>
                            <div class="options-stat-item">
                                <span class="k">均持仓</span>
                                <span class="v" id="opt-stats-avg-hold">—</span>
                            </div>
                            <div class="options-stat-item">
                                <span class="k">盈单持仓</span>
                                <span class="v" id="opt-stats-win-hold">—</span>
                            </div>
                            <div class="options-stat-item">
                                <span class="k">亏单持仓</span>
                                <span class="v" id="opt-stats-loss-hold">—</span>
                            </div>
                            <div class="options-stat-item">
                                <span class="k">持仓中</span>
                                <span class="v" id="opt-stats-open-hold">—</span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="options-pos-pane" data-opt-pos-pane="history" role="tabpanel" aria-labelledby="opt-pos-tab-history" hidden>
                    <div class="options-history-table-wrap">
                        <table class="options-strike-table opt-history-table" id="opt-history-table">
                            <thead>
                                <tr>
                                    <th>合约</th>
                                    <th>张数</th>
                                    <th>权利金</th>
                                    <th>状态</th>
                                    <th>盈亏</th>
                                    <th>时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody id="opt-history-tbody">
                                <tr><td colspan="7" class="muted">加载中…</td></tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/static/options_expiry_countdown.js?v=1"></script>
<script src="/static/options_panel.js?v=64"></script>
