fix bug
This commit is contained in:
@@ -10,10 +10,9 @@ from sdk import OP_BUY, OP_SELL, PositionItem, Tick
|
||||
|
||||
from .order import PlaceOrderRequest
|
||||
from .runtime import Runtime
|
||||
from .state import PendingOrder
|
||||
import logging as log
|
||||
|
||||
LOSS_TIERS = (-30.0, -50.0)
|
||||
LOSS_TIERS = [-50.0]
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
@@ -133,6 +132,8 @@ def handle_profit(
|
||||
)
|
||||
if not runtime.orders.place(runtime.client, request):
|
||||
return TradeDecision(False, "止盈委托失败")
|
||||
|
||||
|
||||
return TradeDecision(True, f"卖出 {volume} 股,订单={order_id}")
|
||||
|
||||
|
||||
@@ -141,19 +142,15 @@ def handle_loss(
|
||||
position: PositionItem,
|
||||
tick: Tick,
|
||||
pnl_rate: float,
|
||||
available: float,
|
||||
available: float
|
||||
) -> TradeDecision:
|
||||
"""按亏损档位、反弹确认和本轮剩余预算提交补仓。"""
|
||||
try:
|
||||
state = runtime.state.get(position.stock_code)
|
||||
except KeyError:
|
||||
return TradeDecision(False, "缺少持仓状态,跳过补仓")
|
||||
|
||||
if state.added_num >= len(LOSS_TIERS):
|
||||
return TradeDecision(False, "已达到最大补仓次数")
|
||||
if pnl_rate > LOSS_TIERS[state.added_num]:
|
||||
add_num = get_add_num(hands=int(position.volume/100),market_value=position.market_value)
|
||||
if add_num >= len(LOSS_TIERS) or add_num < 0:
|
||||
return TradeDecision(False, f"补仓次数无效:{add_num}")
|
||||
if pnl_rate > LOSS_TIERS[add_num]:
|
||||
return TradeDecision(False)
|
||||
if tick.last_price > 200 or position.market_value >= 60_000:
|
||||
if tick.last_price > 200 or position.market_value >= 20_000:
|
||||
return TradeDecision(False, "价格或仓位市值超过补仓限制")
|
||||
if not runtime.add_watch.triggered("补仓", position.stock_code, tick.last_price):
|
||||
return TradeDecision(False, "等待价格反弹确认")
|
||||
@@ -174,15 +171,20 @@ def handle_loss(
|
||||
strategy_name=runtime.account_cfg.strategy,
|
||||
kind="add",
|
||||
)
|
||||
|
||||
#runtime.state.new_order(PendingOrder(order_id, position.stock_code, "added", volume))
|
||||
|
||||
if not runtime.orders.place(runtime.client, request):
|
||||
reserved = amount if runtime.state.busy(position.stock_code) else 0.0
|
||||
return TradeDecision(False, "补仓委托失败或待确认", reserved)
|
||||
return TradeDecision(False, "补仓订单委托失败")
|
||||
|
||||
runtime.add_watch.forget(position.stock_code)
|
||||
return TradeDecision(True, f"买入 {volume} 股,订单={order_id}", amount)
|
||||
|
||||
|
||||
def _position_key(runtime: Runtime, code: str) -> str:
|
||||
return f"{runtime.account_cfg.account_id}:{code}"
|
||||
|
||||
def get_add_num(hands:int,market_value:float) -> int:
|
||||
if market_value>10000:
|
||||
return -1
|
||||
if hands < 2:
|
||||
return 0
|
||||
return -1
|
||||
Reference in New Issue
Block a user