EAfor the newbies

Back to topics list To post a new topic, please log in or register
avatar
106
4x_Gypsy 2015.08.01 17:07
 

I put this EA together as a starting point for those who wish to actually learn to program in MQL4. It is a simple Moving average cross EA that uses function calls, print and alert functions and other types of programming that i use when i program my EA's. It Prints all the Error codes and Trade information so you will know what it is actually doing when it is actively running.

As i don't personally use a EA in my trading I can not guarantee anything other than it does compile and the Strategy Tester i have shows a profit.

I don't usually document my programs so please excuse my documentation in this one, i know it is not very good or informative.

For those who know and do more than i please take this and run with it so the newbies can learn by example and actually doing it themselves. All i ask is that any changes that may be made to this EA be reposted for everyone else to use.

I encourage the new programmers to READ,LEARN and PRACTICE. There are people here who will help you but you must at least try and not expect a handout.


Good Luck to everyone

Attached files:
  myfirstea.mq4  (50.70 KB)
avatar
10
NewT 2015.08.01 22:23 #
 

hi 4x Gygsy


thank you for helping traders like me to improve our mql skills.

anyway im a mql beginner too and have some problem.

if possible please look at my code and help me to fix the problem.

Thank you



when signal called and pending order did set, if pending order get not reach and open, EA dose not delete that and dose not set new pending order for new signal.

What should i do now?


input string StartTime      = "00:00";
input string EndTime        = "23:00"; 
input int    Diff           = 150;   
input int    TakeProfit     = 150;   
input int    Stoploss       = 150;
input int    AddPips        = 50;
input double Lotsize        = 0.1; 
input double Multiply       = 2; 
input int    Max_Cycle      = 3;
input bool   BrokerIsECN    = false;
input int    Magic_Number   = 12345;

input string             com1       = "****** Indicator Settings ******";
input int                Shift = 20;

string gs_comment = "SSA";
datetime gd_close_time = TimeCurrent();
bool gd_close_all;
datetime time_bar;
int gd_signal;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--    
  int gd_last_tp=-1; 
  if(!gd_close_all && Hit_TP(gd_last_tp,gd_close_time)) {
  gd_close_all = true; 
  }
  
  if(gd_close_all) {
     CloseAll();
     if(orderCount(-1,Magic_Number)<1) gd_close_all = false;
     return;
  }
       
  double buy_lots=0, sell_lots=0;  
  double buy_sl=0, sell_sl=0;  
  int cnt_trade=0, cnt_buy=0, cnt_sell=0, cnt_bstop=0, cnt_sstop=0;
  datetime last_time=0;
  int last_type =-1;
  double buy_pip_sl=0, sell_pip_sl=0;  
  double buy_pip_tp=0, sell_pip_tp=0;  
  int last_level=0;
  for(int cnt=0;cnt<OrdersTotal();cnt++)  {
   int select =  OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
   if(OrderSymbol()!=Symbol()) continue;
   if(OrderMagicNumber() == Magic_Number) {
      cnt_trade++;
      if(OrderType()<2&&OrderOpenTime() > last_time )  {
        last_time = OrderOpenTime() ;
        last_type =OrderType();
      }
      if(OrderType()==OP_BUY) {
      cnt_buy++;
      buy_lots = OrderLots();
      buy_sl = OrderStopLoss();
      buy_pip_sl = OrderOpenPrice() - OrderStopLoss();
      buy_pip_tp = OrderTakeProfit() - OrderOpenPrice();
      }
      if(OrderType()==OP_SELL) {
      cnt_sell++;
      sell_lots = OrderLots();
      sell_sl = OrderStopLoss();
      sell_pip_tp = OrderOpenPrice() - OrderTakeProfit();
      sell_pip_sl = OrderStopLoss() - OrderOpenPrice();
      }
      if(OrderType()==OP_BUYSTOP) cnt_bstop++;
      if(OrderType()==OP_SELLSTOP) cnt_sstop++;
      last_level = get_number(OrderComment());
      }
  }
  
  double gd_lotsize = CheckLots(Multiply*MathMax(buy_lots,sell_lots));
  
  if(last_level>=Max_Cycle) return;
  
  if(last_type==1 && cnt_bstop < 1) {
  int send = PendingOrder(Symbol(),OP_BUYSTOP,gd_lotsize,sell_sl,sell_sl-sell_pip_sl-AddPips*Point,sell_sl+sell_pip_tp+AddPips*Point,gs_comment+(string)(last_level+1),Magic_Number);
  
  }
  
  if(last_type==0 && cnt_sstop <1 )   {
  int send = PendingOrder(Symbol(),OP_SELLSTOP,gd_lotsize,buy_sl,buy_sl+buy_pip_sl+AddPips*Point,buy_sl-buy_pip_tp-AddPips*Point,gs_comment+(string)(last_level+1),Magic_Number);
  }
  
   if(Time[0]>time_bar && WorkingHour(StartTime,EndTime) ) {
     if(cnt_trade>0) { time_bar = Time[0]; gd_signal =0; }
     else {
     gd_signal = Cal_Signal();
     if(gd_signal>0) time_bar = Time[0];
     }
  }
  
 if(AccountEquity()<Lotsize*MarketInfo(Symbol(),MODE_MARGINREQUIRED)) return;

  if( gd_signal==1 && cnt_trade <1 ) {
     double price_stop;
     if(Highest(10)-High[1]<=Diff*Point) price_stop =High[1]+Diff*Point+Shift*Point; else price_stop = High[1]+Shift*Point;
     double sl =0, tp =0;
     if(Stoploss>0) sl = price_stop - Stoploss*Point;
     if(TakeProfit>0) tp = price_stop + TakeProfit*Point;
     int send = PendingOrder(Symbol(),OP_BUYSTOP,Lotsize,price_stop,sl,tp,gs_comment+"1",Magic_Number);
     if( send >0 ) gd_signal =0;
  }

  if( gd_signal==2 && cnt_trade <1 ) {
     double price_stop;
     if((High[1]-Lowest(10)>=Diff*Point) && (High[1]-Lowest(10)<11*Point)) price_stop =Low[1]-Diff*Point-Shift*Point; else price_stop = Low[1]-Shift*Point;
     double sl =0, tp =0;
     if(Stoploss>0) sl = price_stop + Stoploss*Point;
     if(TakeProfit>0) tp = price_stop - TakeProfit*Point;
     int send = PendingOrder(Symbol(),OP_SELLSTOP,Lotsize,price_stop,sl,tp,gs_comment+"1",Magic_Number);
     if( send >0 ) gd_signal =0;
  }  
  }
//+------------------------------------------------------------------+

int MarketOrder(string a_symbol_0, int a_cmd_8, double a_lots_12, double a_stoploss_28, double a_takeprofit_36,string a_comment, int a_magic) {
   int l_error_64; int l_ticket_60; int l_slippage_68 = 8;
   double a_point = MarketInfo(a_symbol_0,MODE_POINT);
   double a_stoplevel = MarketInfo(a_symbol_0,MODE_STOPLEVEL); 
   double a_price_20 = MarketInfo(a_symbol_0,MODE_ASK);
   color l_color_92 = clrGreen;
   double sl = 0, tp  = 0;
   if( a_stoploss_28 > 0 ) sl  = MathMin(a_price_20 - a_stoploss_28*a_point,a_price_20 - a_stoplevel*a_point);
   if( a_takeprofit_36 > 0 ) tp  = MathMax(a_price_20 + a_takeprofit_36*a_point,a_price_20 + a_stoplevel*a_point);
   if (a_cmd_8 == OP_SELL) { 
   l_color_92 = clrRed; 
   a_price_20 = MarketInfo(a_symbol_0,MODE_BID); 
   if( a_stoploss_28 > 0 ) sl = MathMax(a_price_20+a_stoploss_28*a_point,a_price_20 + a_stoplevel*a_point);
   if( a_takeprofit_36 > 0 ) tp  = MathMin(a_price_20 - a_takeprofit_36*a_point,a_price_20 - a_stoplevel*a_point);
   }   
   if (BrokerIsECN) {
   l_ticket_60 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, a_price_20, l_slippage_68, 0, 0, a_comment, a_magic, 0, l_color_92);
   if (l_ticket_60 > 0 && (sl != 0 || tp !=0 )) int gi_modify = OrderModify(l_ticket_60, OrderOpenPrice(), sl, tp , 0,l_color_92);
   } else {
   l_ticket_60 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, a_price_20, l_slippage_68, sl, tp, a_comment, a_magic, 0, l_color_92);
   }
   if (l_ticket_60 <= 0) {
      l_error_64 = GetLastError();
      return (0);
   }
   return (l_ticket_60);}

bool WorkingHour(string a_starthour , string a_endhour) {
  
  datetime gi_time_01 = StrToTime(TimeToStr(iTime(NULL,PERIOD_D1,0), TIME_DATE) + " " + a_starthour);
  datetime gi_time_02 = StrToTime(TimeToStr(iTime(NULL,PERIOD_D1,0), TIME_DATE) + " " + a_endhour);
 
  if( gi_time_01 > gi_time_02 && TimeCurrent() < gi_time_02 )
  gi_time_01 = StrToTime(TimeToStr(iTime(NULL,PERIOD_D1,1), TIME_DATE) + " " + a_starthour);
    
  datetime datetime_0 = TimeCurrent();
  if ( gi_time_01 < gi_time_02 && gi_time_01 <= datetime_0 && datetime_0 <= gi_time_02 ) return (true);
  if ( gi_time_01 > gi_time_02 && datetime_0 >= gi_time_01 ) return (true);
  
  return (false);
}




int PendingOrder( string a_symbol, int a_type, double a_lots, double a_price, double a_stoploss, double a_takeprofit,string a_comment, int a_magic ) {
  double a_ask = MarketInfo(a_symbol,MODE_ASK);
  double a_bid = MarketInfo(a_symbol,MODE_BID);
  double a_point = MarketInfo(a_symbol,MODE_POINT);
  int a_stoplevel = (int)MarketInfo(a_symbol,MODE_STOPLEVEL);
  datetime expirationTime = 0;
  color  a_color = clrBlue;
  if(a_type==OP_SELLSTOP||a_type==OP_SELLLIMIT ) {
     a_color = clrRed;
  }  
  int ticket;
  ticket=OrderSend(a_symbol,a_type,a_lots,a_price,0,a_stoploss,a_takeprofit,a_comment,a_magic,expirationTime,clrNONE);
  if(ticket==-1) Print("Error " + (string) GetLastError() +" when sending order.");
  return (ticket);
}
avatar
106
4x_Gypsy 2015.08.01 23:11 #
 
NewT:

hi 4x Gygsy


thank you for helping traders like me to improve our mql skills.

anyway im a mql beginner too and have some problem.

if possible please look at my code and help me to fix the problem.

Thank you


when signal called and pending order did set, if pending order getnot reach and open, EA dose not delete that and dose not set new pendingorder for new signal.

What should i do now?


Have you tried compiling this code? I m getting 9 errors.

'MQL_Help.mq4'    MQL_Help.mq4    1    1
'Hit_TP' - function not defined    MQL_Help.mq4    54    24
'CloseAll' - function not defined    MQL_Help.mq4    61    7
'orderCount' - function not defined    MQL_Help.mq4    62    10
'get_number' - function not defined    MQL_Help.mq4    104    21
'CheckLots' - function not defined    MQL_Help.mq4    108    22
'Cal_Signal' - function not defined    MQL_Help.mq4    128    20
'Highest' - wrong parameters count    MQL_Help.mq4    138    10
'Lowest' - wrong parameters count    MQL_Help.mq4    149    19
'Lowest' - wrong parameters count    MQL_Help.mq4    149    55
9 error(s), 0 warning(s)        10    1


To be truthful I may not be the best one to help with this because judging from the errors I suspect you haven't posted all of the code or you are trying something I don't understand.

Maybe someone else can be more helpful.


avatar
10
NewT 2015.08.02 09:10 #
 

thank you dear gypsy

yes i did not post my code completely because there is not any error in the source and 0 error in compiling.

I have just a problem in pending orders function and the code for that is here.

Will be thankful of someone try to help me and if needed i will post the code completely.


Thanks

Back to topics list  

To add comments, please log in or register