heikinashi-EA 7.01





//+------------------------------------------------------------------+
//|                                               heikinashi-exp.mq4 |
//|                                  Nick Bilak, beluck[AT]gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Nick Bilak"
#property link      "http://www.mql4.info/"
#include <stdlib.mqh>

extern int expertId = 1;

extern int TakeProfit=70;
extern int StopLoss=90;  
extern int TrailingStop = 50;  

extern double Lots = 0.1;

extern int MaMetod  = 2;
extern int MaPeriod = 1;
extern int MaMetod2  = 3;
extern int MaPeriod2 = 100;

extern bool TradeOnColor=true; //true - trade on color change, false - trade on line cross

extern int    slippage=3;   	//slippage for market order processing
extern int    shift=1;			//shift to current bar, 

extern int    OrderTriesNumber=50; //to repeate sending orders when got some error

extern string    EAName="heikenashi07.01"; 

bool buysig,sellsig,closebuy,closesell; int lastsig,tries,co;


void start()  {


   //---- check for history and trading
   if(Bars<100) return; // just deleted trade alowed

   CheckForSignals();
   co=CalculateCurrentOrders();
   if (co>0) CheckForClose();
   co=CalculateCurrentOrders();
   CheckForOpen();
   
   TrailStop();
        
}


//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders()
  {
   int ord;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==expertId) ord++;
     }
//---- return orders volume
   return(ord);
}

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForSignals() {

      buysig=false;
      sellsig=false;
      closebuy=false;
      closesell=false;

      //indicators variables
      double ha0 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,0,shift);
      double ha1 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,1,shift);
      double ha0_2 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,0,shift+1);
      double ha1_2 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,1,shift+1);
      double ha11 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,shift);
      double ha12 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,shift+1);
      double ha21 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,shift);
      double ha22 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,shift+1);
      double hamax,hamin,hamax1,hamin1;
      hamax=MathMax(MathMax(MathMax(ha11,ha0),ha21),ha1);
      hamin=MathMin(MathMin(MathMin(ha11,ha0),ha21),ha1);
      hamax1=MathMax(MathMax(MathMax(ha12,ha0_2),ha22),ha1_2);
      hamin1=MathMin(MathMin(MathMin(ha12,ha0_2),ha22),ha1_2);
      Comment(hamax,"  ",hamin);
      
      if (TradeOnColor) {
	     if (ha11<ha21 && ha12>ha22) buysig=true;
	     if (ha11>ha21 && ha12<ha22) sellsig=true;
      } else {
	     if (Close[shift]>hamax && Close[shift+1]<=hamax1) buysig=true;
	     if (Close[shift]<hamin && Close[shift+1]>=hamin1) sellsig=true;
	   }
      closebuy=sellsig;
      closesell=buysig;
}

void CheckForOpen() {
   int    res,tr;
//---- sell conditions
   if(sellsig)  {
	   if (co==0 && lastsig!=Time[0]) {
	     res = OpenAtMarket(OP_SELL,Lots);
	     lastsig=Time[0];
	   }
      return;
   }
//---- buy conditions
   if(buysig)  {
	   if (co==0 && lastsig!=Time[0]) {
	     res = OpenAtMarket(OP_BUY,Lots);
	     lastsig=Time[0];
	   }
      return;
   }
}
  
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()  {
   bool bres; int tr;
   for(int i=0;i<OrdersTotal();i++)  {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)  break;
      if(OrderMagicNumber()!=expertId || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
         if (closebuy) {
            bres=CloseAtMarket(OrderTicket(),OrderLots());
            break;
         }
      if(OrderType()==OP_SELL) 
         if (closesell) {
            bres=CloseAtMarket(OrderTicket(),OrderLots());
            break;
         }
   }
}



int OpenAtMarket(int mode,double lot) {
   int    res,tr,col;
   double openprice,sl,tp;
   tries=0;
   while (res<=0 && tries<OrderTriesNumber) {
      tr=0; while (tr<5 && !IsTradeAllowed()) { tr++; Sleep(2000); }
      RefreshRates();
      if (mode==OP_SELL) {
         openprice=Bid; 
         sl=openprice+StopLoss*Point;
         tp=openprice-TakeProfit*Point;
         col=Red;
      } else {
         openprice=Ask;
         sl=openprice-StopLoss*Point;
         tp=openprice+TakeProfit*Point;
         col=Blue;
      }
      res=OrderSend(Symbol(),mode,lot,openprice,slippage,sl,tp,EAName+"_"+expertId,expertId,0,col);
      tries++;
   }
   return(res);
}


bool CloseAtMarket(int ticket,double lot) {
   bool bres=false; int tr;
   tries=0;
   while (!bres && tries<OrderTriesNumber) {
      tr=0; while (tr<5 && !IsTradeAllowed()) { tr++; Sleep(2000); }
      RefreshRates();
      bres=OrderClose(ticket,lot,OrderClosePrice(),slippage,White);
      if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError()));
      tries++;
   }
}


void TrailStop() {
   bool bres;
   double StopLoss;
   if ( TrailingStop > 2 ) {
      for (int i = 0; i < OrdersTotal(); i++) {
         if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
         if ( OrderSymbol() != Symbol() || OrderMagicNumber() != expertId )  continue;
         if ( OrderType() == OP_BUY ) {
            if ( Bid < OrderOpenPrice()+TrailingStop*Point )  continue;
            StopLoss = Bid-TrailingStop*Point;
            if ( StopLoss > OrderStopLoss() ) {
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
					   if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError()));
            }
         }
   
         if ( OrderType() == OP_SELL ) {
            if ( Ask > OrderOpenPrice()-TrailingStop*Point )  continue;
            StopLoss = Ask+TrailingStop*Point;
            if ( StopLoss < OrderStopLoss() ) {
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold);
					   if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError()));
            }
         }
      }
   }
   return;
}



Sample





Analysis



Market Information Used:

Series array that contains close prices for each bar
Series array that contains open time of each bar


Indicator Curves created:


Indicators Used:




Custom Indicators Used:
Heiken_Ashi_Smoothed

Order Management characteristics:
Checks for the total of open orders

It automatically opens orders when conditions are reached
It Closes Orders by itself

Other Features: