本策略是一个基于市场连续运动特征的量化交易系统,通过分析价格连续上涨或下跌的频次来捕捉市场反转机会。策略核心是通过设定连续涨跌的阈值,在达到阈值时采取反向操作,同时结合持仓时间和K线形态等多维度指标进行交易决策。该策略充分利用了市场的反转特性,在价格出现超买或超卖特征时进行反向操作。
策略的核心逻辑包括以下几个关键要素: 1. 连续次数统计:系统会实时统计价格的连续上涨和下跌次数,并与预设的阈值进行比较。 2. 交易方向选择:可以选择做多或做空两个方向,做多时关注连续下跌,做空时关注连续上涨。 3. 持仓周期管理:设定固定的持仓周期,到期自动平仓,避免过度持仓。 4. 十字星过滤:引入十字星判断来过滤市场震荡期间的假信号。 5. 仓位控制:采用单一仓位进行交易,不进行加仓或分批建仓操作。
该策略是一个基于市场反转特征的量化交易系统,通过分析价格连续运动来捕捉市场反转机会。策略设计合理,风险可控,但需要根据市场环境调整参数。通过持续优化和完善,该策略有望在实际交易中取得稳定收益。建议在实盘之前进行充分的历史数据回测,并在模拟盘中验证策略的有效性。
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-27 08:00:00
period: 2d
basePeriod: 2d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Streak-Based Trading Strategy", overlay=true)
// User Inputs
trade_direction = input.string(title="Trade Direction", defval="Long", options=["Long", "Short"]) // Option to choose Long or Short
streak_threshold = input.int(title="Streak Threshold", defval=8, minval=1) // Input for number of streaks before trade
hold_duration = input.int(title="Hold Duration (in periods)", defval=7, minval=1) // Input for holding the position
doji_threshold = input.float(0.01, title="Doji Threshold (%)", minval=0.001) / 100 // Doji sensitivity
// Calculate win or loss streak
is_doji = math.abs(close - open) / (high - low) < doji_threshold
win = close > close[1] and not is_doji
loss = close < close[1] and not is_doji
// Initialize variables for streak counting
var int win_streak = 0
var int loss_streak = 0
var bool in_position = false
var int hold_counter = 0
// Track streaks (only when not in a position)
if not in_position
if win
win_streak += 1
loss_streak := 0
else if loss
loss_streak += 1
win_streak := 0
else
win_streak := 0
loss_streak := 0
// Logic for closing the position after the holding duration
if in_position
hold_counter -= 1
if hold_counter <= 0
strategy.close_all() // Close all positions
in_position := false // Reset position flag
win_streak := 0 // Reset streaks after position is closed
loss_streak := 0
// Trade condition (only when no position is open and streak is reached)
if not in_position
if trade_direction == "Long" and loss_streak >= streak_threshold
strategy.entry("Long", strategy.long) // Open a long position
in_position := true
hold_counter := hold_duration // Set holding period
if trade_direction == "Short" and win_streak >= streak_threshold
strategy.entry("Short", strategy.short) // Open a short position
in_position := true
hold_counter := hold_duration // Set holding period
// Plotting streaks for visualization
plot(win_streak, color=color.green, title="Winning Streak", style=plot.style_histogram, linewidth=2)
plot(loss_streak, color=color.red, title="Losing Streak", style=plot.style_histogram, linewidth=2)