darwinex / dwx-zeromq-connector

Wrapper library for algorithmic trading in Python 3, providing DMA/STP access to Darwinex liquidity via a ZeroMQ-enabled MetaTrader Bridge EA.
https://blog.darwinex.com/zeromq-interface-python-r-metatrader4/
BSD 3-Clause "New" or "Revised" License
341 stars 227 forks source link

DWX_ClosePartial function works with order without selecting it via OrderSelect #98

Closed mashida closed 3 years ago

mashida commented 3 years ago

https://github.com/darwinex/dwx-zeromq-connector/blob/f416e3cc569dfe9b3ce59253f3a118308dd0fab7/v2.0.1/mql4/DWX_ZeroMQ_Server_v2.0.1_RC8.mq4#L917

When this function starts - it reads the data from Metatrader about order which is selected by OrderSelect function, like in DWX_ModifyOrder function.

But, as order with ticket we send into this function is not selected - EA start to partial close some other order. So it should be selected first.

elvinex commented 3 years ago

Hi, thank you for raising this issue. If the ticket sent from python is not zero, it will return false if the order could not be selected:

if(ticket != 0) {
  zmq_ret = zmq_ret + "'_action': 'CLOSE', '_ticket': " + IntegerToString(ticket);

  if (OrderSelect(ticket, SELECT_BY_TICKET)) {
     zmq_ret = zmq_ret + ", '_response': 'CLOSE_PARTIAL'";
  } else {
     zmq_ret = zmq_ret + ", '_response': 'CLOSE_PARTIAL_FAILED'";
     return(false);
  }
}

If the ticket is zero, then it would indeed close the last selected order because the function DWX_ClosePartial() is also used in DWX_CloseAtMarket() and before that one is executed the order is selected.

So the only wrong case I can think of is when you submit a partial close from python with ticket=0. We could simple add a check at case 4: // CLOSE PARTIAL and only execute if ticket != 0. Or is there another case where it would lead to a problem?

elvinex commented 3 years ago

I made a small update to fix the case that I explained in my last post.