First Indicator: The Lipi Editor
The Lipi Editor is where you’ll craft and refine your scripts. While you can use any text editor to write Lipi scripts, the built-in Lipi Editor on GoCharting offers numerous advantages:
- Syntax highlighting specific to Lipi Script.
- Syntax reminders that appear when you hover over language elements.
- A seamless write/compile/run workflow—saving a script that’s already loaded on your chart executes it immediately.
To open the Lipi Editor, click on the Lipi Editor tab at the bottom of your GoCharting chart. This will bring up the editor’s interface, where you can start coding your first script.
First Version
We will now create our first working Lipi Script: an implementation of the MACD indicator in Lipi Script.
indicator("MACD #1")
fast = 12
slow = 26
fastMA = talib.ema(close, fast)
slowMA = talib.ema(close, slow)
macd = fastMA - slowMA
signal = talib.ema(macd, 9)
plot(macd, color = color.blue)
plot(signal, color = color.orange)
Steps to Add Your First Lipi Script
- Open the Lipi Editor in GoCharting.
- Click on the “New” located at the top right of the editor and create a new script by choosing a name for your script.
- Copy the example script provided above. Use the button on the top-right of the code widget to copy it with a single click.
- Paste the code in the editor.
- Click “Save”. Your script is now saved to GoCharting’s cloud under your account. It remains private, and only you can use it unless you choose to share it.
- Click “Add to Chart” in the Editor’s menu bar. The MACD indicator will appear in a separate pane under your chart.
Your first Lipi script is running on your chart, which should appear like this:
Let’s look at our script’s code, line by line:
Line 1: indicator("MACD #1")
Defines the name of the script that will appear on the chart as “MACD”.
Line 2: fast = 12
Defines a fast
integer variable which will be the length of the fast EMA.
Line 3: slow = 26
Defines a slow
integer variable which will be the length of the slow EMA.
Line 4: fastMA = talib.ema(close, fast)
Defines the variable fastMA
, containing the result of the EMA calculation (Exponential Moving Average) with a length equal to fast
(12), on the close series, i.e., the closing price of bars.
Line 5: slowMA = talib.ema(close, slow)
Defines the variable slowMA
, containing the result of the EMA calculation with a length equal to slow
(26), from close.
Line 6: macd = fastMA - slowMA
Defines the variable macd
as the difference between the two EMAs.
Line 7: signal = talib.ema(macd, 9)
Defines the variable signal
as a smoothed value of macd
using the EMA algorithm (Exponential Moving Average) with a length of 9.
Line 8: plot(macd, color = color.blue)
Calls the plot
function to output the variable macd
using a blue line.
Line 9: plot(signal, color = color.orange)
Calls the plot
function to output the variable signal
using an orange line.
Second Version
The first version of our script calculated MACD “manually”, but since Lipi Script is designed to write indicators, built-in Lipi Script functions exist for many common indicators, including one for MACD: talib.macd()
.
Here is the second version of our script:
indicator("MACD #2")
fastInput = input(12, "Fast length")
slowInput = input(26, "Slow length")
[macdLine, signalLine, histLine] = talib.macd(close, fastInput, slowInput, 9)
plot(macdLine, color = color.blue)
plot(signalLine, color = color.orange)
Changes and Steps to Add the Second Script
Note that we have:
- Added inputs so we can change the lengths for the MAs.
- We now use the
talib.macd()
built-in function to calculate our MACD, which saves us three lines and makes our code easier to read.
Now, let’s repeat the same process as before to copy that code into a new indicator:
- Copy the example script above.
- Select all the code already in the editor and replace it with the second version of our script.
- Click “Save”.
- Click “Add to Chart” in the Editor’s menu bar. The “MACD #2” indicator will appear in a separate pane under the “MACD #1” indicator.
- Your second Lipi Script is now running on your chart. If you double-click on the indicator’s name on your chart, you will bring up the script’s “Settings/options” tab, where you can now change the slow and fast lengths.
Let’s Look at the Changes in the Second Version of Our Script
-
Line 2: indicator(“MACD #2”)
We have changed#1
to#2
so the second version of our indicator displays a different name on the chart. -
Line 3: fastInput = input(12, “Fast length”)
Instead of assigning a constant value to a variable, we have used theinput()
function so we can change the value in our script’s “Settings/Inputs” tab.12
will be the default value, and the field’s label will be “Fast length”. If the value is changed in the “Inputs” tab, thefastInput
variable’s content will contain the new value, and the script will re-execute on the chart with that new value. As recommended by the Lipi Script Style Guide, we addInput
to the end of the variable’s name to remind us that its value comes from a user input. -
Line 4: slowInput = input(26, “Slow length”)
We do the same for the slow length, using a different variable name, default value, and text string for the field’s label. -
Line 5: [macdLine, signalLine, histLine] = talib.macd(close, fastInput, slowInput, 9)
This is where we call thetalib.macd()
built-in function to perform all the first version’s calculations in one line. The function requires four parameters (the values after the function name, enclosed in parentheses). It returns three values into the three variables instead of only one, like the functions we used in the first version. That’s why we need to enclose the list of three variables receiving the function’s result in square brackets to the left of the=
sign. Note that two of the values we pass to the function are the “input” variables containing the fast and slow lengths:fastInput
andslowInput
. -
Line 6 and 7:
The variable names we are plotting have changed, but these lines are doing the same thing as in our first version.
Summary
Our second version performs the same calculations as the first, but now we can change the two lengths used to calculate it. The code is also simpler and shorter by three lines. We’ve improved the script!