Metatrader 5 coding questions / issues - page 10

 

Whoever is coding using mql5 should look at this post : https://www.mql5.com/en/forum/181297/page48.

A lot of previously working stuff will not work correctly any more until the code is adjusted to latest versions of metatrader 5 - so it is not an error in you, but again metatarder 5 decided that compatibility with previous versions of metatrader 5 is "not a priority".

 

t will never end, won't it? They fix one hole and another 5 emerges

 

Dear Mladen,

my question is relative to an ea...how is possible to code the ea with this logic in the better way:

if(PositionSelect(_Symbol)==false) // we have no opened position

{

buy is allowed only if the last trade was a sell and viceversa. (so the first time both signal are allowed)

Thank you

doc

 
dr.house7:
Dear Mladen,

my question is relative to an ea...how is possible to code the ea with this logic in the better way:

if(PositionSelect(_Symbol)==false) // we have no opened position

{

buy is allowed only if the last trade was a sell and viceversa. (so the first time both signal are allowed)

Thank you

doc

doc

will see what can I come up with (to make it simple and re-usable)

what might be a problem is that positions (deals) can have multiple orders and then you are going to depend on a last order even though the position itself could have both types of orders in it (the legacy of metatrader 5 and its "simplicity")

 
mladen:
doc

will see what can I come up with (to make it simple and re-usable)

what might be a problem is that positions (deals) can have multiple orders and then you are going to depend on a last order even though the position itself could have both types of orders in it (the legacy of metatrader 5 and its "simplicity")

Thanks,

yes, indeed I don't know how to properly use the #include in this particular case...the ea should look back for Nbars for find the last deal (if there is one).

Hope in your big knowledge.

 
mladen:
Doc

Try this function :

uint getLastOrderType(string forSymbol)

{

if (HistorySelect(0,TimeCurrent()))

{

int totalPastOrders = HistoryOrdersTotal();

uint lastOrderType = 0;

while(totalPastOrders>0 && !IsStopped())

{

ulong ticket = HistoryOrderGetTicket(totalPastOrders-1);

if (ticket > 0 && (string)HistoryOrderGetString(ticket,ORDER_SYMBOL)==forSymbol)

{

lastOrderType = (uint)HistoryOrderGetInteger(ticket,ORDER_TYPE); break;

}

else totalPastOrders--;

}

return(lastOrderType);

}

return(0);

}[/PHP]

It will return the order type of the last order executed for the rquired symbol (an example call of could be something like this :

[PHP] Comment(getLastOrderType(_Symbol));

Thanks again,

you post quickly than me ...I'll check

 

Doc

Try this function :

int getLastOrderType(string forSymbol)

{

if (HistorySelect(0,TimeCurrent()))

{

int totalPastOrders = HistoryOrdersTotal();

uint lastOrderType = -1;

while(totalPastOrders>0 && !IsStopped())

{

ulong ticket = HistoryOrderGetTicket(totalPastOrders-1);

if (ticket > 0 && (string)HistoryOrderGetString(ticket,ORDER_SYMBOL)==forSymbol)

{

lastOrderType = (uint)HistoryOrderGetInteger(ticket,ORDER_TYPE); break;

}

else totalPastOrders--;

}

return((int)lastOrderType);

}

return(-1);

}[/PHP]

It will return the order type of the last order executed for the rquired symbol (an example call of could be something like this :

[PHP] Comment(getLastOrderType(_Symbol));
 

One thing that might be very annoying and potentially very dangerous : when you change symbols in metatrader 5 and if the newly loaded symbol haven't been loaded for a while, you might experience something very similar to a platform freeze. It is uselles to place !IsStopped() in the code that is going to be used since your code is simply stopped as long as the download and new data preparation is completed and it will never reach a point where it can at least exit.

In cases when you have an EA running, the EA execution will be stopped too, so, do not change symbols in times of news (or you are going to miss it). After so long a time still having this problem seems to be rather strange at the least

 

I'm a bit confused

Dear Mladen,

could you please take a look at the code of this "ea draft" and tell me what mistakes I made this time, from your point of view?

Not all the code, only the final part about CloseBuySignal and CloseSellSignal.

Thanks alot

doc

Files:
x_tsd.mq5  15 kb
 

this is the Journal:

2013.10.18 15:35:42 2013.05.20 00:06:02 failed instant sell 3.00 EURUSD at 1.28444

2013.10.18 15:35:42 2013.05.20 00:06:01 failed instant sell 3.00 EURUSD at 1.28445

2013.10.18 15:35:42 2013.05.20 00:06:00 close sell

2013.10.18 15:35:42 2013.05.20 00:06:00 order performed buy 3.00 at 1.28483 [#3 buy 3.00 EURUSD at 1.28483]

2013.10.18 15:35:42 2013.05.20 00:06:00 deal performed [#3 buy 3.00 EURUSD at 1.28483]

2013.10.18 15:35:42 2013.05.20 00:06:00 deal #3 buy 3.00 EURUSD at 1.28483 done (based on order #3)

2013.10.18 15:35:42 2013.05.20 00:06:00 instant buy 3.00 EURUSD at 1.28483 (1.28444 / 1.28483 / 1.28444)

2013.10.18 15:35:42 2013.05.20 00:06:00 price corrected from 1.28444 to 1.28483, deviation: 10000 (instant buy 3.00 EURUSD at 1.28444)(1.28444 / 1.28483 / 1.28444)

2013.10.18 15:35:42 2013.05.20 00:05:59 order performed sell 3.00 at 1.28444 [#2 sell 3.00 EURUSD at 1.28444]

2013.10.18 15:35:42 2013.05.20 00:05:59 deal performed [#2 sell 3.00 EURUSD at 1.28444]

2013.10.18 15:35:42 2013.05.20 00:05:59 deal #2 sell 3.00 EURUSD at 1.28444 done (based on order #2)

2013.10.18 15:35:42 2013.05.20 00:05:59 instant sell 3.00 EURUSD at 1.28444 (1.28444 / 1.28472 / 1.28444)

Reason: