Envelope 2.11_all_gbpusdm15

Author: tageiger
Profit factor:
0.67

Technical Logic Overview: Envelope 2.mq4

This document provides a non-technical explanation of the logic used by the "Envelope 2" trading script. The script is designed to automate trade entries, manage risk via dynamic lot sizing, and handle profit targets and trailing stops based on moving average envelopes.

1. Core Indicators

The script relies on three primary technical components to make decisions:

  • Moving Average (MA): A base line used to determine the general trend of the market.
  • Upper Envelope: A calculated boundary line above the moving average.
  • Lower Envelope: A calculated boundary line below the moving average.

2. Trade Entry Logic

The script looks for specific price action relative to the Envelopes to place "Pending Orders" (orders that are only activated if the price hits a specific level).

Buy Opportunities

If the current market price is currently between the Upper and Lower Envelopes, the script places three separate Buy Stop orders:

  • Order 1: Set to trigger at the Upper Envelope with a primary profit target.
  • Order 2: Set to trigger at the Upper Envelope with a secondary profit target.
  • Order 3: Set to trigger at the Upper Envelope with a third profit target.

Sell Opportunities

Similarly, if the price is between the Envelopes, the script places three Sell Stop orders:

  • Order 1: Set to trigger at the Lower Envelope with a primary profit target.
  • Order 2: Set to trigger at the Lower Envelope with a secondary profit target.
  • Order 3: Set to trigger at the Lower Envelope with a third profit target.

3. Smart Risk Management

The script includes a sophisticated system for determining how much money to risk on each trade:

  • Dynamic Lot Sizing: Instead of using a fixed amount, the script calculates the position size based on your account's available margin and a user-defined risk percentage.
  • Loss Adjustment (Decrease Factor): If the script detects a string of recent losing trades in its history, it automatically reduces the size of subsequent trades. This is intended to protect the account balance during periods of poor market performance.

4. Trade Management & Trailing Stops

Once a trade is activated, the script manages it actively:

  • Trailing Stop Loss: The script monitors the price as it moves in your favor. It can "trail" the stop-loss order to lock in profits by moving it along the Moving Average or the opposite Envelope (depending on user settings). This ensures that if a winning trade suddenly reverses, you keep a portion of the gains.
  • Time Filters: The script only opens new trades during specific hours defined by the user.
  • Auto-Cleanup: At the end of the trading day (based on the user's "Time Close" setting), any pending orders that were not triggered are automatically deleted to keep the workspace clean for the next session.

5. Summary of Operation Flow

  1. Analyze: Check current market price against Envelopes and check account history for losses.
  2. Calculate: Determine the safest trade size based on risk and recent performance.
  3. Execute: Place up to six pending orders (3 Buy, 3 Sell) at the Envelopes during allowed hours.
  4. Monitor: Move stop-losses automatically as price moves in favor of the trade.
  5. Cleanup: Delete unactivated orders and reset for the next cycle.
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
30 Views
0 Downloads
0 Favorites
Envelope 2.11_all_gbpusdm15
//+------------------------------------------------------------------+
//|                                                   Envelope 2.mq4 |
//|                                                         tageiger |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "tageiger"
#property link      "http://www.metaquotes.net"

//---- input parameters
extern int        Magic = 12000;
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              =1;
extern double     MaximumRisk       =0.03;
extern double     DecreaseFactor    =3;
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

USD/CHF Jul 2025 - Sep 2025
1.19
Total Trades 46
Won Trades 36
Lost trades 10
Win Rate 78.26 %
Expected payoff 6.29
Gross Profit 1774.81
Gross Loss -1485.55
Total Net Profit 289.26
-100%
-50%
0%
50%
100%
GBP/CAD Oct 2025 - Feb 2026
1.17
Total Trades 21
Won Trades 18
Lost trades 3
Win Rate 85.71 %
Expected payoff 3.54
Gross Profit 502.48
Gross Loss -428.16
Total Net Profit 74.32
-100%
-50%
0%
50%
100%
EUR/USD Oct 2025 - Feb 2026
1.09
Total Trades 42
Won Trades 34
Lost trades 8
Win Rate 80.95 %
Expected payoff 2.61
Gross Profit 1325.20
Gross Loss -1215.60
Total Net Profit 109.60
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
1.07
Total Trades 12
Won Trades 11
Lost trades 1
Win Rate 91.67 %
Expected payoff 1.64
Gross Profit 317.90
Gross Loss -298.20
Total Net Profit 19.70
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
0.97
Total Trades 108
Won Trades 94
Lost trades 14
Win Rate 87.04 %
Expected payoff -0.88
Gross Profit 3479.50
Gross Loss -3575.00
Total Net Profit -95.50
-100%
-50%
0%
50%
100%
AUD/USD Oct 2025 - Feb 2026
0.89
Total Trades 103
Won Trades 82
Lost trades 21
Win Rate 79.61 %
Expected payoff -3.57
Gross Profit 3076.10
Gross Loss -3444.00
Total Net Profit -367.90
-100%
-50%
0%
50%
100%
USD/CAD Oct 2024 - Jan 2025
0.88
Total Trades 37
Won Trades 33
Lost trades 4
Win Rate 89.19 %
Expected payoff -3.02
Gross Profit 836.05
Gross Loss -947.79
Total Net Profit -111.74
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.74
Total Trades 91
Won Trades 65
Lost trades 26
Win Rate 71.43 %
Expected payoff -9.15
Gross Profit 2329.70
Gross Loss -3162.30
Total Net Profit -832.60
-100%
-50%
0%
50%
100%
AUD/USD Oct 2024 - Jan 2025
0.69
Total Trades 129
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -11.74
Gross Profit 3393.10
Gross Loss -4907.40
Total Net Profit -1514.30
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
0.63
Total Trades 60
Won Trades 46
Lost trades 14
Win Rate 76.67 %
Expected payoff -14.57
Gross Profit 1504.90
Gross Loss -2379.10
Total Net Profit -874.20
-100%
-50%
0%
50%
100%
USD/JPY Jul 2025 - Sep 2025
0.47
Total Trades 115
Won Trades 96
Lost trades 19
Win Rate 83.48 %
Expected payoff -22.43
Gross Profit 2260.27
Gross Loss -4839.30
Total Net Profit -2579.03
-100%
-50%
0%
50%
100%
GBP/AUD Oct 2025 - Feb 2026
0.34
Total Trades 89
Won Trades 74
Lost trades 15
Win Rate 83.15 %
Expected payoff -41.45
Gross Profit 1902.36
Gross Loss -5591.78
Total Net Profit -3689.42
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
0.30
Total Trades 34
Won Trades 24
Lost trades 10
Win Rate 70.59 %
Expected payoff -63.83
Gross Profit 908.70
Gross Loss -3079.00
Total Net Profit -2170.30
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.26
Total Trades 29
Won Trades 17
Lost trades 12
Win Rate 58.62 %
Expected payoff -35.16
Gross Profit 356.30
Gross Loss -1375.80
Total Net Profit -1019.50
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.00
Total Trades 11
Won Trades 11
Lost trades 0
Win Rate 100.00 %
Expected payoff 31.77
Gross Profit 349.44
Gross Loss 0.00
Total Net Profit 349.44
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.00
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%

Comments