Errors, bugs, questions - page 3186

 
ycomp #:

let's say I'm going to do about 20+ StringAdd() calls and with most of those calling IntegerToString() or DoubleToString() before passing the string to StringAdd() and also 20+ StringAdd() calls to add a "\n"

the goal is to make one big string here with all the data on separate lines.

would that probably be faster or slower than just using one big StringFormat() call to create a string from all of those values?

I have chosen not to consider StringConcatenate at all because I'm trying to make my code compatible with both MQL4 and 5 and the syntax is different and because they are variadic functions I can't spin my own function to call them, and I would rather keep the actual calls that pass the data the same for both mql4 and 5 since I have a lot of them.

also, did StringAdd for MQL4 & 5 use to have different syntaxes? because I see it mentioned in an article on the MQL5 site about migrating from MQL4 to 5 but I can't see any difference in syntax or description in the docs for the MQL4 StringAdd() and the MQL5 StringAdd()
I bet on StringFormat(), but you should try and let us know
 

I believe I remember reading somewhere in the docs that, while primitive numeric types are not initialized, that strings are initialized when declared.

However, I cannot find anything about that here: https://www.mql5.com/en/docs/basis/variables/initialization

Therefore I'm unsure why I think I read this in the docs. So it could be:

  1. I'm misremembering and all strings need to be initialized; or
  2. string member variables in classes don't need to be initialized but regular string variables do?; or
  3. string member variables in classes and outside of classes do not need to be initialized?

Could someone please clarify? thanks

Documentation on MQL5: Language Basics / Variables / Initialization of Variables
Documentation on MQL5: Language Basics / Variables / Initialization of Variables
  • www.mql5.com
Initialization of Variables - Variables - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
ycomp #: I remember reading somewhere in the docs that, while primitive numeric types are not initialized, that strings are initialized when declared.
  1. Numerics just contain random junk in MT5 (and MT4 with strict).
  2. There is a difference between a uninitialized string variable (NULL) and a string with an empty string ("").
  3. Always initialize your variables before you read them. Classes are irrelevant.
 

recently I had to return more than one value from a function. I chose to create a class and return and object pointer...

I mean I created a class for the Result Type and returned a pointer to a new object I created of that class e.g. ResultType* calculate();

However I just tested returning a complex struct in MQL (it has a string inside) from a function and that seems to compile.

So should I have created a complex struct instead and returned that?

Please ignore the fact that objects need to be deleted, that is not a big deal for my code.. there is an obvious spot to call delete and I call it there. So the fact that "new" objects need to be deleted is not really important.

the ResultType class itself has only 3 member variables (but many functions) - an enum, double and string. 

update: ok I found this under "return" in the docs

> With  the return operator you can't return any arrays, class objects, variables of compound structure type.

so I guess it is not possible even if though it (returning structs with strings) compiles just fine.

hmm, I even tried creating struct with an object inside.. and returning that struct compiled fine also (MT4).. 

makes me wonder why there is no compile error. Is it something that is allowed but undocumented?
 

Hi Everyone, I am trying to learn how code Time Filter and I wrote all these codes from youtube. I set stop trading time 17:30 at first then changed 17:00 but I am getting stop time 17:30 the old time and TradeIsAllowed: false signal when I test it and  in Comment. Can you help me where the error might be. Thanks.
#include <Trade\Trade.mqh>
CTrade trade;

//Create an string for the start trading time 
input string StartTradingTime="10:00";

//Create an string for the stop trading time 
input string StopTradingTime="17:00";

//create a string for the current time 
string CurrentTime;

//create a variable for trading permission
bool TradingIsAllowed=false;

void OnTick()
  {
   //Get the Ask Price
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   //get the local time 
   datetime time=TimeLocal();
   
   //format the time and create a string
   CurrentTime=TimeToString(time,TIME_MINUTES);
   
   //if it is trading time 
   if(CheckTradingTime()==true)
   if(PositionsTotal()==0)
   {
   trade.Buy(0.10,NULL,Ask,(Ask-200*_Point),(Ask+200*_Point),NULL);
   }
   Comment("TradingIsAllowed: ", TradingIsAllowed,"\n",
            "Current Time: ", CurrentTime,"\n",
            "Start Trading Time: ", StartTradingTime,"\n",
            "Stop Trading Time: ", StopTradingTime);
  
  }
  
bool CheckTradingTime()

  {
  //Allow Trading if substring equals StartTradingTime
  if(StringSubstr(CurrentTime,0,5)==StartTradingTime)
  TradingIsAllowed=true;
  
  //Allow Trading if substring equals StopTradingTime
  if(StringSubstr(CurrentTime,0,5)==StopTradingTime)
  TradingIsAllowed=false;
  
  //return the result to the main function
  return TradingIsAllowed;
  }
 
ycomp #:

recently I had to return more than one value from a function. I chose to create a class and return and object pointer...

I mean I created a class for the Result Type and returned a pointer to a new object I created of that class e.g. ResultType* calculate();

However I just tested returning a complex struct in MQL (it has a string inside) from a function and that seems to compile.

So should I have created a complex struct instead and returned that?

Please ignore the fact that objects need to be deleted, that is not a big deal for my code.. there is an obvious spot to call delete and I call it there. So the fact that "new" objects need to be deleted is not really important.

the ResultType class itself has only 3 member variables (but many functions) - an enum, double and string. 

update: ok I found this under "return" in the docs

> With  the return operator you can't return any arrays, class objects, variables of compound structure type.

so I guess it is not possible even if though it (returning structs with strings) compiles just fine.

hmm, I even tried creating struct with an object inside.. and returning that struct compiled fine also (MT4).. 

makes me wonder why there is no compile error. Is it something that is allowed but undocumented?

There is performance issue, a pointer is 4 or 8 bytes (it's actually an handle in MQL5 not sure how it's coded internally). While returning a structure will copy the full structure on the stack to return it.

Please show your code, and the link to the documentation you refer to.

 
Alain Verleyen #:

There is performance issue, a pointer is 4 or 8 bytes (it's actually an handle in MQL5 not sure how it's coded internally). While returning a structure will copy the full structure on the stack to return it.

Please show your code, and the link to the documentation you refer to.

is performance really an issue if a struct would have only a few variables in it, e.g. a few numeric types and a string?

this code compiles and runs fine, no errors. I did not test the validity of anything beyond that.

> With  the return operator you can't return any arrays, class objects, variables of compound structure type.

but it contains strings and objects. So I wonder why there is no compile time error. Or if this is legal code?

https://docs.mql4.com/basis/operators/return

it specifically says you can't return compound structures, which according to the docs ones that have strings are:

https://docs.mql4.com/basis/types/classes#simple_structure

so I wonder why it can compile when trying to return a struct with a string inside.

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

class Foo {
int g;
};

struct SResult {
private:
   enum SError { 
      
      Invalid,
      NoValid,
      InvalidQty
   };

Foo f;
   double Sqty; 
   string sErrMsg;

   bool hello() { return Sqty < 0; }
};


SResult test() {
   SResult b;
   return b;
}


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   test();
  }
//+------------------------------------------------------------------+

Return Operator - Operators - Language Basics - MQL4 Reference
Return Operator - Operators - Language Basics - MQL4 Reference
  • docs.mql4.com
Return Operator - Operators - Language Basics - MQL4 Reference
 
ycomp #:

is performance really an issue if a struct would have only a few variables in it, e.g. a few numeric types and a string?

this code compiles and runs fine, no errors. I did not test the validity of anything beyond that.

> With  the return operator you can't return any arrays, class objects, variables of compound structure type.

but it contains strings and objects. So I wonder why there is no compile time error. Or if this is legal code?

https://docs.mql4.com/basis/operators/return

it specifically says you can't return compound structures, which according to the docs ones that have strings are:

https://docs.mql4.com/basis/types/classes#simple_structure

so I wonder why it can compile when trying to return a struct with a string inside.

Please don't waste people time posting mql4 question in topic/section about mql5.

There is a dedicated MT4/mql4 section for that.


This part of the documentation seems obsolete in mql5 too.
 
Alain Verleyen #:

Please don't waste people time posting mql4 question in topic/section about mql5.

There is a dedicated MT4/mql4 section for that.


This part of the documentation seems obsolete in mql5 too.

I checked both help files, sorry if I posted the wrong link. But it said the same thing in both. I also tested the code in both, it ran in both, but you asked for code so pasted what I currently still had open. I forgot to mark it as universal code but the question was about MQL5.. MQL5 is my primary MQL development target

 
ycomp #:

I checked both help files, sorry if I posted the wrong link. But it said the same thing in both.

This documentation is obviously obsolete.
Reason: