khramkov / MQL5-JSON-API

Metaquotes MQL5 - JSON - API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

POSITIONS: Damaged parameter of string type

jekroll opened this issue · comments

I have a problem when using action "POSITIONS":

api.construct_and_send({
     "action" => "POSITIONS"
})

The client API was written in Crystal-lang (https://github.com/jekroll/mt5crystal) following your examples based on Python3.

I receive most of time the following data:
{"error":true,"lastError":"5040","description":"Damaged parameter of string type","function":"GetPositions"}

That problem is annoying because MT5 keeps opening a warning window.
It sometimes works properly.

Is it a known bug?
Any advice?

Hello,
my workaround is as follows:
MQL5-JSON-API:

  1. File: iNCLUDE/MQL5-JSON_API/Broker.mqh

In void GetPositions(CJAVal &dataObject)

34 position["type"]=EnumToString(ENUM_POSITION_TYPE(PositionGetInteger(POSITION_TYPE)));
// FDF 2021-05-27:
// CJAVal["key"]=String tries to convert it to int and is causing {'error': True, 'lastError': '5040', 'description': 'Damaged parameter of string type', 'function': 'GetPositions'}
// Reseting eventual errors with line below
mControl.mResetLastError();

While parsing position["type"]=EnumToString() error 5040 was generated.
My workaround was to reset last error after line execution

commented

same issue

I traced this issue to the operator= (string a) in Include/json.mqh because it calls StringToInteger & StringToDouble on all strings assigned! This is bad because those functions expect the strings to contain numbers so will often throw errors, in my case it was the symbol string in the positions call (as my broker puts periods in symbol names so they might look like floats). That said it does not just affect the POSITIONS action - but may cause problems anywhere a string is used in JSON response so it's a high risk bug.

I fixed it by ignoring the errors, not sure what the purpose of trying to convert all strings to integers and doubles would be anyway - trying to detect a more suitable JSON field data type?

My workaround is to change Include/json.mqh @ line 203 to detect if the error state changes then ignore it by resetting.

int err = GetLastError();
m_iv   = StringToInteger (m_sv);
m_dv   = StringToDouble (m_sv);
if ((err == 0) && (GetLastError() != 0)) { //Ignore conversion errors if they occur
   ResetLastError();
}```

i tryed, and it didn't work

i tryed, and it didn't work

I will also take a look at this as well

commented

+1 after recompiling the error is gone please merge the fix from device-host