In this guide, you’ll learn how to create your own custom trading indicators with zero programming or math experience. By the end of this tutorial, you’ll be equipped with simple steps to build three custom indicators using basic concepts and a powerful tool: TradingView’s Pine Script.
Getting Started with Pine Script
Pine Script is TradingView’s programming language for creating custom technical analysis indicators. Best of all, it’s free to use. To begin, make sure you sign up for TradingView if you haven’t already; there’s a link in the description to help you get started.
Opening Pine Script Editor:
- Open a chart on TradingView. The type of security doesn’t matter for this tutorial, as we just need a chart to apply the indicator to.
- At the bottom of the window, find the “Pine Editor” tab and click on it. This opens the Pine Script editor where you’ll write your custom code.
- To make the editor easier to work with, click on the three dots at the top-right corner of the editor and select “Open in a new window.”
Understanding the Basics: When you start, you’ll see default lines in the editor. The line indicator("My Script")
is always there and should not be removed. This line sets the name of your custom indicator. The default line plot(close)
is a function that plots the closing prices of the security on the chart.
Creating Your First Indicator: EMA
To create a simple Exponential Moving Average (EMA), we can use Pine Script’s built-in functions. A function in programming is like a pre-built tool that does complex work for you; you just need to tell it what to do.
Building the EMA Indicator:
- Replace
plot(close)
with the following code:ta.ema()
is the built-in function for calculating the EMA. The first parameter,close
, refers to the source data (closing prices), and20
is the length of the EMA.ema1
is a variable that stores the calculated EMA.
- Click “Add to Chart,” and you’ll see the EMA plotted over your chart.
Explanation: Using built-in functions like ta.ema()
saves you from needing to understand the underlying math. You simply input the source data and the length, and the function outputs the EMA.
Creating a More Complex Indicator: EMA Difference
Next, we’ll create an indicator that shows the difference between two EMAs, a technique similar to the MACD. This type of indicator helps identify trends and potential reversals.
Steps to Create EMA Difference:
- Define a second EMA with a longer length:
- Calculate the difference between the two EMAs:
- Plot the difference:
Explanation: ema_diff
will show an oscillating line, moving above and below zero as the short-term EMA crosses over the long-term EMA. This is similar to how the MACD works, and it can signal bullish or bearish trends.
Creating an Indicator to Identify Market Choppiness
Understanding whether a market is trending or choppy can be crucial for traders. We’ll create an indicator to help identify when the market is choppy.
Steps to Build a Choppiness Indicator:
- Set up a simple moving average (SMA) with an adjustable length:
- The
input.int()
function allows you to set a default value for the SMA length (150) and provides a title for the setting.
- The
- Use the
ta.crossover()
function to detect when the price crosses above the moving average: - Count the number of bars since the last crossover:
- Plot the result:
Explanation: This bars_since
variable tells you how many bars have passed since the last time the price crossed the moving average. Large values indicate strong trends, while small values suggest choppy markets.
Adding Customizable Inputs to Indicators
Customizable inputs make your indicators more flexible. Here’s how to create inputs that can be changed without altering the code:
Adding an Input for Length: Replace the hard-coded length with:
Benefits of Custom Inputs: This change allows you to adjust the SMA length directly from the chart settings without modifying the code itself.
Creating a Momentum Indicator with True Strength Index (TSI)
For a momentum indicator, we’ll use the built-in TSI function. This will help identify the strength and direction of a trend.
Steps to Build a TSI Indicator:
- Create input variables for short and long lengths:
- Create the TSI variable:
- Create an EMA of the TSI line:
- Plot the TSI and EMA TSI lines:
Adding Color and Conditional Coloring: To differentiate between the lines, we can set colors, and to fill the space between them, we use the fill()
function:
Conclusion
By understanding these basic Pine Script concepts and using built-in functions, you can create a wide range of custom trading indicators without needing any programming or math expertise. Start simple, experiment with different functions, and build more complex indicators over time. This opens up a new level of customization in your trading strategy.