fix trend,zt
This commit is contained in:
@@ -8,8 +8,8 @@ from libs.calc import calc_buy_volume, calculate_min_profit_rate
|
||||
from libs.grid_take_profit import GridState
|
||||
from sdk import OP_BUY, OP_SELL, PositionItem, Tick
|
||||
|
||||
from .order import PlaceOrderRequest
|
||||
from .runtime import Runtime
|
||||
from libs.order import PlaceOrderRequest
|
||||
from libs.runtime import Runtime
|
||||
import logging as log
|
||||
|
||||
LOSS_TIERS = [-50.0]
|
||||
@@ -34,11 +34,15 @@ def manage_positions(
|
||||
# 遍历处理每个持仓
|
||||
for position in positions:
|
||||
try:
|
||||
available = max(0,0,available)
|
||||
available = max(0, 0, available)
|
||||
code = position.stock_code
|
||||
tick = ticks.get(code)
|
||||
if code in runtime.account_cfg.excluded_codes:
|
||||
log.info("[Position - ] 代码=%s,名称=%s,止盈=跳过,补仓=跳过,原因=已配置为排除股票", code, position.stock_name)
|
||||
log.info(
|
||||
"[Position - ] 代码=%s,名称=%s,止盈=跳过,补仓=跳过,原因=已配置为排除股票",
|
||||
code,
|
||||
position.stock_name,
|
||||
)
|
||||
continue
|
||||
if (
|
||||
not code
|
||||
@@ -47,7 +51,11 @@ def manage_positions(
|
||||
or tick is None
|
||||
or tick.last_price <= 0
|
||||
):
|
||||
log.warning("[Position - ] 代码=%s,名称=%s,止盈=跳过,补仓=跳过,原因=持仓或行情数据无效", code or "未知", position.stock_name)
|
||||
log.warning(
|
||||
"[Position - ] 代码=%s,名称=%s,止盈=跳过,补仓=跳过,原因=持仓或行情数据无效",
|
||||
code or "未知",
|
||||
position.stock_name,
|
||||
)
|
||||
continue
|
||||
|
||||
pnl_rate = round(
|
||||
@@ -77,18 +85,29 @@ def manage_positions(
|
||||
elif runtime.account_cfg.enable_loss_add_position:
|
||||
loss_add_action = "大盘信号不允许"
|
||||
|
||||
if pnl_rate>=0:
|
||||
if pnl_rate >= 0:
|
||||
log.info(
|
||||
"[Position ↑ ] 代码=%s,名称=%s,盈亏=%.2f%%,止盈=%s,补仓=%s",
|
||||
code, position.stock_name, pnl_rate, profit_action, loss_add_action,
|
||||
code,
|
||||
position.stock_name,
|
||||
pnl_rate,
|
||||
profit_action,
|
||||
loss_add_action,
|
||||
)
|
||||
else:
|
||||
log.info(
|
||||
"[Position ↓ ] 代码=%s,名称=%s,盈亏=%.2f%%,止盈=%s,补仓=%s",
|
||||
code, position.stock_name, pnl_rate, profit_action, loss_add_action,
|
||||
code,
|
||||
position.stock_name,
|
||||
pnl_rate,
|
||||
profit_action,
|
||||
loss_add_action,
|
||||
)
|
||||
except Exception:
|
||||
log.exception("[Position] 持仓处理异常,代码=%s,继续处理后续持仓", position.stock_code)
|
||||
log.exception(
|
||||
"[Position] 持仓处理异常,代码=%s,继续处理后续持仓",
|
||||
position.stock_code,
|
||||
)
|
||||
|
||||
|
||||
def handle_profit(
|
||||
@@ -132,8 +151,7 @@ def handle_profit(
|
||||
)
|
||||
if not runtime.orders.place(runtime.client, request):
|
||||
return TradeDecision(False, "止盈委托失败")
|
||||
|
||||
|
||||
|
||||
return TradeDecision(True, f"卖出 {volume} 股,订单={order_id}")
|
||||
|
||||
|
||||
@@ -142,10 +160,12 @@ def handle_loss(
|
||||
position: PositionItem,
|
||||
tick: Tick,
|
||||
pnl_rate: float,
|
||||
available: float
|
||||
available: float,
|
||||
) -> TradeDecision:
|
||||
"""按亏损档位、反弹确认和本轮剩余预算提交补仓。"""
|
||||
add_num = get_add_num(hands=int(position.volume/100),market_value=position.market_value)
|
||||
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]:
|
||||
@@ -171,10 +191,10 @@ def handle_loss(
|
||||
strategy_name=runtime.account_cfg.strategy,
|
||||
kind="add",
|
||||
)
|
||||
|
||||
|
||||
if not runtime.orders.place(runtime.client, request):
|
||||
return TradeDecision(False, "补仓订单委托失败")
|
||||
|
||||
|
||||
runtime.add_watch.forget(position.stock_code)
|
||||
return TradeDecision(True, f"买入 {volume} 股,订单={order_id}", amount)
|
||||
|
||||
@@ -182,9 +202,10 @@ def handle_loss(
|
||||
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:
|
||||
|
||||
def get_add_num(hands: int, market_value: float) -> int:
|
||||
if market_value > 10000:
|
||||
return -1
|
||||
if hands < 2:
|
||||
return 0
|
||||
return -1
|
||||
return -1
|
||||
|
||||
Reference in New Issue
Block a user