This script is designed to automatically trade on the Forex market using the MetaTrader platform. It analyzes price trends using two technical indicators: MACD and ADX, and then it automatically buys or sells currency pairs based on specific patterns these indicators create.
Here's a simplified breakdown:
-
Initialization: When the script starts, it sets up some initial parameters defined by the user such as risk tolerance and the amount of funds to allocate per trade.
-
Data Collection: The script constantly monitors the market, specifically gathering information from two key indicators:
- MACD (Moving Average Convergence Divergence): This indicator helps identify potential buy or sell signals based on the relationship between two moving averages of the price. It essentially highlights when momentum is shifting in the market.
- ADX (Average Directional Index): This indicator measures the strength of a trend, telling the script if the market is trending strongly enough to warrant a trade. It also uses PlusDI and MinusDI to know the direction of the trend (up or down).
-
Decision Making (Trading Logic): The core of the script lies in its decision-making process. It checks the values of MACD, ADX, PlusDI, and MinusDI against pre-defined levels and looks for specific combinations.
- Buy Condition: The script will initiate a buy order (betting the price will go up) if:
- The ADX is strong and strengthening.
- The PlusDI is above MinusDI which mean price are trending upward
- The MACD indicator suggests a buying opportunity.
- Sell Condition: Conversely, the script will initiate a sell order (betting the price will go down) if:
- The ADX is strong and strengthening.
- The PlusDI is below MinusDI which mean price are trending downward
- The MACD indicator suggests a selling opportunity.
- Buy Condition: The script will initiate a buy order (betting the price will go up) if:
-
Order Management: Before entering any trades, the script checks if there are already open positions. It check the actual value of the MACD indicator to take decision to close or hold the order. If there is an open BUY order and the indicator MACD signals an downtrend it will close the BUY order. The same logic applies for SELL orders. After entering a trade, the script automatically sets a "Stop Loss" to limit potential losses.
-
Time Delay: To avoid over-trading, the script includes a time delay that prevents it from executing new trades too frequently.
In essence, this script is an automated system that aims to identify and capitalize on trending market conditions based on the signals generated by the MACD and ADX indicators. It's important to understand that, like any automated trading system, it carries risk, and its performance depends heavily on the chosen parameters and market conditions.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| macd_adx.mq4 |
//| emsjoflo |
//+------------------------------------------------------------------+
#property copyright "emsjoflo"
//If you make money with this expert, please let me know emsjoflo@yaho.com
// I also accept donations ;)
//It is only a bare-bones expert with no safeties. Let me know if you want me to modify it for your purposes.
//---- input parameters
extern int DMILevels=25;
extern int ADXLevel=15;
extern double lots=0.1;
extern int StopLoss=60 ;
extern int Slippage=4;
//I'm not sure where the best place to define variables is...so I put them here and it works
double macd,macdsig,ADX,PlusDI,MinusDI,ADXpast,PlusDIpast,MinusDIpast,SL;
int i, buys, sells;
datetime lasttradetime;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//get moving average info
macd=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
macdsig=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
ADX=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1);
PlusDI=iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,1);
MinusDI=iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,1);
ADXpast=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,2);
PlusDIpast=iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,2);
MinusDIpast=iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,2);
//check for open orders first
if (OrdersTotal()>0)
{
buys=0;
sells=0;
for (i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS);
if (OrderSymbol()==Symbol())
{
if (OrderType()== OP_BUY)
{
if (macd > 0 && macdsig > 0 && macd < macdsig) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Orange);
else buys++;
}
if (OrderType()== OP_SELL)
{
if (macd < 0 && macdsig < 0 && macd > macdsig) OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Lime);
else sells++;
}
}
}
}
if (ADX > ADXpast && ADX> ADXLevel && PlusDI > MinusDI && PlusDI > PlusDIpast && PlusDI > DMILevels && macd < 0 && macdsig < 0 && macd > macdsig && buys == 0 && (CurTime()-lasttradetime)/60 > Period())
{
//Print("Buy condition");
if (StopLoss >1) SL=Ask-StopLoss*Point;
OrderSend(Symbol(),OP_BUY,lots,Ask,Slippage,SL,0,"macd_adx",123,0,Lime);
lasttradetime=CurTime();
}
if (ADX > ADXpast && ADX> ADXLevel && PlusDI < MinusDI && MinusDI > MinusDIpast && MinusDI > DMILevels && macd > 0 && macdsig > 0 && macd < macdsig && sells ==0 && (CurTime()-lasttradetime)/60 > Period())
{
//Print ("Sell condition");
if (StopLoss>1) SL=Bid+StopLoss*Point;
OrderSend(Symbol(),OP_SELL,lots,Bid,Slippage,SL,0,"macd_adx",123,0,Red);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments