Okay, here's a breakdown of what this MetaTrader script does, explained in a way that avoids technical jargon and focuses on the overall logic:
This script is like an automated assistant that watches the market and makes decisions about buying and selling a currency pair. It's based on a popular trading strategy using a technical indicator called MACD (Moving Average Convergence Divergence). It also uses Moving Averages.
Here's how it works, step by step:
-
Initial Setup: The script starts by getting some basic information. It finds out the current price of the currency pair and sets up a few rules provided by the user, such as:
- How much profit to aim for on each trade (Take Profit).
- How much of the account to risk on each trade (Lots).
- How far behind the price to move the stop-loss order to protect profits (Trailing Stop).
- MACD levels for open and close orders.
- Period for the moving average indicator.
-
Checking Market Conditions: The core of the script involves monitoring the MACD indicator and a moving average (MA). The MACD helps identify potential buying and selling opportunities by comparing two moving averages of prices. It also looks at a signal line to confirm these opportunities. The moving average looks if the current trend is up or down.
-
Looking for Trading Opportunities: The script constantly checks if the MACD and the moving average line are showing signals that line up with the user-defined rules.
- Buying Signals: It looks for moments when the MACD suggests the price is about to go up, and also if the moving average is also uptrending, then the script considers buying.
- Selling Signals: Conversely, it looks for when the MACD suggests the price is about to go down, and the moving average is also downtrending, signaling a possible selling opportunity.
-
Opening Trades: If the script detects a valid buying or selling opportunity based on the MACD and moving average signals, it automatically places an order to open a trade.
-
Managing Existing Trades: If the script finds no open trades it goes back to step 2. If it finds open trades then, the script constantly monitors any trades that are already open, and decides:
- When to Close (Take Profit): It watches the MACD indicator. If the MACD signals that the price is likely to reverse direction, it automatically closes the trade to secure the profits.
- Trailing Stop: It uses the trailing stop feature. This means that as the price moves in a favorable direction, the stop-loss order (an order to automatically close the trade if the price moves against you) is automatically adjusted to lock in profits.
-
Repeating the Process: The script repeats steps 2-5 continuously, constantly watching the market, looking for new opportunities, and managing existing trades.
In short: This script automates a trading strategy that uses the MACD indicator and a moving average to identify and execute trades, aiming to make profits while managing risk using a take profit and a trailing stop feature.
//+------------------------------------------------------------------+
//| MACD Sample.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double TrailingStop = 30;
extern double MACDOpenLevel=3;
extern double MACDCloseLevel=2;
extern double MATrendPeriod=26;
double Points;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init ()
{
Points = MarketInfo (Symbol(), MODE_POINT);
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double MacdCurrent=0, MacdPrevious=0, SignalCurrent=0;
double SignalPrevious=0, MaCurrent=0, MaPrevious=0;
int cnt=0, total;
// ïåðâè÷íûå ïðîâåðêè äàííûõ
// âàæíî óäîñòîâåðèòüñÿ ÷òî ýêñïåðò ðàáîòàåò íà íîðìàëüíîì ãðàôèêå è
// ïîëüçîâàòåëü ïðàâèëüíî âûñòàâèë âíåøíèå ïåðåìåííûå (Lots, StopLoss,
// TakeProfit, TrailingStop)
// â íàøåì ñëó÷àå ïðîâåðÿåì òîëüêî TakeProfit
if(Bars<100)
{
Print("bars less than 100");
return(0); // íà ãðàôèêå ìåíåå 100 áàðîâ
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // ïðîâåðÿåì TakeProfit
}
// ðàäè óïðîùåíèÿ è óñêîðåíèÿ êîäà, ñîõðàíèì íåîáõîäèìûå
// äàííûå èíäèêàòîðîâ âî âðåìåííûõ ïåðåìåííûõ
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MaCurrent=iMA(NULL,0,MATrendPeriod,MODE_EMA,0,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,MODE_EMA,0,PRICE_CLOSE,1);
// òåïåðü íàäî îïðåäåëèòüñÿ - â êàêîì ñîñòîÿíèè òîðãîâûé òåðìèíàë?
// ïðîâåðèì, åñòü ëè ðàíåå îòêðûòûå ïîçèöèè èëè îðäåðû?
if(OrdersTotal()<1)
{
// íåò íè îäíîãî îòêðûòîãî îðäåðà
// íà âñÿêèé ñëó÷àé ïðîâåðèì, åñëè ó íàñ ñâîáîäíûå äåíüãè íà ñ÷åòó?
// çíà÷åíèå 1000 âçÿòî äëÿ ïðèìåðà, îáû÷íî ìîæíî îòêðûòü 1 ëîò
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money");
return(0); // äåíåã íåò - âûõîäèì
}
// ïðîâåðèì, íå ñëèøêîì ëè ÷àñòî ïûòàåìñÿ îòêðûòüñÿ?
// åñëè ïîñëåäíèé ðàç òîðãîâàëè ìåíåå ÷åì 5 ìèíóò(5*60=300 ñåê)
// íàçàä, òî âûõîäèì
// If((CurTime-LastTradeTime)<300) { Exit }
// ïðîâåðÿåì íà âîçìîæíîñòü âñòàòü â äëèííóþ ïîçèöèþ (BUY)
if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
MathAbs(MacdCurrent)>(MACDOpenLevel*Points) && MaCurrent>MaPrevious)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Points,"macd sample",16384,0,Red); // èñïîëíÿåì
if(GetLastError()==0)Print("Order opened : ",OrderOpenPrice());
return(0); // âûõîäèì, òàê êàê âñå ðàâíî ïîñëå ñîâåðøåíèÿ òîðãîâîé îïåðàöèè
// íàñòóïèë 10-òè ñåêóíäíûé òàéìàóò íà ñîâåðøåíèå òîðãîâûõ îïåðàöèé
}
// ïðîâåðÿåì íà âîçìîæíîñòü âñòàòü â êîðîòêóþ ïîçèöèþ (SELL)
if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDOpenLevel*Points) && MaCurrent<MaPrevious)
{
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Points,"macd sample",16384,0,Red); // èñïîëíÿåì
if(GetLastError()==0)Print("Order opened : ",OrderOpenPrice());
return(0); // âûõîäèì
};
// çäåñü ìû çàâåðøèëè ïðîâåðêó íà âîçìîæíîñòü îòêðûòèÿ íîâûõ ïîçèöèé.
// íîâûå ïîçèöèè îòêðûòû íå áûëè è ïðîñòî âûõîäèì ïî Exit, òàê êàê
// âñå ðàâíî àíàëèçèðîâàòü íå÷åãî
return(0);
};
// ïåðåõîäèì ê âàæíîé ÷àñòè ýêñïåðòà - êîíòðîëþ îòêðûòûõ ïîçèöèé
// 'âàæíî ïðàâèëüíî âîéòè â ðûíîê, íî âûéòè - åùå âàæíåå...'
total=OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // ýòî îòêðûòàÿ ïîçèöèÿ? OP_BUY èëè OP_SELL
OrderSymbol()==Symbol()) // èíñòðóìåíò ñîâïàäàåò?
{
if(OrderType()==OP_BUY) // îòêðûòà äëèííàÿ ïîçèöèÿ
{
// ïðîâåðèì, ìîæåò óæå ïîðà çàêðûâàòüñÿ?
if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDCloseLevel*Points))
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // çàêðûâàåì ïîçèöèþ
return(0); // âûõîäèì
};
// ïðîâåðèì - ìîæåò ìîæíî/íóæíî óæå òðåéëèíã ñòîï ñòàâèòü?
if(TrailingStop>0) // ïîëüçîâàòåëü âûñòàâèë â íàñòðîéêàõ òðåéëèíãñòîï
{ // çíà÷èò ìû èäåì åãî ïðîâåðÿòü
if(Bid-OrderOpenPrice()>Points*TrailingStop)
{
if(OrderStopLoss()<Bid-Points*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Points*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
else // èíà÷å ýòî êîðîòêàÿ ïîçèöèÿ
{
// ïðîâåðèì, ìîæåò óæå ïîðà çàêðûâàòüñÿ?
if(MacdCurrent<0 && MacdCurrent>SignalCurrent &&
MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Points))
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // çàêðûâàåì ïîçèöèþ
return(0); // âûõîäèì
}
// ïðîâåðèì - ìîæåò ìîæíî/íóæíî óæå òðåéëèíã ñòîï ñòàâèòü?
if(TrailingStop>0) // ïîëüçîâàòåëü âûñòàâèë â íàñòðîéêàõ òðåéëèíãñòîï
{ // çíà÷èò ìû èäåì åãî ïðîâåðÿòü
if((OrderOpenPrice()-Ask)>(Points*TrailingStop))
{
if(OrderStopLoss()==0.0 ||
OrderStopLoss()>(Ask+Points*TrailingStop))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Points*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
// the end.
Comments