Heiken299_EA





//+------------------------------------------------------------------+
//|                                               Heiken200.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICMA  20050000

extern double Lots               = 0.1;
extern double MaximumRisk        = 0.5;
extern double TakeProfit = 2000;
extern double TrailingStop = 0;
extern double Stoploss = 60;
extern double risk = 10;
double lotMM;

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   double CheckHeikenAshi;
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
//      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)

// Since you pass the Symbol() you can use this

      if(OrderSymbol()==symbol && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
// =================================================================================
// PYRAMIDING - LINEAR
// Money Management risk exposure compounding
// =================================================================================
   
  
//+------------------------------------------------------------------+
//| Check Heikin Ashi for buy or sell signal                         |
//| Use OP_BUY to check for enter buy or exit sell                   |
//| Use OP_SELL to check for enter sell or exit buy                  |
//+------------------------------------------------------------------+

bool CheckHeikenAshi(int cmd)
{
   double HA0, HA1, HA2, HA3;
   
   HA0 = iCustom(NULL,0,"Heiken Ashi",0,0);
   HA1 = iCustom(NULL,0,"Heiken Ashi",1,0);
   HA2 = iCustom(NULL,0,"Heiken Ashi",2,0);
   HA3 = iCustom(NULL,0,"Heiken Ashi",3,0);
   
   if (cmd == OP_SELL)
   {
     if (HA2 > HA3 && HA1 < HA0) return(true);
   }
   if (cmd == OP_BUY)
   {
      if (HA2 < HA3 && HA1 > HA0) return(true);
   }
   return(false);
   
}


//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   int    res;
   
// Use Variables for speed
// Same values are used to check buy or sell so no need to get values twice
// also easier to read

  double HA0, HA1, HA2, HA3;  // delete this line if you use CheckHeikenAshi function
   double Close1, MA200,AO;
   
   HA0 = iCustom(NULL,0,"Heiken Ashi",0,0);  // delete this line if you use CheckHeikenAshi function
   HA1 = iCustom(NULL,0,"Heiken Ashi",1,0);  // delete this line if you use CheckHeikenAshi function
   HA2 = iCustom(NULL,0,"Heiken Ashi",2,0);  // delete this line if you use CheckHeikenAshi function
   HA3 = iCustom(NULL,0,"Heiken Ashi",3,0);  // delete this line if you use CheckHeikenAshi function
   Close1 = Close[1];
   MA200 = iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,0);
   AO = iAO(NULL,0,0);
   
   
//---- sell conditions
  if (CheckHeikenAshi(OP_SELL) && Close1 < MA200 && AO < 0) // Function version
  // if (HA2 > HA3 && HA1 < HA0 && Close1 < MA200 && AO < 0)
     {
      res=OrderSend(Symbol(),OP_SELL,MathCeil(AccountBalance() * risk / 100000),Bid,1,Bid+Stoploss*Point,Bid-TakeProfit*Point,"",MAGICMA,0,Red);
      return;
     }
//---- buy conditions
  if (CheckHeikenAshi(OP_BUY) && Close1 > MA200 && AO > 0)  // Function version
  // if (HA2 < HA3 && HA1 > HA0 && Close1 > MA200 && AO > 0)
     {
      res=OrderSend(Symbol(),OP_BUY,MathCeil(AccountBalance() * risk / 100000),Ask,1,Ask-Stoploss*Point,Ask+TakeProfit*Point,"",MAGICMA,0,Blue);
      return;
     }
//----
  }
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
// Use Variables for speed
// Same values are used to check close buy or close sell so no need to get values twice
// also easier to read
   double HA0, HA1, HA2, HA3;  // delete this line if you use CheckHeikenAshi function
   
   HA0 = iCustom(NULL,0,"Heiken Ashi",0,0);  // delete this line if you use CheckHeikenAshi function
   HA1 = iCustom(NULL,0,"Heiken Ashi",1,0);  // delete this line if you use CheckHeikenAshi function
   HA2 = iCustom(NULL,0,"Heiken Ashi",2,0);  // delete this line if you use CheckHeikenAshi function
   HA3 = iCustom(NULL,0,"Heiken Ashi",3,0);  // delete this line if you use CheckHeikenAshi function

//----
   for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
          if (CheckHeikenAshi(OP_SELL)) // Function version check Sell condition to exit Buy
        //    if(HA2 > HA3 && HA1 < HA0)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,1,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 // go to short position
           {
            // should it be closed?
         if (CheckHeikenAshi(OP_BUY)) // Function version check Buy condition to exit Sell
       //     if (HA2 < HA3 && HA0 > HA1)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,1,Violet); // close position
               return(0); // exit
              }
            // check for trailing stop
            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);
  }
// the end.
//----

//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+--------------------------------------------------------------



Sample





Analysis



Market Information Used:

Series array that contains close prices for each bar


Indicator Curves created:


Indicators Used:


Moving average indicator
Bill Williams Awesome oscillator


Custom Indicators Used:
Heiken Ashi

Order Management characteristics:
Checks for the total of open orders

It automatically opens orders when conditions are reached
It Closes Orders by itself
It can change open orders parameters, due to possible stepping strategy

Other Features:

BackTest : EURUSD on H1

From 2009-08-01 to 2009-10-01 Profit Factor:0.41 Total Net Profit:-9772.50

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:0.49 Total Net Profit:-9715.00

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-05-01 to 2010-05-31 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-06-01 to 2010-06-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-02-27 Profit Factor:0.72 Total Net Profit:-7575.60

BackTest : USDCAD on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDCHF on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDJPY on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.18 Total Net Profit:-9802.12

Request Backtest for Heiken299_EA


From : (yyyy/mm/dd) To: (yyyy/mm/dd)

Pair: Period: