This script is an automated trading system for the MetaTrader platform. Here's how it works:
Overall Strategy:
The script aims to identify potential buying or selling opportunities based on the relationship between two moving averages (a "fast" one and a "slow" one) and a momentum indicator. Think of it as trying to catch trends early by looking for confirmation signals from these different indicators.
Key Components and their Logic:
-
Input Parameters (Customizable Settings):
FastPeriod
,SlowPeriod
,MomPeriod
: These are numbers that determine how far back the script looks when calculating the moving averages and the momentum. Shorter periods make the indicators more sensitive to recent price changes, while longer periods smooth out the data.MomFilter
: A threshold for the momentum. The momentum needs to be above this level to consider a buy and below the negative of this level to consider a sell. It acts as a filter to avoid trades based on weak momentum.PercentCapital
,Lots
: Determine the size of the trades.Lots
specifies a fixed size but this script does not use thePercentCapital
to change the lot size.Slippage
: The maximum acceptable difference between the requested price and the actual price when the order is executed. It is a parameter of risk.StopLoss
,TakeProfit
: These define how much the price needs to move against or in favor of the trade before it's automatically closed, limiting potential losses or securing profits.ExpertMagicNumber
: A unique identifier for the script's orders. This allows the script to manage only its own trades and not interfere with other trades.
-
Moving Averages (MAs):
- The script calculates two Exponential Moving Averages (EMAs): a "fast" EMA and a "slow" EMA. The "fast" EMA reacts more quickly to price changes than the "slow" EMA.
- The script compares the current and previous values of the MAs.
-
Momentum Indicator:
- The script calculates the momentum of the price. Momentum is a measure of the speed of price change.
- The script compares the current and previous values of the momentum indicator and uses
MomFilter
to determine if the momentum is strong enough.
-
Trading Logic:
- Buy Signal:
- The script checks if the "fast" EMA is currently above the "slow" EMA, and was below it in the previous period (a "crossover"). This suggests an upward trend.
- It also checks if the momentum is above a certain positive threshold (
MomFilter
) and is increasing (current momentum is greater than previous momentum). This confirms the upward trend. - If both conditions are met, the script closes any existing sell orders and opens a buy order.
- Sell Signal:
- The script checks if the "fast" EMA is currently below the "slow" EMA, and was above it in the previous period (a "crossover"). This suggests a downward trend.
- It also checks if the momentum is below a certain negative threshold (-
MomFilter
) and is decreasing (current momentum is less than previous momentum). This confirms the downward trend. - If both conditions are met, the script closes any existing buy orders and opens a sell order.
- Buy Signal:
-
Order Management
- Before opening a new order, the script closes the opposite order.
In Simple Terms:
The script is like a trend-following robot. It watches the price movements, calculates some averages and momentum, and tries to identify when a new trend is starting. If it thinks the price is going up, it buys. If it thinks the price is going down, it sells. It also includes basic risk management features like stop-loss and take-profit to protect against losses and secure gains.
Profitability Reports
//+------------------------------------------------------------------+
//| Graal-FxProg_team.mq4 |
//| Rosh |
//| http://www.investo.ru/forum/viewtopic.php?t=124777 |
//+------------------------------------------------------------------+
#property copyright "Rosh"
#property link "http://www.investo.ru/forum/viewtopic.php?t=124777"
//---- input parameters
extern int FastPeriod=5;
extern int SlowPeriod=21;
extern int MomPeriod=14;
extern double MomFilter=0.004;
extern double PercentCapital=10.0;
extern double Lots=0.1;
extern int Slippage=3;
extern int StopLoss=10;
extern int TakeProfit=10;
extern int ExpertMagicNumber=2002;
int myBars;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int cnt;
double curFastMA=iMA(NULL,0,FastPeriod,0,MODE_EMA,PRICE_CLOSE,1);
double curSlowMA=iMA(NULL,0,SlowPeriod,0,MODE_EMA,PRICE_OPEN,1);
double prevFastMA=iMA(NULL,0,FastPeriod,0,MODE_EMA,PRICE_CLOSE,2);
double prevSlowMA=iMA(NULL,0,SlowPeriod,0,MODE_EMA,PRICE_OPEN,2);
double curMom=iMomentum(NULL,0,MomPeriod,PRICE_CLOSE,1)-100.0;
double prevMom=iMomentum(NULL,0,MomPeriod,PRICE_CLOSE,2)-100.0;
//----
if (Bars!=myBars)
{
myBars=Bars;
if (curFastMA>curSlowMA && prevFastMA<prevSlowMA && curMom>MomFilter && curMom>prevMom)
{
if (OrdersTotal()>0)
{
for (cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderType()==OP_SELL) {OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White);Sleep(30000);}
}
}
OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,Ask+TakeProfit*Point,"buy",ExpertMagicNumber,0,Blue);
}
if (curFastMA<curSlowMA && prevFastMA>prevSlowMA && curMom<-MomFilter && curMom<prevMom)
{
if (OrdersTotal()>0)
{
for (cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderType()==OP_BUY) {OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White);Sleep(30000);}
}
}
OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,Bid-TakeProfit*Point,"sell",ExpertMagicNumber,0,Red);
}
}
//----
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
---