//+------------------------------------------------------------------+
//+                           Code generated using FxPro Quant 2.1.4 |
//+------------------------------------------------------------------+
#property strict

#define __STRATEGY_MAGIC 1001000000
#define __SLEEP_AFTER_EXECUTION_FAIL 400

//Input variables
input double _TAKE_PROFIT = 15;			// TAKE PROFIT
input double _LOTES = 3;			// LOTES

//Global declaration
double _MA_PRESENTE_PERIODO_9;
double _MA_PRESENTE_PERIODO_14;
bool _AND;

int init() {

   return(0);
}

int start() {

   
   //Local declaration
   bool _Vender = false;
   _MA_PRESENTE_PERIODO_9 = iMA(Symbol(), 5, 9, 0, 0, 0, 0);
   _MA_PRESENTE_PERIODO_14 = iMA(Symbol(), 5, 14, 0, 0, 0, 0);
   _AND = ((_MA_PRESENTE_PERIODO_9 < iMA(Symbol(), 5, 9, 1, 0, 0, 0)) && 
   (_MA_PRESENTE_PERIODO_9 < _MA_PRESENTE_PERIODO_14) && 
   (_MA_PRESENTE_PERIODO_14 < iMA(Symbol(), 5, 14, 1, 0, 0, 0)));
   if( _AND ) _Vender = Sell(1, _LOTES, 1, 0, 1, _TAKE_PROFIT, 5, 1, 0, "");

   return(0);
}

bool Sell(int MagicIndex, double Lots, int StopLossMethod, double StopLossPoints, int TakeProfitMethod, double TakeProfitPoints, int Slippage, int MaxOpenTrades,
          int MaxFrequencyMins, string TradeComment)
{
   static double pipSize = 0;   
   if(pipSize == 0) pipSize = Point * (1 + 9 * (Digits == 3 || Digits == 5));

   double sl = 0, tp = 0;
   double stopLossPoints = 0, takeProfitPoints = 0;

   int numberOfOpenTrades = 0;
   
   for(int i=OrdersTotal()-1;i>=0;i--){
      if(!OrderSelect(i, SELECT_BY_POS)) continue;
      if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
      numberOfOpenTrades ++;    
   }   

   if(MaxOpenTrades > 0 && numberOfOpenTrades >= MaxOpenTrades) return(false);  
       
   if(MaxFrequencyMins  > 0)
   {   
      int recentSeconds = MaxFrequencyMins * 60;

      for(int i=OrdersTotal()-1;i>=0;i--){
         if(!OrderSelect(i, SELECT_BY_POS)) continue;
         if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
         if(TimeCurrent() - OrderOpenTime() < recentSeconds) return(false);
      }  

      int hstTotal=OrdersHistoryTotal();
   
      for(int i=hstTotal-1;i>=0;i--)
      {  
         if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
         if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;      
         if(TimeCurrent() - OrderOpenTime() < recentSeconds) return(false);
         break;            
      }  
   }
   
   if(Lots < MarketInfo(Symbol(),MODE_MINLOT)) return(false);
   
   if(AccountFreeMarginCheck(Symbol(), OP_SELL,Lots) <= 0) {
      Print("Sell order error: insufficient capital");
      return(false);
   }     	
   
   if (StopLossPoints > 0)
   {
      if(StopLossMethod == 0)
      {
         sl = NormalizeDouble(Bid + StopLossPoints * Point, Digits);
         stopLossPoints = StopLossPoints;
      }
      else if(StopLossMethod == 1)
      {
         sl = NormalizeDouble(Bid + StopLossPoints * pipSize, Digits);
         stopLossPoints = StopLossPoints * (1 + 9 * (Digits == 3 || Digits == 5));
      }
      else
      {
         sl = StopLossPoints;
         stopLossPoints = (sl - Bid)/Point;         
      }
   }
 
   if (TakeProfitPoints > 0)
   {
      if(TakeProfitMethod == 0)
      {
         tp = NormalizeDouble(Bid - TakeProfitPoints * Point, Digits);
         takeProfitPoints = TakeProfitPoints;
      }
      else if (TakeProfitMethod == 1)
      {
         tp = NormalizeDouble(Bid - TakeProfitPoints * pipSize, Digits);
         takeProfitPoints = TakeProfitPoints * (1 + 9 * (Digits == 3 || Digits == 5));
      }
      else
      {
         tp = TakeProfitPoints;
         takeProfitPoints = (Bid - tp)/Point; 
      }
   }     
      
   double stopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD);
   
   if( (sl > 0 && stopLossPoints <= stopLevel) || (tp > 0 && takeProfitPoints <= stopLevel) )
   {
      Print("Cannot Sell: Stop loss and take profit must be at least " 
      + DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD),0) 
      + " points away from the current price");
      return (false);
   }
      
   RefreshRates();
	int result = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, sl, tp, "FxProQuant" + "(" + WindowExpertName() + ") " + TradeComment,__STRATEGY_MAGIC + MagicIndex);

	if (result == -1){
	   Print("Failed to Sell: " + IntegerToString(GetLastError()));
	   Sleep(__SLEEP_AFTER_EXECUTION_FAIL); 
	   return(false); 
	} 
	
   return(true);	
}
