This script is designed to automate trading on the MetaTrader platform based on moving average crossovers and recent high/low prices.
Here's a breakdown of what it does:
-
Initialization: The script starts by defining variables and adjustable parameters like
perma,maxi_per, andmini_per. These parameters control the sensitivity and timeframes used in calculations, respectively. -
Order Check: It checks if there are any existing buy or sell orders open. It keeps track of open BUY and SELL trades.
-
Moving Average Calculation: It calculates three simple moving averages (SMAs) of closing prices, shifted by one, two, and three periods. These averages smooth out price fluctuations to identify trends.
-
High/Low Price Detection: The script monitors for instances when moving averages cross each other. When the first MA is less then the second MA and the second MA is greater than the third MA, the script flags a potential SELL condition and records the highest price within a specified period (
maxi_per). Conversely, When the first MA is greater then the second MA and the second MA is less than the third MA, the script flags a potential BUY condition and records the lowest price within a specified period (mini_per). These values are saved into themaximumandminimumarrays. -
Order Placement: Based on the moving average crossover and the lack of previous open orders it generates trading signals.
- If the first moving average is less than the second, and the second is greater than the third, and there aren't any open SELL orders, the script will attempt to open a SELL order.
- If the first moving average is greater than the second, and the second is less than the third, and there aren't any open BUY orders, the script will attempt to open a BUY order.
-
Order Modification (Potential Stop Loss): The script iterates through all open orders and attempts to modify their stop loss levels based on the recently detected high or low prices.
- If there's an open SELL order, it checks if the most recent high price is greater than the closing price from the last period. If so, it attempts to adjust the stop loss of the SELL order to the most recent high. If not it reduces the index.
- If there's an open BUY order, it checks if the most recent low price is less than the closing price from the last period. If so, it attempts to adjust the stop loss of the BUY order to the most recent low. If not it reduces the index.
In essence, the script aims to identify potential entry points based on moving average crossovers and then manage risk by potentially setting or adjusting stop-loss orders based on recent price extremes. It continuously monitors and adjusts its actions based on price movements and the presence of existing orders.
//+------------------------------------------------------------------+
//| MA.S.R_Trading_02.mq4 |
//| FORTRADER.RU |
//| http://www.fortrader.ru |
//+------------------------------------------------------------------+
#property copyright "FORTRADER.RU"
#property link "http://www.fortrader.ru"
double maximum[100000];
double minimum[100000];
int l,m,flopen,b,s,total,cnt,flopens;
extern int perma=5;
extern int maxi_per=5;
extern int mini_per=5;
int start()
{
total=OrdersTotal();
b=0;s=0;
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY)
{ b=1;}
if(OrderType()==OP_SELL)
{ s=1;}
}
/*--------------------------------------------------*/
double ma1=iMA(NULL,0,perma,0,MODE_SMA,PRICE_CLOSE,1);
double ma2=iMA(NULL,0,perma,0,MODE_SMA,PRICE_CLOSE,2);
double ma3=iMA(NULL,0,perma,0,MODE_SMA,PRICE_CLOSE,3);
if(ma1<ma2 && ma2>ma3)
{
maximum[m]=High[iHighest(NULL,0,MODE_HIGH,maxi_per,1)];
m++;
}
if(ma1>ma2 && ma2<ma3)
{
minimum[l]=Low[iLowest(NULL,0,MODE_LOW,mini_per,1)];
l++;
}
if(ma1<ma2 && ma2>ma3 && s==0)
{
OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",Green);
}
if(ma1>ma2 && ma2<ma3 && b==0)
{
OrderSend(Symbol(),OP_BUY,0.1,Bid,3,0,0,"",Red);
}
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL)
{
if(maximum[m-1]>Close[1])
{
OrderModify(OrderTicket(),OrderOpenPrice(),maximum[m-1],0,0,Yellow);
}
else{m--;}
}
if(OrderType()==OP_BUY)
{
if(minimum[l-1]<Close[1])
{
OrderModify(OrderTicket(),OrderOpenPrice(),minimum[l-1],0,0,Yellow);
}else{l--;}
}
}
return(0);
}
Comments