//+------------------------------------------------------------------+
//|                                                 RSI Cross EA.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//---- Include
#include <stderror.mqh>
#include <stdlib.mqh>

//---- Extern Variables
extern int     RSI_Period        = 12;
extern int     RSISell           = 80;
extern int     RSIBuy            = 20;
extern int     applied_price = PRICE_CLOSE;

//---- Variables
double         RSICurrent,RSIPrevious;
int            counted_bars, nShift, limit, i;

//---- Prise de position
extern int     TakeProfit        = 15;
extern int     StopLoss          = 20;
int            MagicNumber       = 230307;
string         NameEA            = "RSI Cross EA";
double         myPrice, myStopLoss, myTakeProfit, myLots, PriceNextMove;
int            ticket, cnt, total, digits;
int            Slippage          =0;

//---- Optimisation des Lots
extern double  Lots              = 0.1;

//---  Debugage
extern bool    Debug;
int            OrderErr;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
   {
//---- Expert Advisor
      digits = MarketInfo(Symbol(),MODE_DIGITS);
//----
      return(0);
   }

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
   {
//----
   
//----
      return(0);
   }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
   {
//      counted_bars=IndicatorCounted();
//---- 
//      if(counted_bars<0) 
//         return(-1);
//      if(counted_bars>0) 
//         counted_bars--;
//      limit=Bars;
//      for(i=0; i<limit; i++)
//         {
            RSICurrent = iRSI(NULL,0,RSI_Period,applied_price,0);
            RSIPrevious = iRSI(NULL,0,RSI_Period,applied_price,1);
//---- ALARM! ALARM!   
//---  Buy Alarm
            if(
            (RSICurrent >= RSIBuy
            && RSIPrevious < RSIBuy)
            )
               {
                  //ExtMapBuffer1[i] = Low[i] -nShift*Point;
                  Alert("RSI Cross EA Going for a BUY Trend Sesion ",RSICurrent," Price ",Close[1]," for ", Symbol(),"-",Period());
                  PlaySound("alert.wav");
//                  myLots = LotsOptimized();
                  myLots = Lots;
                  myPrice = MarketInfo(Symbol(), MODE_ASK);
                  myStopLoss = 0;
                  if ( StopLoss > 0 ) 
                     myStopLoss = myPrice - StopLoss * Point ;
                  myTakeProfit = 0;
                  if ( TakeProfit > 0 )
                     myTakeProfit = myPrice + TakeProfit * Point;
// Normalize all price / stoploss / takeprofit to the proper # of digits.
                  if (digits > 0)
                     {
                        myPrice = NormalizeDouble( myPrice, digits);
                        myStopLoss = NormalizeDouble( myStopLoss, digits);
                        myTakeProfit = NormalizeDouble( myTakeProfit, digits);
                     }
                  ticket=OrderSend( Symbol(), OP_BUY, myLots, myPrice, Slippage, myStopLoss, myTakeProfit, "FirstBuy", MagicNumber, 0, Green);
                  if (ticket > 0)
                     {
                        if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) )
                           {
                              if (Debug) 
                                 Print("BUY order opened : ", OrderOpenPrice());
                           }
                        else
                           {
                              OrderErr = GetLastError();
                              Print("Error opening BUY order : (" + OrderErr + ") " + ErrorDescription( OrderErr) );
                           }
                     }
               }
//---  Sell Alarm       
            if (
            (RSICurrent <= RSISell
            && RSIPrevious > RSISell)
            )
               {
                  //ExtMapBuffer2[i] = High[i] + nShift*Point;
                  Alert("RSI Cross EA Going for a SELL Trend Sesion ",RSICurrent," Price ",Close[1]," for ", Symbol(),"-",Period());
                  PlaySound("alert.wav");
                  myLots = Lots;
                  myPrice = MarketInfo(Symbol(), MODE_BID);
                  myStopLoss = 0;
                  if ( StopLoss > 0 ) 
                     myStopLoss = myPrice + StopLoss * Point ;
                  myTakeProfit = 0;
                  if ( TakeProfit > 0 )
                     myTakeProfit = myPrice - TakeProfit * Point;
// Normalize all price / stoploss / takeprofit to the proper # of digits.
                  if (digits > 0)
                     {
                        myPrice = NormalizeDouble( myPrice, digits);
                        myStopLoss = NormalizeDouble( myStopLoss, digits);
                        myTakeProfit = NormalizeDouble( myTakeProfit, digits);
                     }
                  ticket=OrderSend( Symbol(), OP_SELL, myLots, myPrice, Slippage, myStopLoss, myTakeProfit, "First Sell", MagicNumber, 0, Red);
                  if (ticket > 0)
                     {
                        if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) )
                           {
                              if (Debug) 
                                 Print("SELL order opened : ", OrderOpenPrice());
                           }
                        else
                           {
                              OrderErr = GetLastError();
                              Print("Error opening SELL order : (" + OrderErr + ") " + ErrorDescription( OrderErr) );
                           }
                     }
               }
//         }
//----
      return(0);
   }
//+------------------------------------------------------------------+