Navigation´╝ÜHome > Content >

Scalping_Pro_System_edu.mq4

Time: 2013-09-12 | Download file:Scalping_Pro_System_edu.mq4

/*
   2013-12-09 by Capella at http://worldwide-invest.org/
	- Changed all possible variables to externals and renamed them
	- Commented out backtesting hole
	2013-12-12 by Capella at http://worldwide-invest.org/
	- Fixed bugs that caused OrderSend errors 130, 131 and 4107 caused by wrong SL and wrong lot size
	- Deleted unused variables 
	- Added EA_Comment
	2013-12-16 by Capella at http://worldwide-invest.org/
	- Added check so that SL is not withing StopLevel (and not just within spread)
*/

extern string EA_Comment = "";          // Comment to be printed in Terminal windows for each trade
extern double Risk = 0.01;              // Risk for calculating dynamic lots
extern int MagicNumber = 20050111;      // Unique magic number for this EA
extern double TakeProfit = 100.0;       // Take Profit in Pips
extern double StopLoss = 90.0;          // StopLoss in Pips
extern double Slippage = 80.0;          // Slippage in Points
extern int Max_Orders = 20;             // Max allowed Buys or Sells
double DeleteLevel = 99.0;              // 1-99% If Profit < -Balance * DeleteLevel/100 then delete alkl pending orders 
extern bool Use_FixedLots = FALSE;      // If True then calculate Lots dynamically based on Risk, otherwise use Fixed Lot size
extern double FixedLot = 0.01;          // Fix lot size if Use_FixedLots is set to True
extern double PriceDiff = 900.0;        // Adjust price for pending orders
extern double LowHighDiff_SL = 5.0;     // Check High or Low +/- so many points must High or Low differ for StopLoss
extern double LowHighDiff_Open = 120.0; // Check High or Low +/- so many points must High or Low differ for price to open orders
extern double LotIncrement = 3.0;       // A factor for calculating increased lots when opening additional positions
extern bool LimitOrders = FALSE;			 // Open LimitOrders beside StopOrders
extern bool DynamicLots = TRUE;			 // Use Dynamically calculated lots
extern double Start_Hour = 0.0;         // Low and High values are calculated in such intervals
extern double Start_Minute = 0.0;       // - "" -
extern double LimitOrder_Lots = 2.0;    // Static lot size for limit orders
extern double Initial_Lot = 0.1;        // Lot size starts / is reset to this value

int Gi_220;
double G_price_224;
double G_price_232;
double G_ilow_240;
double G_ihigh_248;
double Min_Lot;
double Max_Lot;
double StopLevel;

int init()
{
   Min_Lot = MarketInfo ( Symbol(), MODE_MINLOT ); 
   Max_Lot = MarketInfo ( Symbol(), MODE_MAXLOT ); 
   StopLevel = MarketInfo ( Symbol(), MODE_STOPLEVEL );
}

// 945D754CB0DC06D04243FCBA25FC0802
double f0_0() {
   double Ld_ret_0 = Initial_Lot;
   int hist_total_8 = OrdersHistoryTotal();
   int count_12 = 0;
   Ld_ret_0 = NormalizeDouble(AccountFreeMargin() * Risk / 1000.0, 1);
   if (DynamicLots == TRUE) Ld_ret_0 = NormalizeDouble(AccountFreeMargin() * Risk / 1000.0, 2);
   if (LotIncrement > 0.0) {
      for (int pos_16 = hist_total_8 - 1; pos_16 >= 0; pos_16--) {
         if (OrderSelect(pos_16, SELECT_BY_POS, MODE_HISTORY) == FALSE) {
            Print("Error in history!");
            break;
         }
         if (OrderSymbol() != Symbol() || OrderType() > OP_SELL) continue;
         if (OrderProfit() > 0.0) break;
         if (OrderProfit() < 0.0) count_12++;
      }
      if (DynamicLots == TRUE && count_12 > 1) Ld_ret_0 = NormalizeDouble(Ld_ret_0 - Ld_ret_0 * count_12 / LotIncrement, 2);
      if (DynamicLots == FALSE && count_12 > 1) Ld_ret_0 = NormalizeDouble(Ld_ret_0 - Ld_ret_0 * count_12 / LotIncrement, 1);
   }
   if (DynamicLots == TRUE && Ld_ret_0 < 0.01) Ld_ret_0 = 0.01;
   if (DynamicLots == FALSE && Ld_ret_0 < 0.1) Ld_ret_0 = 0.1;
   
   if ( Ld_ret_0 < Min_Lot )
      Ld_ret_0 = Min_Lot;
   if ( Ld_ret_0 > Max_Lot )
      Ld_ret_0 = Max_Lot;   
   
   return (Ld_ret_0);
}
	  	    						 		 	 		 				 	 	   	     							 	   	  			   			 		   					   	 		  	 			 	 		  			 		   	 	         				  	 		 	  	 	 		 	 			  
// EA2B2676C28C0DB26D39331A336C6B92
void start() 
{
   double time_0;
   double time_8;
   double order_open_price_16;
   double order_open_price_24;
   double order_open_price_32;
   double order_open_price_48;
   double order_open_price_56;
   double order_open_price_64;
   double lots_80 = f0_0();
   int result;
   bool Li_88 = TRUE;

   
   if (Use_FixedLots == TRUE) 
		lots_80 = FixedLot;
/*
   // This is only used to prevent backtests from failing!!!	
   if ((TimeCurrent() > D'01.01.2002 07:00' && TimeCurrent() < D'02.01.2002 07:00') || (TimeCurrent() > D'28.12.2010 07:00' && TimeCurrent() < D'03.01.2011 07:00')) 
		Li_88 = FALSE;
*/
	Comment("AccountProfit=  ", AccountProfit(), "                  AccountBalance =  ", AccountBalance(), "                       lot=     ", NormalizeDouble(AccountFreeMargin() * Risk / 1000.0,
      5), "              http://scalping Digits =  ", Digits);
   if (iVolume(NULL, PERIOD_M1, 0) == 1.0) {
      ObjectDelete("sell");
      ObjectDelete("byu");
      G_ilow_240 = 0;
      G_ihigh_248 = 0;
      time_0 = 0;
      time_8 = 0;
      for (int Li_92 = 1; Li_92 < 720000; Li_92++) {
         if (TimeHour(Time[Li_92]) == Start_Hour && TimeMinute(Time[Li_92]) == Start_Minute && time_0 == 0.0) 
				time_0 = Time[Li_92];
         else 
			{
            if (TimeHour(Time[Li_92]) == Start_Hour && TimeMinute(Time[Li_92]) == Start_Minute && time_8 == 0.0) 
				{
               time_8 = Time[Li_92];
               break;
            }
         }
      }
      for (int Li_96 = 1; Li_96 < 200000; Li_96++) {
         if (iTime(NULL, PERIOD_M1, Li_96) > time_8 && iTime(NULL, PERIOD_M1, Li_96) < time_0) 
			{
            if (G_ilow_240 > iLow(NULL, PERIOD_M1, Li_96) || G_ilow_240 == 0.0) G_ilow_240 = iLow(NULL, PERIOD_M1, Li_96);
            if (G_ihigh_248 < iHigh(NULL, PERIOD_M1, Li_96)) G_ihigh_248 = iHigh(NULL, PERIOD_M1, Li_96);
         }
         if (iTime(NULL, PERIOD_M1, Li_96) < time_8) break;
      }
   }
   int count_100 = 0;
   int count_104 = 0;
   for (int pos_108 = 0; pos_108 < OrdersTotal(); pos_108++) {
      if (OrderSelect(pos_108, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUY) count_100++;
         if (OrderType() == OP_SELL) count_104++;
      }
   }
   int count_112 = 0;
   int count_116 = 0;
   for (int pos_120 = 0; pos_120 < OrdersTotal(); pos_120++) {
      if (OrderSelect(pos_120, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUYSTOP) count_112++;
         if (OrderType() == OP_SELLSTOP) count_116++;
      }
   }
   int count_124 = 0;
   int count_128 = 0;
   for (int pos_132 = 0; pos_132 < OrdersTotal(); pos_132++) {
      if (OrderSelect(pos_132, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUYLIMIT) count_124++;
         if (OrderType() == OP_SELLLIMIT) count_128++;
      }
   }
   int count_136 = 0;
   int count_140 = 0;
   for (int pos_144 = 0; pos_144 < OrdersHistoryTotal(); pos_144++) {
      if (OrderSelect(pos_144, SELECT_BY_POS, MODE_HISTORY) == TRUE) {
         if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber)
            if (OrderOpenTime() >= time_0) count_136++;
         if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber)
            if (OrderOpenTime() >= time_0) count_140++;
      }
   }
   double order_lots_164 = 0;
   double order_lots_172 = 0;
   for (int pos_180 = 0; pos_180 < OrdersTotal(); pos_180++) {
      if (OrderSelect(pos_180, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
      if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber) {
         if (order_open_price_48 == 0.0) order_open_price_48 = OrderOpenPrice();
         if (OrderOpenPrice() < order_open_price_48) {
            order_open_price_64 = order_open_price_56;
            order_open_price_56 = order_open_price_48;
            order_open_price_48 = OrderOpenPrice();
         }
         if (order_lots_172 == 0.0) order_lots_172 = OrderLots();
         if (OrderLots() > order_lots_172) order_lots_172 = OrderLots();
      }
      if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber) {
         if (order_open_price_16 == 0.0) order_open_price_16 = OrderOpenPrice();
         if (OrderOpenPrice() > order_open_price_16) {
            order_open_price_32 = order_open_price_24;
            order_open_price_24 = order_open_price_16;
            order_open_price_16 = OrderOpenPrice();
         }
         if (order_lots_164 == 0.0) order_lots_164 = OrderLots();
         if (OrderLots() > order_lots_164) order_lots_164 = OrderLots();
      }
   }
   if (TimeCurrent() >= time_0 && TimeCurrent() <= time_0 + 1000.0) {
      for (int pos_184 = 0; pos_184 <= OrdersTotal(); pos_184++) {
         if (OrderSelect(pos_184, SELECT_BY_POS) == TRUE) {
            if (OrderType() == OP_SELLSTOP && OrderMagicNumber() == MagicNumber) OrderDelete(OrderTicket());
            if (OrderType() == OP_BUYSTOP && OrderMagicNumber() == MagicNumber) OrderDelete(OrderTicket());
         }
      }
   }
   if ((count_124 > 0 && count_100 == 0) || (count_128 > 0 && count_104 == 0)) {
      for (int pos_188 = 0; pos_188 <= OrdersTotal(); pos_188++) {
         if (OrderSelect(pos_188, SELECT_BY_POS) == TRUE) {
            if (OrderType() == OP_SELLLIMIT && count_104 == 0 && OrderMagicNumber() == MagicNumber) OrderDelete(OrderTicket());
            if (OrderType() == OP_BUYLIMIT && count_100 == 0 && OrderMagicNumber() == MagicNumber) OrderDelete(OrderTicket());
         }
      }
   }
   for (int pos_192 = 0; pos_192 < OrdersHistoryTotal(); pos_192++) {
      if (OrderSelect(pos_192, SELECT_BY_POS, MODE_TRADES) == TRUE) {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUYSTOP && OrderOpenTime() > time_8 && OrderOpenTime() < time_0) OrderDelete(OrderTicket());
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELLSTOP && OrderOpenTime() > time_8 && OrderOpenTime() < time_0) OrderDelete(OrderTicket());
      }
   }
   if (StopLoss > 0.0) {
      G_price_224 = NormalizeDouble(G_ilow_240 - LowHighDiff_SL * Point + StopLoss * Point, Digits);
      G_price_232 = NormalizeDouble(G_ihigh_248 + LowHighDiff_SL * Point - StopLoss * Point, Digits);
   }
   if (StopLoss == 0.0) {
      G_price_224 = 0;        // Sellstop SL
      G_price_232 = 0;        // Buystop SL
   }
// Added by Capella ----------------------------------------------------------------	
   double MaxStop = MathMax ( StopLevel, MarketInfo ( Symbol(), MODE_SPREAD ) ) / 100000; 	// Calculate largest value of either spread or Stoplevel	
	double sellprice = G_ilow_240 - LowHighDiff_SL * Point;
	double buyprice = G_ihigh_248 + LowHighDiff_SL * Point;
	double sellstop = G_price_224;
	double buystop = G_price_232;
	if (sellprice > Bid) sellprice = Bid;
	if (buyprice < Ask) buyprice = Ask;
	if (sellstop > Bid) sellstop = Ask - MaxStop;  
	if (buystop < Ask) buystop = Bid + MaxStop;
//----------------------------------------------------------------------------------	
   if (Ask <= G_ilow_240 + LowHighDiff_Open * Point && TimeCurrent() >= time_0 + 195.0)
      if (count_104 == 0 && count_116 == 0 && count_140 == 0 && Li_88 == TRUE) 
		{	
	     result = OrderSend(Symbol(), OP_SELLSTOP, lots_80, NormalizeDouble (sellprice, Digits), Slippage, NormalizeDouble (sellstop, Digits), 0, EA_Comment, MagicNumber, 0, Red);
        if ( result == -1 ) 
         Print ( "SELLSTOP, Lots = ", DoubleToStr ( lots_80, 2 )," Price = ", DoubleToStr ( sellprice, Digits ),", SL = ", DoubleToStr ( sellstop, Digits ) );
      }   
   if (Ask >= G_ihigh_248 - LowHighDiff_Open * Point && TimeCurrent() >= time_0 + 195.0)
      if (count_100 == 0 && count_112 == 0 && count_136 == 0 && Li_88 == TRUE) 
      {
			result = OrderSend(Symbol(), OP_BUYSTOP, lots_80, NormalizeDouble (buyprice, Digits), Slippage, NormalizeDouble (buystop, Digits), 0, EA_Comment, MagicNumber, 0, Blue);
         if ( result == -1 ) 
            Print ( "BUYSTOP, Lots = ", DoubleToStr ( lots_80, 2),", Price = ", DoubleToStr ( buyprice, Digits ),", SL = ", DoubleToStr ( buystop, Digits ) );   
      }
   if (count_100 == 1 || count_104 == 1) {
      for (int pos_196 = 0; pos_196 < OrdersTotal(); pos_196++) {
         if (OrderSelect(pos_196, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
         if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber && OrderTakeProfit() == 0.0) 
				OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderOpenPrice() + TakeProfit * Point, 0, Red);
         if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber && OrderTakeProfit() == 0.0) 
				OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderOpenPrice() - TakeProfit * Point, 0, Red);
      }
   }
   double Ld_200 = 0;
   double Ld_208 = 0;
   double price_216 = 0;
   double price_224 = 0;
   for (int pos_232 = 0; pos_232 < OrdersTotal(); pos_232++) {
      if (OrderSelect(pos_232, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
      if (OrderType() == OP_SELL) Ld_200 += (order_open_price_16 + PriceDiff * Point - OrderOpenPrice()) * OrderLots();
      if (OrderType() == OP_BUY) Ld_208 += (OrderOpenPrice() - (order_open_price_48 - PriceDiff * Point)) * OrderLots();
   }
   if (order_lots_164 > 0.0) price_216 = NormalizeDouble((Ld_200 + lots_80 * TakeProfit * Point) / (LimitOrder_Lots * order_lots_164) + order_open_price_16 + PriceDiff * Point, Digits);
   if (order_lots_172 > 0.0) price_224 = NormalizeDouble((-(Ld_208 + TakeProfit * Point * lots_80) / (LimitOrder_Lots * order_lots_172)) + order_open_price_48 - PriceDiff * Point, Digits);
   if (AccountProfit() < (-AccountBalance()) * DeleteLevel / 100.0) {
      for (int count_236 = 0; count_236 <= 1000; count_236++) {
         for (int pos_240 = 0; pos_240 <= OrdersTotal(); pos_240++) {
            if (OrderSelect(pos_240, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
            Gi_220++;
            if (OrderType() == OP_SELLLIMIT && OrderMagicNumber() == MagicNumber) OrderDelete(OrderTicket());
            if (OrderType() == OP_BUYLIMIT && OrderMagicNumber() == MagicNumber) OrderDelete(OrderTicket());
            if (OrderType() == OP_BUYSTOP && OrderMagicNumber() == MagicNumber) OrderDelete(OrderTicket());
            if (OrderType() == OP_SELLSTOP && OrderMagicNumber() == MagicNumber) OrderDelete(OrderTicket());
            if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber) OrderClose(OrderTicket(), OrderLots(), Bid, 3, White);
            if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber) OrderClose(OrderTicket(), OrderLots(), Ask, 3, White);
            if (OrdersTotal() == 0) break;
         }
         if (OrdersTotal() == 0) break;
         Sleep(10000);
      }
   }
	// default LimitOrders is set to False
   if (LimitOrders == TRUE) 	{
		if (count_104 > 0 && count_116 == 0 && price_216 != 0.0 && count_128 == 0 && count_104 < Max_Orders && Ask > price_216 - LowHighDiff_Open * Point) 
		{
			result = OrderSend(Symbol(), OP_SELLLIMIT, LimitOrder_Lots * order_lots_164, NormalizeDouble (price_216, Digits), Slippage, 0, 0, EA_Comment, MagicNumber, 0, Red);
         if ( result == -1 )
             Print ( "SELLIMIT, Price = ", DoubleToStr ( price_216, Digits ),", SL = 0.00" );        
      }
      if (count_104 >= 2) {
         for (int pos_244 = 0; pos_244 < OrdersTotal(); pos_244++) {
            if (OrderSelect(pos_244, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
            if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber) 
					OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), order_open_price_24 + PriceDiff * Point, 0, Red);
         }
      }
      if (count_100 > 0 && count_112 == 0 && price_224 != 0.0 && count_124 == 0 && count_100 < Max_Orders && Ask < price_224 + LowHighDiff_Open * Point) 
		{
			result = OrderSend(Symbol(), OP_BUYLIMIT, LimitOrder_Lots * order_lots_172, NormalizeDouble (price_224, Digits), Slippage, 0, 0, EA_Comment, MagicNumber, 0, Blue);
         if ( result == -1 )
            Print ( "BUYLIMIT, Price = ", DoubleToStr ( price_224, Digits ),", SL = 0.00" );              
      }
      if (count_100 >= 2) {
         for (int pos_248 = 0; pos_248 < OrdersTotal(); pos_248++) {
            if (OrderSelect(pos_248, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
            if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber) 
					OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), order_open_price_56 - PriceDiff * Point, 0, Red);
         }
      }
   }
}

Recommend