用户
I need to backtest my EMA and MACD covergence strategy
TradingView indicator & stratagy developer
I need to backtest my EMA and MACD covergence strategy
To backtest your EMA and MACD convergence strategy using Pine Script, you'll need to follow a few steps. These steps include defining the EMA (Exponential Moving Average) and MACD (Moving Average Convergence Divergence) indicators, setting up conditions for entry and exit based on the convergence of these indicators, and then coding the strategy logic in Pine Script. Here's a basic outline to get you started:
Define EMA and MACD Parameters:
Identify Convergence Criteria:
Search the web for the best settings and let’s get started on the code
Based on the research, the most commonly recommended settings for MACD in a trading strategy are:
Standard Setting (12, 26, 9): This is the default MACD setting, widely used by traders across different markets. It is considered a well-balanced setting that provides reliable signals【17†source】【18†source】【19†source】【20†source】.
Alternative Settings:
These settings provide a good starting point for your EMA and MACD convergence strategy. However, it's important to note that the effectiveness of these settings can vary depending on the market conditions and the specific assets you are trading. It's always advisable to do your own testing and adjust the settings based on your trading style and the market's behavior.
Let's proceed with coding this strategy in Pine Script. We can start with the standard MACD setting (12, 26, 9) and you can adjust it later based on your testing results.
Let’s go for the more aggressive strategy
For a more aggressive EMA and MACD convergence strategy, we'll use the Short-Term MACD settings of (5, 35, 5). This approach is more suited for traders who are willing to take on more risk for potentially greater rewards.
Here's a basic structure of the Pine Script code for this strategy:
Define the Short-Term MACD Parameters:
shortTermFastLength = 5
shortTermSlowLength = 35
shortTermSignalSmoothing = 5
macd = ema(close, shortTermFastLength) - ema(close, shortTermSlowLength)
signal = ema(macd, shortTermSignalSmoothing)
Define the Strategy Entry and Exit Conditions:
Implementing the Strategy:
strategy("EMA and MACD Convergence Strategy", overlay=true)
longCondition = crossover(macd, signal)
if (longCondition)
strategy.entry("Long", strategy.long)
shortCondition = crossunder(macd, signal)
if (shortCondition)
strategy.close("Long")
Plot the MACD and Signal Line for Visualization:
plot(macd, title="MACD Line", color=color.blue)
plot(signal, title="Signal Line", color=color.orange)
This script sets up an aggressive trading strategy based on the MACD indicator with the specified short-term settings. Remember, this is a more aggressive strategy and might involve higher risk. It's recommended to test this strategy in a simulated environment before applying it to live trades. Additionally, you may need to tweak and optimize these settings further based on the specific characteristics of the market you are trading in.
Now let’s put it all together so I can copy and paste the working code
Certainly! Below is a complete Pine Script code for an aggressive EMA and MACD convergence strategy using the short-term settings (5, 35, 5). You can copy and paste this code into your TradingView Pine Editor to use it:
//@version=4
strategy("EMA and MACD Convergence Strategy - Aggressive", overlay=true)
// Define MACD Settings
shortTermFastLength = 5
shortTermSlowLength = 35
shortTermSignalSmoothing = 5
// Calculate MACD and Signal Line
macd = ema(close, shortTermFastLength) - ema(close, shortTermSlowLength)
signal = ema(macd, shortTerm
There is no Ema in this
You're right, the code I provided focuses on the MACD indicator without incorporating the EMA (Exponential Moving Average) directly. Let's integrate EMA into your strategy. We'll use the same aggressive settings for MACD and include an EMA to create entry and exit signals based on its convergence with the MACD line.
Here's the revised Pine Script code:
//@version=4
strategy("Aggressive EMA and MACD Convergence Strategy", overlay=true)
// Define MACD Settings
shortTermFastLength = 5
shortTermSlowLength = 35
shortTermSignalSmoothing = 5
// Calculate MACD and Signal Line
macd = ema(close, shortTermFastLength) - ema(close, shortTermSlowLength)
signal = ema(macd, shortTermSignalSmoothing)
// Define EMA Setting
emaLength = 9 // You can adjust this length based on your preference
emaValue = ema(close, emaLength)
// Entry Conditions
longCondition = crossover(macd, signal) and crossover(close, emaValue)
if (longCondition)
strategy.entry("Long", strategy.long)
// Exit Conditions
shortCondition = crossunder(macd, signal) or crossunder(close, emaValue)
if (shortCondition)
strategy.close("Long")
// Plot the MACD, Signal Line, and EMA for Visualization
plot(macd, title="MACD Line", color=color.blue)
plot(signal, title="Signal Line", color=color.orange)
plot(emaValue, title="EMA", color=color.red)
This script includes both the MACD indicator and an EMA. The strategy triggers a long entry when both the MACD line crosses over the signal line and the price crosses over the EMA. It exits the position when either the MACD line crosses under the signal line or the price crosses under the EMA.
Remember to test and adjust the EMA length and other parameters according to the specific asset and timeframe you are trading. As always, backtesting in a simulated environment before live trading is highly recommended.
Add a second 21 Ema , I want a signal when the Emas cross and when the sentiment agrees with the macd
Certainly, we can add a second EMA with a length of 21 and modify the strategy to include a condition where a signal is generated when both EMAs cross and this crossover agrees with the MACD sentiment. Here's the updated Pine Script code:
//@version=4
strategy("Aggressive Dual EMA and MACD Convergence Strategy", overlay=true)
// Define MACD Settings
shortTermFastLength = 5
shortTermSlowLength = 35
shortTermSignalSmoothing = 5
// Calculate MACD and Signal Line
macd = ema(close, shortTermFastLength) - ema(close, shortTermSlowLength)
signal = ema(macd, shortTermSignalSmoothing)
// Define EMA Settings
emaLength1 = 9 // First EMA Length
emaValue1 = ema(close, emaLength1)
emaLength2 = 21 // Second EMA Length
emaValue2 = ema(close, emaLength2)
// Entry Conditions
longCondition = crossover(emaValue1, emaValue2) and crossover(macd, signal)
if (longCondition)
strategy.entry("Long", strategy.long)
// Exit Conditions
shortCondition = crossunder(emaValue1, emaValue2) or crossunder(macd, signal)
if (shortCondition)
strategy.close("Long")
// Plot the MACD, Signal Line, and EMAs for Visualization
plot(macd, title="MACD Line", color=color.blue)
plot(signal, title="Signal Line", color=color.orange)
plot(emaValue1, title="EMA 9", color=color.red)
plot(emaValue2, title="EMA 21", color=color.green)
This code sets up a strategy where a long position is entered when the 9-period EMA crosses over the 21-period EMA, and this crossover is in agreement with the MACD line crossing over its signal line. The position is closed when either the 9-period EMA crosses under the 21-period EMA or the MACD line crosses under its signal line.
Remember, this is an aggressive strategy and may carry higher risks. It's advisable to backtest thoroughly and adjust the parameters as needed to match your trading style and the market's characteristics.
Add a second 21 Ema , I want a signal when the Emas cross and when the sentiment agrees with the macd
Understood, we'll adjust the strategy to include a second EMA with a period of 21 and generate signals based on the criteria that both EMAs should cross and the direction of the cross should agree with the MACD sentiment.
Here's the modified Pine Script code:
//@version=4
strategy("Dual EMA and MACD Convergence Strategy", overlay=true)
// Define MACD Settings
shortTermFastLength = 5
shortTermSlowLength = 35
shortTermSignalSmoothing = 5
// Calculate MACD and Signal Line
macd = ema(close, shortTermFastLength) - ema(close, shortTermSlowLength)
signal = ema(macd, shortTermSignalSmoothing)
// Define EMA Settings
emaLength1 = 9 // First EMA Length
emaValue1 = ema(close, emaLength1)
emaLength2 = 21 // Second EMA Length
emaValue2 = ema(close, emaLength2)
// Entry Conditions
longCondition = crossover(emaValue1, emaValue2) and crossover(macd, signal)
shortCondition = crossunder(emaValue1, emaValue2) and crossunder(macd, signal)
// Implementing Strategy
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
// Plotting for Visualization
plot(macd, title="MACD Line", color=color.blue)
plot(signal, title="Signal Line", color=color.orange)
plot(emaValue1, title="EMA 9", color=color.red)
plot(emaValue2, title="EMA 21", color=color.green)
In this code:
This setup ensures that the trade is taken only when both the EMA crossover and MACD agree on the market sentiment, aiming to increase the reliability of the signals. As always, backtest this strategy on historical data to understand its performance and adjust the parameters according to your trading style and market conditions.