ScriptingFAQGeneral

FAQ

Plot Arrows on the Chart

You can use plotshape with the styles shape.arrowup and shape.arrowdown:

indicator('Ex 1', overlay = true)
condition = close >= open
plotshape(condition, color = color.lime, style = shape.arrowup, text = "Buy")
plotshape(not condition, color = color.red, style = shape.arrowdown, text = "Sell")

image

You can use the plotchar function with any Unicode character:

indicator('buy/sell arrows', overlay = true)
condition = close >= open
plotchar(not condition, char='↓', color = color.lime, text = "Buy")
plotchar(condition, char='↑', location = location.belowbar, color = color.red, text = "Sell")

image

Plot a Dynamic Horizontal Line

The hline function in Lipi Script is limited to plotting only a constant value. Here is a simple script that provides a workaround to plot a changing hline:

indicator("Horizontal line", overlay = true)
plot(close[10], trackprice = true, offset = -9999)
// `trackprice = true` plots horizontal line on close[10]
// `offset = -9999` hides the plot
plot(close, color = #FFFFFFFF)  // forces display

Plot a Vertical Line on Condition

indicator("Vertical line", overlay = true)
// scale.none means do not resize the chart to fit this plot
// if the bar being evaluated is the last baron the chart (the most recent bar), then cond is true
cond = barstate.islast
// when cond is true, plot a histogram with a line with height value of 100,000,000,000,000,000,000.00
// (10 to the power of 20)
// when cond is false, plot no numeric value (nothing is plotted)
// use the style of histogram, a vertical bar
plot(cond ? 10e20 : na, style = plotStyle.histogram)

Access the Previous Value

//...
s = 0.0
s := nz(s[1]) // Accessing previous values
if (condition)
    s := s + 1

Get a 5-Days High

Look back 5 days from the current bar to find the highest bar, and plot a star character at that price level above the current bar.

image

indicator("High of last 5 days", overlay = true)
 
// Milliseconds in 5 days: millisecs * secs * mins * hours * days
MS_IN_5DAYS = 1000 * 60 * 60 * 24 * 5
 
// The range check begins 5 days from the current time.
leftBorder = timenow - time < MS_IN_5DAYS
// The range ends on the last bar of the chart.
rightBorder = barstate.islast
 
// ————— Keep track of highest `high` during the range.
// Intialize `maxHi` with `var` on bar zero only.
// This way, its value is preserved, bar to bar.
static float maxHi = na
if leftBorder {
if not leftBorder[1]{
//Range's first bar.
maxHi := high
} else if not rightBorder {
//On other bars in the range, track highest `high`.
maxHi := math.max(maxHi, high)
}
}
 
// Plot level of the highest `high` on the last bar.
plotchar(rightBorder ? maxHi : na, "Level", "—", location.absolute, size = size.normal)
// When in range, color the background.
bgcolor(leftBorder and not rightBorder ? color.new(color.aqua, 70) : na)
 

Count Bars in a Dataset

Get a count of all the bars in the loaded dataset. This may be useful for calculating flexible lookback periods based on the number of bars.

indicator("Bar Count", overlay = true)
plot(bar_index + 1, style = plotStyle.histogram)

Enumerate Bars in a Day

indicator("My Script", overlay = true)
 
isNewDay() {
    d = dayofweek
return    na(d[1]) or d != d[1]
}
plot(talib.barssince(isNewDay()), style = plotStyle.cross)

Find the Highest and Lowest Values for the Entire Dataset

indicator("", "", true)
 
allTimetHi(source) {
    var atHi = source
 return   atHi := math.max(atHi, source)
}
allTimetLo(source) {
    var atLo = source
  return  atLo := math.min(atLo, source)
}
plot(allTimetHi(close), "ATH", color.green)
plot(allTimetLo(close), "ATL", color.red)

Query the Last Non-Na Value

You can use the script below to avoid gaps in a series:

indicator("")
ser = close >= open ? close : na
vw = fixnan(ser)
plot(ser, style = plotStyle.linebr, color = color.red)
// series has na values
plot(vw)
// all na values are replaced with the last non-empty value