//+------------------------------------------------------------------+
//|                                                 CroisementMM.mq4 |
//|                                                            Brice |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Brice"
#property link      ""

//---- parametres saisis

extern int StopLoss=10;
extern int BreakEven=10;
extern bool Pyramidage=false;
extern int TakeProfit=50;
extern int TrailingStop=15;
extern int Period_MA_Small=5;
extern int Period_MA_Big=10;
extern double lots_en_pourcent=0.05;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
double MA_Small, MA_Big, MA_Small_pre, MA_Big_pre, lotSize;
int buyON=0;
int sellON=0;
MA_Small=iMA(Symbol(),0,Period_MA_Small,0,MODE_SMA,PRICE_CLOSE,0);
MA_Big=iMA(Symbol(),0,Period_MA_Big,0,MODE_EMA,PRICE_CLOSE,0);
MA_Small_pre=iMA(Symbol(),0,Period_MA_Small,0,MODE_SMA,PRICE_CLOSE,1);
MA_Big_pre=iMA(Symbol(),0,Period_MA_Big,0,MODE_EMA,PRICE_CLOSE,1);

//Code ci-dessous visant à savoir s'il y a déjà un ordre d'achat ou de vente ouvert
if (OrdersTotal()>0)
{
   for (int cnt=0;cnt<OrdersTotal();cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS);
      if(OrderType()==OP_BUY) buyON=1;
      if(OrderType()==OP_SELL) sellON=1;   
   }
}
if(MA_Small>MA_Big && MA_Small_pre<MA_Big_pre && buyON==0) //Si la moyenne mobile de petite période est au-dessus de la MM de grande période
{
   lotSize=lots_en_pourcent*AccountEquity()/100;
   OrderSend(Symbol(),OP_BUY,lotSize,Ask,3,0,0); //Ordre d'achat. Les stop loss et take profit sont donnés dans la fonction OrdersList()
}
if(MA_Small<MA_Big && MA_Small_pre>MA_Big_pre && sellON==0) //Si le contraire
{
   lotSize=lots_en_pourcent*AccountEquity()/100;
   OrderSend(Symbol(),OP_SELL,lotSize,Bid,3,0,0); //Ordre de vente
}

if (OrdersTotal()>0) OrdersList();
return(0);
}

//+------------------------------------------------------------------+
//Traitement de chaque ordre, pour fixer un stop loss, un take profit, un breakeven et un trailing stop
//Très inspiré du code de Christophe Sangouard disponible sur o-bo.com
void OrdersList()
{       
    for (int cnt=0;cnt<OrdersTotal();cnt++)
    {
     OrderSelect(cnt, SELECT_BY_POS);     
        if (OrderSymbol()==Symbol())
        {
            if (OrderType()==OP_BUY)
            {
               //----Stop Loss
               if (OrderStopLoss()==0)
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-StopLoss*Point,OrderOpenPrice()+TakeProfit*Point,0,LightGreen);
               }
               //---- Trailing Stop
               if (Bid-OrderStopLoss()>Point*TrailingStop)
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+(TrailingStop/2)*Point,OrderTakeProfit(),0,LightGreen);
               }
               //---- BreakEven
               if (Bid-OrderOpenPrice()>Point*BreakEven&&OrderStopLoss()<OrderOpenPrice())
               {
                  //OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+StopLoss*Point,OrderTakeProfit(),0,LightGreen);
                  if(Pyramidage==true)
                  {
                     OrderSend(Symbol(),OP_BUY,0.1,Ask,30,0,0,NULL,123456,Red);
                  }
               }
            }
            if (OrderType()==OP_SELL)
            {
               //---- Stop Loss
               if (OrderStopLoss()==0)
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+StopLoss*Point,OrderOpenPrice()-TakeProfit*Point,0,Yellow);
               }
               //---- Trailing Stop
               if (OrderStopLoss()-Ask>Point*TrailingStop)
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+(TrailingStop/2)*Point,OrderTakeProfit(),0,LightGreen);
               }
               //---- BreakEven
               if (OrderOpenPrice()-Ask>Point*BreakEven&&OrderStopLoss()>OrderOpenPrice())
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-StopLoss*Point,OrderTakeProfit(),0,LightGreen);
                  if(Pyramidage==true)
                  {
                     OrderSend(Symbol(),OP_SELL,0.1,Bid,30,0,0,NULL,123456,Red);
                  }
               }
            }
         }   
      }
}
//+------------------------------------------------------------------+