OPEN-SOURCE SCRIPT

NSDT HAMA Candles Strategy

//version=5
strategy("NSDT HAMA Candles Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, initial_capital=10000, currency=currency.USD)

// -----------------------------
// Gradient Function
// -----------------------------
f_c_gradientAdvDecPro(_source, _center, _steps, _c_bearWeak, _c_bearStrong, _c_bullWeak, _c_bullStrong) =>
var float _qtyAdvDec = 0.0
var float _maxSteps = math.max(1, _steps)
bool _xUp = ta.crossover(_source, _center)
bool _xDn = ta.crossunder(_source, _center)
float _chg = ta.change(_source)
bool _up = _chg > 0
bool _dn = _chg < 0
bool _srcBull = _source > _center
bool _srcBear = _source < _center
_qtyAdvDec :=
_srcBull ? (_xUp ? 1 : _up ? math.min(_maxSteps, _qtyAdvDec + 1) : _dn ? math.max(1, _qtyAdvDec - 1) : _qtyAdvDec) :
_srcBear ? (_xDn ? 1 : _dn ? math.min(_maxSteps, _qtyAdvDec + 1) : _up ? math.max(1, _qtyAdvDec - 1) : _qtyAdvDec) :
_qtyAdvDec
var color _return = na
_return :=
_srcBull ? color.new(color.lime, 0) :
_srcBear ? color.new(color.red, 0) :
color.new(color.yellow, 0) // Neutral color as yellow
_return

// -----------------------------
// MA Calculation Function
// -----------------------------
mat(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"RMA" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
"HMA" => ta.hma(source, length)
"TMA" => ta.sma(ta.sma(source, length), length)
=> na

// -----------------------------
// Inputs
// -----------------------------
bull = input.color(color.new(color.green, 0), title = "Bull Color")
bear = input.color(color.new(color.red, 0), title = "Bear Color")
neutral = input.color(color.new(color.yellow, 0), title = "Neutral Color")
show_ma = input.bool(true, title = "Show MA")
ma_type = input.string("WMA", title = "MA Type", options=["SMA", "EMA", "RMA", "WMA", "VWMA", "HMA", "TMA"])
ma_source = input.source(close, title = "MA Source")
ma_length = input.int(55, title = "MA Length", minval=1)
UseGradient = input.bool(true, title = "Use Gradient Colors")
stepn = input.int(5, title = "Max Gradient Steps", minval=1)

// Calculate MA and Gradient Color
ma = mat(ma_source, ma_length, ma_type)
col = f_c_gradientAdvDecPro(ma, ta.ema(ma, 3), stepn, neutral, bear, neutral, bull)

// -----------------------------
// MA Info Inputs
// -----------------------------
WickColor = input.color(color.new(color.gray, 80), title = "Wick Color", tooltip="Suggest Full Transparency.")
OpenLength = input.int(25, minval=1, title="Length Open", inline="Open")
OpenType = input.string('EMA', title='Type', options=['EMA', 'SMA', 'WMA'], inline="Open")
HighLength = input.int(20, minval=1, title="Length High", inline="High")
HighType = input.string('EMA', title='Type', options=['EMA', 'SMA', 'WMA'], inline="High")
LowLength = input.int(20, minval=1, title="Length Low", inline="Low")
LowType = input.string('EMA', title='Type', options=['EMA', 'SMA', 'WMA'], inline="Low")
CloseLength = input.int(20, minval=1, title="Length Close", inline="Close")
CloseType = input.string('EMA', title='Type', options=['EMA', 'SMA', 'WMA'], inline="Close")
LengthMA = input.int(55, minval=1, title="MA Line Length", inline="MA Info")
MAType = input.string('EMA', title='MA Line Type', options=['EMA', 'SMA', 'WMA'], inline="MA Info")
MASource = input.source(hl2, title="MA Source")

// Function to Calculate Moving Averages
funcCalcMA1(type1, src1, len1) =>
switch type1
"EMA" => ta.ema(src1, len1)
"SMA" => ta.sma(src1, len1)
"WMA" => ta.wma(src1, len1)
=> na

funcCalcOpen(type_open, src_open, len_open) =>
switch type_open
"EMA" => ta.ema(src_open, len_open)
"SMA" => ta.sma(src_open, len_open)
"WMA" => ta.wma(src_open, len_open)
=> na

funcCalcHigh(type_high, src_high, len_high) =>
switch type_high
"EMA" => ta.ema(src_high, len_high)
"SMA" => ta.sma(src_high, len_high)
"WMA" => ta.wma(src_high, len_high)
=> na

funcCalcLow(type_low, src_low, len_low) =>
switch type_low
"EMA" => ta.ema(src_low, len_low)
"SMA" => ta.sma(src_low, len_low)
"WMA" => ta.wma(src_low, len_low)
=> na

funcCalcClose(type_close, src_close, len_close) =>
switch type_close
"EMA" => ta.ema(src_close, len_close)
"SMA" => ta.sma(src_close, len_close)
"WMA" => ta.wma(src_close, len_close)
=> na

// Calculate MA and Candle Components
MA1 = funcCalcMA1(MAType, MASource, LengthMA)
CandleOpen = funcCalcOpen(OpenType, (open[1] + close[1])/2, OpenLength)
CandleHigh = funcCalcHigh(HighType, math.max(high, close), HighLength)
CandleLow = funcCalcLow(LowType, math.min(low, close), LowLength)
CandleClose = funcCalcClose(CloseType, (open + high + low + close)/4, CloseLength)

// Determine Candle Body Color
BodyColor = CandleOpen > CandleOpen[1] ? color.green : color.red

// Apply Colors to Candles and MA Line
barcolor(UseGradient ? col : BodyColor)
plotcandle(CandleOpen, CandleHigh, CandleLow, CandleClose, color=UseGradient ? col : BodyColor, title="HAMA Candles", wickcolor=WickColor, bordercolor=na)
plot(show_ma ? MA1 : na, title="MA Line", color=UseGradient ? col : BodyColor, style=plot.style_line, linewidth=2)

// -----------------------------
// Alerts
// -----------------------------

// Existing Alerts
alertcondition(ta.rising(MA1, 2), title="MA Rising", message="MA Rising")
alertcondition(ta.falling(MA1, 2), title="MA Falling", message="MA Falling")
alertcondition(ta.crossover(high, MA1), title="High Crossing MA", message="High Crossing MA")
alertcondition(ta.crossunder(low, MA1), title="Low Crossing MA", message="Low Crossing MA")

// Custom Alert: Yellow Candle Followed by Higher/Lower Open with Additional Conditions
yellowCandle = (UseGradient ? col == color.new(color.yellow, 0) : BodyColor == color.yellow)
yellowCandleDetected = yellowCandle[1] // Previous candle was yellow

// New Conditions based on user requirements
// For Buy: Previous yellow candle's close < close of candle before yellow candle, and current open > open of yellow candle
previousYellowClose = CandleClose[1] // Close of yellow candle
previousCloseBeforeYellow = close[2] // Close of the candle before yellow candle
currentOpen = open
openOfYellowCandle = CandleOpen[1] // Open of yellow candle

buyCondition = yellowCandleDetected and (previousYellowClose < previousCloseBeforeYellow) and (currentOpen > openOfYellowCandle)
alertcondition(buyCondition, title="Buy Signal", message="Buy signal: A yellow candle was followed by a candle with a higher open and the yellow candle's close was lower than the previous candle's close.")

// For Sell: Previous yellow candle's close > close of candle before yellow candle, and current open < open of yellow candle
sellCondition = yellowCandleDetected and (previousYellowClose > previousCloseBeforeYellow) and (currentOpen < openOfYellowCandle)
alertcondition(sellCondition, title="Sell Signal", message="Sell signal: A yellow candle was followed by a candle with a lower open and the yellow candle's close was higher than the previous candle's close.")

// Additional Alerts When a Yellow Candle Appears
alertcondition(yellowCandle, title="Yellow Candle Appeared", message="A yellow candle has appeared on the chart.")

// -----------------------------
// Strategy Entry and Exit
// -----------------------------

// Strategy Entry based on Buy and Sell Conditions
if (buyCondition)
strategy.entry("Buy", strategy.long)

if (sellCondition)
strategy.entry("Sell", strategy.short)

// Example Exit Conditions (Bạn có thể tùy chỉnh thêm)
exitLongCondition = ta.crossunder(ma, ta.ema(ma, 3)) // Exit long when MA crosses under its EMA
if (exitLongCondition)
strategy.close("Buy")

exitShortCondition = ta.crossover(ma, ta.ema(ma, 3)) // Exit short when MA crosses over its EMA
if (exitShortCondition)
strategy.close("Sell")

// -----------------------------
// Watermark
// -----------------------------
var table Watermark = table.new(position.bottom_left, 1, 1, border_width = 0)
if barstate.islast
table.cell(Watermark, 0, 0, text="North Star Day Trading - NSDT HAMA Candles Strategy", text_color=color.new(color.white, 95), text_size=size.small, bgcolor=color.new(color.black, 100))
Bands and ChannelsBill Williams IndicatorsBreadth Indicators

Mã nguồn mở

Theo tinh thần TradingView thực sự, tác giả của tập lệnh này đã xuất bản dưới dạng nguồn mở để các nhà giao dịch có thể hiểu và xác minh. Chúc mừng tác giả! Bạn có thể sử dụng miễn phí. Tuy nhiên, bạn cần sử dụng lại mã này theo Quy tắc nội bộ. Bạn có thể yêu thích nó để sử dụng nó trên biểu đồ.

Bạn muốn sử dụng tập lệnh này trên biểu đồ?

Thông báo miễn trừ trách nhiệm