Okay, here's a breakdown of what this MetaTrader script does, explained in a way that someone who doesn't code can understand.
Overall Goal:
The script is designed to automatically open and close trades in the Forex market, aiming to profit from price movements. It uses a system based on daily price ranges and a few configurable parameters.
Here's how it works step-by-step:
- Setup and Configuration:
 
- 
The script starts by defining some settings that the user can adjust. These settings control things like:
- 
Lot Size: The amount of currency to trade in each transaction. This determines how much profit or loss you make per pip (a small unit of price change).
 - 
Money Management: Whether or not to automatically adjust the lot size based on the amount of money in the trading account (to manage risk). If enabled, you can set:
- The percentage of your account balance to risk on a single trade.
 - Minimum and maximum lot sizes to ensure the trade sizes stay within acceptable bounds.
 
 - 
Slippage: A tolerance level for the difference between the requested price and the actual price at which a trade is executed.
 - 
Magic Number: A unique identifier that helps the script manage its own trades separately from other trades.
 - 
Hour to Close: The hour of the day when the script will automatically close any open trades.
 
 - 
 
- Daily Calculations:
 
- 
Once a day (when a new day starts), the script calculates the average daily price range. This involves:
- Looking at the highest and lowest prices for the previous 1, 5, 10, and 20 days.
 - Calculating the difference between the high and low for each of those periods (the daily range).
 - Averaging those ranges to get a general idea of how much the price typically moves in a day.
 - It then divides this average range in half.  This half-range value is used to determine:
- The "Trailing Stop": How far the price has to move in your favor before the stop-loss order (described later) starts to follow the price to protect profits.
 - The "Stop Loss": How far the price can move against you before the trade is automatically closed to limit losses.
 
 
 
- Managing Existing Trades:
 
- 
The script constantly checks for existing trades that it has opened (using the "Magic Number" to identify them).
- Trailing Stop Functionality: If a trade is making a profit, the script adjusts the stop-loss order to "trail" behind the price. This means that as the price moves further into profit, the stop-loss order is moved along with it, securing some of those profits.
 - End-of-Day Closing: If it's past the specified "hourtoclose", the script automatically closes any open trades (both buy and sell). This helps to avoid holding positions overnight, which can be riskier.
 
 
- Opening New Trades:
 
- If there are no existing trades open (or if the script has closed the existing trades for the day), the script looks for opportunities to open new trades.
 - Buying and Selling Logic:
- The script calculates a "buying price" and a "selling price" based on the day's high and low prices, adjusted by the calculated half-range value.
 - If the current price is higher than the "buying price", the script opens a "buy" order (hoping the price will continue to rise).
 - If the current price is lower than the "selling price", the script opens a "sell" order (hoping the price will continue to fall).
 - Each trade is opened with a "stop-loss" order to limit potential losses.
 - A limit is put in place to only allow a maximum of 2 trades to be placed
 
 
In Simple Terms:
Imagine the script as a robot trader that:
- Learns how much the price usually moves each day.
 - Waits for the price to break above or below a certain level, based on the day's high/low and the usual price movement.
 - Opens a trade, betting that the price will continue in that direction.
 - If the trade starts making money, it moves the "safety net" (stop-loss) to protect those profits.
 - Closes all trades at a specific time each day.
 
//+------------------------------------------------------------------+
//|                                            FXIgorSystem1.1.mq4   |
//|                                               Diego G. Almeida   |
//|           Magic number and MM added by Project1972               |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
extern double lots= 0.01; // 0.01 is a single micro lot
extern bool MM = false;       // if true the lots size will increase based on account balance 
extern int risk=1;           // percent of account balance to risk on each trade (only if mm is enabled)
extern double MinLots=0.1;   // minimal lots you want to trade or your broker allow (only if mm is enabled)
extern double MAXLots=10;    // max lots you want to trade or your broker allow (only if mm is enabled)               
//extern double takeProfit   = 0;                         
extern double slippage     = 10;
int      MagicNumber = 123787;
extern int hourtoclose = 20; //default 18 Alpari Fibogroup and MIG, 19 Northfinance and FXDD, 17 IBFX
double highday=0,lowday=0,h=0,l=0,media=0,mediacalc=0,pcompra=0,pvenda=0,TrailingStop=0,lotsi=0,stopLoss=0;
int cnt,dia=0,op=0,limitepos=0;
int init() 
{
   return(0);
}
int start()
{
if (MM==true) {{ lotsi=NormalizeDouble(AccountBalance()*risk/100000.0,1); }
if (lotsi<MinLots){ lotsi=MinLots; }
if (lotsi>MAXLots){ lotsi=MAXLots; }
}
else { lotsi=lots; }   
if(AccountFreeMargin() < 1000*lotsi) {
  Print("Not enough money to trade ");
      return(0); }
           
int orders=0,horas=0;
if(Day() != dia)
{
int R1=0,R5=0,R10=0,R20=0,RAvg=0;
int i=0;
   R1 =  (iHigh(NULL,PERIOD_D1,1)-iLow(NULL,PERIOD_D1,1))/Point;
   for(i=1;i<=5;i++)
      R5    =    R5  +  (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
   for(i=1;i<=10;i++)
      R10   =    R10 +  (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
   for(i=1;i<=20;i++)
      R20   =    R20 +  (iHigh(NULL,PERIOD_D1,i)-iLow(NULL,PERIOD_D1,i))/Point;
   R5 = R5/5;
   R10 = R10/10;
   R20 = R20/20;
   RAvg  =  (R1+R5+R10+R20)/4;    
   media=RAvg/2;
   mediacalc=MathCeil(media);
   TrailingStop=mediacalc;
   stopLoss=mediacalc;
   h=0;
   l=0;
   dia=Day();
   op=0;
   limitepos=0;
}
   highday=iHigh(NULL,PERIOD_D1,0);
   lowday=iLow(NULL,PERIOD_D1,0);
if(highday > h)
   h=highday;
if(lowday < l)
   l=lowday;
Print("Trailing stop ",TrailingStop);
Print("Mediacalc ",mediacalc);
horas=hourtoclose - Hour();
int total=OrdersTotal();
  if(total>0)
   { 
   for(cnt=0;cnt<total;cnt++)
   {
      if(OrderSymbol() == Symbol() && OrderType() == OP_BUY)
      {
         op=1;
      }
      if(OrderSymbol() == Symbol() && OrderType() == OP_SELL)
      {
         op=2;
      }
   } 
   for(cnt=0;cnt<total;cnt++)
    {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
      {
      if(TrailingStop > 0) {             
               if(Bid-OrderOpenPrice() > TrailingStop*Point)
                {
                  if(OrderStopLoss() < (Bid - TrailingStop*Point))
                     //if(OrderTakeProfit() == 0)
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,OrderTakeProfit(),0,Blue);
               }
            }
         
      }   else {                              
            if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
            {
            if(TrailingStop > 0) {             
               if(OrderOpenPrice() - Ask > TrailingStop*Point)
                {
                  if(OrderStopLoss() == 0 || OrderStopLoss() > Ask + TrailingStop*Point)
                     //if(OrderTakeProfit() == 0)
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Red);
                }           
            }
          } 
        }
      if(OrderSymbol()==Symbol() && OrderType() == OP_BUY && OrderMagicNumber()==MagicNumber)
      {
         if(Hour() >= hourtoclose && Hour() < 23)
            OrderClose(OrderTicket(),OrderLots(),Bid,3,Lime);
         orders=1;
      }
      if(OrderSymbol()==Symbol() && OrderType() == OP_SELL && OrderMagicNumber()==MagicNumber)
      {
         if(Hour() >= hourtoclose && Hour() < 23)
            OrderClose(OrderTicket(),OrderLots(),Ask,3,Lime);
          orders=1;
      }
    
   
    }
  } 
else
{
   //horas=hourtoclose - Hour();
   if(horas >= 1)
   {
   
      if(orders == 0 && op == 2 && limitepos <2)
      {
         OrderSend(Symbol(),OP_BUY,lotsi,Ask,slippage,Ask-stopLoss*Point,0,"Power:Buy",MagicNumber,0,Lime);
         limitepos+=1;
      }
      if(orders == 0 && op == 1 && limitepos <2)
      {
         OrderSend(Symbol(),OP_SELL,lotsi,Bid,slippage,Bid+stopLoss*Point,0,"Power:Sell",MagicNumber,0,Red);
         limitepos+=1;
      }
   }
}
if(horas >=1)
{
if(orders == 0 && op == 0 && limitepos <2)
{
   pcompra=lowday+mediacalc*Point;
   pvenda=highday-mediacalc*Point;
   
   Comment("\n Break Channel: ",mediacalc," pips \n Buying Price: ",pcompra,"\n Selling Price: ",pvenda,"\n Lots: ",lotsi);
   
   Print("Buying Price: ",pcompra);
   Print("Selling Price: ",pvenda);
   Print("Lots: ",lotsi);
   if(Bid >= pcompra)
   {
      Print("Buy");
      OrderSend(Symbol(),OP_BUY,lotsi,Ask,slippage,Ask-stopLoss*Point,0,"Power:Buy",MagicNumber,0,Lime);      
      limitepos+=1;
   }
if(Bid <= pvenda)
{
      Print("Sell");
      OrderSend(Symbol(),OP_SELL,lotsi,Bid,slippage,Bid+stopLoss*Point,0,"Power:Sell",MagicNumber,0,Red);
      limitepos+=1;
}
}
}
return(0);
}
            
Comments