//@version=6
// strategy.risk.allow_entry_in(long):持多时的反向 entry 变成平仓而非反转;空仓时的做空 entry 被忽略
strategy("S_risk_allow_entry_in", overlay = false,
initial_capital = 100000, currency = currency.NONE,
default_qty_type = strategy.fixed, default_qty_value = 10, pyramiding = 1,
commission_type = strategy.commission.percent, commission_value = 0, slippage = 0,
margin_long = 0, margin_short = 0, close_entries_rule = "FIFO",
process_orders_on_close = false, calc_on_order_fills = false, calc_on_every_tick = false,
backtest_fill_limits_assumption = 0, use_bar_magnifier = false, fill_orders_on_standard_ohlc = false)
strategy.risk.allow_entry_in(strategy.direction.long)
int cycleLen = 16
int phase = bar_index % cycleLen
int cycle = math.floor(bar_index / cycleLen)
if phase == 0
strategy.entry("L", strategy.long)
if phase == 4
strategy.entry("S", strategy.short) // 持多:应只平仓,不开空
if phase == 8
strategy.entry("S2", strategy.short) // 空仓:应被忽略
if phase == 12
strategy.close_all(comment = "c1")
if phase == 15
strategy.cancel_all()
// ---- 对齐列 ----
plot(bar_index, "bar_index")
plot(cycle, "cycle")
plot(phase, "phase")
plot(strategy.position_size, "pos")
plot(strategy.position_avg_price, "avg_price")
plot(strategy.equity, "equity")
plot(strategy.openprofit, "openprofit")
plot(strategy.netprofit, "netprofit")
plot(strategy.closedtrades, "closedtrades")
plot(strategy.opentrades, "opentrades")