Download MetaTrader 5

EA open a position late (For beginners)

To add comments, please log in or register
Find out how social trading can improve your profits
David Ricardo Balaguera Mesa
10
David Ricardo Balaguera Mesa 2014.09.11 02:18 
Hi im new in this forum and i can not do than the EA buy in the open the candlestick, therefore very expensive purchase and sell very cheap

//+------------------------------------------------------------------+
//|                                              Ichimoku-Simple.mq4 |
//|                                     David Ricardo Balaguera Mesa |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "David Ricardo Balaguera Mesa"
#property link      "https://www.mql5.com"
#property version   "1.00"

extern int  Tenkan=8;
extern int  Kinjun=22;
extern int  SenkouB=44;
extern int  ShiftCloud=10;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

#define MAGICMA  2
int CalculateCurrentOrders(string symbol)
{
   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)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//--- return orders volume
   if(buys>0) return(buys);
   if(sells>0)       return(sells);
   return 0;
  }
  
void CheckForOpen()
{
   double t,k,sa,sb,open,close,resb,e,e2,ress=0;
   bool    res=false;

//--- get info
   e=GetLastError();
   Alert("error open=",e); 
   t=iCustom(Symbol(),Period(),"Ichimoku2",Tenkan,Kinjun,SenkouB,ShiftCloud,MODE_TENKANSEN,0,0); //Valor Tenkan 
   //Alert("t = ",t);
   k=iCustom(Symbol(),Period(),"Ichimoku2",Tenkan,Kinjun,SenkouB,ShiftCloud,MODE_KIJUNSEN,1,0);//Valor Kinjun
   //Alert("k = ",k);
   sa=iCustom(Symbol(),Period(),"Ichimoku2",Tenkan,Kinjun,SenkouB,ShiftCloud,MODE_SENKOUSPANA,5,0);//Valor senkou A
   //Alert("senkou a = ",sa);
   
   sb=iCustom(Symbol(),Period(),"Ichimoku2",Tenkan,Kinjun,SenkouB,ShiftCloud,MODE_SENKOUSPANB,6,0);//Valor Senkou B
   Alert("senkou b open= ",sb);

   close=iClose( Symbol() , PERIOD_M15 , 1);
   open=iOpen (Symbol() , PERIOD_M15 , 1);
   //e2=GetLastError();
   //Alert("error open",e2);         
         Alert(" Iclose open =",close);
//--- sell conditions   
   if(k<sa && k<sb && close<sa && close<sb)
     {
      while(ress==-1)
      {
         ress=OrderSend(Symbol(),OP_SELL,0.01,Bid,2,0,0,"",MAGICMA,Red);
         Alert("Ok sell= ",ress);
         //Alert("Osenkou a sell= ",sa);
         //Alert("Osenkou b sell= ",sb);
         //Alert("Oclose1 sell=",close);
         //Alert("Oclose2 sell=",close); 
         
      }
      
      
     }
//--- buy conditions
   if(k>sa && k>sb && close>sa && close>sb)
     {
      do
      {
         resb=OrderSend(Symbol(),OP_BUY,0.01,Ask,2,0,0,"",MAGICMA,0,Blue);
         Alert("Ok buy= ",k);
         Alert("Osenkou a buy= ",sa);
         Alert("Osenkou b buy= ",sb);
         Alert("Oclose1 buy=",close);
         Alert("Oclose2 buy=",close); 
      } while(resb==-1);
      
     }
//---
}

void CheckForClose()
  {
      double t,k,sa,sb,close,a;
//--- go trading only for first tiks of new bar

   t=iCustom(Symbol(),Period(),"Ichimoku2",Tenkan,Kinjun,SenkouB,ShiftCloud,MODE_TENKANSEN,0,0); //Valor Tenkan 
   //Alert("t = ",t);
   k=iCustom(Symbol(),Period(),"Ichimoku2",Tenkan,Kinjun,SenkouB,ShiftCloud,MODE_KIJUNSEN,1,0);//Valor Kinjun
   //Alert("k = ",k);
   sa=iCustom(Symbol(),Period(),"Ichimoku2",Tenkan,Kinjun,SenkouB,ShiftCloud,MODE_SENKOUSPANA,2,0);//Valor senkou A
   //Alert("senkou a = ",sa);
   sb=iCustom(Symbol(),Period(),"Ichimoku2",Tenkan,Kinjun,SenkouB,ShiftCloud,MODE_SENKOUSPANB,3,0);//Valor Senkou B
   //Alert("senkou b close= ",sb);
   close=iClose( Symbol() , PERIOD_M15 , 1);
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //--- check order type 
      
      // e=GetLastError();
      //Alert("error",e);

      //Alert(" Iclose =",close);
      if(OrderType()==OP_BUY)
        {
        
         //if(k<sa && k<sb && close<sa && close<sb)OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         if((k<=sa && k>=sb && close<=sa && close>=sb) || (k>=sa && k<=sb && close>=sa && close<=sb) || (k<=sa && k>=sb && close<=sa && close<=sb)||(k>=sa && k<=sb && close<=sa && close<=sb) ||(k<=sa && k<=sb && close<=sa && close<=sb) ) 
         {
            OrderClose(OrderTicket(),0.01,Bid,2,White);

            Alert("Ck buy= ",k);
            Alert("Csenkou a buy= ",sa);
            Alert("Csenkou b buy= ",sb);
            Alert("Cclose1 buy=",close);
            Alert("Cclose2 buy=",close);
            
         }
         
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if((k<=sa && k>=sb && close<=sa && close>=sb) || (k>=sa && k<=sb && close>=sa && close<=sb) || (k<=sa && k>=sb && close>=sa && close>=sb)||(k>=sa && k<=sb && close>=sa && close>=sb) ||(k>=sa && k>=sb && close>=sa && close>=sb)  )
         {
            //while(res==false)
            //{x
            OrderClose(OrderTicket(),0.01,Ask,2,White);
            Alert("Ck sell= ",k);
            Alert("Csenkou a sell= ",sa);
            Alert("Csenkou b sell= ",sb);
            Alert("Cclose1 sell=",close);
            Alert("Cclose2 sell=",close);               
            //}
         }
         break;
        }
     }
//---
  }
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
     if(Bars<100 || IsTradeAllowed()==false)
      return;
//--- calculate open orders by current symbol

   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
   

  }
//+------------------------------------------------------------------+
To add comments, please log in or register