strange tester behaviour

 

Hi All

When I test my EA for some reason it only partially closes open positions, see report Ticket 1 is 7.84 lots but only 5.45 lots were closed,

and a new buy order # 2 was created with the same parameters as ticket 1, adding the closed amount of ticket 1 to ticket number 2 yields the same lot size

as the original ticket 1. This does not happen if the position is closed by price hitting either SL or TP, in those cases they are closed in their entirety.

This does not happen if the lot size is fixed ie 1 or 0.1.

1 2007.04.10 00:00 buy stop 1 7.84 159.77 159.47 161.27
2 2007.04.10 06:45 buy 1 7.84 159.77 159.47 161.27
3 2007.04.11 00:00 close 1 5.45 160.01 159.47 161.27 1355.63 11355.63
4 2007.04.11 00:00 buy 2 2.39 159.77 159.47 161.27

These are the lines of code which close the positions.

if(OrderType() == 0){OrderClose(Ticket,lots,Bid,1);entryset = 0;Print("buy trade closed");}   //CLOSE OPEN BUY POSITION      
else
if(OrderType() == 1){OrderClose(Ticket,lots,Ask,1);entryset = 0;Print("Sell trade closed");}   // CLOSE OPEN SELL POSITION

Thoughts? Ideas? Wild guesses?

Thanks

Keith

 

It is obvious from the sliver of code you revealed that your lots value is wrong.

Fix it.

"This does not happen if the position is closed by price hitting either SL or TP, in those cases they are closed in their entirety."

That is because MT4 does not use your variable "lots" to close those trades.

 
phy wrote >>

It is obvious from the sliver of code you revealed that your lots value is wrong.

Fix it.

"This does not happen if the position is closed by price hitting either SL or TP, in those cases they are closed in their entirety."

That is because MT4 does not use your variable "lots" to close those trades.

Ok

I'm not too sure what can be wrong with variable lots? There is more than enough margin and if there were not I would assume that the tester would not open the position.

Having opened the position I would assume that at that time there is no issue with the variable "lots".

Having opened the position I assume that the size is fixed, as far as I know the only possible way to change the size is to use a partial close, as you can

see I did not do that, and I am not even sure if MT4 allows it.

The tester should not use my variable "lots" to close the position but rather the "size" of the position as recorded in the ticket.(The value of "lots" might have changed).

I am also puzzled by the creation of Ticket 2, there are no Open_Buy commands in the script, all positions are opened via Buy_Stop and Sell_Stop, so if the

buy was generated in my code it should have been preceeded by "Buy_Stop".

Is it possible I have a corrupt version of MT4 or an error in the tester?

Thanks for your thoughts

Keith

 

"lots" is your own varable.

What value do you give it?

That is not shown in your code sample.

 
phy wrote >>

"lots" is your own varable.

What value do you give it?

That is not shown in your code sample.

Here it is:
lots = double MathMin(risklots,maxlots)*0.4;     

Where: risklots is the maximum number of lots I wish to risk.

maxlots is the maximum number of lots permitted by available margin.

0.4 is a fudge faactor I added during testing, when I discovered I was far too aggressive, eventually it would be replaced

by a variable or incorporated into risklots.

When manually checking the results I can confirm that "lots" does hold the value of number of lots opened except for the case for ticket 2.

In all cases the sum of new mystery order and that of the partial order equals the original "lots" amount.

Truthfully I'm still hung up on how an opened order can be changed.

Again thanks for your thoughts.

Keith

 
When you open a trade, your available margin changes, therefore, your (subsequent) calculation of lots (probably) changes.
 
phy wrote >>
When you open a trade, your available margin changes, therefore, your calculation of lots changes.

Yes maxlots is recalculated for each trade and manual checking confirms that it is correct.

I apologize for giving you so little information to work with. I am delusional enough to think that this strategy my prove to be profitable

and am loath to reveal the logic behind the codes.

  free = AccountFreeMargin();                                    //amount of available margin     
  double One_Lot=MarketInfo(symb,MODE_MARGINREQUIRED);           //Cost per 1 lot      
  maxlots = free/One_Lot;                                         //maximum number of lots permitted by available cash
  lots = double MathMin(risklots,maxlots)*0.4;                    //actual number of lots to trade

If you are curious here is the open order statement:

Ticket = OrderSend( Symbol(),OP_BUYSTOP,lots,entry,3,stop,TakeP,"nil",111,0,0);

Once an order is open nothing should change with respect to that order, this is a very simple ea hits stop or takes profit or closes at end of day.

needless to say it is driving me nuts....short trip!!

Thanks

Keith

 
phy wrote >>

Nothing changed in your postion.

You are sending a value of lots in OrderClose() that closes part (not all) of the position.

Rethink your logic.

Great!!! So you can partially close an order. So I need to find a way to convert "lots" into a constant for each order. Should be able to do that.

Thanks again; your willingness to share your consummate expertise is greatly appreciated, as is your patience!!!

Cheers

Keith

 
I've told you what your problem is, good luck.
Reason: