//+------------------------------------------------------------------+
//|                                                     King EA .mq4 |
//|                                                        opensurce |
//|                                                      www.tsd.com |
//+------------------------------------------------------------------+
#property copyright "opensurce"
#property link      "www.tsd.com"




extern int       Magic         = 1110;
extern double    Lots          = 1.0;
extern int       SL            = 25;
extern int       TP            = 5;
extern int       Start         = 21;
extern int       End           = 6;
extern double    RSI_BUY       = 30;
extern double    RSI_SELL      = 70;
extern int       RSI_PERIOD    = 10;
extern int       RSI_TF        = 1;
extern bool      MM            = false;
extern int       LotsPercent   = 10;
extern int    MaxOpenTime_BUY  = 10;
extern int    MaxOpenTime_SELL = 10;
/////////////////////////////////////////
int       MaxLot        = 100;
double    MinLot        = 0.01;
double    RSI_BUY_Zera  = 40;
double    RSI_SELL_Zera = 60;
bool      Compra        = true;
bool      Venda         = true;

int    i,tmp,ordtotal,closeprice,cnt,total;

//+------------------------------------------------------------------+

int isMgNum(int magic)
{
 ordtotal = OrdersTotal();
for ( i = 0; i < ordtotal; i++)
   {
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   if ((OrderMagicNumber() == magic) && (OrderSymbol() == Symbol())) return(1);
   }
}

//+------------------------------------------------------------------+

int init()
{
return(0);
}

//+------------------------------------------------------------------+

int deinit()
{
return(0);
}

//+------------------------------------------------------------------+

int start()
{

//+------------------------------------------------------------------+
//|Easy Money Management                                             |
//+------------------------------------------------------------------+
double free;
tmp = (TimeCurrent()-OrderOpenTime())/36000.0;

if (AccountFreeMargin() < free * 150.0) return(0);

if (MM)
   {
   if (LotsPercent > 0)
      Lots = NormalizeDouble(MathCeil(AccountFreeMargin() / 1000.0 * LotsPercent) / 10.0,1);
         else
      Lots = Lots;
   }
   
   if (Lots < MinLot) Lots = MinLot;
   if (Lots > MaxLot) Lots = MaxLot;
   
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+   

double IN;

double RSI_1   = iRSI(NULL, RSI_TF, RSI_PERIOD, PRICE_CLOSE, 0);
double RSI_2   = iRSI(NULL, RSI_TF, RSI_PERIOD, PRICE_CLOSE, 1);

Comment("Trading hours: ",Start," to ",End,"\n","Trading is disabled");
if ((Start < End) && ((TimeHour(TimeCurrent()) < Start ) || (TimeHour(TimeCurrent()) >= End))) return(0);
else
if ((Start > End) && ((TimeHour(TimeCurrent()) < Start ) && (TimeHour(TimeCurrent()) >= End))) return(0);
else
Comment("Trading hours: ",Start," to ",End,"\n","Trading is enabled");

IN = NormalizeDouble(AccountFreeMargin() / 2500,2);

if ((isMgNum(Magic) == 0) && (RSI_1 < RSI_BUY)&& (RSI_1< RSI_2))
   {
   if (Compra)
   OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Ask - SL * Point, Ask + TP * Point,0,Magic,0,Blue);
   Compra = false;
   Venda = true;
  }
 
  if(MaxOpenTime_BUY>0)
  {
    for(cnt=0;cnt>OrdersTotal();cnt++)
    {
      if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
      {
         if (((NormalizeDouble(tmp,8)-MaxOpenTime_BUY)>=0))
        {     
          RefreshRates();
          if (OrderType()==OP_BUY)
            closeprice=Bid;
          if (OrderClose(OrderTicket(),OrderLots(),closeprice,10,Yellow))
          {
            Print("CloseLong(),OrderSelect():",OrderTicket());
            OrderPrint();
          }
          else 
            Print("Error close Long order :",GetLastError());        
        } 
      }
      else 
        Print("TimeOver",GetLastError());
    } 
    }



if ((isMgNum(Magic) == 0) && (RSI_1 > RSI_SELL)&& (RSI_1> RSI_2))
   {
   if (Venda)
   OrderSend(Symbol(),OP_SELL,Lots,Bid,0,Bid + SL * Point,Bid - TP * Point,0,Magic,0,Red);
   Venda = false;
   Compra = true;
   }
  
  if(MaxOpenTime_SELL>0)
  {
    for(cnt=0;cnt<OrdersTotal();cnt++)
    {
      if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
      {
         if (((NormalizeDouble(tmp,8)-MaxOpenTime_SELL)>=0))
        {     
          RefreshRates();
          if (OrderType()==OP_SELL)
            closeprice=Ask;          
          if (OrderClose(OrderTicket(),OrderLots(),closeprice,10,Yellow))
          {
            Print("CloseShort(),OrderSelect():",OrderTicket());
            OrderPrint();
          }
          else 
            Print("Error close Sell order :",GetLastError());        
        } 
      }
      else 
        Print("TimeOver",GetLastError());
    } 
  }     
   
   
if ( ( RSI_1 < RSI_SELL_Zera ) && ( RSI_1 > RSI_BUY_Zera ))
   {
   Venda = true;
   Compra = true;
}
}