نمونهها: میانگین متحرک و روند
برنامههای نمونه بر پایهٔ میانگین متحرک: یک میانگین ساده، دو میانگین با علامت تقاطع، پسزمینهٔ روند، کندلهای روند و یک تایمفریم بالاتر.
هر برنامهٔ این صفحه کامل است: آن را در یک اسکریپت جدید در ویرایشگر کپی کنید، ذخیره و بعد افزودن به نمودار را بزنید. تنظیمات را از پنل تنظیمات اندیکاتور تغییر دهید.
میانگین متحرک ساده#
کوچکترین اندیکاتور کاربردی: میانگین length قیمت بستهٔ آخر، رسمشده روی نمودار قیمت.
// A simple moving average: the mean of the last `length` closes.
// This is a complete indicator; the header says what kind of script it is.
indicator "Simple moving average"
edition = 1
display = price
input length: int = 20, name = "Length", min = 1
let average = tech.sma(close, length)
plot(average, name = "SMA", color = color.orange, width = 2)چه چیزی یاد میدهد: هدر، یک ورودی، tech.sma و یک خط رسمشده با نام و رنگ. خط از جایی شروع میشود که length کندل برای میانگینگیری وجود داشته باشد.
دو میانگین با علامت تقاطع#
یک میانگین متحرک نمایی تند و یک کند، با یک مثلث روی کندلی که میانگین تند، کند را قطع میکند.
// Two exponential moving averages and a mark where the fast one crosses the slow one.
// EMA(t) = alpha * price + (1 - alpha) * EMA(t - 1), with alpha = 2 / (length + 1).
indicator "EMA pair"
edition = 1
display = price
input fast_length: int = 12, name = "Fast length", min = 1
input slow_length: int = 26, name = "Slow length", min = 2
let fast = tech.ema(close, fast_length)
let slow = tech.ema(close, slow_length)
plot(fast, name = "Fast", color = color.green)
plot(slow, name = "Slow", color = color.red)
mark.shape(
tech.crosses_above(fast, slow),
name = "Up",
shape = triangle_up,
place = below,
color = color.green,
)
mark.shape(
tech.crosses_below(fast, slow),
name = "Down",
shape = triangle_down,
place = above,
color = color.red,
)چه چیزی یاد میدهد: tech.ema، tech.crosses_above و tech.crosses_below، و mark.shape برای علامت گذاشتن روی کندلهایی که یک شرط در آنها درست است. علامت below (زیر) یا above (بالای) کندل قرار میگیرد.
پسزمینهٔ روند#
پسزمینه تا وقتی قیمت بسته بالای یک میانگین متحرک است سبز و تا وقتی زیر آن است قرمز میشود.
// A background that follows the trend: green above the moving average, red below it, nothing while it is not known yet.
indicator "Trend background"
edition = 1
display = price
input length: int = 50, name = "Length", min = 2
let trend = tech.sma(close, length)
let tint = if close > trend then color.fade(color.green, 88) else color.fade(color.red, 88)
chart.background(if is_na(trend) then na else tint, name = "Trend")
plot(trend, name = "Trend line", color = color.gray)چه چیزی یاد میدهد: رنگی که با if ... then ... else انتخاب و با color.fade کمرنگ میشود، و chart.background. تا وقتی میانگین هنوز مقداری ندارد، پسزمینه خالی میماند (na).
کندلهای روند#
خود کندلها رنگ روند را میگیرند.
// Candles coloured by the trend: green while the close is above the moving average, red below it.
indicator "Trend candles"
edition = 1
display = price
input length: int = 50, name = "Length", min = 2
let trend = tech.ema(close, length)
let tint = if close > trend then color.green else color.red
chart.candles(if is_na(trend) then na else tint, name = "Trend candles")
plot(trend, name = "Trend line", color = color.gray)چه چیزی یاد میدهد: chart.candles کندلهای خود نمودار را دوباره رنگ میکند؛ na رنگ عادی آنها را نگه میدارد.
فاصله از میانگین#
قیمت بسته چقدر از میانگینش دور شده است، به درصد. مقداری که از صفر دور باشد یعنی قیمت از میانگینش فاصلهٔ زیادی گرفته است.
// How far the close is from its moving average, in percent of the average.
// Above zero the close is above the average; the further from zero, the more stretched the move.
indicator "Distance from the average"
edition = 1
display = pane
input length: int = 50, name = "Length", min = 2
let average = tech.ema(close, length)
let distance = 100 * (close - average) / average
plot(
distance,
name = "Distance %",
style = histogram,
color = if distance >= 0 then color.green else color.red,
)
level(0, name = "Zero", color = color.gray)چه چیزی یاد میدهد: محاسبه روی سریها، هیستوگرامی که رنگش از علامت مقدار پیروی میکند، و یک level روی صفر.
میانگینی از تایمفریم بالاتر#
EMA بیستدورهای نمودار 4 ساعته، رسمشده روی هر نموداری که نگاه میکنید، کنار همان EMA روی خود نمودار.
// A request for another timeframe: the 4-hour EMA of the close, drawn on whatever chart the script is on.
// `data.series` evaluates the expression on the other timeframe and gives back its last closed value.
indicator "Higher timeframe average"
edition = 1
display = price
input tf: tf = "240", name = "Timeframe"
input length: int = 20, name = "Length", min = 2
let other = data.series(tf, tech.ema(close, length), symbol = symbol.id)
plot(other, name = "EMA on the other timeframe", color = color.orange, width = 2)
plot(tech.ema(close, length), name = "EMA on this chart", color = color.gray)چه چیزی یاد میدهد: یک ورودی tf و data.series که یک عبارت را روی تایمفریم دیگری حساب میکند. بهطور پیشفرض مقدار آخرین کندل بستهشدهٔ آن تایمفریم را میدهد، پس خط تا وقتی کندل بالاتر در حال شکلگیری است تغییر نمیکند؛ صفحهٔ تایمفریمها و نمادهای دیگر گزینهها را توضیح میدهد.