Questions from Beginners MQL5 MT5 MetaTrader 5 - page 33

 

Good evening! Pretty sure this function will produce exactly what I wanted, well almost. But the machine won't compile, it swears like this

'}' - not all control paths return a value htghtgfhthf.mq5 61 10 It's telling her I have something wrong with logic, not all returns are specified there. Yes, they are! On the contrary, if we put them after each opirator of if, it will be nonsense, the program will immediately produce the wrong result. It's short and to the point. If you can, how can you make it accept it?

string OrderType() { 
 string Si="";

 long lo;
 bool Bull=false; 
 Bull =PositionGetInteger(POSITION_TYPE,lo);
 if ( Bull ) {
 if ( lo==POSITION_TYPE_BUY) Si="OP_BUY";
 if ( lo==POSITION_TYPE_SELL) Si="OP_SELL";
 return (Si); }


 // ENUM_ORDER_TYPE ORType;

 Bull =OrderGetInteger(ORDER_TYPE,lo);
 if ( Bull ) {
 if ( lo==ORDER_TYPE_BUY_LIMIT) Si="OP_BUY_LIMIT";
 if ( lo==ORDER_TYPE_SELL_LIMIT) Si="OP_SELL_LIMIT";
 if ( lo==ORDER_TYPE_BUY_STOP) Si="OP_BUY_STOP";
 if ( lo==ORDER_TYPE_SELL_STOP) Si="OP_SELL_STOP";
 return (Si); }

 }
 
Dimka-novitsek:

Good evening! Pretty sure this function will produce exactly what I wanted, well almost. But the machine won't compile, it swears like this

'}' - not all control paths return a value htghtgfhthf.mq5 61 10 It's telling me I have something wrong with logic, not all returns are specified there. Yes, they are! On the contrary, if we put them after each opirator of if, it will be nonsense, the program will immediately produce the wrong result. It's short and to the point. If you can, how can you make it accept?

A word of advice: learn some technical English. Let me have a look at the code.

As for the subject: the function must return a value of the string type. Inside the function we use the if statement, which contains the return statements. Question: if the if statement detects that Bull == false, what should the function return?

 
Of course, Si="" ! Empty value.
 

Aha!!! Thank you!!!

Thanks!!!!

 
Dimka-novitsek:

Good evening! Pretty sure this function will produce exactly what I wanted, well almost. But the machine won't compile, it swears like this

'}' - not all control paths return a value htghtgfhthf.mq5 61 10 It's telling her I have something wrong with my logic, not all returns are specified there. Yes, they are! On the contrary, if we put them after each opirator of if, it will be nonsense, the program will immediately produce the wrong result. It's short and to the point. If it can, how to make it accept?

not all control paths return a value - there is no full control over value return.

It means that in string OrderType(), which must have a return value since the type is different from void, there is no guaranteed output with a return value.

Although you have set as many as 2 operators return, both of them are protected by the if condition. What will happen if both conditions are false?

Then the machine can jump out the window at will.

By the way, in this function it makes more sense to use switch counter, instead of a bunch of if's, which essentially do the same job as switch.

ZZZY Plus, we should not forget that the position must be preselected, as well as the order. And there is a standard function EnumToString for translation of enumerations into strings, so no need to re-invent the wheel.

 
THANK YOU!!!
 
//+------------------------------------------------------------------+
//|                                                  Sample_Tick.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

int Tick_All;

MqlTick           last_tick;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   Tick_All=0;

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(!SymbolInfoTick(_Symbol,last_tick))
     {
      Print("Failed to get Symbol info!");
     }

   Tick_All++;
   Print(Tick_All,"   ",last_tick.bid,"   ",last_tick.ask,"  ",last_tick.last," ",last_tick.volume);
  }
//+------------------------------------------------------------------+


I don't understand why OnTick is called although the price does not change?????

2012.09.10 11:52:11 Sample_Tick (EURUSD,M5) 493 1.2787 1.2789 1.2788 300000
2012.09.10 11:52:10 Sample_Tick (EURUSD,M5) 492 1.2787 1.2789 1.2788 300000
2012.09.10 11:52:09 Sample_Tick (EURUSD,M5) 491 1.2787 1.2789 1.2788 300000
2012.09.10 11:52:07 Sample_Tick (EURUSD,M5) 490 1.2787 1.2789 1.2788 300000

 
dentraf:

I do not understand why OnTick is triggered although price does not change?????

2012.09.10 11:52:11 Sample_Tick (EURUSD,M5) 493 1.2787 1.2789 1.2788 300000
2012.09.10 11:52:10 Sample_Tick (EURUSD,M5) 492 1.2787 1.2789 1.2788 300000
2012.09.10 11:52:09 Sample_Tick (EURUSD,M5) 491 1.2787 1.2789 1.2788 300000
2012.09.10 11:52:07 Sample_Tick (EURUSD,M5) 490 1.2787 1.2789 1.2788 300000

Is the last price update time unchanged as well?
 
Yedelkin:
the time of the last price update does not change either?

2012.09.10 15:37:20 Sample_Tick (EURUSD.m,M5) 20 1.27841 1.27852 1.27841 2000000 2012.09.10 14:37:19
2012.09.10 15:37:20 Sample_Tick (EURUSD.m,M5) 19 1.27841 1.27852 1.27841 2000000 2012.09.10 14:37:19
2012.09.10 15:37:20 Sample_Tick (EURUSD.m,M5) 18 1.27841 1.27852 1.27841 2000000 2012.09.10 14:37:18

or

2012.09.10 15:37:12 Sample_Tick (EURUSD.m,M5) 6 1.27835 1.27845 1.27835 1000000 2012.09.10 14:37:11
2012.09.10 15:37:12 Sample_Tick (EURUSD.m,M5) 5 1.27835 1.27845 1.27835 1000000 2012.09.10 14:37:11
2012.09.10 15:37:12 Sample_Tick (EURUSD.m,M5) 4 1.27835 1.27845 1.27835 1000000 2012.09.10 14:37:10

 
dentraf:

2012.09.10 15:37:20 Sample_Tick (EURUSD.m,M5) 20 1.27841 1.27852 1.27841 2000000 2012.09.10 14:37:19

Price change time is GMT+3, I think. Whose server is it?