نمونهها: اسیلاتورها
برنامههای نمونهای که یک اسیلاتور را در پنل جداگانه رسم میکنند: RSI دستنویس و داخلی، MACD، استوکاستیک و شمارندهٔ کندلها.
اسیلاتور مقداری است که بین دو حد یا حول صفر حرکت میکند، برای همین این اسکریپتها در هدر display = pane دارند و پنل جداگانهای زیر نمودار میگیرند.
RSI دستنویس#
شاخص قدرت نسبی از روی تعریفش: میانگین سود تقسیم بر میانگین زیان، که به عددی بین 0 و 100 تبدیل میشود.
// Wilder's Relative Strength Index written out: RS = average gain / average loss and RSI = 100 - 100 / (1 + RS).
// The averages are Wilder's running means (tech.rma), the same smoothing the built-in tech.rsi uses.
indicator "RSI by hand"
edition = 1
display = pane
input length: int = 14, name = "Length", min = 2
let change = close - close[1]
let gain = math.max(change, 0)
let loss = math.max(-change, 0)
let average_gain = tech.rma(gain, length)
let average_loss = tech.rma(loss, length)
let strength = average_gain / average_loss
let value = 100 - 100 / (1 + strength)
plot(value, name = "RSI", color = color.purple)
level(70, name = "Overbought", color = color.red, style = dashed)
level(30, name = "Oversold", color = color.green, style = dashed)چه چیزی یاد میدهد: close[1] (قیمت بستهٔ قبلی)، math.max، میانگین جاری وایلدر tech.rma و دو خط level خطچین.
RSI با ناحیهها و علامتها#
RSI داخلی، یک نوار رنگشده بین دو سطح آن، و یک علامت هر بار که از یک ناحیهٔ حدی بیرون میآید.
// The built-in RSI with two levels, a shaded middle band and a mark each time it leaves a zone.
indicator "RSI zones"
edition = 1
display = pane
input length: int = 14, name = "Length", min = 2
input upper: float = 70, name = "Upper level"
input lower: float = 30, name = "Lower level"
let value = tech.rsi(close, length)
let top = level(upper, name = "Upper", color = color.red, style = dashed)
let bottom = level(lower, name = "Lower", color = color.green, style = dashed)
shade(top, bottom, color = color.fade(color.purple, 92), name = "Middle band")
plot(value, name = "RSI", color = color.purple, width = 2)
mark.shape(
tech.crosses_below(value, upper),
name = "Leaves overbought",
shape = triangle_down,
place = above,
color = color.red,
)
mark.shape(
tech.crosses_above(value, lower),
name = "Leaves oversold",
shape = triangle_up,
place = below,
color = color.green,
)چه چیزی یاد میدهد: tech.rsi، یک shade بین دو سطحی که در نام نگه داشته شدهاند، و mark.shape روی عبور از یک سطح.
MACD#
اختلاف یک EMA تند و یک EMA کند، یک خط سیگنال، و فاصلهٔ بین آنها به شکل هیستوگرام.
// MACD: the difference of a fast and a slow EMA, a signal EMA of that difference, and the gap between them as a histogram.
indicator "MACD"
edition = 1
display = pane
input fast_length: int = 12, name = "Fast length", min = 1
input slow_length: int = 26, name = "Slow length", min = 2
input signal_length: int = 9, name = "Signal length", min = 1
let (macd_line, signal_line, hist_value) = tech.macd(
close,
fast_length = fast_length,
slow_length = slow_length,
signal_length = signal_length,
)
plot(
hist_value,
name = "Histogram",
style = histogram,
color = if hist_value >= 0 then color.green else color.red,
)
plot(macd_line, name = "MACD", color = color.blue)
plot(signal_line, name = "Signal", color = color.orange)چه چیزی یاد میدهد: تابعی که سه مقدار را با هم برمیگرداند و با let (a, b, c) = ... از هم جدا میشوند، و هیستوگرامی که با علامت مقدارش رنگ میگیرد.
اسیلاتور استوکاستیک#
قیمت بسته کجای بازهٔ length کندل آخر قرار دارد، هموارشده به %K و %D.
// The stochastic oscillator: where the close sits inside the last `length` bars' range, smoothed into %K and %D.
// raw %K = 100 * (close - lowest low) / (highest high - lowest low)
indicator "Stochastic"
edition = 1
display = pane
input length: int = 14, name = "Length", min = 1
input smooth: int = 3, name = "Smoothing", min = 1
let lowest_low = tech.lowest(low, length)
let highest_high = tech.highest(high, length)
let raw = 100 * (close - lowest_low) / (highest_high - lowest_low)
let k = tech.sma(raw, smooth)
let d = tech.sma(k, smooth)
plot(k, name = "%K", color = color.blue)
plot(d, name = "%D", color = color.orange)
level(80, name = "Upper", color = color.gray, style = dashed)
level(20, name = "Lower", color = color.gray, style = dashed)چه چیزی یاد میدهد: tech.highest و tech.lowest روی یک پنجره، و میانگینِ یک میانگین.
کندلها از آخرین سقف#
شمارندهٔ کندلهایی که از آخرین باری که قیمت بسته سقف 20 کندلی ساخته گذشته است.
// A value the script keeps between bars: `state` starts once, `mut` changes it.
// This counts the bars since the close last made a 20-bar high, and resets it on every new high.
indicator "Bars since a high"
edition = 1
display = pane
input length: int = 20, name = "Length", min = 2
state count = 0
let new_high = close >= tech.highest(close, length)
if new_high
count = 0
else
count += 1
plot(count, name = "Bars since", style = histogram, color = color.teal)چه چیزی یاد میدهد: state، مقداری که اسکریپت از یک کندل تا کندل بعد نگه میدارد. یک بار از 0 شروع میشود و بعد در هر کندل یا صفر میشود یا یکی به آن اضافه میشود.