//+------------------------------------------------------------------+
//+                           Code generated using FxPro Quant 2.1.4 |
//+------------------------------------------------------------------+
#property strict

#define __STRATEGY_MAGIC 1001000000
#define __SLEEP_AFTER_EXECUTION_FAIL 400

//Input variables
input int _MA_Fast_Period = 50;			// MA Fast Period
input int _MA_Slow_Period = 200;			// MA Slow Period
input int _Slippage = 0;			// Slippage
input int _TakeProfit = 200;			// TakeProfit
input int _StopLoss = 200;			// StopLoss
input double _Lots = 1;			// Lots

//Global declaration
bool _Compare;
bool _Compare_1;
bool _MA_cross_down;
bool _MA_cross_up;
int OTy=9, OTi;
double OOP, OL, OTP, OSL;

int init() {

   return(0);
}

int start() {

   
   //Local declaration
   bool _Sell = false;
   bool _Buy = false;
   _Compare = (iMA(Symbol(), 0, _MA_Fast_Period, 0, 1, 0, 0) >= iMA(Symbol(), 0, _MA_Slow_Period, 0, 1, 0, 0));
   _Compare_1 = (iMA(Symbol(), 0, _MA_Fast_Period, 0, 1, 0, 1) >= iMA(Symbol(), 0, _MA_Slow_Period, 0, 1, 0, 1));
   _MA_cross_down = (!_Compare && 
   _Compare_1);
   _MA_cross_up = (_Compare && 
   !_Compare_1);
   if( _MA_cross_down && !__isExist( 0, Symbol() ) ) {
      _Sell = _Lots >= MarketInfo( Symbol() ,MODE_MINLOT) && AccountFreeMarginCheck( Symbol(), 1, _Lots) >= 0;
      if( _Sell == true ) _Sell = OrderSend( Symbol(), 1, _Lots, MarketInfo( Symbol(), MODE_BID ), _Slippage, __stopLossValue( Symbol(), 1, MarketInfo( Symbol(), MODE_BID ), _StopLoss ), __takeProfitValue( Symbol(), 1, MarketInfo( Symbol(), MODE_BID ), _TakeProfit ), "FxProQuant" + "(" + WindowExpertName() + ") " + "", __STRATEGY_MAGIC + 0 ) >= 0;
      if( _Sell == false ) Sleep(__SLEEP_AFTER_EXECUTION_FAIL); 
   }
   if( _MA_cross_up && !__isExist( 0, Symbol() ) ) {
      _Buy = _Lots >= MarketInfo( Symbol() ,MODE_MINLOT) && AccountFreeMarginCheck( Symbol(), 0, _Lots) >= 0;
      if( _Buy == true ) _Buy = OrderSend( Symbol(), 0, _Lots, MarketInfo( Symbol(), MODE_ASK ), _Slippage, __stopLossValue( Symbol(), 0, MarketInfo( Symbol(), MODE_ASK ), _StopLoss ), __takeProfitValue( Symbol(), 0, MarketInfo( Symbol(), MODE_ASK ), _TakeProfit ), "FxProQuant" + "(" + WindowExpertName() + ") " + "", __STRATEGY_MAGIC + 0 ) >= 0;
      if( _Buy == false ) Sleep(__SLEEP_AFTER_EXECUTION_FAIL); 
   }

   if (__isExist(0,Symbol())==false && OTy!=9)
      {if (OTy==0)
          {OrderSend( Symbol(), 4, OL, OOP, _Slippage, OSL, OTP, "FxProQuant" + "(" + WindowExpertName() + ") " + "AP", __STRATEGY_MAGIC + 0 );};
       if (OTy==1)
          {OrderSend( Symbol(), 5, OL, OOP, _Slippage, OSL, OTP, "FxProQuant" + "(" + WindowExpertName() + ") " + "AP", __STRATEGY_MAGIC + 0 );};
       for(int i = 0; i < OrdersTotal(); i++)
          {OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
           if (OrderSymbol()==Symbol() && OrderOpenPrice()==OOP && OrderComment()=="FxProQuant" + "(" + WindowExpertName() + ") " + "AP")
              {OTi=OrderTicket();};
          };
       OTy=9;
       
       };
   return(0);
}

bool __selectOrderByMagic(int magic, string symbol)
{
   for(int i = 0; i < OrdersTotal(); i++)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == __STRATEGY_MAGIC + magic && OrderSymbol() == symbol)
         if (OrderComment()=="FxProQuant" + "(" + WindowExpertName() + ") " + "")
            {OTy=OrderType();
             OOP=OrderOpenPrice();
             OSL=OrderStopLoss();
             OTP=OrderTakeProfit();
             OL=OrderLots();
             OrderDelete(OTi,0);
             OTi=NULL;};
             
         return(true);
   }
   return(false);
}



bool __isExist(int magic, string symbol)
{
   return(__selectOrderByMagic(magic, symbol));
}



double __takeProfitValue(string symbol,int orderType, double price, double points)
{
   if (points == 0)
      return (0);
   else if (orderType == 0 || orderType == 2 || orderType == 4)
      return ( NormalizeDouble( price + MarketInfo( symbol, MODE_POINT ) * points, (int)MarketInfo( symbol, MODE_DIGITS ) ) );
   else 
      return ( NormalizeDouble( price - MarketInfo( symbol, MODE_POINT ) * points, (int)MarketInfo( symbol, MODE_DIGITS ) ) );
}



double __stopLossValue(string symbol, int orderType, double price, double points)
{
   if (points == 0)
      return (0);
   else if (orderType == 0 || orderType == 2 || orderType == 4)
      return ( NormalizeDouble( price - MarketInfo( symbol, MODE_POINT ) * points, (int)MarketInfo( symbol, MODE_DIGITS ) ) );
   else 
      return ( NormalizeDouble( price + MarketInfo( symbol, MODE_POINT ) * points, (int)MarketInfo( symbol, MODE_DIGITS ) ) );
}

