envelope_scalper5min





#property copyright "Copyright c 2007, ideaforex@inbox.com"
#property link "pipsweak"

extern int xEnv_Period = 6;
extern int xEnv_Shift = 1;
extern int xEnv_Mode = 2;
extern int xEnv_Price = 4;
extern double xEnv_Dev = 0.03;
extern bool xMM = TRUE;
extern int _xMM_RP = 28;
extern double xLots = 1.0;
extern bool xSLM = TRUE;
extern int _xSLM_SL = 32;
extern int _xSLM_SL2 = 5;
extern bool xTPM = TRUE;
extern int _xTPM_TP = 5;
extern int _xTPM_TP2 = 3;
extern string xPair = "EURUSD";
extern int _xPair_Spread = 2;
extern int _xPair_Spread_Max = 3;
extern int _xPair_Slip_New = 3;
extern int _xPair_Slip_Exit = 6;
extern bool xTime = TRUE;
extern int _xTime_GMT = 0;
extern int _xTime_From = 17;
extern int _xTime_To = 5;
extern bool xRealistic = TRUE;
extern int xMagic = 1981005;
double g_tick_value_268;
bool gb_276;
bool gb_280;
bool gb_284;
bool g_is_spread_ok_288;
bool g_is_expired_272;
bool g_is_trade_time_276;
bool gb_300;
bool gb_304;
bool gb_308;
datetime g_str2time_312;
string gs_316 = "2007.07.21";			// The expired day. Modify it if you want run it at a live account.  

bool GoodTime(int ai_0, int ai_4) {
 if (xTime) {
   if (ai_4 > 0 && ai_4 < 6 && Period() == PERIOD_M5 && ai_0 >= _xTime_From - _xTime_GMT || ai_0 <= _xTime_To - _xTime_GMT) {
     g_is_trade_time_276 = TRUE;
     return (TRUE);
   } else {
     g_is_trade_time_276 = FALSE;
     return (FALSE);
   }
 } else {
   g_is_trade_time_276 = TRUE;
   return (TRUE);
 }
}

double SkyHigh() {
 double l_min_lot_0 = MarketInfo(xPair, MODE_MINLOT);
 double l_max_lot_8 = MarketInfo(xPair, MODE_MAXLOT);
 double l_margin_required_16 = MarketInfo(xPair, MODE_MARGINREQUIRED);
 double l_lot_step_24 = MarketInfo(xPair, MODE_LOTSTEP);
 double l_lots_32;
 int li_40;
 if (l_lot_step_24 >= 1.0) li_40 = 0;
 else {
   if (l_lot_step_24 >= 0.1) li_40 = 1;
   else li_40 = 2;
 }
 l_lots_32 = NormalizeDouble(AccountFreeMargin() / l_margin_required_16 * _xMM_RP / 100.0, li_40);
 if (l_lots_32 < l_min_lot_0) l_lots_32 = l_min_lot_0;
 if (l_lots_32 > l_max_lot_8 && xRealistic) l_lots_32 = l_max_lot_8;
 return (l_lots_32);
}

int init() {
 int li_0 = Bars;
 if (li_0 < 0) return (-1);
 if (li_0 > 0) li_0--;
 gb_276 = FALSE;
 gb_280 = FALSE;
 gb_284 = FALSE;
 g_is_spread_ok_288 = FALSE;
 gb_300 = FALSE;
 gb_304 = FALSE;
 gb_308 = FALSE;
 g_str2time_312 = StrToTime(gs_316);
 g_is_expired_272 = FALSE;
 if (CurTime() >= g_str2time_312) g_is_expired_272 = TRUE;
 g_tick_value_268 = MarketInfo(xPair, MODE_TICKVALUE);
 return (0);
}

int deinit() {
 return (0);
}

int start() {
 bool lb_0 = FALSE;
 bool lb_4 = FALSE;
 bool lb_8 = FALSE;
 bool lb_12 = FALSE;
 bool lb_16 = FALSE;
 if (CurTime() >= g_str2time_312) g_is_expired_272 = TRUE;
 int l_market_info_20 = MarketInfo(xPair, MODE_SPREAD);
 if (_xPair_Spread != l_market_info_20) _xPair_Spread = l_market_info_20;
 if (_xPair_Spread <= _xPair_Spread_Max && _xPair_Spread != 0) g_is_spread_ok_288 = TRUE;
 else g_is_spread_ok_288 = FALSE;
 double l_ienvelopes_24 = iEnvelopes(NULL, 0, xEnv_Period, xEnv_Mode, xEnv_Shift, xEnv_Price, xEnv_Dev, MODE_UPPER, 0);
 double l_ienvelopes_32 = iEnvelopes(NULL, 0, xEnv_Period, xEnv_Mode, xEnv_Shift, xEnv_Price, xEnv_Dev, MODE_LOWER, 0);
 if (Bid >= l_ienvelopes_24) {
   lb_4 = TRUE;
   lb_12 = TRUE;
   if (!gb_308) {
     gb_300 = TRUE;
     gb_308 = TRUE;
     gb_304 = FALSE;
   } else gb_300 = FALSE;
 } else {
   if (Bid <= l_ienvelopes_32) {
     lb_0 = TRUE;
     lb_8 = TRUE;
     if (!gb_304) {
       gb_300 = TRUE;
       gb_308 = FALSE;
       gb_304 = TRUE;
     } else gb_300 = FALSE;
   }
 }
 int l_ord_total_40 = OrdersTotal();
 if (l_ord_total_40 > 0) {
   for (int li_44 = 0; li_44 < l_ord_total_40; li_44++) {
     OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
     if (OrderSymbol() == Symbol() && OrderMagicNumber() == xMagic) {
       if (OrderType() == OP_BUY) {
         gb_284 = TRUE;
         if (OrderProfit() >= _xTPM_TP * g_tick_value_268 * OrderLots()) {
           OrderClose(OrderTicket(), OrderLots(), Bid, _xPair_Slip_Exit, Green);
           lb_16 = TRUE;
           gb_284 = FALSE;
         } else {
           if (OrderProfit() <= -1.0 * (_xSLM_SL * g_tick_value_268 * OrderLots())) {
             OrderClose(OrderTicket(), OrderLots(), Bid, _xPair_Slip_Exit, Red);
             gb_280 = TRUE;
             gb_284 = FALSE;
           } else {
             if (lb_12) {
               OrderClose(OrderTicket(), OrderLots(), Bid, _xPair_Slip_Exit, Red);
               gb_284 = FALSE;
             }
           }
         }
       } else {
         if (OrderType() == OP_SELL) {
           gb_284 = TRUE;
           if (OrderProfit() >= _xTPM_TP * g_tick_value_268 * OrderLots()) {
             OrderClose(OrderTicket(), OrderLots(), Ask, _xPair_Slip_Exit, Green);
             lb_16 = TRUE;
             gb_284 = FALSE;
           } else {
             if (OrderProfit() <= -1.0 * (_xSLM_SL * g_tick_value_268 * OrderLots())) {
               OrderClose(OrderTicket(), OrderLots(), Ask, _xPair_Slip_Exit, Red);
               gb_280 = TRUE;
               gb_284 = FALSE;
             } else {
               if (lb_8) {
                 OrderClose(OrderTicket(), OrderLots(), Ask, _xPair_Slip_Exit, Orange);
                 gb_284 = FALSE;
               }
             }
           }
         }
       }
     }
   }
 } else {
   if (gb_284) {
     gb_280 = TRUE;
     gb_284 = FALSE;
   }
 }
 int l_ord_ticket_48;

 // -------------------------------------------------------- Modified
 // Original condition, can't run at a live account
 // if (IsDemo() && !g_is_expired_272 && gb_284 == FALSE && gb_280 == FALSE && gb_276 == FALSE && GoodTime(Hour(), DayOfWeek()) && g_is_spread_ok_288) {
 // Modified condition.
 if (!g_is_expired_272 && gb_284 == FALSE && gb_280 == FALSE && gb_276 == FALSE && GoodTime(Hour(), DayOfWeek()) && g_is_spread_ok_288) {
   if (xMM && _xMM_RP > 0 && _xMM_RP < 100) xLots = SkyHigh();
   if (lb_0) {
     double l_stoploss_52;
     double l_takeprofit_60;
     if (xSLM) l_stoploss_52 = Ask - (_xSLM_SL + _xSLM_SL2) * Point;
     else l_stoploss_52 = 0;
     if (xTPM && _xTPM_TP > 5) l_takeprofit_60 = Ask + (_xTPM_TP + _xTPM_TP2) * Point;
     else l_takeprofit_60 = 0;
     l_ord_ticket_48 = OrderSend(Symbol(), OP_BUY, xLots, Ask, _xPair_Slip_New, l_stoploss_52, l_takeprofit_60, "EA: Buy #" + xMagic, xMagic, 0, DodgerBlue);
     if (l_ord_ticket_48 > 0) {
       if (OrderSelect(l_ord_ticket_48, SELECT_BY_TICKET, MODE_TRADES)) gb_284 = TRUE;
       else Print("Error opening BUY order : ", GetLastError());
     }
   } else {
     if (lb_4) {
       double l_stoploss_68;
       double l_takeprofit_76;
       if (xSLM) l_stoploss_68 = Bid + (_xSLM_SL + _xSLM_SL2) * Point;
       else l_stoploss_68 = 0;
       if (xTPM && _xTPM_TP > 5) l_takeprofit_76 = Bid - (_xTPM_TP + _xTPM_TP2) * Point;
       else l_takeprofit_76 = 0;
       l_ord_ticket_48 = OrderSend(Symbol(), OP_SELL, xLots, Bid, _xPair_Slip_New, l_stoploss_68, l_takeprofit_76, "EA: Sell #" + xMagic, xMagic, 0, LimeGreen);
       if (l_ord_ticket_48 > 0) {
         if (OrderSelect(l_ord_ticket_48, SELECT_BY_TICKET, MODE_TRADES)) gb_284 = TRUE;
         else Print("Error opening SELL order : ", GetLastError());
       }
     }
   }
 }
 if (gb_276 && !lb_0 && !lb_4) gb_276 = TRUE;
 if (gb_280 && gb_300) gb_280 = FALSE;
 string ls_84 = "";
 if (g_is_expired_272) ls_84 = ls_84 + "Expiration date: " + TimeToStr(g_str2time_312) + " [Expired]\n";
 else ls_84 = ls_84 + "Expiration date: " + TimeToStr(g_str2time_312) + " [OK]\n";
 if (g_is_spread_ok_288) ls_84 = ls_84 + "Spread: " + _xPair_Spread + " [OK]\n";
 else ls_84 = ls_84 + "Spread: " + _xPair_Spread + " [Wrong]\n";
 if (g_is_trade_time_276) ls_84 = ls_84 + "Time to trade [OK]\n";
 else ls_84 = ls_84 + "Time to trade [No]\n";
 if (xPair != Symbol()) ls_84 = ls_84 + "Wrong external parameter: xPair is not equal to " + Symbol() + ". Change it!!";
 if (Period() != PERIOD_M5) ls_84 = ls_84 + "Wrong chart period. Must be 5 minutes. Change it!!";
 Comment(ls_84);
 return (0);
}



Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:

Envelopes indicator


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders
It Closes Orders by itself
It automatically opens orders when conditions are reached

Other Features:


BackTest : USDJPY on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDCAD on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2009-08-01 to 2009-10-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-02-27 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-03-01 to 2010-03-27 Profit Factor:0.00 Total Net Profit:0.00

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-04-16 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-05-01 to 2010-05-31 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-06-01 to 2010-06-30 Profit Factor:0.00 Total Net Profit:0.00

Request Backtest for envelope_scalper5min


From : (yyyy/mm/dd) To: (yyyy/mm/dd)

Pair: Period: