该策略是一个基于技术分析中下降楔形形态的趋势突破交易系统。它通过动态识别价格中的高点和低点,构建上下趋势线,在价格突破上趋势线时进入多头仓位。策略采用动态止盈止损机制来控制风险和锁定利润。这是一个经典的技术分析交易方法的程序化实现,特别适合在下跌趋势即将结束时捕捉反转机会。
策略的核心逻辑包括以下几个关键步骤: 1. 使用枢轴点(Pivot)方法动态识别价格走势中的高点和低点 2. 记录并保存最近的两个高点和低点及其对应的时间索引 3. 基于这些点计算上下趋势线的斜率 4. 判断是否形成下降楔形:要求两个高点递减、两个低点递减,且上趋势线斜率小于下趋势线斜率 5. 当价格突破上趋势线时,触发买入信号 6. 设置基于入场价格的百分比止盈止损条件
这是一个设计合理的趋势交易策略,通过程序化方式实现了传统技术分析方法。策略的优势在于能够自动化识别市场结构并捕捉潜在的趋势反转机会。但同时也需要注意假突破和参数优化等问题。通过进一步优化和完善,该策略有望在实际交易中取得更好的效果。
/*backtest
start: 2024-02-22 00:00:00
end: 2024-05-08 00:00:00
period: 10m
basePeriod: 1m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
strategy("Falling Wedge Strategy by Nitin", overlay=true, margin_long=100, margin_short=100)
// Input parameters
leftBars = input.int(5, "Left Bars for Pivot", minval=1, maxval=20)
rightBars = input.int(5, "Right Bars for Pivot", minval=1, maxval=20)
takeProfitPercent = input.float(2, "Take Profit %", minval=0.1, maxval=100)/100
stopLossPercent = input.float(1, "Stop Loss %", minval=0.1, maxval=100)/100
// Global variables
var float buyPrice = na
var line upperLine = na
var line lowerLine = na
// Detect pivot highs and lows
ph = ta.pivothigh(leftBars, rightBars)
pl = ta.pivotlow(leftBars, rightBars)
// Track last two pivot highs
var float[] highs = array.new_float()
var int[] highIndices = array.new_int()
if not na(ph)
array.unshift(highs, ph)
array.unshift(highIndices, bar_index[rightBars])
if array.size(highs) > 2
array.pop(highs)
array.pop(highIndices)
// Track last two pivot lows
var float[] lows = array.new_float()
var int[] lowIndices = array.new_int()
if not na(pl)
array.unshift(lows, pl)
array.unshift(lowIndices, bar_index[rightBars])
if array.size(lows) > 2
array.pop(lows)
array.pop(lowIndices)
// Calculate trendlines and signals
if array.size(highs) >= 2 and array.size(lows) >= 2
h1 = array.get(highs, 0)
h2 = array.get(highs, 1)
i1 = array.get(highIndices, 0)
i2 = array.get(highIndices, 1)
l1 = array.get(lows, 0)
l2 = array.get(lows, 1)
j1 = array.get(lowIndices, 0)
j2 = array.get(lowIndices, 1)
m_upper = (h1 - h2) / (i1 - i2)
m_lower = (l1 - l2) / (j1 - j2)
currentUpper = h2 + m_upper * (bar_index - i2)
currentLower = l2 + m_lower * (bar_index - j2)
if h1 < h2 and l1 < l2 and m_upper < m_lower and m_upper < 0 and m_lower < 0
// Buy signal on breakout
if ta.crossover(close, currentUpper)
strategy.entry("Buy", strategy.long)
buyPrice := close
strategy.exit("Take Profit/Stop Loss", "Buy", stop=buyPrice * (1 - stopLossPercent), limit=buyPrice * (1 + takeProfitPercent))
// Plotting
plotshape(strategy.position_size > 0 ? buyPrice : na, "Buy Price", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, text="BUY")
plot(strategy.position_size > 0 ? buyPrice * (1 - stopLossPercent) : na, "Stop Loss", color=color.red, linewidth=2)
plot(strategy.position_size > 0 ? buyPrice * (1 + takeProfitPercent) : na, "Take Profit", color=color.green, linewidth=2)