# Explanation of the Metatrader MQL Script Logic
This script is designed for use with MetaTrader, a popular platform for trading financial markets. The script, named "FX Sniper" by Louw Coetzer, uses technical analysis to provide trade signals based on market data.
## Overview
The primary function of this script is to analyze price movements and generate signals indicating whether the market is likely moving upwards ("LONG") or downwards ("SHORT"). It does so using a custom indicator called "Ergodic CCI" (Commodity Channel Index).
### Key Components
1. **User Options:**
- **Email Notifications:** Users can choose to receive email alerts when a signal changes.
- **Sound Alerts:** Users can enable sound notifications for signal changes.
2. **Parameters:**
- `pq`, `pr`, and `ps`: These parameters control the periods used in exponential moving averages (EMAs) calculations, which are crucial for determining market trends.
- `trigger`: This parameter sets a threshold for generating signals.
3. **Buffers:**
- The script uses multiple buffers to store intermediate calculations and final indicator values. These include:
- `mtm[]` and `absmtm[]`: Arrays that calculate the difference between consecutive closing prices.
- `var1[]`, `var2[]`, `var2a[]`, `var2b[]`: Buffers for storing EMA results used in further calculations.
4. **Indicator Calculation:**
- The script calculates two main indicators:
- **ErgoCCI:** A custom indicator derived from EMAs of price movements.
- **MainCCI:** An additional line calculated as an EMA of the ErgoCCI, serving as a trigger for signals.
5. **Signal Generation:**
- The script compares `MainCCI` and `ErgoCCI`. If `MainCCI` is greater than `ErgoCCI`, it generates a "SHORT" signal, indicating a potential downward trend.
- Conversely, if `MainCCI` is less than `ErgoCCI`, it generates a "LONG" signal, suggesting an upward trend.
- If both indicators are equal, the market is considered "NEUTRAL."
6. **Notifications:**
- When a new signal is generated, and it differs from the previous one, the script can send alerts via email or sound notifications, depending on user settings.
### Usage
This script is intended to assist traders in making informed decisions by providing timely signals based on technical analysis. It automates part of the trading process by continuously monitoring market conditions and alerting users to potential opportunities.
### Conclusion
The "FX Sniper" script leverages mathematical calculations to analyze price movements and generate actionable trade signals. By using EMAs and custom indicators, it aims to provide traders with insights into market trends, helping them make better-informed trading decisions.
Price Data Components
Indicators Used
Miscellaneous
0
Views
0
Downloads
0
Favorites
#FX_Sniper's_Ergodic_CCI_Trigger
//+------------------------------------------------------------------+
//| Louw Coetzer aka FX Sniper |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_width1 1
#property indicator_color2 Lime
#property indicator_style2 2
extern bool EmailON=false;
extern bool SoundON=true;
// Ergo Variables
extern int pq =2;
extern int pr = 10;
extern int ps = 5;
extern int trigger =3;
double alertTag;
//---- indicator buffers
string signal;
string lastsignal;
double mtm[];
double absmtm[];
double ErgoCCI[];
double MainCCI[];
double var1[];
double var2[];
double var2a[];
double var2b[];
//double valor1[];
//double valor2[];
//double extvar[];
//double cciSignal[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(8);
//---- drawing settings
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,Lime);
SetIndexStyle(1,DRAW_LINE,STYLE_DOT,1,Lime);
SetIndexBuffer(0,ErgoCCI);
SetIndexLabel(0,"Ergodic CCI");
SetIndexBuffer(1,MainCCI);
SetIndexLabel(1,"Trigger Line");
SetIndexBuffer(2,mtm);
SetIndexBuffer(3,var1);
SetIndexBuffer(4,var2);
SetIndexBuffer(5,absmtm);
SetIndexBuffer(6,var2a);
SetIndexBuffer(7,var2b);
//---- name for DataWindow and indicator subwindow label
//IndicatorShortName("Xard777 ");
//IndicatorDigits(Digits-4);
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Calculations |
//+------------------------------------------------------------------+
int start()
{
int limit;
int i;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- main loop
//---- done
for(i=0; i <= Bars; i++)
{
mtm[i]= Close[i]- Close[i +1];
}
for(i=0; i <= Bars-1; i++)
{
absmtm[i] = MathAbs(mtm[i]);
}
for(i=0; i <= Bars-1; i++)
{
var1[i]= iMAOnArray(mtm,0,pq,0,MODE_EMA,i);
}
for(i=0; i <= Bars-1; i++)
{
var2[i]= iMAOnArray(var1,Bars,pr,0,MODE_EMA,i);
}
for(i=0; i <= Bars-1; i++)
{
var2a[i]= iMAOnArray(absmtm,0,pq,0,MODE_EMA,i);
}
for(i=0; i <= Bars-1; i++)
{
var2b[i]= iMAOnArray(var2a,0,pr,0,MODE_EMA,i);
}
for(i=0; i <= Bars-1; i++)
{
ErgoCCI[i] = (500 * iMAOnArray(var2,0,ps,0,MODE_EMA,i))/(iMAOnArray(var2b,0,ps,0,MODE_EMA,i)); //var2a[i]/var2b[i];
}
for(i=0; i<=Bars; i++)
{
MainCCI[i]=iMAOnArray(ErgoCCI,0,trigger,0,MODE_EMA,i);
}
for(i=0; i<=Bars; i++)
{
if(MainCCI[i] > ErgoCCI[i]){
{signal = "SHORT";}
if(lastsignal != signal && alertTag!=Time[0]){
if (SoundON) Alert("Direction going SHORT ","\n Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
if (EmailON) SendMail("Direction going SHORT ","\n Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
alertTag = Time[0];
lastsignal=signal;
}
}
if (MainCCI[i] < ErgoCCI[i]){
{signal = " LONG";}
if(lastsignal != signal && alertTag!=Time[0]){
if (SoundON) Alert("Direction going LONG ","\n Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
if (EmailON) SendMail("Direction going LONG ","\n Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
alertTag = Time[0];
lastsignal=signal;
}}
if (MainCCI[i] == ErgoCCI[i])
{signal = "NEUTRAL";}
IndicatorShortName(" "+signal+" "+(DoubleToStr(ErgoCCI[i]-MainCCI[i],Digits-4))+" ");
IndicatorDigits(Digits-4);
return(0); }
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---