Backtest EMA MACD Strategy

Anonymous

I need to backtest my EMA and MACD covergence strategy

TradingView indicator & stratagy developer logo
TradingView indicator & stratagy developer

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:

  1. Define EMA and MACD Parameters:

    • Choose the periods for the short-term and long-term EMAs.
    • Define the MACD parameters, typically including a fast length, slow length, and signal smoothing.
  2. Identify Convergence Criteria:

    • Convergence between EMA and MACD can be based on various criteria, such as the EMA crossing the MACD line or the MACD line crossing its signal
Anonymous

Search the web for the best settings and let’s get started on the code

TradingView indicator & stratagy developer logo
TradingView indicator & stratagy developer

Based on the research, the most commonly recommended settings for MACD in a trading strategy are:

  1. 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】.

  2. Alternative Settings:

    • Short-Term (5, 35, 5): Suitable for aggressive traders who are willing to take more risk for potentially greater rewards【17†source】.
    • For 1-hour Timeframes (H1): Optimal values are 12, 26, 9, which is similar to the standard setting【21†source】.
    • For Shorter Timeframes (5 and 15 minutes): Recommended parameters are 21, 31, 1【21†source】.
    • For 1-minute Timeframe: Effective settings include 13, 21, 1; 21, 34, 1; and 31, 144, 1【21†source】.

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.

Anonymous

Let’s go for the more aggressive strategy

TradingView indicator & stratagy developer logo
TradingView indicator & stratagy developer

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:

  1. Define the Short-Term MACD Parameters:

    pine
    1shortTermFastLength = 5 2shortTermSlowLength = 35 3shortTermSignalSmoothing = 5 4macd = ema(close, shortTermFastLength) - ema(close, shortTermSlowLength) 5signal = ema(macd, shortTermSignalSmoothing)
  2. Define the Strategy Entry and Exit Conditions:

    • For entry, you might consider conditions like the MACD line crossing above the signal line.
    • For exit, a common condition could be the MACD line crossing below the signal line.
  3. Implementing the Strategy:

    pine
    1strategy("EMA and MACD Convergence Strategy", overlay=true) 2longCondition = crossover(macd, signal) 3if (longCondition) 4 strategy.entry("Long", strategy.long) 5 6shortCondition = crossunder(macd, signal) 7if (shortCondition) 8 strategy.close("Long")
  4. Plot the MACD and Signal Line for Visualization:

    pine
    1plot(macd, title="MACD Line", color=color.blue) 2plot(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.

Anonymous

Now let’s put it all together so I can copy and paste the working code

TradingView indicator & stratagy developer logo
TradingView indicator & stratagy developer

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:

pine
1//@version=4 2strategy("EMA and MACD Convergence Strategy - Aggressive", overlay=true) 3 4// Define MACD Settings 5shortTermFastLength = 5 6shortTermSlowLength = 35 7shortTermSignalSmoothing = 5 8 9// Calculate MACD and Signal Line 10macd = ema(close, shortTermFastLength) - ema(close, shortTermSlowLength) 11signal = ema(macd, shortTerm
Anonymous

There is no Ema in this

TradingView indicator & stratagy developer logo
TradingView indicator & stratagy developer

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:

pine
1//@version=4 2strategy("Aggressive EMA and MACD Convergence Strategy", overlay=true) 3 4// Define MACD Settings 5shortTermFastLength = 5 6shortTermSlowLength = 35 7shortTermSignalSmoothing = 5 8 9// Calculate MACD and Signal Line 10macd = ema(close, shortTermFastLength) - ema(close, shortTermSlowLength) 11signal = ema(macd, shortTermSignalSmoothing) 12 13// Define EMA Setting 14emaLength = 9 // You can adjust this length based on your preference 15emaValue = ema(close, emaLength) 16 17// Entry Conditions 18longCondition = crossover(macd, signal) and crossover(close, emaValue) 19if (longCondition) 20 strategy.entry("Long", strategy.long) 21 22// Exit Conditions 23shortCondition = crossunder(macd, signal) or crossunder(close, emaValue) 24if (shortCondition) 25 strategy.close("Long") 26 27// Plot the MACD, Signal Line, and EMA for Visualization 28plot(macd, title="MACD Line", color=color.blue) 29plot(signal, title="Signal Line", color=color.orange) 30plot(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.

Anonymous

Add a second 21 Ema , I want a signal when the Emas cross and when the sentiment agrees with the macd

TradingView indicator & stratagy developer logo
TradingView indicator & stratagy developer

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:

pine
1//@version=4 2strategy("Aggressive Dual EMA and MACD Convergence Strategy", overlay=true) 3 4// Define MACD Settings 5shortTermFastLength = 5 6shortTermSlowLength = 35 7shortTermSignalSmoothing = 5 8 9// Calculate MACD and Signal Line 10macd = ema(close, shortTermFastLength) - ema(close, shortTermSlowLength) 11signal = ema(macd, shortTermSignalSmoothing) 12 13// Define EMA Settings 14emaLength1 = 9 // First EMA Length 15emaValue1 = ema(close, emaLength1) 16 17emaLength2 = 21 // Second EMA Length 18emaValue2 = ema(close, emaLength2) 19 20// Entry Conditions 21longCondition = crossover(emaValue1, emaValue2) and crossover(macd, signal) 22if (longCondition) 23 strategy.entry("Long", strategy.long) 24 25// Exit Conditions 26shortCondition = crossunder(emaValue1, emaValue2) or crossunder(macd, signal) 27if (shortCondition) 28 strategy.close("Long") 29 30// Plot the MACD, Signal Line, and EMAs for Visualization 31plot(macd, title="MACD Line", color=color.blue) 32plot(signal, title="Signal Line", color=color.orange) 33plot(emaValue1, title="EMA 9", color=color.red) 34plot(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.

Anonymous

Add a second 21 Ema , I want a signal when the Emas cross and when the sentiment agrees with the macd

TradingView indicator & stratagy developer logo
TradingView indicator & stratagy developer

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:

pine
1//@version=4 2strategy("Dual EMA and MACD Convergence Strategy", overlay=true) 3 4// Define MACD Settings 5shortTermFastLength = 5 6shortTermSlowLength = 35 7shortTermSignalSmoothing = 5 8 9// Calculate MACD and Signal Line 10macd = ema(close, shortTermFastLength) - ema(close, shortTermSlowLength) 11signal = ema(macd, shortTermSignalSmoothing) 12 13// Define EMA Settings 14emaLength1 = 9 // First EMA Length 15emaValue1 = ema(close, emaLength1) 16 17emaLength2 = 21 // Second EMA Length 18emaValue2 = ema(close, emaLength2) 19 20// Entry Conditions 21longCondition = crossover(emaValue1, emaValue2) and crossover(macd, signal) 22shortCondition = crossunder(emaValue1, emaValue2) and crossunder(macd, signal) 23 24// Implementing Strategy 25if (longCondition) 26 strategy.entry("Long", strategy.long) 27 28if (shortCondition) 29 strategy.entry("Short", strategy.short) 30 31// Plotting for Visualization 32plot(macd, title="MACD Line", color=color.blue) 33plot(signal, title="Signal Line", color=color.orange) 34plot(emaValue1, title="EMA 9", color=color.red) 35plot(emaValue2, title="EMA 21", color=color.green)

In this code:

  • A long position is entered when the 9-period EMA crosses above the 21-period EMA and the MACD line crosses above its signal line.
  • A short position is entered when the 9-period EMA crosses below the 21-period EMA and the MACD line crosses below its signal line.

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.