This script is designed to automatically trade on the MetaTrader platform. Here?s how it works, broken down step-by-step:
-
Setup: The script starts by defining a few adjustable settings:
Period_MA_Long
: This determines the period for calculating a long-term moving average. Think of this as the number of past days used to calculate an average price.Period_BB
: This sets the period for calculating Bollinger Bands, which are used to measure market volatility. It represents the number of past days used to determine the standard deviationreserve
: A parameter to set reserve from bollinger bands used to set the take profit valuesdeviation
: A parameter for the calculation of the bollinger bandsLots
: The size of the trades the script will make.
-
Safety Checks: It then performs a series of checks to avoid errors:
- It makes sure there is enough historical price data available (at least as much as the settings for the moving average and Bollinger Bands require).
- It verifies that the market is active and trading volume is present.
- It confirms there's enough money in the trading account to place a trade of the specified size.
-
Trend Identification: The script calculates several technical indicators to understand the current market trend. These are:
- Long-term Moving Averages: It calculates two moving averages on daily data. The main comparison is between a more recent day and a past day, based on the
Period_MA_Long
setting, in order to evaluate the general direction of the trend - Bollinger Bands: It calculates the upper, middle and lower bands. These bands widen or narrow depending on how volatile the price is.
- Long-term Moving Averages: It calculates two moving averages on daily data. The main comparison is between a more recent day and a past day, based on the
-
Position Management: The script then checks if there are any existing open trades. If there are, it loops through them to consider if the trades must be closed
- Closing Existing Trades: If the current price action suggests the trend has reversed (specifically if the price crosses the middle Bollinger Band), the script will automatically close the position to preserve profits or limit losses.
-
Opening New Trades: If there are no open positions, the script evaluates the opportunity to open a new one based on the following logic:
- Buy Condition: It looks for a situation where the price has recently broken above the upper Bollinger Band and where the Moving Average is trending upwards. If both conditions are met, it opens a "buy" order, betting that the price will continue to rise. The script sets a "take profit" level when opening the position
- Sell Condition: Conversely, it looks for a situation where the price has recently broken below the lower Bollinger Band and where the Moving Average is trending downwards. If both conditions are met, it opens a "sell" order, betting that the price will continue to fall. The script sets a "take profit" level when opening the position
In essence, this script tries to identify potential buying or selling opportunities based on price breakouts and trend direction. It manages risk by closing trades when the trend appears to reverse, and it only opens new trades when certain criteria are met, based on the calculated technical indicators.
extern int Period_MA_Long = 100;// Ïåðèîä ñðåäíåé, íåîáõîäèìîé äëÿ îïðåäåëåíèÿ òðåíäà
extern int Period_BB = 25; // Ïåðèîä ñðåäíåé áîëèíäæåðà
extern double reserve=50;
extern double deviation = 1.5;
extern double Lots = 0.1; // Âåëè÷èíà ëîòà
int tmp, tc, st, et, dtmp;
//----------------------------------------------------------------------------------------------------------//
int start()
{
double MA_1_Long,MA_2_Long,BB_MA,BB_n_MA,BB_Up,BB_Low,ATR;
int cnt, total;
//------------------------------------------Çàùèòà îò îøèáîê è êîñÿêîâ--------------------------------------//
if(Bars<Period_MA_Long || Bars<Period_BB) // Åñëè êîëè÷åñòâî ñâå÷åé ìåíåå 100
{
Print("Êîëè÷åñòâî áàðîâ ñëèøêîì ìàëî");
return(0);
}
if (Volume[0] < 1.0) // Åñëè òîðãè èäóò çíà÷èò îáüåì áîëåå 0
{
Comment("Òîðãîâ íåò.. Æäåì íîâûé áàð..");
return(0);
}
if (AccountFreeMargin()<(1000*Lots))
{
Comment("Íåäîñòàòî÷íî äåíåã. Óðîâåíü ìàðæè ìåíåå ...");
return(0);
}
//-----------------------------------------Êîíåö çàùèòû îò êîñÿêîâ-----------------------------------------//
MA_1_Long=iMA(NULL,PERIOD_D1,Period_MA_Long,0,MODE_SMA,PRICE_CLOSE,1);
MA_2_Long=iMA(NULL,PERIOD_D1,Period_MA_Long,0,MODE_SMA,PRICE_CLOSE,4);
BB_MA=iCustom(NULL,0,"Bands",Period_BB,0,deviation,0,1);
BB_n_MA=iCustom(NULL,0,"Bands",Period_BB,0,deviation,0,4);
BB_Up=iCustom(NULL,0,"Bands",Period_BB,0,deviation,1,1);
BB_Low=iCustom(NULL,0,"Bands",Period_BB,0,deviation,2,1);
total=OrdersTotal();
//-------------------------------------------Çàêðûòèå ïîçèöèé------------------------------------------
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if (Close[1]<BB_MA)
{
OrderClose(OrderTicket(),OrderLots(),Bid,50,Violet);
return(0);
}
}
if(OrderType()==OP_SELL)
{
if (Close[1]>BB_MA)
{
OrderClose(OrderTicket(),OrderLots(),Ask,50,Violet);
return(0);
}
}
}
}
//-----------------------------------------------Êîíåö çàêðûòèÿ ïîçèöèè------------------------------------
if(total==0) // åñëè íåò ïîçèöèé
{
// îòêðûòèå äëèííîé ïîçèöèè
if(Close[2]<BB_Up && Close[1]>BB_Up && MA_1_Long>MA_2_Long)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,50,BB_Low-reserve*Point,0,"Äëèííàÿ ïîçèöèÿ",16384,0,Green);
return(0);
}
// îòêðûòèå êîðîòêîé ïîçèöèè
if(Close[2]>BB_Low && Close[1]<BB_Low && MA_1_Long<MA_2_Long)
{
OrderSend(Symbol(),OP_SELL,Lots,Bid,50,BB_Up+reserve*Point,0,"Êîðîòêàÿ ïîçèöèÿ",16384,0,Red);
return(0);
}
}
return(0);
}
Comments