Navigation:Home > Content >

Simple_S&R-v1.1.mq4

Time: 2014-02-03 | Download file:Simple_S&R-v1.1.mq4

//+------------------------------------------------------------------+
//|                                                   Simple S&R.mq4 |
//|                                        Copyright © 2008, LEGRUPO |
//|                                           http://www.legrupo.com |
//|                                                     Version: 1.1 |
//| History:                                                         |
//| 1.0 => Release version to the public                             |
//| 1.1 => fixes on this releases:                                   |
//|        1) StopLoss bug fixed;                                    |
//|        2) Now the EA close the open and pending orders on the    |
//|           end of the day, you just need to see when day ends     |
//|           on your broker. InterbankFX is GMT, so it is 0 on them |
//|        3) You don't need to manage the MagicNumber anymore       |
//+------------------------------------------------------------------+
#include 
#include 
#include 

#property copyright "Copyright © 2007, LEGRUPO Version 1.6"
#property link      "http://www.legrupo.com"

int ticket = 0;
double Price[2];
double buy_sl, sell_sl;
int    giSlippage;
int ticket_r3, ticket_r2, ticket_r1, ticket_s3, ticket_s2, ticket_s1 = 0;
int cmt_r3, cmt_r2, cmt_r1, cmt_s3, cmt_s2, cmt_s1 = 0;
int order_type = -1;

int ExpertID = 883377;
extern double StopLoss = 50.0;
extern double LotSize = 0.1;
extern bool useStopLoss = true;
extern int Slippage = 1;
extern int Time_when_day_ends = 0;
extern string On_the_end_of_day = "What to do on the end of the day?";
extern bool CloseOpenOrders = false;
extern bool ClosePendingOrders = true;
extern color clOpenBuy = CLR_NONE;
extern color clSellBuy = CLR_NONE;
color ExitLongColor = CLR_NONE;
color ExitShortColor = CLR_NONE;
extern string BuyComment = "Simple S&R BUY";
extern string SellComment = "Simple S&R SELL";

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int MagicNumber = MakeMagicNumber();
   double s1 = NormalizeDouble(ObjectGet("S1", OBJPROP_PRICE1), Digits);
   double s2 = NormalizeDouble(ObjectGet("S2", OBJPROP_PRICE1), Digits);
   double s3 = NormalizeDouble(ObjectGet("S3", OBJPROP_PRICE1), Digits);
   double r1 = NormalizeDouble(ObjectGet("R1", OBJPROP_PRICE1), Digits);
   double r2 = NormalizeDouble(ObjectGet("R2", OBJPROP_PRICE1), Digits);
   double r3 = NormalizeDouble(ObjectGet("R3", OBJPROP_PRICE1), Digits);
   double pivot = NormalizeDouble(ObjectGet("PIVIOT", OBJPROP_PRICE1), Digits);
   
   //Comment("Hour: ", Hour(), " - Time_when_day_ends: ",Time_when_day_ends);
   int iOrders=OrdersTotal()-1, i;
   if(Hour()==Time_when_day_ends) {
      Comment("End of the day, EA is relaxing and closing the trades :)");
      if (CloseOpenOrders) {
         //Comment("Deleting open orders with MagicNumber: ",MagicNumber);
         for(i=iOrders; i>=0; i--) {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderMagicNumber() == MagicNumber)) {
              if((OrderType()<=OP_SELL) && GetMarketInfo()) {
                if(!OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Alert(ErrorDescription(GetLastError()));
              }
            }
          }
      }
      if (ClosePendingOrders) {
         //Comment("Deleting pending orders with MagicNumber: ",MagicNumber);
         for(i=iOrders; i>=0; i--) {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderMagicNumber() == MagicNumber)) {
              if(OrderType()>OP_SELL) {
                if(!OrderDelete(OrderTicket())) Alert(ErrorDescription(GetLastError()));
              }
            }
         }
      }
      return(0);
   }

   if (CountShorts(MagicNumber)!= 0 && CountLongs(MagicNumber)!= 0) {
      return(0);
   }
   
   if (Ask > r3) {
      order_type = OP_SELLSTOP;
   } else {
      order_type = OP_SELLLIMIT;
   }
   if (OrderSelect(ticket_r3, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_r3 = OrderSend(Symbol(),order_type,LotSize,r3,Slippage,r3+StopLoss*Point,s1,SellComment,MagicNumber,0,clSellBuy);
      if (ticket_r3 < 0) {
         Alert("Open SELL order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_r3, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_r3=OrderCloseTime();
         if(cmt_r3>0) { // checa se a ordem foi fechada
           ticket_r3 = 0;
           cmt_r3 = 0;
         }
   }
   
   //r2
   if (Ask > r2) {
      order_type = OP_SELLSTOP;
   } else {
      order_type = OP_SELLLIMIT;
   }
   if (OrderSelect(ticket_r2, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_r2 = OrderSend(Symbol(),order_type,LotSize,r2,Slippage,r3+StopLoss*Point,s1,SellComment,MagicNumber,0,clSellBuy);
      if (ticket_r2 < 0) {
         Alert("Open SELL order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_r2, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_r2=OrderCloseTime();
         if(cmt_r2>0) { // checa se a ordem foi fechada
           ticket_r2 = 0;
           cmt_r2 = 0;
         }
   }
   
   //r1
   if (Ask > r1) {
      order_type = OP_SELLSTOP;
   } else {
      order_type = OP_SELLLIMIT;
   }
   if (OrderSelect(ticket_r1, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_r1 = OrderSend(Symbol(),order_type,LotSize,r1,Slippage,r3+StopLoss*Point,s1,SellComment,MagicNumber,0,clSellBuy);
      if (ticket_r1 < 0) {
         Alert("Open SELL order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_r1, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_r1=OrderCloseTime();
         if(cmt_r1>0) { // checa se a ordem foi fechada
           ticket_r1 = 0;
           cmt_r1 = 0;
         }
   }

   //s3
   if (Ask > s3) {
      order_type = OP_BUYLIMIT;
   } else {
      order_type = OP_BUYSTOP;
   }
   if (OrderSelect(ticket_s3, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_s3 = OrderSend(Symbol(),order_type,LotSize,s3,Slippage,s3-StopLoss*Point,r1,BuyComment,MagicNumber,0,clSellBuy);
      if (ticket_s3 < 0) {
         Alert("Open BUY order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_s3, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_s3=OrderCloseTime();
         if(cmt_s3>0) { // checa se a ordem foi fechada
           ticket_s3 = 0;
           cmt_s3 = 0;
         }
   }
   
   //s2
   if (Ask > s2) {
      order_type = OP_BUYLIMIT;
   } else {
      order_type = OP_BUYSTOP;
   }
   if (OrderSelect(ticket_s2, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_s2 = OrderSend(Symbol(),order_type,LotSize,s2,Slippage,s3-StopLoss*Point,r1,BuyComment,MagicNumber,0,clSellBuy);
      if (ticket_s2 < 0) {
         Alert("Open BUY order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_s2, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_s2=OrderCloseTime();
         if(cmt_s2>0) { // checa se a ordem foi fechada
           ticket_s2 = 0;
           cmt_s2 = 0;
         }
   }
   
   //s1
   if (Ask > s1) {
      order_type = OP_BUYLIMIT;
   } else {
      order_type = OP_BUYSTOP;
   }
   if (OrderSelect(ticket_s1, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_s1 = OrderSend(Symbol(),order_type,LotSize,s1,Slippage,s3-StopLoss*Point,r1,BuyComment,MagicNumber,0,clSellBuy);
      if (ticket_s1 < 0) {
         Alert("Open BUY order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_s1, SELECT_BY_TICKET, MODE_TRADES) == true) {
      cmt_s1=OrderCloseTime();
         if(cmt_s1>0) { // checa se a ordem foi fechada
           ticket_s1 = 0;
           cmt_s1 = 0;
         }
   }
   
   Comment("S1: ",s1,"\nS2: ",s2,"\nS3: ",s3,"\nPivot: ",pivot,"\nR1: ",r1,"\nR2: ",r2,"\nR3: ",r3);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Calculate concurrent Long position                               |
//+------------------------------------------------------------------+
int CountLongs(int MagicNumber)
{
 int count=0;
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade0.0 && Price[1]>0.0);
}

//+------------------------------------------------------------------+
//| Make Magic Number                                                |
//+------------------------------------------------------------------+
int MakeMagicNumber()
{
   int SymbolCode  = 0;
   int PeriodCode  = 0;
   int MagicNumber = 0; 
   
   //---- Symbol Code
        if( Symbol() == "AUDCAD" || Symbol() == "AUDCADm" ) { SymbolCode = 1000; }
   else if( Symbol() == "AUDJPY" || Symbol() == "AUDJPYm" ) { SymbolCode = 2000; }
   else if( Symbol() == "AUDNZD" || Symbol() == "AUDNZDm" ) { SymbolCode = 3000; }
   else if( Symbol() == "AUDUSD" || Symbol() == "AUDUSDm" ) { SymbolCode = 4000; }
   else if( Symbol() == "CHFJPY" || Symbol() == "CHFJPYm" ) { SymbolCode = 5000; }
   else if( Symbol() == "EURAUD" || Symbol() == "EURAUDm" ) { SymbolCode = 6000; }
   else if( Symbol() == "EURCAD" || Symbol() == "EURCADm" ) { SymbolCode = 7000; }
   else if( Symbol() == "EURCHF" || Symbol() == "EURCHFm" ) { SymbolCode = 8000; }
   else if( Symbol() == "EURGBP" || Symbol() == "EURGBPm" ) { SymbolCode = 9000; }
   else if( Symbol() == "EURJPY" || Symbol() == "EURJPYm" ) { SymbolCode = 1000; }
   else if( Symbol() == "EURUSD" || Symbol() == "EURUSDm" ) { SymbolCode = 1100; }
   else if( Symbol() == "GBPCHF" || Symbol() == "GBPCHFm" ) { SymbolCode = 1200; }
   else if( Symbol() == "GBPJPY" || Symbol() == "GBPJPYm" ) { SymbolCode = 1300; }
   else if( Symbol() == "GBPUSD" || Symbol() == "GBPUSDm" ) { SymbolCode = 1400; }
   else if( Symbol() == "NZDJPY" || Symbol() == "NZDJPYm" ) { SymbolCode = 1500; }
   else if( Symbol() == "NZDUSD" || Symbol() == "NZDUSDm" ) { SymbolCode = 1600; }
   else if( Symbol() == "USDCAD" || Symbol() == "USDCADm" ) { SymbolCode = 1700; }
   else if( Symbol() == "USDCHF" || Symbol() == "USDCHFm" ) { SymbolCode = 1800; }
   else if( Symbol() == "USDJPY" || Symbol() == "USDJPYm" ) { SymbolCode = 1900; }
                     
   //---- Calculate MagicNumber
   MagicNumber = ExpertID+SymbolCode;
   return(MagicNumber);
}

Recommend