Help for my first EA

Back to topics list To post a new topic, please log in or register
avatar
9
pipcraftsman 2016.01.04 12:02
 
Hi,
I'm very very new to mql coding, and I'm asking you help with some basic concept.
I'm starting from a very simple attempt: I check if thare aren't orders opened or pending, and I open some orders, market and stop.
When all orders are triggered and closed, in loss or profit, and there are no order pending or opened again, it will place the same orders again.
The problem is I'm not able to put it into a cycle; in strategy tester it puts first 10 orders correctly, and when thery are closed, of course, nothing happens.

I've tried with for or while, but I'm very confused with them.

I've tried to put all the code into void onTick(void) function, but nothing happens..

int start()
{
if (OrdersTotal()==0)
{


OrderSend(Symbol(), OP_BUY, 0.01, NormalizeDouble(Ask,Digits), 0, NormalizeDouble(Ask-sl,Digits),NormalizeDouble(Ask+tp,D igits), "10-20-corretto", 0, 0, Green);
OrderSend(Symbol(), OP_SELL, 0.01, NormalizeDouble(Bid,Digits), 0, NormalizeDouble(Bid+sl,Digits),Norm alizeDouble(Bid-tp,Digits), "10-20-corretto", 0, 0, Green);

OrderSend(Symbol(), OP_BUYSTOP, 0.01, NormalizeDouble(Ask+d,Digits), 0, NormalizeDouble(Ask+d-sl,Digits),NormalizeDouble(Ask+d+tp ,Digits), "10-20-corretto", 0, 0, Green);
OrderSend(Symbol(), OP_BUYSTOP, 0.01, NormalizeDouble(Ask+dueD,Digits), 0, NormalizeDouble(Ask+dueD-sl,Digits),NormalizeDouble(Ask+dueD +tp,Digits), "10-20-corretto", 0, 0, Green);
OrderSend(Symbol(), OP_BUYSTOP, 0.01, NormalizeDouble(Ask+treD,Digits), 0, NormalizeDouble(Ask+treD-sl,Digits),NormalizeDouble(Ask+treD +tp,Digits), "10-20-corretto", 0, 0, Green);
OrderSend(Symbol(), OP_BUYSTOP, 0.01, NormalizeDouble(Ask+quattroD,Digits ), 0, NormalizeDouble(Ask+quattroD-sl,Digits),NormalizeDouble(Ask+quat troD+tp,Digits), "10-20-corretto", 0, 0, Green);
OrderSend(Symbol(), OP_SELLSTOP, 0.01, NormalizeDouble(Bid-d,Digits), 0, NormalizeDouble(Bid-d+sl,Digits),NormalizeDouble(Bid-d-tp,Digits), "10-20-corretto", 0, 0, Green);
OrderSend(Symbol(), OP_SELLSTOP, 0.01, NormalizeDouble(Bid-dueD,Digits), 0, NormalizeDouble(Bid-dueD+sl,Digits),NormalizeDouble(Bid-dueD-tp,Digits), "10-20-corretto", 0, 0, Green);
OrderSend(Symbol(), OP_SELLSTOP, 0.01, NormalizeDouble(Bid-treD,Digits), 0, NormalizeDouble(Bid-treD+sl,Digits),NormalizeDouble(Bid-treD-tp,Digits), "10-20-corretto", 0, 0, Green);
OrderSend(Symbol(), OP_SELLSTOP, 0.01, NormalizeDouble(Bid-quattroD,Digits), 0, NormalizeDouble(Bid-quattroD+sl,Digits),NormalizeDouble (Bid-quattroD-tp,Digits), "10-20-corretto", 0, 0, Green);


}
} 

Thank you very much!

avatar
Moderator
3427
GumRai 2016.01.04 12:32 #
 

If all 10 orders are placed correctly at the beginning, then they should be placed ok when all are closed.

Are you sure that all orders have been closed? All the time that there is 1 or more untriggered pending order, new orders will not be placed. 

avatar
9
pipcraftsman 2016.01.04 12:58 #
 

Hi,

thank you for your rapid answer.

You're right: there were some orders on the chart, far from current price, and I did'nt see them.

Now I've reduced to 4 orders for testing and it's ok.

So does int start() function works as cycle itself? I mean: everything you put in the body, it repeats it every tick?

However i past my complete code, maybe you have suggestion for correct coding:

//+------------------------------------------------------------------+
//|                                                   testergrid.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double lot = 0.01;

int init()
{
return(0);
}



int start()
   {
   
double spread = NormalizeDouble(Ask-Bid, Digits);           
double tp = (1* 10*Point*10)-spread;
double sl = (1* 20*Point*10)-spread;
double d =  (1* 10*Point*10);
double dueD =  (1* 20*Point*10);
double treD =  (1* 30*Point*10);
double quattroD =  (1* 40*Point*10);

if (OrdersTotal()==0)

{
 /*  ObjectCreate("xx", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("xx",OrdersTotal(),17, "Verdana", Green);
   ObjectSet("xx", OBJPROP_CORNER, 0);
   ObjectSet("xx", OBJPROP_XDISTANCE, 20);
   ObjectSet("xx", OBJPROP_YDISTANCE, 40);
   */
   
         OrderSend(Symbol(), OP_BUY, 0.01, NormalizeDouble(Ask,Digits), 0, NormalizeDouble(Ask-sl,Digits),NormalizeDouble(Ask+tp,Digits), "10-20-corretto", 0, 0, Green);
         OrderSend(Symbol(), OP_SELL, 0.01, NormalizeDouble(Bid,Digits), 0, NormalizeDouble(Bid+sl,Digits),NormalizeDouble(Bid-tp,Digits), "10-20-corretto", 0, 0, Green);
        
         OrderSend(Symbol(), OP_BUYSTOP, 0.01, NormalizeDouble(Ask+d,Digits), 0, NormalizeDouble(Ask+d-sl,Digits),NormalizeDouble(Ask+d+tp,Digits), "10-20-corretto", 0, 0, Green);
     //    OrderSend(Symbol(), OP_BUYSTOP, 0.01, NormalizeDouble(Ask+dueD,Digits), 0, NormalizeDouble(Ask+dueD-sl,Digits),NormalizeDouble(Ask+dueD+tp,Digits), "10-20-corretto", 0, 0, Green);
     //    OrderSend(Symbol(), OP_BUYSTOP, 0.01, NormalizeDouble(Ask+treD,Digits), 0, NormalizeDouble(Ask+treD-sl,Digits),NormalizeDouble(Ask+treD+tp,Digits), "10-20-corretto", 0, 0, Green);
     //    OrderSend(Symbol(), OP_BUYSTOP, 0.01, NormalizeDouble(Ask+quattroD,Digits), 0, NormalizeDouble(Ask+quattroD-sl,Digits),NormalizeDouble(Ask+quattroD+tp,Digits), "10-20-corretto", 0, 0, Green);

         OrderSend(Symbol(), OP_SELLSTOP, 0.01, NormalizeDouble(Bid-d,Digits), 0, NormalizeDouble(Bid-d+sl,Digits),NormalizeDouble(Bid-d-tp,Digits), "10-20-corretto", 0, 0, Green);
     //    OrderSend(Symbol(), OP_SELLSTOP, 0.01, NormalizeDouble(Bid-dueD,Digits), 0, NormalizeDouble(Bid-dueD+sl,Digits),NormalizeDouble(Bid-dueD-tp,Digits), "10-20-corretto", 0, 0, Green);
     //    OrderSend(Symbol(), OP_SELLSTOP, 0.01, NormalizeDouble(Bid-treD,Digits), 0, NormalizeDouble(Bid-treD+sl,Digits),NormalizeDouble(Bid-treD-tp,Digits), "10-20-corretto", 0, 0, Green);
     //    OrderSend(Symbol(), OP_SELLSTOP, 0.01, NormalizeDouble(Bid-quattroD,Digits), 0, NormalizeDouble(Bid-quattroD+sl,Digits),NormalizeDouble(Bid-quattroD-tp,Digits), "10-20-corretto", 0, 0, Green);

 /*  ObjectCreate("ObjName", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("ObjName",OrdersTotal(),17, "Verdana", Red);
   ObjectSet("ObjName", OBJPROP_CORNER, 0);
   ObjectSet("ObjName", OBJPROP_XDISTANCE, 20);
   ObjectSet("ObjName", OBJPROP_YDISTANCE, 20);
*/
}
return (0);
}


avatar
9
pipcraftsman 2016.01.04 13:05 #
 

Sorry,

I just have two more ideas:

1. how can i put time filtering throughout the day? Telling the EA: trade from xx.xx am to yy.yy pm.

2. I'd like to try to close all orders, pending and opened, when total profit (I mean difference beetween equity and balance) is equal to x

Can you kindly suggest any coding direction?

Thanks

avatar
70
Keelan 2016.01.04 13:15 #
 

It could be, depending on the expression used.

 

start(){

int infinite = 1;

while(infinite == 1){ //We go around and around until you change the value of infinite

}

 

for(;;){ //Should work

}

 

if(infinite == 1) { //Recursion with an if

start();  

}

 

I dont know if start loops, but there are event driven functions built in, like OnTimer() and OnTick() which will execute your code based on time or new tick data.

Don't use too much recursion though the code segments into your cache and without the right escape logic or purpose you'll just burn out resources. :D 

I use OnTick and then combine it with a homebrew timer for specific function calls.  

avatar
70
Keelan 2016.01.04 13:36 #
 
pipcraftsman:

Sorry,

I just have two more ideas:

1. how can i put time filtering throughout the day? Telling the EA: trade from xx.xx am to yy.yy pm.

2. I'd like to try to close all orders, pending and opened, when total profit (I mean difference beetween equity and balance) is equal to x

Can you kindly suggest any coding direction?

Thanks

If you plan on coding you're probably best learning this stuff, there's plenty of documentation.

Here's some really bad code, should get you started.

void timeBasic(){


   string timestr = TimeToStr(TimeCurrent(),TIME_SECONDS);
   string timeMin = TimeToStr(TimeCurrent(),TIME_MINUTES);
   int hours = Hour();
   int day = DayOfWeek();
   Print("hour is ", hours, " Day of week is ", day);
   string time_var = StringSubstr(timestr,6,2);
   int timevar = StringToInteger(time_var);
   
   if(timevar == 0){ // 1 minute
      if((hours > 6 && hours < 12) && (day != (0 || 6))){
         //Non weekend between 6 and 12 depending on your local time
      }
   }

}

 

Your second question just loop through orders and check OrderProfit() > 0 

avatar
9
pipcraftsman 2016.01.04 16:29 #
 

Thank you, I'll do some job and let you now. Thank you very very much.

Max

avatar
7
shooter 2016.01.05 14:47 #
 

Hi!

I'd like to ask you, if I open an order, why don't delete my integers? I have a lot of question, later I'd like to ask it. Thank you for your help!

 

if(_1feltetelLong==1 && !__isExist(0))
   {
   if(OrderSend(Symbol(),0,_Lots,MarketInfo(Symbol(),MODE_ASK),0,MarketInfo(Symbol(),MODE_ASK)-MarketInfo(Symbol(),MODE_POINT)*_SL,MarketInfo(Symbol(),MODE_ASK)+MarketInfo(Symbol(),MODE_POINT)*_TP,"",__STRATEGY_MAGIC)==true){
   if((_1feltetelLong=0)==true){Print("Nyitu_1L ",_1feltetelLong);};
   if((_1feltetelShort=0)==true){Print("Nyitu_1S ",_1feltetelShort);};
   if((_Nullcross=0)==true){Print("Nyitu_0C ",_Nullcross);};
   
   
   
                     }

 

avatar
7
shooter 2016.01.05 16:58 #
 

My last problem was ok, I can do it.

I'd like to write a simple code. I'd like to do a simple scalping strategy with an exponential moving average. When the ask price = moving average. This is the first criteria. Second:  ask=5pip+MA - open long.

Short position is ASK=MA (1.criteria), ask=MA-5pip. I write here the full code. Please help! What is the problem, why don't open positions after every cross (when ASK=MA)? Thank you for your help!

extern double __STRATEGY_MAGIC=115;

//Inputs

//Declaration
extern double _TP=100,_SL=100;
extern double _Lots=0.01;
extern double _Tick=0.00001;
double _Ask=Ask;

double _Ema;
bool _Compare,_Compare2,_Compare3,_Compare4;
bool _Open_Long;
bool _Open_Short;
int _1feltetelLong=0,_1feltetelShort=0,_Nullcross=0;
int d=0;

int start()
  {
   
   
   _Ema=iMA(Symbol(),0,14,0,1,0,0);
  
  if(!__isExist(0)){
    if (NormalizeDouble(_Ask,5)==NormalizeDouble(_Ema,5))       {
   

   
      _Nullcross=1;Print("Szel ",_Nullcross);
                                        }
   }                                     
  if(_Nullcross==1){
  if(_Ask+0.00005 > _Ema){_1feltetelLong=1;}
  if(_Ema-0.00005 > _Ask){_1feltetelShort=1;}
  
  } 
  
  if(_1feltetelLong==1 && !__isExist(0))
   {
   _Open_Long = OrderSend(Symbol(),0,_Lots,MarketInfo(Symbol(),MODE_ASK),0,MarketInfo(Symbol(),MODE_ASK)-MarketInfo(Symbol(),MODE_POINT)*_SL,MarketInfo(Symbol(),MODE_ASK)+MarketInfo(Symbol(),MODE_POINT)*_TP,"",__STRATEGY_MAGIC);
   _1feltetelLong=0;
   _1feltetelShort=0;
   _Nullcross=0;
   
   
   
                     }
   
   if(_1feltetelShort==1 && !__isExist(0))
   {
   _Open_Short = OrderSend(Symbol(),1,_Lots,MarketInfo(Symbol(),MODE_BID),0,MarketInfo(Symbol(),MODE_BID)+MarketInfo(Symbol(),MODE_POINT)*_SL,MarketInfo(Symbol(),MODE_BID)-MarketInfo(Symbol(),MODE_POINT)*_TP,"",__STRATEGY_MAGIC);
   _1feltetelShort=0;
   _1feltetelLong=0;
   _Nullcross=0;
   
   
   
                        
   } 
  
  for (d=0;d<OrdersTotal();d++)
               {
               if(OrderSelect(__Ticket(0),SELECT_BY_TICKET)==true){
               
               
               if (OrderSymbol() == Symbol() && OrderMagicNumber()== __STRATEGY_MAGIC && OrderType()==OP_BUY )
                     {
                        
                        if (NormalizeDouble(Bid-_Tick,5)>NormalizeDouble(OrderOpenPrice(),5)){
                        if(OrderClose(__Ticket(0),_Lots,Bid,0,Red)==true){
                        _1feltetelLong=0;
                        _1feltetelShort=0;
                        _Nullcross=0;
                        
                        }
                        }
                    
                     }         
                                        
   
                              
               if (OrderSymbol() == Symbol() && OrderMagicNumber()== __STRATEGY_MAGIC && OrderType()==OP_SELL)
                     {
                        if (NormalizeDouble(Ask+_Tick,5)<NormalizeDouble(OrderOpenPrice(),5)){
                        if(OrderClose(__Ticket(0),_Lots,Ask,0,Blue)==true){
                        _1feltetelLong=0;
                        _1feltetelShort=0;
                        _Nullcross=0;
                        }
                        }
                              }
  
               }
  }
  
return(0);
}

bool __selectOrderByMagic(int __magic){for(int __i=0;__i<OrdersTotal();__i++){if(OrderSelect(__i,SELECT_BY_POS,MODE_TRADES)&&OrderMagicNumber()==__STRATEGY_MAGIC+__magic)return(true);}return(false);}
bool __isExist(int __magic){return(__selectOrderByMagic(__magic));}
int __Ticket(int __magic){if(!__selectOrderByMagic(__magic))return(0);return(OrderTicket());}

 

Back to topics list  

To add comments, please log in or register