SMA Selectable Timeframe Alerts

miller2018 · study · 72 行 · 点赞 0 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("SMA Selectable Timeframe Alerts", overlay = true, dynamic_requests = true)
 
// SMA settings
length = input.int(200, "SMA length", minval = 1, group = "SMA Settings")
direction = input.string("Both", "Alert direction",
     options = ["Both", "Above only", "Below only"], group = "SMA Settings")
showSMA = input.bool(true, "Show chart-timeframe SMA", group = "SMA Settings")
 
// Select the timeframes to monitor
use15m = input.bool(true, "15 minutes", group = "Alert Timeframes")
use1h  = input.bool(true, "1 hour", group = "Alert Timeframes")
use4h  = input.bool(true, "4 hours", group = "Alert Timeframes")
use1d  = input.bool(true, "1 day", group = "Alert Timeframes")
use2d  = input.bool(true, "2 days", group = "Alert Timeframes")
use3d  = input.bool(true, "3 days", group = "Alert Timeframes")
use1w  = input.bool(true, "1 week", group = "Alert Timeframes")
use1mo = input.bool(true, "1 month", group = "Alert Timeframes")
 
// Chart must be at or below the smallest selected timeframe.
int smallestSeconds = use15m ? 900 : use1h ? 3600 : use4h ? 14400 : use1d ? 86400 : use2d ? 172800 : use3d ? 259200 : use1w ? 604800 : use1mo ? timeframe.in_seconds("1M") : 0
 
if smallestSeconds == 0
    runtime.error("Select at least one alert timeframe.")
 
if timeframe.in_seconds() > smallestSeconds
    runtime.error("Set the chart timeframe at or below the smallest selected alert timeframe.")
 
plot(showSMA ? ta.sma(close, length) : na,
     "Chart SMA", color = color.red, linewidth = 2)
 
// Calculate crosses using completed candles.
confirmedData() =>
    avg = ta.sma(close, length)
    crossedUp = ta.crossover(close, avg)
    crossedDown = ta.crossunder(close, avg)
    [crossedUp[1], crossedDown[1], time[1], close[1], avg[1]]
 
var frames = array.from("15", "60", "240", "1D", "2D", "3D", "1W", "1M")
var names = array.from("15m", "1h", "4h", "1D", "2D", "3D", "1W", "1M")
var enabled = array.from(use15m, use1h, use4h, use1d, use2d, use3d, use1w, use1mo)
varip seen = array.new_int(8, na)
 
string message = ""
 
for i = 0 to array.size(frames) - 1
    if array.get(enabled, i)
        [up, down, candleTime, candleClose, smaValue] = request.security(
             syminfo.tickerid,
             array.get(frames, i),
             confirmedData(),
             gaps = barmerge.gaps_off,
             lookahead = barmerge.lookahead_on)
 
        previousTime = array.get(seen, i)
        newClosedCandle = not na(previousTime) and not na(candleTime) and candleTime != previousTime
 
        allowUp = direction != "Below only"
        allowDown = direction != "Above only"
 
        if barstate.isrealtime and newClosedCandle and ((up and allowUp) or (down and allowDown))
            alertText = syminfo.tickerid + " | " + array.get(names, i)
            alertText := alertText + " | Close crossed " + (up ? "ABOVE" : "BELOW")
            alertText := alertText + " SMA " + str.tostring(length)
            alertText := alertText + " | Close: " + str.tostring(candleClose, format.mintick)
            alertText := alertText + " | SMA: " + str.tostring(smaValue, format.mintick)
            message := message + (str.length(message) > 0 ? "\n" : "") + alertText
 
        array.set(seen, i, candleTime)
 
if barstate.isrealtime and str.length(message) > 0
    alert(message, alert.freq_all)

← 返回列表