Envelope 2.11_all_eurusdm15_v1

Author: tageiger
Profit factor:
0.74

This script is designed for the MetaTrader platform and automates trading based on "Envelope" indicators. Think of Envelopes as bands drawn around a moving average of the price. The script watches these bands and places orders when certain conditions are met.

Here's the breakdown:

  1. Setup & Customization: The script starts by letting you set several parameters. These are like dials and switches that allow you to customize how the script works. Important settings include:

    • The period and timeframe for calculating the Envelopes (how far back to look when calculating the average price and the bands).
    • The deviation of the Envelope bands (how far away from the average price the bands are drawn).
    • The hours of the day during which the script will be active.
    • Take profit levels, which determine how much profit to target on each trade.
    • Lot size, which determines the size of the trades. This can be fixed or automatically calculated based on account risk.
  2. Calculating Lot Size: The script can automatically adjust the size of trades (lots) based on how much money you have in your account and your tolerance for risk. If you've had a series of losing trades, it can reduce the lot size to protect your account.

  3. Identifying Trading Opportunities: The script constantly monitors the price relative to the Envelope bands. Specifically, it looks for situations where the price is between the upper and lower Envelope bands.

  4. Placing Orders: When the script identifies a potential trading opportunity (price is between bands during the specified hours), it places pending orders. A "pending order" is an instruction to the trading platform to automatically execute a trade if the price reaches a specific level. The script places both "Buy Stop" orders (to profit from upward price movement) and "Sell Stop" orders (to profit from downward price movement). It places multiple orders for each direction, each with a different take profit level.

  5. Trailing Stop Loss: The script also implements a "trailing stop loss." This is a protective order that automatically adjusts as the price moves in a favorable direction. The stop loss follows the price, locking in profits and limiting potential losses. The position of the stop loss is determined based on the Envelope indicators or the moving average, depending on the settings chosen by the user.

  6. Order Management: The script keeps track of the orders it has placed. If the trading day ends (as defined by the TimeClose parameter) and any pending orders haven't been triggered, the script cancels them. It also keeps track of already closed positions.

In short, this script uses Envelope indicators to identify potential buy and sell opportunities, automatically places orders, manages risk with a trailing stop loss, and closes any remaining pending orders at the end of the trading day. It aims to automate a trading strategy based on price movements within Envelope bands.

Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicatorEnvelopes indicator
7 Views
0 Downloads
0 Favorites
Envelope 2.11_all_eurusdm15_v1
//+------------------------------------------------------------------+
//|                                                   Envelope 2.mq4 |
//|                                                         tageiger |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "tageiger"
#property link      "http://www.metaquotes.net"

//---- input parameters
extern int        Magic = 11000;
extern int        EnvelopePeriod    =144;
extern int        EnvTimeFrame      =0; //envelope time frame: 0=chart,60=1hr,240=4hr, etc.
extern int        EnvMaMethod       =1; //0=sma,1=ema,2=smma,3=lwma.
extern double     EnvelopeDeviation =1;
extern int        TimeOpen          =0;
extern int        TimeClose         =23;
extern double     FirstTP           =89.0;
extern double     SecondTP          =144.0;
extern double     ThirdTP           =233.0;
extern double     Lots              =0.1;
extern double     MaximumRisk       =0;
extern double     DecreaseFactor    =5;
extern int        MaElineTSL        =0;//0=iMA trailing stoploss  1=Opposite Envelope TSL
int               b1,b2,b3,s1,s2,s3;
double            TSL               =0;

//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
   }

int start()
   {
   int      p=0;p=EnvelopePeriod;
   int      etf=0;etf=EnvTimeFrame;
   int      mam=0;mam=EnvMaMethod;
   double   d=0;d=EnvelopeDeviation;
   double   btp1,btp2,btp3,stp1,stp2,stp3;
   double   bline=0,sline=0,ma=0;
   int      cnt, ticket, total;
   
   int      digit  = MarketInfo(Symbol(),MODE_DIGITS);
   
   ma=iMA(NULL,etf,p,0,mam,PRICE_CLOSE,0);
   bline=iEnvelopes(NULL,etf,p,mam,0,PRICE_CLOSE,d,MODE_UPPER,0);
   sline=iEnvelopes(NULL,etf,p,mam,0,PRICE_CLOSE,d,MODE_LOWER,0);

   total=OrdersTotal();
   if(OrdersTotal()==0)
      {b1=0;b2=0;b3=0;s1=0;s2=0;s3=0;}
   if(OrdersTotal()>0)
      {
      //Print("Total Orders:",OrdersTotal());
      //Print(b1," ",b2," ",b3," ",s1," ",s2," ",s3);
      for(cnt=0;cnt<total;cnt++)
         {
         OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
         if(OrderMagicNumber()==Magic+2)
            {b1=OrderTicket(); }
         if(OrderMagicNumber()==Magic+4)
            {b2=OrderTicket(); }
         if(OrderMagicNumber()==Magic+6)
            {b3=OrderTicket(); }
         if(OrderMagicNumber()==Magic+1)
            {s1=OrderTicket(); }
         if(OrderMagicNumber()==Magic+3)
            {s2=OrderTicket(); }
         if(OrderMagicNumber()==Magic+5)
            {s3=OrderTicket(); }
         }
      //Print ("magic=",OrderMagicNumber());
      }
   if(b1==0)
      {  
      if(Hour()>TimeOpen && Hour()<TimeClose)
         {
         if(bline>Close[0] && sline<Close[0])
            {
            btp1=(NormalizeDouble(bline,digit))+(FirstTP*Point);
            ticket=OrderSend(Symbol(),
                              OP_BUYSTOP,
                              LotsOptimized(),
                              (NormalizeDouble(bline,digit)),
                              0,
                              (NormalizeDouble(sline,digit)),
                              btp1,
                              "",
                              Magic+2,
                              TimeClose,
                              Aqua);
                              if(ticket>0)
                                 {
                                 if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                    {
                                    b1=ticket;
                                    Print(ticket);
                                    }
                                 else Print("Error Opening BuyStop Order: ",GetLastError());
                                 return(0);
                                 }
            }
         }
      }         

   if(b2==0)
      {
      if(Hour()>TimeOpen && Hour()<TimeClose)
         {
         if(bline>Close[0] && sline<Close[0])
            {      
            btp2=(NormalizeDouble(bline,digit))+(SecondTP*Point);
            ticket=OrderSend(Symbol(),
                              OP_BUYSTOP,
                              LotsOptimized(),
                              (NormalizeDouble(bline,digit)),
                              0,
                              (NormalizeDouble(sline,digit)),
                              btp2,
                              "",
                              Magic+4,
                              TimeClose,
                              Aqua);
                              if(ticket>0)
                                 {
                                 if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                    {
                                    b2=ticket;
                                    Print(ticket);
                                    }
                                 else Print("Error Opening BuyStop Order: ",GetLastError());
                                 return(0);
                                 }
            }
         }
      }                              

   if(b3==0)
      {
      if(Hour()>TimeOpen && Hour()<TimeClose)
         {
         if(bline>Close[0] && sline<Close[0])
            {      
            btp3=(NormalizeDouble(bline,digit))+(ThirdTP*Point);
            ticket=OrderSend(Symbol(),
                              OP_BUYSTOP,
                              LotsOptimized(),
                              (NormalizeDouble(bline,digit)),
                              0,
                              (NormalizeDouble(sline,digit)),
                              btp3,
                              "",
                              Magic+6,
                              TimeClose,
                              Aqua);
                              if(ticket>0)
                                 {
                                 if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                    {
                                    b3=ticket;
                                    Print(ticket);
                                    }
                                 else Print("Error Opening BuyStop Order: ",GetLastError());
                                 return(0);
                                 }
            }
         }
      }                     
   
   if(s1==0)
      {
      if(Hour()>TimeOpen && Hour()<TimeClose)
         {
         if(bline>Close[0] && sline<Close[0])
            {      
            stp1=NormalizeDouble(sline,digit)-(FirstTP*Point);
            ticket=OrderSend(Symbol(),
                              OP_SELLSTOP,
                              LotsOptimized(),
                              (NormalizeDouble(sline,digit)),
                              0,
                              (NormalizeDouble(bline,digit)),
                              stp1,
                              "",
                              Magic+1,
                              TimeClose,
                              HotPink);
                              if(ticket>0)
                                 {
                                 if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                    {
                                    s1=ticket;
                                    Print(ticket);
                                    }
                                 else Print("Error Opening SellStop Order: ",GetLastError());
                                 return(0);
                                 }
            }
         }
      }

   if(s2==0)
      {
      if(Hour()>TimeOpen && Hour()<TimeClose)
         {
         if(bline>Close[0] && sline<Close[0])
            {      
            stp2=NormalizeDouble(sline,digit)-(SecondTP*Point);
            ticket=OrderSend(Symbol(),
                              OP_SELLSTOP,
                              LotsOptimized(),
                              (NormalizeDouble(sline,digit)),
                              0,
                              (NormalizeDouble(bline,digit)),
                              stp2,
                              "",
                              Magic+3,
                              TimeClose,
                              HotPink);
                              if(ticket>0)
                                 {
                                 if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                    {
                                    s2=ticket;
                                    Print(ticket);
                                    }
                                 else Print("Error Opening SellStop Order: ",GetLastError());
                                 return(0);
                                 }
            }
         }
      }                     
   
   if(s3==0)
      {
      if(Hour()>TimeOpen && Hour()<TimeClose)
         {
         if(bline>Close[0] && sline<Close[0])
            {      
            stp3=NormalizeDouble(sline,digit)-(ThirdTP*Point);
            ticket=OrderSend(Symbol(),
                              OP_SELLSTOP,
                              LotsOptimized(),
                              (NormalizeDouble(sline,digit)),
                              0,
                              (NormalizeDouble(bline,digit)),
                              stp3,
                              "",
                              Magic+5,
                              TimeClose,
                              HotPink);
                              if(ticket>0)
                                 {
                                 if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                    {
                                    s3=ticket;
                                    Print(ticket);
                                    }
                                 else Print("Error Opening SellStop Order: ",GetLastError());
                                 return(0);
                                 }
            }
         }
      }
   for(cnt=0;cnt<total;cnt++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);            
      if(OrderType()==OP_BUY)
         {
         if(MaElineTSL==0) {TSL=NormalizeDouble(ma,digit); }
         if(MaElineTSL==1) {TSL=NormalizeDouble(sline,digit); }
         if(Close[0]>OrderOpenPrice())
            {
            if((Close[0]>sline) && (TSL>OrderStopLoss()))
               {
               double bsl;bsl=TSL;
               OrderModify(OrderTicket(),
                           OrderOpenPrice(),
                           bsl,
                           OrderTakeProfit(),
                           0,//Order expiration server date/time
                           Green);
               Sleep(10000);
               }
            }
         }
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);            
      if(OrderType()==OP_SELL)
         {
         if(MaElineTSL==0) {TSL=NormalizeDouble(ma,digit); }
         if(MaElineTSL==1) {TSL=NormalizeDouble(bline,digit); }         
         if(Close[0]<OrderOpenPrice())
            {
            if((Close[0]<bline) && (TSL<OrderStopLoss()))
               {
               double ssl;ssl=TSL;
               OrderModify(OrderTicket(),
                           OrderOpenPrice(),
                           ssl,
                           OrderTakeProfit(),
                           0,//Order expiration server date/time
                           Red);
               Sleep(10000);
               }
            }
         }      
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);   
      if(Hour()==TimeClose && OrderType()==OP_BUYSTOP)
         {
         OrderDelete(OrderTicket());
         if(OrderTicket()==b1) {b1=0; return;}
         if(OrderTicket()==b2) {b2=0; return;}
         if(OrderTicket()==b3) {b3=0; return;}                  
         }
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);            
      if(Hour()==TimeClose && OrderType()==OP_SELLSTOP)
         {
         OrderDelete(OrderTicket());
         if(OrderTicket()==s1) {s1=0; return;}
         if(OrderTicket()==s2) {s2=0; return;}
         if(OrderTicket()==s3) {s3=0; return;}
         }
      OrderSelect(b1,SELECT_BY_TICKET);
      if(OrderClosePrice()>0) {b1=0;}
      OrderSelect(b2,SELECT_BY_TICKET);
      if(OrderClosePrice()>0) {b2=0;}
      OrderSelect(b3,SELECT_BY_TICKET);
      if(OrderClosePrice()>0) {b3=0;}
      OrderSelect(s1,SELECT_BY_TICKET);
      if(OrderClosePrice()>0) {s1=0;}
      OrderSelect(s2,SELECT_BY_TICKET);
      if(OrderClosePrice()>0) {s2=0;}     
      OrderSelect(s3,SELECT_BY_TICKET);
      if(OrderClosePrice()>0) {s3=0;}         
      }
   }

Profitability Reports

GBP/AUD Jul 2025 - Sep 2025
0.45
Total Trades 43
Won Trades 37
Lost trades 6
Win Rate 86.05 %
Expected payoff -9.69
Gross Profit 335.95
Gross Loss -752.50
Total Net Profit -416.55
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
2970.20
Total Trades 1922
Won Trades 1913
Lost trades 9
Win Rate 99.53 %
Expected payoff 1293.50
Gross Profit 2486946.00
Gross Loss -837.30
Total Net Profit 2486108.70
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.56
Total Trades 60
Won Trades 42
Lost trades 18
Win Rate 70.00 %
Expected payoff -7.22
Gross Profit 557.10
Gross Loss -990.30
Total Net Profit -433.20
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
0.84
Total Trades 525
Won Trades 472
Lost trades 53
Win Rate 89.90 %
Expected payoff -1.58
Gross Profit 4463.80
Gross Loss -5292.09
Total Net Profit -828.29
-100%
-50%
0%
50%
100%
USD/CHF Jan 2025 - Jul 2025
0.86
Total Trades 237
Won Trades 198
Lost trades 39
Win Rate 83.54 %
Expected payoff -2.30
Gross Profit 3244.40
Gross Loss -3789.38
Total Net Profit -544.98
-100%
-50%
0%
50%
100%
USD/CAD Jan 2025 - Jul 2025
0.41
Total Trades 131
Won Trades 104
Lost trades 27
Win Rate 79.39 %
Expected payoff -10.97
Gross Profit 1001.26
Gross Loss -2438.36
Total Net Profit -1437.10
-100%
-50%
0%
50%
100%
GBP/CAD Jan 2025 - Jul 2025
1.04
Total Trades 307
Won Trades 285
Lost trades 22
Win Rate 92.83 %
Expected payoff 0.39
Gross Profit 2851.98
Gross Loss -2732.28
Total Net Profit 119.70
-100%
-50%
0%
50%
100%
GBP/AUD Jan 2025 - Jul 2025
0.47
Total Trades 243
Won Trades 211
Lost trades 32
Win Rate 86.83 %
Expected payoff -9.18
Gross Profit 2012.91
Gross Loss -4244.50
Total Net Profit -2231.59
-100%
-50%
0%
50%
100%
EUR/USD Jan 2025 - Jul 2025
0.74
Total Trades 266
Won Trades 225
Lost trades 41
Win Rate 84.59 %
Expected payoff -4.07
Gross Profit 3107.60
Gross Loss -4190.10
Total Net Profit -1082.50
-100%
-50%
0%
50%
100%
AUD/USD Jan 2025 - Jul 2025
0.54
Total Trades 237
Won Trades 166
Lost trades 71
Win Rate 70.04 %
Expected payoff -7.79
Gross Profit 2181.10
Gross Loss -4027.10
Total Net Profit -1846.00
-100%
-50%
0%
50%
100%

Comments