This script is designed to automate trading on the MetaTrader platform based on certain technical indicators. Here's a breakdown of how it works:
Overall Strategy:
The script tries to identify potential buying and selling opportunities based on the relative positions of a few custom indicators and moving averages. It essentially attempts to buy when prices are low relative to a calculated range, and sell when prices are high.
Key Components:
-
Initialization: When the script starts, it sets up some initial parameters (variables). These parameters act like settings for the trading strategy, such as:
period,period1,period2: These determine how far back in time certain indicator calculations look (think of it like the "sensitivity" of the indicators).stopmaxandstopmin: These define the maximum and minimum range to control the size of the automatic stop-loss used to protect the trade.HoursandHours1: Defines after how much time trades are closed.lot: The amount of currency to trade in each transaction.magicBUY,magicSELL,magicBUY1,magicSELL1: Unique identification numbers for each type of trade the script places. This helps the script manage its own orders without interfering with manually placed trades or other automated strategies.
-
Data Collection: The script regularly checks the current market conditions and indicator values. It calculates:
MAXandMIN: Highest and lowest values calculated using a custom indicator named "trend_v3_5." This likely defines an upper and lower boundary or channel.H: Half the distance betweenMAXandMIN.A0,A1,A2: Values from other custom indicators ("trend_v3" and "trend_v2"). These are used as triggers for initiating buy or sell orders based on how they relate toMAXandMIN.
-
Trading Logic (Buy Orders):
- The script checks if a "buy" order of type "magicBUY" is already open. If not, it looks for a situation where indicator
A0is below theMINline ANDA1is greater than or equal to zero. - If both conditions are met, it places a buy order.
- The
stopvariable is calculated based onH, the distance between MAX and MIN, acting as a stop loss. - The script also includes a similar logic block for opening buy orders of type "magicBUY1", using
A0,MAX, andA2with different rules, opening trades only ifA0is above theMAXline ANDA2is less than or equal toMAX. - For both the script checks if the current order is older than a set amount of hours ("Hours" and "Hours1"). If it is and profitable, the script closes it.
- The script checks if a "buy" order of type "magicBUY" is already open. If not, it looks for a situation where indicator
-
Trading Logic (Sell Orders):
- The script checks if a "sell" order of type "magicSELL" is already open. If not, it checks if
A0is above theMAXline ANDA1is less than or equal to zero. - If these conditions are met, it places a sell order.
- The
stopvariable is calculated based onH, the distance between MAX and MIN, acting as a stop loss. - Similar to the buy orders, the script has another logic block for opening sell orders of type "magicSELL1", using
A0,MIN, andA2with the ruleA0being below theMINline andA2being greater than or equal toMIN. - For both the script checks if the current order is older than a set amount of hours ("Hours" and "Hours1"). If it is and profitable, the script closes it.
- The script checks if a "sell" order of type "magicSELL" is already open. If not, it checks if
-
Order Management:
- The script keeps track of the orders it has placed using the "magic number".
- After a set amount of hours has passed since opening a trade, the script check if it is profitable. If so, the trade gets closed.
In Summary: This script is an automated trading system that uses custom indicators to find potential buy and sell signals, places orders, and manages them by closing them automatically after a specific duration if in profit.
//+------------------------------------------------------------------+
//| canal-m_a_sim-v2.mq4 |
//| m_a_sim@mail.ru - Ñèìàêîâ Ìèõàèë|
//| http://www.mql4.com/ru/users/m_a_sim |
//+------------------------------------------------------------------+
#property copyright "m_a_sim@mail.ru - Ñèìàêîâ Ìèõàèë"
#property link "http://www.mql4.com/ru/users/m_a_sim"
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
extern int period =200;
extern int period1 =6;
extern int period2 =6;
extern int stopmax=200;
extern int stopmin=100;
extern int Hours=72;
extern int Hours1=72;
extern double lot=0.1;
extern int magicBUY=101;
extern int magicSELL=201;
extern int magicBUY1=102;
extern int magicSELL1=202;
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int bar;
double MAX,MIN,H,A0,A1,A2;
int start()
{
//----
int i, j,jj, k, g, q, ticket,l;
double stop;
if (bar!=Bars ){
MAX=iCustom(NULL,0,"trend_v3_5",period,1,1);
MIN=iCustom(NULL,0,"trend_v3_5",period,2,1);
H=(MAX-MIN)/2;
A0=iCustom(NULL,0,"trend_v3",period1,0,1);
A2=iCustom(NULL,0,"trend_v3",period1,0,2);
A1=iCustom(NULL,0,"trend_v2",period2,0,1);
bar=Bars;
}
//BUY=====================================
jj=0;
if (OrdersTotal()>0){
for(i=0;i<OrdersTotal();i++){
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==magicBUY){jj=1;}
}}
if (jj==0 ){
if (A0<MIN && A1>=0){
stop=H;
if (H>stopmax*Point){stop=stopmax*Point;}
if (H<stopmin*Point){stop=stopmin*Point;}
ticket=OrderSend(Symbol(),OP_BUY ,lot,NormalizeDouble(Ask, Digits),5,Bid-stop,0," ",magicBUY,0, Blue );
}}
if (OrdersTotal()>0){
for(i=0;i<OrdersTotal();i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==magicBUY){
if ( Time[0]-OrderOpenTime()>Hours*60*60 ) {
if (OrderOpenPrice()<Bid ){OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid, Digits),5,Violet); }
}}}}
jj=0;
if (OrdersTotal()>0){
for(i=0;i<OrdersTotal();i++){
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==magicBUY1){jj=1;}
}}
if (jj==0 ){
if (A0>MAX && A2<=MAX){
stop=H;
if (H>stopmax*Point){stop=stopmax*Point;}
if (H<stopmin*Point){stop=stopmin*Point;}
ticket=OrderSend(Symbol(),OP_BUY ,lot,NormalizeDouble(Ask, Digits),5,Bid-stop,0," ",magicBUY1,0, Blue );
}}
if (OrdersTotal()>0){
for(i=0;i<OrdersTotal();i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==magicBUY1){
if ( Time[0]-OrderOpenTime()>Hours1*60*60 ) {
if (OrderOpenPrice()<Bid ){OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid, Digits),5,Violet); }
}}}}
//SELL================================
jj=0;
if (OrdersTotal()>0){
for(i=0;i<OrdersTotal();i++){
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==magicSELL){jj=1;}
}}
if (jj==0 ){
if (A0>MAX && A1<=0){
stop=H;
if (H>stopmax*Point){stop=stopmax*Point;}
if (H<stopmin*Point){stop=stopmin*Point;}
ticket=OrderSend(Symbol(),OP_SELL ,lot,NormalizeDouble(Bid, Digits),5,Ask+stop,0," ",magicSELL,0, Blue );
}}
if (OrdersTotal()>0){
for(i=0;i<OrdersTotal();i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==magicSELL){
if ( Time[0]-OrderOpenTime()>Hours*60*60 ) {
if (OrderOpenPrice()>Ask ){OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask, Digits),5,Violet); }
}}}}
//----
jj=0;
if (OrdersTotal()>0){
for(i=0;i<OrdersTotal();i++){
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==magicSELL1){jj=1;}
}}
if (jj==0 ){
if (A0<MIN && A2>=MIN){
stop=H;
if (H>stopmax*Point){stop=stopmax*Point;}
if (H<stopmin*Point){stop=stopmin*Point;}
ticket=OrderSend(Symbol(),OP_SELL ,lot,NormalizeDouble(Bid, Digits),5,Ask+stop,0," ",magicSELL1,0, Blue );
}}
if (OrdersTotal()>0){
for(i=0;i<OrdersTotal();i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()==magicSELL1){
if ( Time[0]-OrderOpenTime()>Hours1*60*60 ) {
if (OrderOpenPrice()>Ask ){OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask, Digits),5,Violet); }
}}}}
return(0);
}
//+------------------------------------------------------------------+
Comments