This automated trading script is designed to manage and execute trades based on a technical analysis strategy known as a Moving Average Crossover. Below is a professional breakdown of the logic used by this tool.
Core Strategy Overview
The script monitors two different Moving Averages (MAs) simultaneously. Moving averages are indicators used to smooth out price data to identify the overall direction of the market trend. By using two different periods (for example, a "fast" moving average and a "slow" moving average), the script can identify specific moments when the momentum of the market shifts.
How the Logic Works
1. Signal Detection (The Crossover) The system constantly calculates the current values of two Moving Averages. It looks for specific "crossovers" to determine when to enter or exit a trade:
- Buy Signal: Triggered when the faster moving average crosses from below to above the slower moving average. This typically suggests that upward momentum is increasing.
- Sell Signal: Triggered when the faster moving average crosses from above to below the slower moving average. This typically suggests that downward momentum is taking over.
2. Trade Execution Rules To ensure organized trading and prevent over-exposure, the script follows strict operational rules:
- New Bar Verification: The script is designed to check for signals only at the start of a new price candle (a "New Bar"). This prevents the system from opening multiple trades or reacting prematurely to minor price fluctuations within a single candle.
- Position Management: The logic includes a check to ensure that it does not open redundant positions. For instance, if a Buy position is already active, it will not open another one.
- Automatic Closures: The script actively monitors existing trades. If the market trend reverses (i.e., a "Sell" crossover occurs while a "Buy" trade is still open), the script will automatically close the current position to protect capital and prepare for the new direction.
3. Customizable Parameters The logic is built to be flexible, allowing users to define several key components without needing to alter the underlying code:
- Moving Average Type: You can choose between different calculation methods (such as Simple, Exponential, Smoothed, or Linear Weighted).
- Price Source: You can decide which price data the moving averages should base their calculations on (e.g., Close price, Open price, High/Low, or Median prices).
- Time Offsets and Periods: Users can define how far back the averages look and apply "shifts" to adjust the timing of the signals.
Summary of Behavior
In short, this script acts as an automated trend-follower. It waits for a specific mathematical confirmation that the market trend has shifted, enters a trade in the direction of that shift at the start of a new candle, and automatically exits the trade if the indicators suggest the trend has reversed.
//============================================================================================
//
//
//
//
//
//============================================================================================
extern int MA1_Period=3; // Ïåðèîä 1-é ÌÀ
extern int MA2_Period=13; // Ïåðèîä 2-é ÌÀ
extern int MA1_Method=0; // Ìåòîä âû÷èñëåíèÿ ÌÀ1 (SMA=0,EMA=1,SMMA=2,LWMA=3)
extern int MA2_Method=3; // Ìåòîä âû÷èñëåíèÿ ÌÀ2 (SMA=0,EMA=1,SMMA=2,LWMA=3)
extern int MA1_Price=0; // Ìåòîä âû÷èñëåíèÿ öåíû ÌÀ1
extern int MA2_Price=4; // Ìåòîä âû÷èñëåíèÿ öåíû ÌÀ2
extern int MA1_Shift=0; // Âðåìåííîé ñäâèã ÌÀ1
extern int MA2_Shift=0; // Âðåìåííîé ñäâèã ÌÀ2
extern double Lot = 0.1; // Ôèêñèðîâàííûé ëîò
extern int slippage = 0; // Îòêëîíåíèå öåíû äëÿ ðûíî÷íûõ îðäåðîâ
int New_Bar; // 0/1 Ôàêò îáðàçîâàíèÿ íîâîãî áàðà
int Time_0; // Âðåìÿ íà÷àëà íîâîãî áàðà
int PosOpen; // Íàïðàâëåíèå ïåðåñå÷åíèÿ
int PosClose; // Íàïðàâëåíèå ïåðåñå÷åíèÿ
int total; // Êîëè÷åñòâî îòêðûòûõ îðäåðîâ
double MA1_0; // Òåêóùåå çíà÷åíèå 1-é ÌÀ (ðîçîâ)
double MA1_1; // Ïðåäûäóùåå çíà÷åíèå 1-é ÌÀ (ðîçîâ)
double MA2_0; // Òåêóùåå çíà÷åíèå 2-é ÌÀ (ãîëóáàÿ)
double MA2_1; // Ïðåäûäóùåå çíà÷åíèå 2-é ÌÀ (ãîëóáàÿ)
int orderBuy; // 1 = ôàêò íàëèèÿ îðäåðà Buy
int orderSell; // 1 = ôàêò íàëèèÿ îðäåðà Sell
//============================================================================================
int init()
{
}
//============================================================================================
int start()
{
orderBuy=0;
orderSell=0;
double price;
int openOrders=0;
int total=OrdersTotal(); // Îáùåå êîëè÷åñòâî îðäåðîâ
for(int i=total-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) // Âûáèðàåì îðäåð
{
if(OrderType()==OP_BUY) // Åñëè ñòîèò îðäåð íà ïîêóïêó
{
orderBuy=1;
if(CrossPositionClose()==1) // Çàêðûâåì îðäåð, åñëè óäîâëåòâîðÿåò
{ // óñëîâèþ CrossPositionClose()=1
price=MarketInfo(Symbol(),MODE_BID);
OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
}
}
if(OrderType()==OP_SELL) // Åñëè ñòîèò îðäåð íà ïîêóïêó
{
orderSell=1;
if(CrossPositionClose()==2) // Çàêðûâåì îðäåð, åñëè óäîâëåòâîðÿåò
{ // óñëîâèþ CrossPositionClose()=2
price=MarketInfo(Symbol(),MODE_ASK);
OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
}
}
}
}
New_Bar=0; // Äëÿ íà÷àëà îáíóëèìñÿ
if (Time_0 != Time[0]) // Åñëè óæå äðóãîå âðåìÿ íà÷àëà áàðà
{
New_Bar= 1; // À âîò è íîâûé áàð
Time_0 = Time[0]; // Çàïîìíèì âðåìÿ íà÷àëà íîâîãî áàðà
}
MA1_0=iMA(NULL,0, MA1_Period, MA1_Shift,MAMethod(MA1_Method), MAPrice(MA1_Price), 0); // Òåêóùåå çíà÷åíèå 1-é ÌÀ
MA1_1=iMA(NULL,0, MA1_Period, MA1_Shift,MAMethod(MA1_Method), MAPrice(MA1_Price), 1); // Ïðåäûäóùåå çíà÷åíèå 1-é ÌÀ
MA2_0=iMA(NULL,0, MA2_Period, MA2_Shift,MAMethod(MA2_Method), MAPrice(MA2_Price), 0); // Òåêóùåå çíà÷åíèå 2-é ÌÀ
MA2_1=iMA(NULL,0, MA2_Period, MA2_Shift,MAMethod(MA2_Method), MAPrice(MA2_Price), 1); // Ïðåäûäóùåå çíà÷åíèå 2-é ÌÀ
if (CrossPositionOpen()==1 && New_Bar==1) // Äâèæåíèå ñíèçó ââåðõ = îòêð. Buy
{
OpenBuy();
}
if (CrossPositionOpen()==2 && New_Bar==1) // Äâèæåíèå ñâåðõó âíèç = îòêð. Sell
{
OpenSell();
}
return;
}
//============================================================================================
int CrossPositionOpen()
{
PosOpen=0; // Âîò ãäå ñîáàêà çàðûòà!!:)
if ((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0)) // Ïåðåñå÷åíèå ñíèçó ââåðõ
{
PosOpen=1;
}
if ((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0)) // Ïåðåñå÷åíèå ñâåðõó âíèç
{
PosOpen=2;
}
return(PosOpen); // Âîçâðàùàåì íàïðàâëåíèå ïåðåñå÷åí.
}
//============================================================================================
int CrossPositionClose()
{
PosClose=0; // Âîò ãäå ñîáàêà çàðûòà!!:)
if ((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0)) // Ïåðåñå÷åíèå ñâåðõó âíèç {
{
PosClose=1;
}
if ((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0)) // Ïåðåñå÷åíèå ñíèçó ââåðõ
{
PosClose=2;
}
return(PosClose); // Âîçâðàùàåì íàïðàâëåíèå ïåðåñå÷åí.
}
//============================================================================================
int OpenBuy()
{
if (total==1)
{
OrderSelect(0, SELECT_BY_POS,MODE_TRADES); // Âûäåëèì îðäåð
if (OrderType()==OP_BUY) return; // Åñëè îí buy, òî íå îòêðûâàåìñÿ
}
OrderSend(Symbol(),OP_BUY, Lot, Ask, slippage, 0, 0, "Buy: MA_cross_Method_PriceMode", 1, 0, CLR_NONE);// Îòêðûâàåìñÿ
return;
}
//============================================================================================
int OpenSell()
{
if (total==1)
{
OrderSelect(0, SELECT_BY_POS,MODE_TRADES); // Âûäåëèì îðäåð
if (OrderType()==OP_SELL) return; // Åñëè îí sell, òî íå îòêðûâàåìñÿ
}
OrderSend(Symbol(),OP_SELL, Lot, Bid, slippage, 0, 0, "Sell: MA_cross_Method_PriceMode", 2, 0, CLR_NONE);
return;
}
//============================================================================================
int MAMethod(int MA_Method)
{
switch(MA_Method)
{
case 0: return(0); // Âîçâðàùàåò MODE_SMA=0
case 1: return(1); // Âîçâðàùàåò MODE_EMA=1
case 2: return(2); // Âîçâðàùàåò MODE_SMMA=2
case 3: return(3); // Âîçâðàùàåò MODE_LWMA=3
}
}
//============================================================================================
int MAPrice(int MA_Price)
{
switch(MA_Price)
{
case 0: return(PRICE_CLOSE); // Âîçâðàùàåò PRICE_CLOSE=0
case 1: return(PRICE_OPEN); // Âîçâðàùàåò PRICE_OPEN=1
case 2: return(PRICE_HIGH); // Âîçâðàùàåò PRICE_HIGH=2
case 3: return(PRICE_LOW); // Âîçâðàùàåò PRICE_LOW=3
case 4: return(PRICE_MEDIAN); // Âîçâðàùàåò PRICE_MEDIAN=4
case 5: return(PRICE_TYPICAL); // Âîçâðàùàåò PRICE_TYPICAL=5
case 6: return(PRICE_WEIGHTED); // Âîçâðàùàåò PRICE_WEIGHTED=6
}
}
//============================================================================================
Comments