ema_smma

This script is designed to analyze and compare two different types of moving averages based on current market price data. Its primary goal is to calculate the mathematical "gap" or difference between these two indicators at every point in the historical price chart and export that data into a readable file.

How it Works

The logic follows a structured three-step process:

1. Calculating Two Different Indicators The script looks at two specific technical indicators simultaneously:

  • The SMMA (Smoothed Moving Average): This is calculated using a specific timeframe set by the user (defaulting to 21 periods). It provides a smoother look at price trends by giving more weight to older data.
  • The EMA (Exponential Moving Average): The script automatically doubles the period of the SMMA to create a much larger, longer-term exponential average. This type of average reacts more quickly to recent price changes while still accounting for historical trends.

2. Measuring the Difference For every single candle or "bar" on your trading chart (from the most recent one back to the very beginning of the history), the script performs a subtraction. It takes the value of the SMMA and subtracts the value of the longer EMA. This result tells you how far apart these two trend lines are at any specific moment in time.

3. Data Exporting Instead of just showing these numbers on your screen, the script compiles all these results into a spreadsheet-style format (a CSV file). It records:

  • The position of the bar on the chart.
  • The exact numerical difference between the two averages.

The Final Result

Once the script runs, you will have a data file saved on your computer. This allows you to see a complete history of how these two indicators interacted over time. Traders often use this type of logic to study "divergence"—identifying moments where the gap between short-term and long-term averages widens or narrows, which can help in understanding market momentum.

Indicators Used
Moving average indicator
Miscellaneous
Uses files from the file systemIt writes information to file
12 Views
0 Downloads
0 Favorites
ema_smma
#property show_inputs

extern int _SMMA_PERIOD = 21;

int start()
  {
   int handle = FileOpen( "ema_smma.csv", FILE_CSV | FILE_WRITE, " " );
   for( int i = Bars - 1; i >= 0; i-- )
   {
      double diff = iMA( NULL, 0, _SMMA_PERIOD,       0, MODE_SMMA, PRICE_CLOSE, i ) - 
                    iMA( NULL, 0, 2*_SMMA_PERIOD - 1, 0, MODE_EMA,  PRICE_CLOSE, i ); 
      double diff_n = NormalizeDouble( diff, 8 );              
      FileWrite( handle, "i = ", i, "\t\t difference = ", diff_n );
   }
   FileClose( handle );   
   return(0);
  }

Comments