//+------------------------------------------------------------------+
//|                                                        222.mq4   |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern double TakeProfit   = 500;
extern double Lots         = 0.1;
extern double Stoploss     = 30;
extern double TrailingStop = 30;

extern int HMA_Period      = 20;
extern int HMA_PriceType   = 0;
extern int HMA_Method      = 3;




//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double a;
int b;
int total, ticket, cnt;

 int i, counted_bars=IndicatorCounted();
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   



if(Bars<100);
{
Print("bars less than 100");
return(0);
}


if(TakeProfit<10);
{
Print("TakeProfit less than 10");
return(0);
}

total=OrdersTotal();
if(total<1);

{

if(AccountFreeMargin()<(1000*Lots));
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}

if(iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,1,i+1)<100000 
    && iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,1,i+1)>iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,1,2)
    && iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,1,i+2)<=iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,1,3))
    

{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoploss*Point, Ask+TakeProfit*Point ,"comment",16384,0,Green);


if(ticket>0);
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}



if(iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,3,1)<100000 
    && iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,3,1)<iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,3,2)
    && iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,3,2)>=iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,3,3)) 
     
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+Stoploss*Point,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
if(ticket>0);
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}


for(cnt=0;
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()); 
{
if(OrderType()==OP_BUY); 
{

if(iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,3,1)<100000 
    && iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,3,1)<iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,3,2)
    && iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,3,2)>=iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,3,3)) 
     
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0);
{
if(Bid-OrderOpenPrice()>Point*TrailingStop);
{
if(OrderStopLoss()<Bid-Point*TrailingStop);
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else 
{

if(iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,1,1)<100000 
    && iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,1,1)>iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,1,2) 
    && iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,1,2)<=iCustom(NULL,0,"HMA Color",HMA_Period,HMA_PriceType,HMA_Method,TRUE,2,0,1,3)) 
   
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); 
}

if(TrailingStop>0);
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop));
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0));
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}


