//+------------------------------------------------------------------+
//|                                               Moving Average.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#include <stderror.mqh> 

extern int MagicNumber = 20050610;

extern bool AlertOnly = true;
extern bool SoundAlert  =    false; // 0 = disabled
extern bool EmailAlert  =    true;
extern string UserComment = "MA_EA";
extern string mm                 = "MoneyManagement settings";
extern double Lots               = 0.1;
extern double MaximumRisk        = 0.02;
extern double DecreaseFactor     = 3;
extern string m0                 = "Moving Average Settings";
extern double MA_Period          = 200;
extern string m1                 = " Modes";
extern string m2                 = "0=sma, 1=ema, 2=smma, 3=lwma";
extern int    MA_Mode            = 0;
extern string ap0                = "Applied Price";
extern string ap1                = "0=close, 1=open, 2=high, 3=low, 4=median((h+l/2))";
extern string ap2 =              ", 5=typical((h+l+c)/3), 6=weighted((h+l+c+c)/4)";
extern int    MA_Price           = 0;
extern double MA_Shift           = 0;

extern int     TakeProfit = 130;
extern int     StopLoss=60;//  0= no stoploss
extern int     Slippage = 3;

extern string  ts0 = "---TrailingStopLoss---";
extern string  ts1 = " 1. None";
extern string  ts2 = " 2. Standard at input";
extern string  ts3 = " 3. Trail immediately";
extern int     TrailingStopMethod = 2;
extern string  ts12 = "Settings for Type 2";
extern double  TrailingStop = 20;

double   myPoint;

int init()
{
   myPoint = SetPoint();
   return(0);
  }


void DoAlerts(int cmd)
{
   if (cmd == OP_BUY)
   {
     if (SoundAlert) Alert(Symbol(),"  Price Cross MA  Up @  Hour ",Hour(),"  Minute ",Minute());
     if (EmailAlert) SendMail("BUY " + Symbol(), " Hora " + Hour() + ":" + Minute());
   }
      
   if (cmd == OP_SELL)
   {
      if (SoundAlert) Alert(Symbol()," Price Cross MA Down @  Hour ",Hour(),"  Minute ",Minute());
      if (EmailAlert) SendMail("SELL " + Symbol(), " Hora " + Hour() + ":" + Minute());
   }
}

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   ma=iMA(NULL,0,MA_Period,MA_Shift,MA_Mode,MA_Price,0);
//---- sell conditions
   if(Open[1]>ma && Close[1]<ma)  
     {
      DoAlerts(OP_SELL);
      if (!AlertOnly) OpenTrade(OP_SELL);
      return;
     }
//---- buy conditions
   if(Open[1]<ma && Close[1]>ma)  
     {
      DoAlerts(OP_BUY);
      if (!AlertOnly) OpenTrade(OP_BUY);
      return;
     }
//----
  }
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   ma=iMA(NULL,0,MA_Period,MA_Shift,MA_Mode,MA_Price,0);
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderSymbol()!=Symbol()) continue;
      if(OrderMagicNumber()!=MagicNumber) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Open[1]>ma && Close[1]<ma)
           OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         else
           HandleTrailingStop(OP_BUY, OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit());
         
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[1]<ma && Close[1]>ma)
           OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         else
           HandleTrailingStop(OP_SELL, OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit());
        }
     }
//----
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders
   if(CalculateCurrentOrders()==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }


void OpenTrade(int signal)
{  
  int res, err, ticket;
  double mLots;
  double TPprice,STprice;
  
   RefreshRates();
   
   mLots = LotsOptimized();
      
   if (signal==OP_BUY) 
   {
      res=OrderSend(Symbol(),OP_BUY,mLots,Ask,Slippage,0,0,UserComment,MagicNumber,0,Green);
      if (res > 0)
      {
         ticket = res;
         OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);
         if (StopLoss != 0 || TakeProfit != 0)
	      {
            TPprice = 0;
            if (TakeProfit > 0) TPprice=TakeLong(OrderOpenPrice(), TakeProfit);
            STprice = 0;
            if (StopLoss > 0)
            {
              STprice=StopLong(OrderOpenPrice(), StopLoss);
		        STprice = ValidStopLoss(OP_BUY,Bid, STprice);
		      }   
            
 // Normalize stoploss / takeprofit to the proper # of digits.
            if (Digits > 0) 
            {
              STprice = NormalizeDouble( STprice, Digits);
              TPprice = NormalizeDouble( TPprice, Digits); 
            }
		      ModifyOrder(ticket, OrderOpenPrice(), STprice, TPprice, LightGreen);
		   }
         
      }
   } 
   else if (signal==OP_SELL) 
   {
      res=OrderSend(Symbol(),OP_SELL,mLots,Bid,Slippage,0,0,UserComment,MagicNumber,0,Red);
      if (res > 0)
      {
         ticket = res;
         OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);
         if (StopLoss != 0 || TakeProfit != 0)
	      {
            TPprice = 0;
            if (TakeProfit > 0) TPprice=TakeShort(OrderOpenPrice(),TakeProfit);
            STprice = 0;
            if (StopLoss > 0)
            {
              STprice=StopShort(OrderOpenPrice() ,StopLoss);
		        STprice = ValidStopLoss(OP_SELL,Ask, STprice); 
		      }  
 // Normalize stoploss / takeprofit to the proper # of digits.
            if (Digits > 0) 
            {
               STprice = NormalizeDouble( STprice, Digits);
              TPprice = NormalizeDouble( TPprice, Digits); 
            }
		      ModifyOrder(ticket, OrderOpenPrice(), STprice, TPprice, LightGreen);
		   }
       }
   }
      
   if(res<0)
   {
     err = GetLastError();
     Print("OrderSend failed with error(" + err + ") " + ErrorDescription(err) + " Lots:" + DoubleToStr(mLots, 2) );
   }
   return;
}

int ModifyOrder(int ord_ticket,double op, double oSL, double oTP, color mColor)
{
    int CloseCnt, err;
    double myStop, myTake;
    
    CloseCnt=0;
    while (CloseCnt < 3)
    {
       if (OrderModify(ord_ticket,op,oSL,oTP,0,mColor))
       {
         CloseCnt = 3;
       }
       else
       {
          err=GetLastError();
          Print(CloseCnt," Error modifying order : (", err , ") " + ErrorDescription(err));
         if (err>0) CloseCnt++;
       }
    }
}

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders()
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()!=Symbol()) continue;
      if (OrderMagicNumber()!=MagicNumber) continue;
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }

//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }

double SetPoint()
{
   double mPoint;
   
   if (Digits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}


double StopLong(double price,int stop)
{
 if(stop==0)
  return(0);
 else
  return(price-(stop*myPoint));
}

double StopShort(double price,int stop)
{
 if(stop==0)
  return(0);
 else
  return(price+(stop*myPoint));
}

double TakeLong(double price,int take)
{
 if(take==0)
  return(0);
 else
  return(price+(take*myPoint));
}

double TakeShort(double price,int take)
{
 if(take==0)
  return(0);
 else
  return(price-(take*myPoint));
}

double ValidStopLoss(int type, double price, double SL)
{

   double minstop;
   
   if (SL < 0.1) return(SL);
   
   minstop = MarketInfo(Symbol(),MODE_STOPLEVEL);
   if (Digits == 3 || Digits == 5) minstop = minstop / 10;
   if (type == OP_BUY)
   {
		 if((price - SL) < minstop*myPoint) SL = price - minstop*myPoint;
   }
   if (type == OP_SELL)
   {
       if((SL-price) < minstop*myPoint)  SL = price + minstop*myPoint;  
   }

   return(SL);   
}

//+------------------------------------------------------------------+
//| HandleTrailingStop                                               |
//| Type 1 is do not trail                                           |
//| Type 2 waits for price to move the amount of the trailStop       |
//|        before moving stop loss then moves like type 4            |
//| Type 3 moves the stoploss without delay.                         |
//+------------------------------------------------------------------+
void HandleTrailingStop(int cmd, int ticket, double op, double os, double tp)
{
   switch (TrailingStopMethod)
   {
     case 2 : Delayed_TrailingStop (cmd, ticket, op, os, tp);
              break;
     case 3 : Immediate_TrailingStop (cmd, ticket, op, os, tp);
              break;
	}
}

//+------------------------------------------------------------------+
//|                                       Immediate_TrailingStop.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by MrPip,robydoby314@yahoo.com   |
//|                                                                  |   
//| Moves the stoploss without delay.                                |
//+------------------------------------------------------------------+
void Immediate_TrailingStop(int type, int ticket, double op, double os, double tp)
{

   int digits;
   double pt, pBid, pAsk, BuyStop, SellStop;

   digits = MarketInfo(Symbol( ), MODE_DIGITS);
   
   if (type==OP_BUY)
   {
     pBid = MarketInfo(Symbol(), MODE_BID);
     pt = StopLoss * myPoint;
     if(pBid-os > pt)
     {
       BuyStop = pBid - pt;
       if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
		 BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop);   
       if (os < BuyStop) OrderModify(ticket,op,BuyStop,tp, 0,LightGreen);
		 return;
	  }
   }
   if (type==OP_SELL)
   {
     pAsk = MarketInfo(Symbol(), MODE_ASK);
     pt = StopLoss * myPoint;
     if(os - pAsk > pt)
     {
       SellStop = pAsk + pt;
       if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
       SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);  
       if (os > SellStop) OrderModify(ticket,op,SellStop,tp,0, DarkOrange);
		 return;
     }
   }   
}

//+------------------------------------------------------------------+
//|                                         Delayed_TrailingStop.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by MrPip,robydoby314@yahoo.com   |
//|                                                                  |   
//| Waits for price to move the amount of the TrailingStop           |
//| Moves the stoploss pip for pip after delay.                      |
//+------------------------------------------------------------------+
void Delayed_TrailingStop(int type, int ticket, double op, double os, double tp)
{
   int digits;
   double pt, pBid, pAsk, BuyStop, SellStop;

   pt = TrailingStop * myPoint;
   digits = MarketInfo(Symbol(), MODE_DIGITS);
   
   if (type==OP_BUY)
   {
     pBid = MarketInfo(Symbol(), MODE_BID);
     BuyStop = pBid - pt;
     if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits);
	  BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop);   
     if (pBid-op > pt && os < BuyStop) OrderModify(ticket,op,BuyStop,tp,0,LightGreen);
	  return;
   }
   if (type==OP_SELL)
   {
     pAsk = MarketInfo(Symbol(), MODE_ASK);
     pt = TrailingStop * myPoint;
     SellStop = pAsk + pt;
     if (digits > 0) SellStop = NormalizeDouble( SellStop, digits);
     SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop);  
     if (op - pAsk > pt && os > SellStop) OrderModify(ticket,op,SellStop,tp,0,DarkOrange);
	  return;
   }   
}
//+------------------------------------------------------------------+


