请求将改进版Heikin Ashi指标转换为交易策略(含明确多空触发条件)
Converted Improved Heikin Ashi Trading Strategy
Hey there, I’ve turned your improved Heikin Ashi indicator into a functional trading strategy that follows your specified entry rules to a T. Here’s the full Pine Script code, plus breakdowns of key changes:
Complete Strategy Code
//@version=5 strategy("Improved Heikin Ashi Trend Strategy", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, commission_type=strategy.commission.percent, commission_value=0.1) ///////////////////////////////////////////////////////////////////////////////// // Inputs (retained from original indicator) ///////////////////////////////////////////////////////////////////////////////// REAL = input(false, title="Use Real Close?", group="Heikin Ashi [Improved]", tooltip="Get the real closing price per bar close") HA = input(true, title="Add Original Heikin Ashi Smoothness?", group="Heikin Ashi [Improved]", tooltip="Add an extra layer of smoothness.") s = input.int(1, minval=1, maxval=100, title="Candle Smoothness", group="Heikin Ashi [Improved]") ShowAvg = input(true, title="Show Heikin Ashi Moving Average [improved]", group="Heikin Ashi Moving Average [improved]", tooltip="Display a moving average based on the improved Heikin Ashi bars.") len = input.int(25, minval=1, maxval=500, title="Length", group="Heikin Ashi Moving Average [improved]", tooltip="Set moving average length.") ///////////////////////////////////////////////////////////////////////////////// // Improved Heikin Ashi Calculation (unchanged from original indicator) ///////////////////////////////////////////////////////////////////////////////// close0 = (open + high + low + close) / 4 close0 := na(close0) ? nz(na) : (nz(close0) + nz(close)) / 2 open0 = float(na) open0 := na(open0[1]) ? nz(na) : (nz(open0[s]) + nz(close0[1])) / 2 high0 = math.max(high, math.max(open0, close0)) low0 = math.min(low, math.min(open0, close0)) h = math.max(high0, low0) l = math.min(high0, low0) ///////////////////////////////////////////////////////////////////////////////// // Original Heikin Ashi Smoothness (unchanged from original indicator) ///////////////////////////////////////////////////////////////////////////////// haclose = (open0 + h + l + close0) / 4 haOpen = float(na) haOpen := na(haOpen[1]) ? (open0 + close0) / 2 : (nz(haOpen[1]) + nz(haclose[1])) / 2 hahigh = math.max(h, math.max(haOpen, haclose)) halow = math.min(l, math.min(haOpen, haclose)) ///////////////////////////////////////////////////////////////////////////////// // Candle Mode (fixed typo from original code) ///////////////////////////////////////////////////////////////////////////////// o_ = HA ? haOpen : open0 h_ = HA ? hahigh : h // Corrected "hahich" to valid variable "hahigh" l_ = HA ? halow : l c_ = HA ? (REAL ? close : haclose) : (REAL ? close : close0) ///////////////////////////////////////////////////////////////////////////////// // Coloring (unchanged from original indicator) ///////////////////////////////////////////////////////////////////////////////// BODY = close0 > open0 ? color.lime : color.red BORDER = close0 > open0 ? color.lime : color.red if HA BODY := haclose > haOpen ? color.lime : color.red BORDER := haclose > haOpen ? color.lime : color.red ///////////////////////////////////////////////////////////////////////////////// // Plot Candles (unchanged from original indicator) ///////////////////////////////////////////////////////////////////////////////// plotcandle(o_, h_, l_, c_, color=BODY, bordercolor=BORDER, title="Heikin Ashi [improved]") ///////////////////////////////////////////////////////////////////////////////// // Improved Heikin Ashi Moving Average (unchanged from original indicator) ///////////////////////////////////////////////////////////////////////////////// avg = math.avg(high0, low0, hahigh, halow, c_) HA_movingAverage = ta.wma(avg, len) col = HA_movingAverage > HA_movingAverage[1] ? color.lime : color.red plot(ShowAvg ? HA_movingAverage : na, color=col, title="Heikin Ashi Moving Average [improved]") ///////////////////////////////////////////////////////////////////////////////// // Trading Strategy Rules (added per your requirements) ///////////////////////////////////////////////////////////////////////////////// // Long Entry: Real price close crosses above HA_movingAverage longCondition = ta.crossover(close, HA_movingAverage) // Short Entry: Real price close crosses below HA_movingAverage shortCondition = ta.crossunder(close, HA_movingAverage) // Execute trades: Close opposite position before entering new one if (longCondition) strategy.close("Short", comment="Close Short on Long Signal") strategy.entry("Long", strategy.long, comment="Enter Long") if (shortCondition) strategy.close("Long", comment="Close Long on Short Signal") strategy.entry("Short", strategy.short, comment="Enter Short")
Key Details & Fixes
- Strategy Setup: Swapped
indicator()withstrategy()to enable trade execution, including default capital allocation and commission settings that mirror real-world trading. - Typo Correction: Fixed a small error in the original code (
hahich→hahigh) to ensure all calculations run without errors. - Entry Logic: Implemented your exact rules using
ta.crossover()andta.crossunder()to avoid repeated entries on consecutive bars—trades only trigger when the close price first breaks through the trend line. - Trade Management: Added automatic opposite-position closing (e.g., close a short position when a long signal fires) to prevent holding conflicting positions, which is standard for trend-following strategies.
- Preserved Features: All original indicator inputs, Heikin Ashi calculations, and visual elements are kept intact so you can tweak settings like smoothness or MA length as needed.
内容的提问来源于stack exchange,提问作者Mahmud Ayaz
相关产品推荐
相关产品推荐

