PineScript and MACD Crossover: A Comprehensive Coding Guide

Pine Script is a special code used on TradingView, a popular site for looking at financial charts. It lets people make their own tools to figure out what might happen in markets. Next, we have the MACD Crossover strategy written in Pine Script. This code follows the steps we talked about before. Each part of the code is explained in simple words right in the comments. 

This way, you can understand how the code works and how it uses the MACD strategy on TradingView.

Pine Script

				
					//@version=5
indicator(title="MACD Crossover | Unofficed.com", shorttitle="MACD Crossover | Unofficed.com", timeframe="", timeframe_gaps=true)
// Setting up indicator with a title. Version 5 is specified for Pine Script.

// Getting user inputs for the MACD calculation
fast_length = input(title = "Fast Length", defval = 12) // Fast EMA length
slow_length = input(title = "Slow Length", defval = 26) // Slow EMA length
src = input(title = "Source", defval = close) // Source data for EMA calculation, default is closing price
signal_length = input.int(title = "Signal Smoothing",  minval = 1, maxval = 50, defval = 9) // Length for Signal line smoothing
sma_source = input.string(title = "Oscillator MA Type",  defval = "EMA", options = ["SMA", "EMA"]) // Choice between SMA and EMA for MACD calculation
sma_signal = input.string(title = "Signal Line MA Type", defval = "EMA", options = ["SMA", "EMA"]) // Choice of SMA or EMA for Signal line

// Calculating the MACD and Signal line
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length) // Fast Moving Average
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length) // Slow Moving Average
macd = fast_ma - slow_ma // MACD Line
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length) // Signal Line
hist = macd - signal // MACD Histogram

// Setting up alert conditions based on histogram behavior
alertcondition(hist[1] >= 0 and hist < 0, title = 'Rising to falling', message = 'The MACD histogram switched from a rising to falling state') // Alert for histogram going from rising to falling
alertcondition(hist[1] <= 0 and hist > 0, title = 'Falling to rising', message = 'The MACD histogram switched from a falling to rising state') // Alert for histogram going from falling to rising

// Plotting the MACD components on the chart
hline(0, "Zero Line", color = color.new(#787B86, 50)) // Horizontal line at zero for reference
plot(hist, title = "Histogram", style = plot.style_columns, color = (hist >= 0 ? (hist[1] < hist ? #26A69A : #B2DFDB) : (hist[1] < hist ? #FFCDD2 : #FF5252))) // Plotting the Histogram with color based on direction
plot(macd,   title = "MACD",   color = #2962FF) // Plotting the MACD line
plot(signal, title = "Signal", color = #FF6D00) // Plotting the Signal line

				
			
The strategy is made available on TradingView. It’s open source and free, allowing you to utilize it for your personal research and analysis.

[Quant] MACD Crossover Strategy | Unofficed.com by Amit_Ghosh on TradingView.com

Script Breakdown:

Let’s create a structured breakdown and explanation of your Pine Script for the MACD Crossover strategy:

Indicator Setup

  • indicator(title="MACD Crossover | Unofficed.com", ...): This line sets up the indicator with a title. We’re using Pine Script version 5, indicated by //@version=5.

User Inputs

  • fast_length = input(...): Users can enter the length for the fast EMA. Default is 12.
  • slow_length = input(...): Users can enter the length for the slow EMA. Default is 26.
  • src = input(...): Defines the data source for EMA calculation, usually the closing price.
  • signal_length = input.int(...): Length for the signal line’s smoothing, default is 9.
  • sma_source and sma_signal: Allow users to choose between using a Simple Moving Average (SMA) or Exponential Moving Average (EMA) for both the MACD and the signal line calculations.

MACD Calculation

  • fast_ma and slow_ma: These lines calculate the fast and slow moving averages. The script checks the user’s choice (SMA or EMA) and calculates accordingly.
  • macd = fast_ma - slow_ma: The MACD line is derived by subtracting the slow MA from the fast MA.
  • signal = ...: Similar to the MAs, this line calculates the Signal line based on user choice between SMA and EMA.
  • hist = macd - signal: This is the MACD Histogram, calculated as the difference between the MACD line and the Signal line.

Alert Conditions

  • alertcondition(...): These conditions create alerts for when the histogram changes its state from rising to falling or vice versa.

Plotting

  • hline(0, ...): Draws a horizontal line at zero for reference.
  • plot(hist, ...): Plots the histogram. The color of the bars changes based on whether the histogram is rising or falling.
  • plot(macd, ...): Plots the MACD line.
  • plot(signal, ...): Plots the Signal line.

In the line fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length), the script is using a ternary operator to decide between calculating a Simple Moving Average (SMA) or an Exponential Moving Average (EMA) for the fast MA. If sma_source is SMA

  • it calculates the SMA using ta.sma(src, fast_length)
  • otherwise, it calculates the EMA using ta.ema(src, fast_length)

This allows the script to dynamically adapt based on user preference for the type of moving average.

 

Although MACD and the signal line were initially designed over EMAs, modern trading and charting platforms allow us to alter the settings to play with it. So, We have added the options of SMA in the PineScript.

Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *

×Close