//+------------------------------------------------------------------+
//| Copyright 2006, Open Source                                      |
//| http://www.finanzaonline.com                                     |
//+------------------------------------------------------------------+
//Modified by KMRunner & Spider4896
//MagicRSI v5

#property copyright "Copyright 2006, Open Source"
#property link      " "
#include <stdlib.mqh>
#define MAGIC 943448
string Name_Expert   = "Magic RSI v5";

extern double Lots          = 1;
extern int    MaxBuyOrders  = 1;  //Leave at 1, no spacing between trades placed
extern int    MaxSellOrders = 1;  //Leave at 1, no spacing between trades placed
extern int    BuyRSI        = 20;
extern int    SellRSI       = 93;
extern double RSITF         = 15;
extern double RSIL          = 7;
extern int    MATF          = 15;
extern double MA1           = 13;
extern double MA2           = 25;
extern double StopLoss     = 34;
extern double TakeProfit    = 999;   //10 is best on EURUSD but if you use Targetlvl please change TP to "2000"
extern double TargetLevel   = 3; //Leave at 9999,This will be updated later (10 works well if wanted to use)     
extern int    Slippage      = 1;
extern bool   LotIncrease   = true;  
extern bool   IsAcctMini    = false;
extern bool   UseSound      = false;


color  clOpenBuy     = Blue;
color  clCloseBuy    = Aqua;
color  clOpenSell    = Red;
color  clCloseSell   = Violet;
color  clModiBuy     = Blue;
color  clModiSell    = Red;

string NameFileSound = "alert.wav";

double StartingBalance = 0;                 
bool   BuyTradeAllowed    = false;              
bool   SellTradeAllowed    = false;             

//+-------------+
//| Custom init |
//|-------------+
int init()
  {
   if(LotIncrease)
     {
      StartingBalance = AccountBalance()/Lots;
     }

  }

//+----------------+
//| Custom DE-init |
//+----------------+

int deinit()
  {
   return(0);
}
//+---------------------------------------------------+
//| Closes all Buys this symbol only                  |
//+---------------------------------------------------+
void CloseAllThisSymbolBuy()
{
 
   for(int i = 0; i < OrdersTotal(); i++)
       {
       OrderSelect(i, SELECT_BY_POS);
       bool result = false;
       if (OrderSymbol()==Symbol() && OrderMagicNumber() == MAGIC)  
         {
           if ( OrderType() == OP_BUY)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
         }
       }
     
  return; 
}
   
//+-----------------------------------------------------+
//| Closes all Sells this symbol only                   |
//+-----------------------------------------------------+
void CloseAllThisSymbolSell()
{

   for(int i = 0; i < OrdersTotal(); i++)
      {
      OrderSelect(i, SELECT_BY_POS);
      bool result = false;
      if (OrderSymbol()==Symbol() && OrderMagicNumber() == MAGIC) 
       {
         if ( OrderType() == OP_SELL)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
       }
     }
 
  return; 
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){
   
   double BuyLots, SellLots, BuyProfit, SellProfit, BuyTarget, SellTarget;
   double OrdersBUY, OrdersSELL;
   int    OrdersPerSymbol;
       
     
   for(int i=0;i<OrdersTotal();i++)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
           {
           OrdersPerSymbol++;
           if(OrderType()==OP_BUY)  OrdersBUY++;
           if(OrderType()==OP_SELL) OrdersSELL++;
           if(OrderType()==OP_BUY)  BuyLots += OrderLots();
           if(OrderType()==OP_SELL) SellLots += OrderLots();
           if (OrderType() == OP_BUY)  BuyProfit  +=  OrderProfit() + OrderSwap() + OrderCommission();
           if (OrderType() == OP_SELL) SellProfit +=  OrderProfit() + OrderSwap() + OrderCommission();
           }
      }
      
      BuyTarget  = Lots * TargetLevel;   
      SellTarget = Lots * TargetLevel;
   if(!IsAcctMini)
      {
      BuyTarget = BuyTarget * 10;
      SellTarget = SellTarget * 10;
      }
                      
   if(BuyProfit  > BuyTarget)  CloseAllThisSymbolBuy(); 
   if(SellProfit > SellTarget) CloseAllThisSymbolSell();
    
   if(StopLoss<10){
      Print("StopLoss less than 10");
      return(0);
   }

   if(StopLoss<10){
      Print("StopLoss less than 10");
      return(0);
   }

   double diRSI0=iRSI(NULL,RSITF,RSIL,PRICE_CLOSE,0);
   double diRSI1=iRSI(NULL,RSITF,RSIL,PRICE_CLOSE,0);
   double MA11 = iMA(NULL,MATF,MA1,0,MODE_LWMA,PRICE_CLOSE,0);
   double MA22 = iMA(NULL,MATF,MA2,0,MODE_LWMA,PRICE_CLOSE,0);

   if(AccountFreeMargin()<(1000*Lots)){
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0);
   }
   
    if(OrdersPerSymbol==0) BuyTradeAllowed=true;
    if(OrdersPerSymbol==0) SellTradeAllowed=true;
    if(OrdersBUY>MaxBuyOrders-1) BuyTradeAllowed=false;
    if(OrdersSELL>MaxSellOrders-1) SellTradeAllowed=false;
      
      if ((diRSI0<BuyRSI && MA11<MA22) && BuyTradeAllowed){   
         OpenBuy();
         return(0);
      }

      if ((diRSI1>SellRSI && MA11>MA22) && SellTradeAllowed){  
         OpenSell();
         return(0);
      }
   
   return (0);
}

void OpenBuy() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 
   ldLot = GetSizeLot(); 
    if(LotIncrease)
     {
      ldLot=NormalizeDouble(AccountBalance()/StartingBalance,2);
     }
   if(ldLot>50) ldLot = 50;  
   ldStop = GetStopLossBuy();
   ldTake = GetTakeProfitBuy(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol(),OP_BUY,ldLot,Ask,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenBuy); 
   if (UseSound) PlaySound(NameFileSound); 
} 
void OpenSell() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 

   ldLot = GetSizeLot(); 
    if(LotIncrease)
     {
      ldLot=NormalizeDouble(AccountBalance()/StartingBalance,2);
     }
   if(ldLot>100) ldLot = 100;  
   ldStop = GetStopLossSell();
   ldTake = GetTakeProfitSell(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol(),OP_SELL,ldLot,Bid,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenSell); 
   if (UseSound) PlaySound(NameFileSound); 
} 
string GetCommentForOrder() { 	return(Name_Expert); } 
double GetSizeLot()         { 	return(Lots); } 
double GetStopLossBuy()     { 	return(Bid-StopLoss*Point);} 
double GetStopLossSell()    { 	return(Ask+StopLoss*Point);}
double GetTakeProfitBuy()   { 	return(Ask+TakeProfit*Point);} 
double GetTakeProfitSell()  { 	return(Bid-TakeProfit*Point);} 

