//+------------------------------------------------------------------+
//|                                                   Cornflower.mq4 |
//|                                                         Postrock |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Postrock"
#property link      ""

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+

extern int stoploss = 1000;
extern int target = 1000;
extern int timeframe = 15;
extern double lots = 0.01;
extern int candle = 0;
extern int EMA_trigger = 12;

int no_long;
int no_short;
int time_ok;


int init()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//indicators  
double EMA8 = iMA(NULL,timeframe,8,0,MODE_EMA,PRICE_MEDIAN,candle);  
double EMA12 = iMA(NULL,timeframe,12,0,MODE_EMA,PRICE_MEDIAN,candle); 
double EMA24 = iMA(NULL,timeframe,24,0,MODE_EMA,PRICE_MEDIAN,candle); 
double EMA72 = iMA(NULL,timeframe,72,0,MODE_EMA,PRICE_MEDIAN,candle); 
double EMA_trig = iMA(NULL,timeframe,EMA_trigger,0,MODE_EMA,PRICE_MEDIAN,candle);

//necessary shit
double high = iHigh(0, timeframe, candle);
double low = iLow(0, timeframe, candle); 
int cnt;
int total = OrdersTotal();




/*if (time_ok != 1)   
   {
   Alert ("Timeframe invalid, the supported timeframes are: 0,1,5,15,30,60,240,10080,43200");
   Sleep (20000);
   }*/
   


//Print (GetLastError());

//---------------------------------------------------------------------------------------

if (total == 0)
{

if (EMA12 < EMA8 && EMA24 < EMA12 && EMA72 < EMA24)
   {
   if (Ask <= EMA_trig )
      {
         Print ("Go Long");
         OrderSend(Symbol(),OP_BUY,lots,Ask,3,Ask-stoploss*Point,Ask+target*Point,"LONG",00001,0,Green); 
         Sleep (10000);
      }        
   }
   
if (EMA12 > EMA8 && EMA24 > EMA12 && EMA72 > EMA24)
   {
   if (Bid >= EMA_trig )
      {
         Print ("Go Short");
         OrderSend(Symbol(),OP_SELL,lots,Bid,3,Bid+stoploss*Point,Bid-target*Point,"SHORT",00002,0,Red); 
         Sleep (10000);
      }       
   }

}
   
//--------------------------------------------------------------------------------------- 

for (cnt=0;cnt<5;cnt++)
    {
    if (OrderSelect(cnt, SELECT_BY_POS)==true)
       {
         if ( OrderComment() == "LONG")
            {
            no_long = 1;
            }
         
         if ( OrderComment() == "SHORT")
            {
            no_short = 1;
            }
       }
    }
    
if (total == 0)
   {
      no_long = 0;
      no_short = 0;
   }
   
 
/*if (timeframe != 0)
{
if (timeframe==1||timeframe==5||timeframe==15||timeframe==30||timeframe==60||timeframe==240||timeframe==1440||timeframe==10080||timeframe==43200)
   {
   time_ok = 1;
   }
}*/  
   



   

   return(0);
  }
//+------------------------------------------------------------------+