本策略是一个基于资金流量指标(MFI)的自动化交易系统,主要通过识别资产在超卖区域的表现来捕捉潜在的反转机会。策略的核心是当MFI指标从超卖区域(默认值20以下)回升时产生买入信号,并通过限价单、止损和获利了结等多重机制来管理交易风险和收益。该策略特别适合在市场出现超跌反弹时进行布局。
策略运作基于以下几个关键步骤: 1. 持续监测资金流量指标(MFI)的数值变化,当MFI降至预设的超卖阈值(默认20)以下时,系统标记进入超卖区域。 2. 当MFI从超卖区域回升并突破阈值时,系统会在当前价格下方设置一个买入限价单,具体价格由用户定义的百分比决定。 3. 系统会对限价单的有效期进行监控,如果在预设的观察周期(默认5个K线)内未能成交,该订单将被自动取消。 4. 一旦买入订单成交,系统会立即设置止损和获利目标价位,分别基于入场价格的百分比计算。 5. 交易将在触及止损或者获利目标时自动平仓结束。
这是一个设计合理、逻辑清晰的自动化交易策略。通过对MFI指标的灵活运用,结合完善的订单管理机制,能够有效捕捉市场超卖后的反弹机会。策略的可配置性强,便于根据不同市场环境进行优化调整。虽然存在一定的风险,但通过建议的优化方向,可以进一步提升策略的稳定性和盈利能力。适合用于中长期投资,特别是在震荡市场中寻找超跌反弹机会的投资者。
/*backtest
start: 2024-11-04 00:00:00
end: 2024-12-04 00:00:00
period: 3h
basePeriod: 3h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © traderhub
//@version=5
strategy("MFI Strategy with Oversold Zone Exit and Averaging", overlay=true)
// Strategy parameters
mfiPeriod = input.int(title="MFI Period", defval=14) // Period for calculating MFI
mfiOS = input.float(title="MFI Oversold Level", defval=20.0) // Oversold level for MFI
longEntryPercentage = input.float(title="Long Entry Percentage (%)", minval=0.0, step=0.1, defval=0.1) // Percentage for the buy limit order
stopLossPercentage = input.float(title="Stop Loss Percentage (%)", minval=0.0, step=0.1, defval=1.0) // Percentage for the stop-loss
exitGainPercentage = input.float(title="Exit Gain Percentage (%)", minval=0.0, step=0.1, defval=1.0) // Percentage gain for the take-profit
cancelAfterBars = input.int(title="Cancel Order After # Bars", minval=1, defval=5) // Cancel order after a certain number of bars
// Calculate MFI
mfi = ta.mfi(close, mfiPeriod) // MFI with specified period
// Variables for tracking state
var bool inOversoldZone = false // Flag for being in the oversold zone
var float longEntryPrice = na // Price for long entry
var int barsSinceEntryOrder = na // Counter for bars after placing an order
// Define being in the oversold zone
if (mfi < mfiOS)
inOversoldZone := true // Entered oversold zone
// Condition for exiting the oversold zone and placing a limit order
if (inOversoldZone and mfi > mfiOS)
inOversoldZone := false // Leaving the oversold zone
longEntryPrice := close * (1 - longEntryPercentage / 100) // Calculate limit price for entry
strategy.entry("Long Entry", strategy.long, limit=longEntryPrice) // Place a limit order
barsSinceEntryOrder := 0 // Reset counter for bars after placing the order
// Increase the bar counter if the order has not yet been filled
if (not na(barsSinceEntryOrder))
barsSinceEntryOrder += 1
// Cancel order if it hasn’t been filled within the specified number of bars
if (not na(barsSinceEntryOrder) and barsSinceEntryOrder >= cancelAfterBars and strategy.position_size == 0)
strategy.cancel("Long Entry")
barsSinceEntryOrder := na // Reset bar counter
// Set stop-loss and take-profit for filled positions
if (strategy.position_size > 0)
stopLossPrice = longEntryPrice * (1 - stopLossPercentage / 100) // Calculate stop-loss level
takeProfitPrice = longEntryPrice * (1 + exitGainPercentage / 100) // Calculate take-profit level
strategy.exit("Exit Long", from_entry="Long Entry", limit=takeProfitPrice, stop=stopLossPrice)
// Visualize oversold and overbought zones
bgcolor(mfi < mfiOS ? color.new(color.green, 90) : na) // Background in oversold zone
plot(mfi, title="MFI", color=color.blue) // MFI plot
hline(mfiOS, "Oversold Level", color=color.red) // Oversold level line