//+------------------------------------------------------------------+
//|                                               Heiken299_EApk.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
// edits by P. Kubitzki, fxtogo4me@yahoo.com
// 9/09/07 PK changed iCustom Heiken Ashi to Heiken_Ashi_Smoothed
// change MA200 to MA24, running on 15M MA200 is too far out
// change signal candle from 0 to 1 on HA
// 9/10/07 2:50pm KC remove all Awesome Oscillator coding, it delays entry
#define MAGICMA  20050000
extern double Lots               = 0.1;
extern double MaximumRisk        = 0.5;
extern double TakeProfit = 2000; //TakeProfit in not really used, it closes at reverse direction
extern double TrailingStop = 0;
extern double Stoploss = 70; //changed from 60
extern double risk = 2; // changed from 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_Smoothed",0,1);
   HA1 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",1,1);
   HA2 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",2,1);
   HA3 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",3,1);
   
   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, MA24; //, AO;
   
   HA0 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",0,1);  // delete this line if you use CheckHeikenAshi function
   HA1 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",1,1);  // delete this line if you use CheckHeikenAshi function
   HA2 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",2,1);  // delete this line if you use CheckHeikenAshi function
   HA3 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",3,1);  // delete this line if you use CheckHeikenAshi function
   Close1 = Close[1];
//   MA200 = iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,0); previous
   MA24 = iMA(NULL,0,24,0,MODE_EMA,PRICE_CLOSE,1);
   Comment ("\nUSE ON ANY CHART TIMEFRAME","\nEMA24[1] = ",MA24,"\nHA1: High of Blue OR Low of Red HA Candle","\nHA1 = ",HA1);
//   AO = iAO(NULL,0,0);
   
   
//---- sell conditions
  if (CheckHeikenAshi(OP_SELL) && Close1 < MA24 && Bid < HA1) // && AO < 0) // Function version
  // may need to change Bid< HA1 to Bid < Low[1] PK
  
  // 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 > MA24 && Bid > HA1) // && AO > 0)  // Function version
    // may need to change Bid > HA1 to Bid > High[1] PK
  // 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, MA24;  // delete this line if you use CheckHeikenAshi function
   
   HA0 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",0,1);  // delete this line if you use CheckHeikenAshi function
   HA1 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",1,1);  // delete this line if you use CheckHeikenAshi function
   HA2 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",2,1);  // delete this line if you use CheckHeikenAshi function
   HA3 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",3,1);  // delete this line if you use CheckHeikenAshi function
   MA24 = iMA(NULL,0,24,0,MODE_EMA,PRICE_CLOSE,1);
   Comment ("\nUSE ON ANY CHART TIMEFRAME","\nEMA24[1] = ",MA24,"\nHA1: High of Blue OR Low of Red HA Candle","\nHA1 = ",HA1);
//----
   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();
//----
  }
//+--------------------------------------------------------------
             
            
Comments