### Loop Example with Accumulation Source: https://gocharting.com/docs/scripting/language/loops Demonstrates a loop that iterates from 1 to 5 with a step of 2, accumulating the counter values into a variable 'x'. The example highlights how the counter is updated and the loop terminates based on the end value. ```GoCharting x = 0 for i = 1 to 5 step 2 { x := x + i } ``` -------------------------------- ### Lipi Script Structure Example Source: https://gocharting.com/docs/scripting/writing-scripts/style-guide Recommended organization for Lipi scripts, including declaration, imports, constants, inputs, functions, calculations, and visuals. ```lipi ``` -------------------------------- ### Repainting Script Example Source: https://gocharting.com/docs/scripting/concepts/repainting This script demonstrates repainting by using realtime close values to detect crossovers, which can lead to signals changing before a bar closes. ```pine indicator("Repainting", "", true) ma = talib.ema(close, 5) xUp = talib.crossover(close, ma) xDn = talib.crossunder(close, ma) plot(ma, "MA", color.black, 2) bgcolor(xUp ? color.new(color.lime, 80) : xDn ? color.new(color.red, 80) : na) ``` -------------------------------- ### Identifier Naming Conventions and Examples Source: https://gocharting.com/docs/scripting/language/identifiers Demonstrates recommended naming conventions for constants (SNAKE_CASE) and other identifiers (camelCase), along with a simple ternary function. ```gocharting GREEN_COLOR = #4CAF50 MAX_LOOKBACK = 100 int fastLength = 7 // Returns 1 if the argument is `true`, 0 if it is `false` or `na`. zeroOne(boolValue) => boolValue ? 1 : 0 ``` -------------------------------- ### Float Input with Minval and Step Source: https://gocharting.com/docs/scripting/concepts/inputs Illustrates the use of `input.float()` for configuring floating-point values. This example uses `minval` to set a lower bound and `step` to define the increment for the input widget. ```javascript indicator("MA", "", true) maLengthInput = input.int(10, "MA length", minval = 1) bbFactorInput = input.float(1.5, (minval = 0), (step = 0.5)) ma = talib.sma(close, maLengthInput) bbWidth = talib.stdev(ma, maLengthInput) * bbFactorInput bbHi = ma + bbWidth bbLo = ma - bbWidth plot(ma, "MA", color.aqua) plot(bbHi, "BB Hi", color.gray) plot(bbLo, "BB Lo", color.gray) ``` -------------------------------- ### Call talib.vwma() with keyword arguments Source: https://gocharting.com/docs/scripting/language/built-ins This example shows how to call the talib.vwma() function using keyword arguments, allowing for reordering. This is useful for functions with many parameters, like indicator(). ```javascript myVwma = talib.vwma(source=close, length=20) ``` -------------------------------- ### Generic Input Function Examples Source: https://gocharting.com/docs/scripting/concepts/inputs Demonstrates the usage of the generic `input()` function with various data types. The function infers the input type from the `defval` argument. ```javascript indicator("`input()`", "", true) a = input(1, "input int") b = input(1.0, "input float") c = input(true, "input bool") d = input(color.orange, "input color") e = input("1", "input string") f = input(close, "series float") plot(na) ``` -------------------------------- ### Pine Script Plot Count Example Source: https://gocharting.com/docs/scripting/writing-scripts/limitations Demonstrates how different plotting functions generate varying numbers of plot counts. Understanding this helps in managing the script's plot count limit. ```pine indicator("Plot count example") bool isUp = close > open color isUpColor = isUp ? color.green : color.red bool isDn = not isUp color isDnColor = isDn ? color.red : color.green // Uses one plot count each. p1 = plot(close, color = color.white) p2 = plot(open, color = na) // Uses two plot counts for the `close` and `color` series. plot(close, color = isUpColor) // Uses one plot count for the `close` series. plotarrow(close, colorup = color.green, colordown = color.red) // Uses two plot counts for the `close` and `colorup` series. plotarrow(close, colorup = isUpColor) // Uses three plot counts for the `close`, `colorup`, and the `colordown` series. plotarrow(close - open, colorup = isUpColor, colordown = isDnColor) // Uses four plot counts for the `open`, `high`, `low`, and `close` series. plotbar(open, high, low, close, color = color.white) // Uses five plot counts for the `open`, `high`, `low`, `close`, and `color` series. plotbar(open, high, low, close, color = isUpColor) // Uses four plot counts for the `open`, `high`, `low`, and `close` series. plotcandle(open, high, low, close, color = color.white, wickcolor = color.white, bordercolor = color.purple) // Uses five plot counts for the `open`, `high`, `low`, `close`, and `color` series. plotcandle(open, high, low, close, color = isUpColor, wickcolor = color.white, bordercolor = color.purple) // Uses six plot counts for the `open`, `high`, `low`, `close`, `color`, and `wickcolor` series. plotcandle(open, high, low, close, color = isUpColor, wickcolor = isUpColor , bordercolor = color.purple) // Uses seven plot counts for the `open`, `high`, `low`, `close`, `color`, `wickcolor`, and `bordercolor` series. plotcandle(open, high, low, close, color = isUpColor, wickcolor = isUpColor , bordercolor = isUp ? color.lime : color.maroon) // Uses one plot count for the `close` series. plotchar(close, color = color.white, text = "|", textcolor = color.white) // Uses two plot counts for the `close`` and `color` series. plotchar(close, color = isUpColor, text = "—", textcolor = color.white) // Uses three plot counts for the `close`, `color`, and `textcolor` series. plotchar(close, color = isUpColor, text = "O", textcolor = isUp ? color.yellow : color.white) // Uses one plot count for the `close` series. plotshape(close, color = color.white, textcolor = color.white) // Uses two plot counts for the `close` and `color` series. plotshape(close, color = isUpColor, textcolor = color.white) // Uses three plot counts for the `close`, `color`, and `textcolor` series. plotshape(close, color = isUpColor, textcolor = isUp ? color.yellow : color.white) // Uses one plot count. bgcolor(isUp ? color.yellow : color.white) // Uses one plot count for the `color` series. fill(p1, p2, color = isUpColor) ``` -------------------------------- ### Valid and Invalid Identifier Examples Source: https://gocharting.com/docs/scripting/language/identifiers Illustrates correct and incorrect ways to name identifiers based on the language rules. Identifiers starting with digits are not valid. ```gocharting myVar _myVar my123Var functionName MAX_LEN max_len maxLen 3barsDown // NOT VALID! ``` -------------------------------- ### Comparison Operator Examples Source: https://gocharting.com/docs/scripting/language/operators Provides examples of basic comparison operations. The result of a comparison between two numerical values is a boolean (true, false, or na). ```Lipi Script 1 > 2 // false 1 != 1 // false // Depends on values of `close` and `open` close >= open ``` -------------------------------- ### Example of Scope Count Exceeding Limit Source: https://gocharting.com/docs/scripting/writing-scripts/limitations This example demonstrates a script that might exceed the scope count limit if the 'if' pattern is repeated many times. The total scope count includes the global scope plus each local scope created by conditional structures. ```Lipi Script indicator("Scopes demo") var x = 0 if close > 0 { x += 0 } if close > 1 { x += 1 } // ... Repeat this `if close > n` pattern until `n = 299`. if close > 299 { x += 299 } plot(x) ``` -------------------------------- ### Integer Literals Source: https://gocharting.com/docs/scripting/language/type-system Examples of integer literals in Lipi Script. These represent whole numbers without fractions. ```Lipi Script 1 - 1 750 ``` -------------------------------- ### Plotting RSI in a Separate Pane Source: https://gocharting.com/docs/scripting/concepts/plots This example demonstrates plotting an RSI indicator in a separate pane with a centerline and conditional coloring. ```pine indicator("RSI") myRSI = talib.rsi(close, 20) bullColor = color.new(color.lime, 0.3) bearColor = color.new(color.red, 0.3) myRSIColor = myRSI > 50 ? bullColor : bearColor plot(myRSI, "RSI", myRSIColor, 3) hline(50) ``` -------------------------------- ### Basic plot() Usage and Styles Source: https://gocharting.com/docs/scripting/concepts/plots Demonstrates plotting lines, crosses, and step lines with different colors and styles. The `linewidth` parameter controls relative size, not pixels. Uses `static` for initializing color variables on the first bar. ```Lipi Script indicator("`plot()`", "", true) plot(high, "Blue `high` line") plot(math.avg(close, open), "Crosses in body center", close > open ? color.lime : color.purple, 6, plotStyle.scatter) plot(math.min(open, close), "Navy step line on body low point", color.navy, 3, plotStyle.stepLine) plot(low, "Gray dot on `low`", color.gray, 3, plotStyle.scatter) color VIOLET = #AA00FF color GOLD = #CCCC00 ma = ta.alma(hl2, 40, 0.85, 6) static almaColor = color.silver almaColor := ma > ma[2] ? GOLD : ma < ma[2] ? VIOLET : almaColor plot(ma, "Two-color ALMA", almaColor, 2) ``` -------------------------------- ### LipiScript with Comments Source: https://gocharting.com/docs/scripting/language/script-structure Comments in LipiScript are defined using double slashes (`//`) and must start on a new line. They are ignored by the compiler. ```lipi indicator("") // This line is a comment a = close plot(a) ``` -------------------------------- ### Create Transparent Colors with `color.new()` Source: https://gocharting.com/docs/scripting/concepts/colors Dynamically calculate column color by adjusting base color and transparency based on volume changes. User inputs control base colors and brightness levels, with a maximum transparency of 80 to ensure visibility. ```pinescript indicator("Volume") // We name our color constants to make them more readable. static color GOLD_COLOR   = #CCCC00ff static color VIOLET_COLOR = #AA00FFff color bullColorInput = input.color(GOLD_COLOR,   "Bull") color bearColorInput = input.color(VIOLET_COLOR, "Bear") int levelsInput = input.int(10, "Gradient levels", minval = 1) // We initialize only once on bar zero with `static`, otherwise the count would reset to zero on each bar. static float riseFallCnt = 0 // Count the rises/falls, clamping the range to: 1 to `i_levels`. riseFallCnt := math.max(1, math.min(levelsInput, riseFallCnt + math.sign(volume - nz(volume[1])))) // Rescale the count on a scale of 80, reverse it and cap transparency to <80 so that colors remains visible. float transparency = 80 - math.abs(80 * riseFallCnt / levelsInput) // Build the correct transparency of either the bull or bear color. color volumeColor = color.new(close > open ? bullColorInput : bearColorInput, transparency) plot(volume, "Volume", volumeColor, 1, plotStyle.histogram) ``` -------------------------------- ### Equivalent if-else Form Source: https://gocharting.com/docs/scripting/language/conditional-structures This demonstrates the equivalent logic of a switch statement using a traditional if-else if-else chain. Use this form when a simple sequential check is more readable or when conditions are not based on direct equality. ```go if s == "Hello" { y := 1 } else if s == "World" { y := 2 } else { y := 3 } ``` -------------------------------- ### Plotting Simple Candles Source: https://gocharting.com/docs/scripting/concepts/bar-plotting Plots simple candles using the OHLC values. This example uses the default color and plots in a separate pane. ```gocharting indicator("Single-color candles") plotcandle(open, high, low, close) ``` -------------------------------- ### Variable Declaration Initialization with `na` Source: https://gocharting.com/docs/scripting/language/variable-declarations Illustrates how to initialize variables with the generic `na` value. Explicitly declaring the type is necessary when `na` is used without an initial value to avoid compile-time errors. ```lipi // compile time error! baseLine0 = na // OK float baseLine1 = na // OK baseLine2 = float(na) ``` -------------------------------- ### Plotting Bar Index Source: https://gocharting.com/docs/scripting/concepts/bar-states Use `bar_index` to plot the current bar's index, starting from 0. Be aware that this can cause indicator repainting. ```pine // Example usage of bar_index plot(bar_index, title="Bar Index") ```