
Đây là một chiến lược xác nhận xu hướng Heikin-Ashi không được vẽ lại sáng tạo, nhằm giải quyết vấn đề vẽ lại chiến lược Heikin-Ashi trong TradingView truyền thống. Bằng cách tính toán Heikin-Ashi bằng tay và nhiều cơ chế xác nhận xu hướng, chiến lược này cung cấp một phương pháp giao dịch đáng tin cậy và minh bạch hơn.
Các nguyên tắc cốt lõi của chiến lược bao gồm ba bước quan trọng:
Tính toán không vẽ lại bằng tay:
Xu hướng đa dạng đã được xác nhận:
Mô hình giao dịch linh hoạt:
Hạn chế tính năng:
Kiểm soát rủi ro tiềm ẩn:
Phương thức điều chỉnh động:
Tăng cường quản lý rủi ro:
Gói chỉ số:
Chiến lược xác nhận xu hướng vẽ lại của Haikan A.S.A. cung cấp cho các nhà giao dịch một công cụ giao dịch đáng tin cậy và minh bạch hơn bằng cách sử dụng tính toán đệm sáng tạo và phương pháp xác nhận xu hướng đa dạng. Bằng cách loại bỏ các vấn đề vẽ lại, lọc các tín hiệu giả mạo và cung cấp mô hình giao dịch linh hoạt, chiến lược này thể hiện tiềm năng đổi mới kỹ thuật trong giao dịch định lượng.
/*backtest
start: 2025-03-15 00:00:00
end: 2025-03-27 00:00:00
period: 3h
basePeriod: 3h
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/
//@version=5
//© PineIndicators
strategy("Heikin-Ashi Non-Repainting Strategy [PineIndicators]", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, max_boxes_count=500, max_labels_count=500, max_lines_count=500, commission_value=0.01, process_orders_on_close=true, slippage= 2, behind_chart=false)
//====================================
// INPUTS
//====================================
// Number of consecutive candles required for entry and exit
openThreshold = input.int(title="Number of Candles for Entry", defval=2, minval=1)
exitThreshold = input.int(title="Number of Candles for Exit", defval=2, minval=1)
// Trade mode selection: "Long & Short", "Only Long", or "Only Short"
tradeMode = input.string(title="Trade Mode", defval="Only Long", options=["Long & Short", "Only Long", "Only Short"])
// Option to invert the trading logic (bullish signals become short signals, and vice versa)
invertTrades = input.bool(title="Invert Trading Logic (Long ↔ Short)", defval=false)
// Option to hide the standard candles (bodies only)
hideStandard = input.bool(title="Hide Standard Candles", defval=true)
// Heikin-Ashi transparency is fixed (0 = fully opaque)
heikinTransparency = 0
//====================================
// HIDE STANDARD CANDLES
//====================================
// Hide the body of the standard candles by setting them to 100% transparent.
// Note: The wicks of the standard candles cannot be hidden via code.
barcolor(hideStandard ? color.new(color.black, 100) : na)
//====================================
// HEIKIN-ASHI CALCULATION
//====================================
// Calculate Heikin-Ashi values manually
haClose = (open + high + low + close) / 4
var float haOpen = na
haOpen := na(haOpen[1]) ? (open + close) / 2 : (haOpen[1] + haClose[1]) / 2
haHigh = math.max(high, math.max(haOpen, haClose))
haLow = math.min(low, math.min(haOpen, haClose))
// Define colors for Heikin-Ashi candles (using fixed transparency)
bullColor = color.new(#0097a7, heikinTransparency)
bearColor = color.new(#ff195f, heikinTransparency)
//====================================
// PLOT HEIKIN-ASHI CANDLES
//====================================
// Plot the manually calculated Heikin-Ashi candles over the chart.
// The candle body, wicks, and borders will be colored based on whether the candle is bullish or bearish.
plotcandle(haOpen, haHigh, haLow, haClose, title="Heikin-Ashi",
color = haClose >= haOpen ? bullColor : bearColor,
wickcolor = haClose >= haOpen ? bullColor : bearColor,
bordercolor = haClose >= haOpen ? bullColor : bearColor,
force_overlay = true)
//====================================
// COUNT CONSECUTIVE TREND CANDLES
//====================================
// Count the number of consecutive bullish or bearish Heikin-Ashi candles.
var int bullishCount = 0
var int bearishCount = 0
if haClose > haOpen
bullishCount := bullishCount + 1
bearishCount := 0
else if haClose < haOpen
bearishCount := bearishCount + 1
bullishCount := 0
else
bullishCount := 0
bearishCount := 0
//====================================
// DEFINE ENTRY & EXIT SIGNALS
//====================================
// The signals are based on the number of consecutive trend candles.
// In normal logic: bullish candles trigger a long entry and bearish candles trigger a short entry.
// If invertTrades is enabled, the signals are swapped.
var bool longEntrySignal = false
var bool shortEntrySignal = false
var bool exitLongSignal = false
var bool exitShortSignal = false
if not invertTrades
longEntrySignal := bullishCount >= openThreshold
shortEntrySignal := bearishCount >= openThreshold
exitLongSignal := bearishCount >= exitThreshold
exitShortSignal := bullishCount >= exitThreshold
else
// Inverted logic: bullish candles trigger short entries and bearish candles trigger long entries.
longEntrySignal := bearishCount >= openThreshold
shortEntrySignal := bullishCount >= openThreshold
exitLongSignal := bullishCount >= exitThreshold
exitShortSignal := bearishCount >= exitThreshold
//====================================
// APPLY TRADE MODE RESTRICTIONS
//====================================
// If the user selects "Only Long", disable short signals (and vice versa).
if tradeMode == "Only Long"
shortEntrySignal := false
exitShortSignal := false
else if tradeMode == "Only Short"
longEntrySignal := false
exitLongSignal := false
//====================================
// TRADING STRATEGY LOGIC
//====================================
// Execute trades based on the calculated signals.
// If a long position is open:
if strategy.position_size > 0
if shortEntrySignal
strategy.close("Long", comment="Reverse Long")
strategy.entry("Short", strategy.short, comment="Enter Short")
else if exitLongSignal
strategy.close("Long", comment="Exit Long")
// If a short position is open:
if strategy.position_size < 0
if longEntrySignal
strategy.close("Short", comment="Reverse Short")
strategy.entry("Long", strategy.long, comment="Enter Long")
else if exitShortSignal
strategy.close("Short", comment="Exit Short")
// If no position is open:
if strategy.position_size == 0
if longEntrySignal
strategy.entry("Long", strategy.long, comment="Enter Long")
else if shortEntrySignal
strategy.entry("Short", strategy.short, comment="Enter Short")