This script is designed for the MetaTrader platform and aims to automate trading decisions on the H4 (4-hour) timeframe. It opens and closes buy/sell orders (trades) based on several technical indicators, specifically moving averages and the Average Directional Index (ADX). Here's a breakdown:
1. Initial Setup & Input Parameters:
- External Parameters (User-Adjustable): The script starts with a section defining settings that traders can modify. These settings control how the script behaves, allowing for customization.
dProfit
: Target profit in money terms (e.g., dollars) at which to close a trade.maPerBig
,ModeBig
,appPriceBig
: These three parameters define the settings for a longer-term Moving Average. They control the period (number of bars used in calculation), the type of Moving Average (Simple, Exponential, etc.), and the price used in the calculation (e.g., closing price).maPerSmal
,ModeSmal
,appPriceSmal
: These three parameters define the settings for a shorter-term Moving Average, similar to the longer-term Moving Average, but with different values.maDif
: Specifies the minimum distance to consider it a change when comparing between short term and long term MA.advPeriod
,appPriceAdx
,advLevelMa
,advLevelPl
,advLevelMi
: These relate to the ADX indicator.advPeriod
sets the ADX calculation period.appPriceAdx
specifies the price to use (e.g. typical price). TheadvLevel
parameters set threshold values for the ADX main line, +DI, and -DI indicators, used to determine trend strength and direction.TakeProfit
: Target profit in points (a unit of price movement) for new trades.UseMM
,PercentMM
,lots
: Parameters that control the lot size (trade size). IfUseMM
is true, the script uses a percentage of the account's free margin to determine the lot size. Otherwise, it uses the fixedlots
value.
MagNum
: A "magic number" used to identify orders opened by this specific script, so it doesn't interfere with other trades.
2. Script Execution (The start()
Function):
- Check for Existing Trades: The script first checks if there are already open orders for the current currency pair and magic number. If it finds existing orders, it sets a flag (
Action1
) tofalse
, preventing the script from opening new trades. - Indicator Calculations: It calculates the values of the Moving Averages (longer and shorter term) and the ADX indicator. It gets the current values and the values from the previous period for each indicator.
- Trading Logic (Opening Orders): This is where the script decides whether to open a new trade:
- Buy Condition: The script checks several conditions. It checks if the current price is above the longer-term moving average, if the price dropped but started going back up above certain threshold, if the short-term MA is above the longer-term MA, and if the ADX indicates a strengthening upward trend (+DI is increasing and above a certain level, -DI is decreasing and below a certain level, ADX is increasing and above a certain level). If all these conditions are met, it places a buy order.
- Sell Condition: It checks several conditions similar to the buy condition but reversed for a downward trend. If all the sell conditions are met, it places a sell order.
- The script calculates the lot size based on the user-defined settings.
- If a trade is successfully opened, it prints a message indicating the type of order (buy/sell) and the opening price.
- Trading Logic (Closing Orders): The script also checks if existing open orders should be closed:
- Profit Target: It iterates through all open orders with the magic number assigned to this script. If an open order's profit (including swap fees) exceeds the
dProfit
value, the script closes the trade.
- Profit Target: It iterates through all open orders with the magic number assigned to this script. If an open order's profit (including swap fees) exceeds the
- Order Management: The script uses functions like
OrderSend
(to open trades) andOrderClose
(to close trades) within the MetaTrader platform.
In simple terms, this script automates a trading strategy based on the combination of Moving Averages and the ADX indicator. It looks for specific price patterns and trend confirmations to open and close trades automatically.
/*-------------------------------------------------------------------+
| MADX-07.mq4 |
| Copyright © 2013, basisforex@gmail.com |
+-------------------------------------------------------------------*/// on the H4 TF
extern double dProfit = 13;
//-----
extern int maPerBig = 25;
extern int ModeBig = 2;//MODE_SMA=0; MODE_EMA=1; MODE_SMMA=2; MODE_LWMA=3;
extern int appPriceBig = 2;//PRICE_CLOSE=0; PRICE_OPEN=1; PRICE_HIGH=2; PRICE_LOW=3; PRICE_MEDIAN=4; PRICE_TYPICAL=5; PRICE_WEIGHTED=6;
//-----
extern int maPerSmal = 5;
extern int ModeSmal = 1;//MODE_SMA=0; MODE_EMA=1; MODE_SMMA=2; MODE_LWMA=3;
extern int appPriceSmal = 0;//PRICE_CLOSE=0; PRICE_OPEN=1; PRICE_HIGH=2; PRICE_LOW=3; PRICE_MEDIAN=4; PRICE_TYPICAL=5; PRICE_WEIGHTED=6;
//-----
extern int maDif = 5;
//-----
extern int advPeriod = 11;
extern int appPriceAdx = 4;//PRICE_CLOSE=0; PRICE_OPEN=1; PRICE_HIGH=2; PRICE_LOW=3; PRICE_MEDIAN=4; PRICE_TYPICAL=5; PRICE_WEIGHTED=6;
extern int advLevelMa = 13;
extern int advLevelPl = 13;
extern int advLevelMi = 14;
//-----
extern int TakeProfit = 299;
extern bool UseMM = false;
extern int PercentMM = 1;
extern double lots = 0.1;
//-----
int MagNum = 10071;
bool Action1;
//+------------------------------------------------------------------+
int Get_Broker_Digit()
{
if(Digits == 5 || Digits == 3)
{
return(10);
}
else
{
return(1);
}
}
//+------------------------------------------------------------------+
double GetLots()
{
if (UseMM)
{
double a;
a = NormalizeDouble((PercentMM * AccountFreeMargin() / 100000), 1);
if(a > 49.9) return(49.9);
else if(a < 0.1)
{
Print("Lots < 0.1");
return(0);
}
else return(a);
}
else return(lots);
}
//+------------------------------------------------------------------+
int start()
{
int totT = OrdersTotal();
Action1 = true;
if (totT > 0)
{
for(int i = 0; i < totT; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagNum)
{
Action1 = false;
break;
}
else
{
Action1 = true;
}
}
}
//-----------------------------------------------
double main0, plus0, minus0, main1, plus1, minus1;
double stMain, stSign;
double maBigL0, maBigL1, maSmalL0, maSmalL1;
int cnt, ticket, total, d;
//==========================================================
maBigL0 = iMA(NULL, 0, maPerBig, 0, ModeBig, appPriceBig, 0);
maBigL1 = iMA(NULL, 0, maPerBig, 0, ModeBig, appPriceBig, 1);
maSmalL0 = iMA(NULL, 0, maPerSmal, 0, ModeSmal, appPriceSmal, 0);
maSmalL1 = iMA(NULL, 0, maPerSmal, 0, ModeSmal, appPriceSmal, 1);
//...............................................................
main0 = iADX(NULL, 0, advPeriod, appPriceAdx, MODE_MAIN, 0);
plus0 = iADX(NULL, 0, advPeriod, appPriceAdx, MODE_PLUSDI, 0);
minus0 = iADX(NULL, 0, advPeriod, appPriceAdx, MODE_MINUSDI, 0);
main1 = iADX(NULL, 0, advPeriod, appPriceAdx, MODE_MAIN, 1);
plus1 = iADX(NULL, 0, advPeriod, appPriceAdx, MODE_PLUSDI, 1);
minus1 = iADX(NULL, 0, advPeriod, appPriceAdx, MODE_MINUSDI, 1);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
d = Get_Broker_Digit();
if(Action1 == true)
{//=============================================================================================================== BUY =============
if(Bid > maBigL0 && Low[1] - maSmalL1 > maDif * Point * d && Low[0] - maSmalL0 > maDif * Point * d && maSmalL0 > maBigL0 &&
main0 > main1 && main0 > advLevelMa && plus0 > plus1 && plus0 > advLevelPl && minus0 < minus1 && minus0 < advLevelMi)
{
ticket = OrderSend(Symbol(), OP_BUY, GetLots(), Ask, 3 * d, 0, Ask + TakeProfit * Point * d, "xax", MagNum, 0, Blue);
if(ticket > 0)
{
if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ", GetLastError());
return(0);
}//============================================================================================================== SELL ============
if(Ask < maBigL0 && maSmalL1 - High[1] > maDif * Point && maSmalL0 - High[0] > maDif * Point && maSmalL0 < maBigL0 &&
main0 > main1 && main0 > advLevelMa && minus0 > minus1 && minus0 > advLevelMi && plus0 < plus1 && plus0 < advLevelPl)
{
ticket = OrderSend(Symbol(), OP_SELL, GetLots(), Bid, 3 * d, 0, Bid - TakeProfit * Point * d, "xax", MagNum, 0, Blue);
if(ticket > 0)
{
if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ", GetLastError());
return(0);
}
return(0);
}
//===============================================================================================================
total = OrdersTotal();
for(cnt = 0; cnt < total; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber() == MagNum)
{
if(OrderType() == OP_BUY)
{
if(OrderProfit() + OrderSwap() > dProfit)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 3 * d, Violet);
return(0);
}
}
else if(OrderType() == OP_SELL)
{
if(OrderProfit() + OrderSwap() > dProfit)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 3 * d, Violet);
return(0);
}
}
}
}
return(0);
}
Comments