//+------------------------------------------------------------------+
//| if you found this ea usefull please donate into my paypal acc    
            //  rbruckner@gmail.com
//  |                                                                  |
//+------------------- DO NOT REMOVE THIS HEADER --------------------+
// to add magic number
#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
#property copyright "docchiro"
#property link      "rbruckner@gmail.com"
extern int MagicNumber = 0;
extern bool SignalMail = False;
extern bool EachTickMode = True;
extern double Lots = 0.01;
extern int Slippage = 3;
extern bool UseStopLoss = FALSE;
extern int StopLoss = 50;
extern bool UseTakeProfit = FALSE;
extern int TakeProfit = 200;
extern bool UseTrailingStop = false;
extern int TrailingStop = 50;
static int              TimeFrame            = 0;
extern int              MaMetod              =2;
extern int              MaPeriod             =6;
extern int              MaMetod2             =3;
extern int              MaPeriod2            =2;
extern bool             TradeOnColor         = true;
extern int              maxtrades=2;
int                     shift = 1; 
extern bool             reverseLogic = true;
double haOpen;
double haClose;
double haOpenB;
double haCloseB;
double haOpenC;
double haCloseC;
bool OBuy=false;
bool OSell = false;
bool OBuyExit=false;
bool OSellExit = false;
bool buysig = false;
bool sellsig=false;
bool closebuy=false;
bool closesell=false;
int BarCount;
int Current;
datetime orderopentime; 
bool TickCheck = False;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   BarCount = Bars;
   if (EachTickMode) Current = 0; else Current = 1;
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
   int Order = SIGNAL_NONE;
   int Total, Ticket;
   double StopLossLevel, TakeProfitLevel;
   if (EachTickMode && Bars != BarCount) TickCheck = False;
   Total = OrdersTotal();
   Order = SIGNAL_NONE;
   //+------------------------------------------------------------------+
   //| Variable Begin                                                   |
   //+------------------------------------------------------------------+
double Var1 = iOpen(NULL, 0, Current + 1);
double Var2 = iOpen(NULL, 0, Current + 2);
double Var5 = iOpen(NULL, 0, Current + 0);
double Var3 = iClose(NULL, 0, Current + 1);
double Var4 = iClose(NULL, 0, Current + 2); 
string V1;
string V2;
string V3;
string V4;
string V5;
string V1V3;
string V2V4;
//double SMA5  = iMA(NULL,0,5,0,0,0,0);
double Buy1_1 =  Var4  -  Var2 ;
double Buy1_2 = 0;
double Buy2_1 =  Var3  -  Var1 ;
double Buy2_2 = 0;
double Sell1_1 =  Var4  -  Var1 ;
double Sell1_2 = 0;
double Sell2_1 =  Var3  -  Var1 ;
double Sell2_2 = 0;
double CloseBuy1_1 =  Var3  -  Var1 ;
double CloseBuy1_2 = 0;
double CloseSell1_1 =  Var3  -  Var1 ;
double CloseSell1_2 = 0;
double Cl1=White;
double Cl2=White;
  //haOpenM15   =iCustom(NULL,PM15,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,0);
//  haCloseM15  =iCustom(NULL,PM15,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,0);
//  haOpenM15B  =iCustom(NULL,PM15,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,1,0);
//  haCloseM15B =iCustom(NULL,PM15,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,0,0);
haOpen    =iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,Current + 0); // current bar
haClose   =iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,Current + 0); // current bar
haOpenB   =iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,Current + 1); // prev bar
haCloseB  =iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,Current + 1); // prev bar
haOpenC   =iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,Current + 2);
haCloseC  =iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,Current + 2);
      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;
	   }
    //alternative for close order sig
      double cha0 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,0,shift-1);
      double cha1 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,1,shift-1);
      double cha0_2 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,0,shift+1-1);
      double cha1_2 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,1,shift+1-1);
      double cha11 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,shift-1);
      double cha12 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,2,shift+1-1);
      double cha21 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,shift-1);
      double cha22 = iCustom(NULL,0,"Heiken_Ashi_Smoothed",MaMetod,MaPeriod,MaMetod2,MaPeriod2,3,shift+1-1);
      
     if (TradeOnColor) {
	     if ((cha11<cha21 && cha12>cha22) ) 
	       { 
	       
	       closesell=true;
	       }
	     if( (cha11>cha21 && cha12<cha22) )
	      {
	      
	      closebuy=true;
	      }
      } 
      //closebuy=sellsig;
      //closesell=buysig;
    //  FOR CLOSE BUY 
    
    // if(NormalizeDouble(Bid,4)<ha11)
    // closebuy=true;
    // if(NormalizeDouble(Bid,4)>ha11) 
    // closesell=true;
    //}
if( (haOpen<haClose) && (haOpenB<haCloseB) && (haOpenC>haCloseC))
  {  
    RefreshRates();
     
    OBuy = true;
    OSell = false;
    OSellExit = true;
    OBuyExit = false;
    }
  else  
 
    {
    OSell = true;
    OBuy = false;
    OBuyExit = true;
    OSellExit = false;
    }
 
V1 = Var1;
V2 = Var2;
V3 = Var3;
V4 = Var4;
V5 = Var5;
V1V3 = Var1-Var3; 
V2V4 = Var2-Var4;
if(Var1>Var3)
   Cl1 = Blue;
if(Var1<Var3)
   Cl1 = Red;
   
if(Var2>Var4)
   Cl2 = Blue;
if(Var2<Var4)
   Cl2 = Red;
 
 
// display
 
   
   //ObjectCreate("Header1",OBJ_LABEL,0,0,0,0,0);
  // ObjectSet("Header1",OBJPROP_XDISTANCE,160);
   //ObjectSet("Header1",OBJPROP_YDISTANCE,100);
   //ObjectSetText("Header1","Open             Close            Diff",10,"Arial",White);
  
  // ObjectCreate("Header2",OBJ_LABEL,0,0,0,0,0);
   //ObjectSet("Header2",OBJPROP_XDISTANCE,80);
  // ObjectSet("Header2",OBJPROP_YDISTANCE,120);
  // ObjectSetText("Header2","Current",10,"Arial",White);
   
  // ObjectCreate("Header3",OBJ_LABEL,0,0,0,0,0);
  // ObjectSet("Header3",OBJPROP_XDISTANCE,80);
 //  ObjectSet("Header3",OBJPROP_YDISTANCE,140);
  // ObjectSetText("Header3","Prev Bar",10,"Arial",White);
   
  // ObjectCreate("Header4",OBJ_LABEL,0,0,0,0,0);
  // ObjectSet("Header4",OBJPROP_XDISTANCE,80);
  // ObjectSet("Header4",OBJPROP_YDISTANCE,160);
  // ObjectSetText("Header4","2 Bars Back",10,"Arial",White);
  
  // ObjectDelete("OPC1");
  // ObjectCreate("OPC1",OBJ_LABEL,0,0,0,0,0);
  // ObjectSet("OPC1",OBJPROP_XDISTANCE,160);
  // ObjectSet("OPC1",OBJPROP_YDISTANCE,120);
  // ObjectSetText("OPC1",V5,10,"Arial",White);
   
   
   
  //   ObjectDelete("OPB1"); 
 //  ObjectCreate("OPB1",OBJ_LABEL,0,0,0,0,0);
  // ObjectSet("OPB1",OBJPROP_XDISTANCE,160);
  // ObjectSet("OPB1",OBJPROP_YDISTANCE,140);
 //  ObjectSetText("OPB1",V1,10,"Arial",Cl1);
   
 //  ObjectDelete("CLB1");
 //  ObjectCreate("CLB1",OBJ_LABEL,0,0,0,0,0);
 //  ObjectSet("CLB1",OBJPROP_XDISTANCE,240);
 //  ObjectSet("CLB1",OBJPROP_YDISTANCE,140);
//   ObjectSetText("CLB1",V3,10,"Arial",Cl1);
   
 //   ObjectDelete("PrevDiff");
  // ObjectCreate("PrevDiff",OBJ_LABEL,0,0,0,0,0);
  // ObjectSet("PrevDiff",OBJPROP_XDISTANCE,320);
  // ObjectSet("PrevDiff",OBJPROP_YDISTANCE,140);
  // ObjectSetText("PrevDiff",V1V3,10,"Arial",Cl1); 
   
  // ObjectDelete("OPB2");
  // ObjectCreate("OPB2",OBJ_LABEL,0,0,0,0,0);
  // ObjectSet("OPB2",OBJPROP_XDISTANCE,160);
  // ObjectSet("OPB2",OBJPROP_YDISTANCE,160);
  // ObjectSetText("OPB2",V2,10,"Arial",Cl2);
   
 //  ObjectDelete("CLB2");
  // ObjectCreate("CLB2",OBJ_LABEL,0,0,0,0,0);
 //  ObjectSet("CLB2",OBJPROP_XDISTANCE,240);
  // ObjectSet("CLB2",OBJPROP_YDISTANCE,160);
  // ObjectSetText("CLB2",V4,10,"Arial",Cl2);
   
 //   ObjectDelete("2BBDiff");
 //  ObjectCreate("2BBDiff",OBJ_LABEL,0,0,0,0,0);
 //  ObjectSet("2BBDiff",OBJPROP_XDISTANCE,320);
 //  ObjectSet("2BBDiff",OBJPROP_YDISTANCE,160);
 //  ObjectSetText("2BBDiff",V2V4,10,"Arial",Cl2);
   
  //if (tradesperbar == 1 && TimeCurrent() - g_datetime_404 < Period() || TimeCurrent() - g_datetime_408 < Period()) g_count_508 = 1;
  
   //+------------------------------------------------------------------+
   //| Variable End                                                     |
   //+------------------------------------------------------------------+
   //Check position
   bool IsTrade = False;
   for (int i = 0; i < Total; i ++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol()) {
         IsTrade = True;
         if(OrderType() == OP_BUY) {
            //Close
            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Buy)                                           |
            //+------------------------------------------------------------------+
                     if (closebuy==true) 
                     {Order = SIGNAL_CLOSEBUY;
                     //Order = SIGNAL_SELL; 
                     }
            //+------------------------------------------------------------------+
            //| Signal End(Exit Buy)                                             |
            //+------------------------------------------------------------------+
            if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck)  || (!EachTickMode && (Bars != BarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid,4), Slippage, MediumSeaGreen);
               
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(NormalizeDouble(Bid,4), Digits) + " Close Buy");
               if (!EachTickMode) BarCount = Bars;
               IsTrade = False;
               //Order = SIGNAL_SELL;
               continue;
            }
            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {                 
               if(NormalizeDouble(Bid,4) - OrderOpenPrice() > Point * TrailingStop) {
                  if(OrderStopLoss() < NormalizeDouble(Bid,4) - Point * TrailingStop) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid,4) - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                     if (!EachTickMode) BarCount = Bars;
                     continue;  
                  }
               } 
            }
         } else {
            //Close
            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Sell)                                          |
            //+------------------------------------------------------------------+
                     if (closesell==true)  
                     {Order = SIGNAL_CLOSESELL;
                    // Order = SIGNAL_BUY;
                     }
            //+------------------------------------------------------------------+
            //| Signal End(Exit Sell)                                            |
            //+------------------------------------------------------------------+
            if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask,4), Slippage, DarkOrange);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(NormalizeDouble(Ask,4), Digits) + " Close Sell");
               if (!EachTickMode) BarCount = Bars;
               IsTrade = False;
               //Order = SIGNAL_BUY;
               continue;
            }
            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {                 
               if((OrderOpenPrice() - NormalizeDouble(Ask,4)) > (Point * TrailingStop)) {
                  if((OrderStopLoss() > (NormalizeDouble(Ask,4) + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask,4) + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                     if (!EachTickMode) BarCount = Bars;
                     continue;
                  } 
               }
            }
         }
      } 
   }
   //+------------------------------------------------------------------+
   //| Signal Begin(Entry)                                              |
   //+------------------------------------------------------------------+
   if (buysig==true) Order = SIGNAL_BUY;
   if (sellsig==true) Order = SIGNAL_SELL;
   //+------------------------------------------------------------------+
   //| Signal End                                                       |
   //+------------------------------------------------------------------+
 
   //Buy
   if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck)  && (orderopentime+30 < iTime(0,0,0))  || (!EachTickMode && (Bars != BarCount)))) {
      if(!IsTrade) {
         //Check free margin
         if (AccountFreeMargin() < (1000 * Lots)) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }
         if (UseStopLoss) StopLossLevel = NormalizeDouble(Ask,4) - StopLoss * Point; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = NormalizeDouble(Ask,4) + TakeProfit * Point; else TakeProfitLevel = 0.0;
         Ticket = OrderSend(Symbol(), OP_BUY, Lots, NormalizeDouble(Ask,4), Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
         OrderSelect(MagicNumber,SELECT_BY_POS);
         orderopentime =  OrderOpenTime();
         Print("order open time buy   :",orderopentime);
         if(Ticket > 0) {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				Print("BUY order opened : ", OrderOpenPrice());
                if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(NormalizeDouble(Ask,4), Digits) + " Open Buy");
			} else {
				Print("Error opening BUY order : ", GetLastError());
			}
         }
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) BarCount = Bars;
         return(0);
      }
   }
 // Print("order totals ..:",OrdersTotal());
   //Sell
   if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck)   && (orderopentime+30 < iTime(0,0,0)) || (!EachTickMode && (Bars != BarCount)))) {
      if(!IsTrade) {
         //Check free margin
         if (AccountFreeMargin() < (1000 * Lots)) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }
         if (UseStopLoss) StopLossLevel = NormalizeDouble(Bid,4) + StopLoss * Point; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = NormalizeDouble(Bid,4) - TakeProfit * Point; else TakeProfitLevel = 0.0;
         Ticket = OrderSend(Symbol(), OP_SELL, Lots, NormalizeDouble(Bid,4), Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
         OrderSelect(MagicNumber,SELECT_BY_POS);
         orderopentime =  OrderOpenTime();
        
         Print("orderopen time sell   :",orderopentime);
         if(Ticket > 0) {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				Print("SELL order opened : ", OrderOpenPrice());
                if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(NormalizeDouble(Bid,4), Digits) + " Open Sell");
			} else {
				Print("Error opening SELL order : ", GetLastError());
			}
         }
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) BarCount = Bars;
         return(0);
      }
   }
   if (!EachTickMode) BarCount = Bars;
  return(0);
  
  
  
  
}
//+------------------------------------------------------------------+
             
            
Comments