这是一个基于时间周期的智能轮动策略,通过在指定的时间周期内进行多空轮动交易来获取收益。策略采用灵活的持仓管理机制,可以根据市场环境自动调整交易方向,同时具备风险控制功能。该策略支持多空双向交易,并可选择性开启摆动交易模式,具有较强的适应性。
策略主要通过时间周期和持仓状态来控制交易。首先通过inActivePeriod()函数确定是否在最近500根K线的有效交易区间内。在有效区间内,策略根据持仓状态(positionHeld)、已持仓时间(barsHeld)和暂停时间(barsPaused)等变量来决定交易行为。当启用摆动交易模式时,策略会在多空方向间快速轮动;当禁用摆动交易模式时,策略会在持仓3个周期后平仓并等待新的交易机会。
该策略通过时间周期控制和多空轮动的方式来获取市场收益,具有较强的灵活性和适应性。虽然存在一些风险,但通过合理的优化和风险控制措施,可以显著提高策略的稳定性和盈利能力。策略的核心优势在于其简单而有效的交易逻辑,适合作为基础策略进行进一步优化和扩展。
/*backtest
start: 2024-10-12 00:00:00
end: 2024-11-11 00:00:00
period: 4h
basePeriod: 4h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Tickerly Test Strategy", overlay=true)
// Inputs
longEnabled = input.bool(true, "Enable Long Trades")
shortEnabled = input.bool(true, "Enable Short Trades")
swingEnabled = input.bool(false, "Enable Swing Trading")
// Variables
var positionHeld = 0
var barsHeld = 0
var barsPaused = 0
var lastAction = "none"
// Function to determine if we're in the last 500 bars
inActivePeriod() =>
barIndex = bar_index
lastBarIndex = last_bar_index
barIndex >= (lastBarIndex - 499)
// Main strategy logic
if inActivePeriod()
if swingEnabled
if positionHeld == 0 and barstate.isconfirmed
if lastAction != "long"
strategy.entry("Long", strategy.long)
positionHeld := 1
barsHeld := 0
lastAction := "long"
else
strategy.entry("Short", strategy.short)
positionHeld := -1
barsHeld := 0
lastAction := "short"
if positionHeld != 0
barsHeld += 1
if barsHeld >= 2
if positionHeld == 1
strategy.entry("Short", strategy.short)
positionHeld := -1
barsHeld := 0
lastAction := "short"
else
strategy.entry("Long", strategy.long)
positionHeld := 1
barsHeld := 0
lastAction := "long"
else
if positionHeld == 0 and barsPaused >= 1 and barstate.isconfirmed
if longEnabled and shortEnabled
if lastAction != "long"
strategy.entry("Long", strategy.long)
positionHeld := 1
barsHeld := 0
barsPaused := 0
lastAction := "long"
else
strategy.entry("Short", strategy.short)
positionHeld := -1
barsHeld := 0
barsPaused := 0
lastAction := "short"
else if longEnabled
strategy.entry("Long", strategy.long)
positionHeld := 1
barsHeld := 0
barsPaused := 0
lastAction := "long"
else if shortEnabled
strategy.entry("Short", strategy.short)
positionHeld := -1
barsHeld := 0
barsPaused := 0
lastAction := "short"
if positionHeld != 0
barsHeld += 1
if barsHeld >= 3
strategy.close_all()
positionHeld := 0
barsHeld := 0
barsPaused := 0 // Reset pause counter when exiting a position
else
barsPaused += 1
// Plotting active period for visual confirmation
plot(inActivePeriod() ? 1 : 0, "Active Period", color=color.new(color.blue, 80), style=plot.style_areabr)