Two questions. - WhooDoo22.

 

Hello MQL4 community,

I have two questions.


2. orderclosing question.

Objective: Open a buy order when iMA 1 > iMA 200 (shift1) AND after the sell order of 0.01 lots closes.

Current code displayed directly below-

   OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES);
   if(OrderType()==OP_SELL)                                                                    {
   if(OrderLots()==0.01)                                                                       {
   if(OrdersTotal()>0)                                                                         {
   if(iMA(NULL,0,1,0,MODE_SMMA,PRICE_CLOSE,1)>(iMA(NULL,0,200,0,MODE_SMMA,PRICE_CLOSE,1)))     { 
     {
      OrderClose(i,0.01,Bid,30,CLR_NONE);    
      i++;
      ticket=OrderSend(Symbol(),OP_BUY,0.04,Ask,30,0,0,"",0,0,Blue);
     }
     }}}}

solution (fail): The code above works, BUT ;),  I wish to create two blocks of code - block 1 for the closing of the sell order of 0.01 when iMA 1 > iMA 200 (shift1) and block 2 for the opening of a buy order when iMA > iMA 200 (shift1).

The problem with the solution (fail) above is that once the sell order closes, the code below recognizes that OrdersTotal()>1 because of the last sell order close of 0.01 lots and the attempting buy order of 0.04 lots. Therefore, the orderclose works, but the ordersend failse (block 1 works, but block 2 fails).

Example:

// block 1.

   OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES);
   if(OrderType()==OP_SELL)                                                                    {
   if(OrderLots()==0.01)                                                                       {
   if(OrdersTotal()>0)                                                                         {
   if(iMA(NULL,0,1,0,MODE_SMMA,PRICE_CLOSE,1)>(iMA(NULL,0,200,0,MODE_SMMA,PRICE_CLOSE,1)))     { 
     {
      OrderClose(i,0.01,Bid,30,CLR_NONE);    
      i++;
      ticket=OrderSend(Symbol(),OP_BUY,0.04,Ask,30,0,0,"",0,0,Blue);
     }
     }}}}

// block 2.

   OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES);
   if(OrdersTotal()==0)                                                                        {  // if orderstotal==0 fails cause of the last close of the sell order close before it
   if(iMA(NULL,0,1,0,MODE_SMMA,PRICE_CLOSE,1)>(iMA(NULL,0,200,0,MODE_SMMA,PRICE_CLOSE,1)))     { 
     
     {ticket=OrderSend(Symbol(),OP_BUY,0.04,Ask,30,0,0,"",0,0,Blue);}}}

Does any coder in MQL4 forum aware of a solution to open an order on the same bar of a closing order? (based on the iMA 1 > iMA 200 (shift1) crossover)



1. spread question

I recently was attempting to come up with a solution to a spread question in the MQL4 forum earlier, which I was unable to solve. I typed up the MarketInfo() function to discover the broker spread, then used the comment function to visualize the result. The spread of the broker I was testing these couple a' strings on had a spread of 1.6. After I typed up the code and popped the script onto the chart, the comment read, "16" instead of 1.6.

Here is a snip of the code I wrote out-

   double spread=MarketInfo("EURUSD",MODE_SPREAD);
   
   Comment("spread = ",spread);

 // and yes the currency pair is EURUSD and is displayed as such in the "Market Watch" window.

Does any coder in the MQL4 forum have any thoughts for a solution to this spread issue?

Thank you.

 

WhooDoo what are you doing

The way you check the trades you have ....

How can if(OrdersTotal()>0)  fail if you select order i ????   What exactly do you think OrdersTotal() is giving you ??

And how can you be sure  OrderClose(i,0.01,Bid,30,CLR_NONE);    succeed ???   Still after it you do i++;

You are not checking the return codes    Bid ==> Why close SELL with Bid ?? not with Ask ??  or better OrderClosePrice()

Your OrderSend function is not dealing 4/5 digit brokers    Although 30 pips slippage (4 digit) will never be not enough

If you know how to deal 4/5 digit brokers then it is very simple to get 1.6 if you use method WHRoeder

by using his pips2points

double spread=MarketInfo("EURUSD",MODE_SPREAD)/pips2points;

 

 If you check last closed trade when if(OrdersTotal() == Totaltrades - 1){ check last closed trade and make Totaltrades = OrdersTotal();

then you will know if you have to open or check for open new trade...

make use of OrderSymbol()   and MagicNumber() 

 

deVries,

"WhooDoo what are you doing"

LOL!

"How can if(OrdersTotal()>0)  fail if you select order i ????"

It can't.

"What exactly do you think OrdersTotal() is giving you ??"

the total open "i" orders.

"how can you be sure  OrderClose(i,0.01,Bid,30,CLR_NONE);    succeed ???"

I am sure it works because the order sell order 0.01 lots closes.

"Still after it you do i++;"

This is because you were unaware of my EA using partial order close function. Now, you are informed (appologies for not stating this earlier).

"You are not checking the return codes    Bid ==> Why close SELL with Bid ??"

Why not? the sell order was opened with bid, I believe it should also be closed with bid. Using bid works fine for me when closing sell orders.

"not with Ask ??  or better OrderClosePrice()"

I do not believe there is any error within my OrderClose() function. I do not know why you are bringing this up, but it makes no difference.

"Your OrderSend function is not dealing 4/5 digit brokers    Although 30 pips slippage (4 digit) will never be not enough"

I am not using a four digit broker, I am using a five digit broker.

"If you know how to deal 4/5 digit brokers then it is very simple to get 1.6 if you use method WHRoeder

by using his pips2points"

double spread=MarketInfo("EURUSD",MODE_SPREAD)/pips2points;

It seems to me that this code simply divides the code I have written by "pips2points". I am assuming this is 10 or 100. I will investigate further.

"make use of OrderSymbol()   and MagicNumber()" 

I do not see the sense in using ordersymbol and magicnumber functions in my code when I can just use-

// orderselect function.
if(OrderType()==OP_BUY){

||

if(OrdersType()==OP_SELL){

 "If you check last closed trade when if(OrdersTotal() == Totaltrades - 1){ check last closed trade and make Totaltrades = OrdersTotal();

then you will know if you have to open or check for open new trade..."

if(OrdersTotal()==Totaltrades (i am assuming this is the ticket number) -1){

This won't work because when the first part of the order is closed, the part that is closed is closed ALONG WITH the ticket name. So what must be done is I must use two types of ticket names.

Example: I use "ticket" (for the first partial close), then "i" (for the last three). I then add i++; every time I close orders to keep the i variable updated. This stuff is a little more complex than the typical buy full position close full position.

I am having difficulty isolating the problem when running the code, but hopefully I'll attain a "eureka" moment and end this whole escapade.

Thank you very much for your response.


 
WhooDoo22:


"You are not checking the return codes    Bid ==> Why close SELL with Bid ??"

Why not? the sell order was opened with bid, I believe it should also be closed with bid. Using bid works fine for me when closing sell orders.

"not with Ask ??  or better OrderClosePrice()"

I do not believe there is any error within my OrderClose() function. I do not know why you are bringing this up, but it makes no difference.

To close a Sell you effectively use a Buy . . . .  and you Buy at Ask,  so to close a Sell you close it at Ask.  Alternatively Select the Order in question and use OrderClosePrice() then there is no need to distinguish between Buys and Sells.

You NEED to check return values . . .  and act on them.
 

Simon,

"To close a Sell you effectively use a Buy . . . .  and you Buy at Ask,  so to close a Sell you close it at Ask."

This is what I understand, please correct if incorrect (i believe you corrected above already, but for clarification).

When opening a buy order use ask.

When closing a buy order use ask.

When opening a sell order use bid.

When closing a sell order use bid.

I do not understand why an EA would use ask when closing a sell order which was opened with bid? :O


" Alternatively Select the Order in question and use OrderClosePrice() then there is no need to distinguish between Buys and Sells."

Can you please provide an example in "SRC" box of using the OrderClosePrice() function?


"You NEED to check return values . . .  and act on them."

What do you mean by "return values"?


Thank you.

 
WhooDoo22:

deVries,

"WhooDoo what are you doing"

LOL!

:


  1. "How can if(OrdersTotal()>0)  fail if you select order i ????"

    It can't.
    Then why is it there, as it serves no useful purpose?


  2. "What exactly do you think OrdersTotal() is giving you ??"

    the total open "i" orders.

    Wrong, it gives you the total open orders on the server, not just the chart the EA is running on.

  3. "how can you be sure  OrderClose(i,0.01,Bid,30,CLR_NONE);    succeed ???"

    I am sure it works because the order sell order 0.01 lots closes.

    You can't be sure because if the orderSelect failed, everything else is below is bogus. Also OrderClose can fail for many reasons, like server busy or connection lost.

  4. "Still after it you do i++;"

    This is because you were unaware of my EA using partial order close function. Now, you are informed (appologies for not stating this earlier).

    Irrelevant. The new ticket number will NEVER be equal to i++, except in the tester and only if there is only one open order.

  5. "You are not checking the return codes    Bid ==> Why close SELL with Bid ??"

    Why not? the sell order was opened with bid, I believe it should also be closed with bid. Using bid works fine for me when closing sell orders.

    "not with Ask ??  or better OrderClosePrice()"

    I do not believe there is any error within my OrderClose() function. I do not know why you are bringing this up, but it makes no difference.

    Read the documentation, you open a buy at the Ask, the close is therefor a sell at the Bid. Your belief is WRONG and it will make a difference when the order doesn't close and you loose money

  6. "Your OrderSend function is not dealing 4/5 digit brokers    Although 30 pips slippage (4 digit) will never be not enough"

    I am not using a four digit broker, I am using a five digit broker.

    "If you know how to deal 4/5 digit brokers then it is very simple to get 1.6 if you use method WHRoeder

    by using his pips2points"

    double spread=MarketInfo("EURUSD",MODE_SPREAD)/pips2points;

    It seems to me that this code simply divides the code I have written by "pips2points". I am assuming this is 10 or 100. I will investigate further.

    It will make a difference if you change brokers, or the broker changes from 4/5. Some brokers have different servers with different digits. The EA should automatically adjust

  7. "make use of OrderSymbol()   and MagicNumber()" 

    I do not see the sense in using ordersymbol and magicnumber functions in my code when I can just use-

    You'll see the sense the moment you go live and put the EA on two different pairs, or open a manual trade with you EA or add another EA to some other chart. Please post how much you loose after stopping the EAs
 
WhooDoo22:

Simon,

"To close a Sell you effectively use a Buy . . . .  and you Buy at Ask,  so to close a Sell you close it at Ask."

1.  This is what I understand, please correct if incorrect (i believe you corrected above already, but for clarification).

When opening a buy order use ask.

When closing a buy order use ask.

When opening a sell order use bid.

When closing a sell order use bid.

I do not understand why an EA would use ask when closing a sell order which was opened with bid? :O


" Alternatively Select the Order in question and use OrderClosePrice() then there is no need to distinguish between Buys and Sells."

2.  Can you please provide an example in "SRC" box of using the OrderClosePrice() function?


"You NEED to check return values . . .  and act on them."

3.  What do you mean by "return values"? 

1.  you are indeed incorrect . . . if I open my Buy at Ask and close my Buy at Ask  when do I pay the Spread ?  If you open a trade and immediately close it you will pay the Spread,  the Spread is the difference between Bid and Ask . . .  so if you Buy you Buy at Ask and when you close your Buy it is by way of a Sell at Bid.  You have now paid the Spread . . .  and no,  it is not optional ;-)

2.  I can but it is simply OrderClosePrice() . . .  read the Documentation.  To use it all you do is use it in place of Bid/Ask in your OrderClose() call . . . .   but to be able to use it you must first select the order you are about to close using OrderSelect().

 

3. return values . . . .   please read this thread,  I spent some time writing it as it is something that people neglect when it is something that they should concentrate on . . .  :  What are Function return values ? How do I use them ?

 

WHRoeder,

Then why is it there, as it serves no useful purpose?

"if(OrdersTotal()>0){" serves the purpose of the code block executing ONLY if orders total is greater than zero. If orders total is not greater than zero, the code block does not execute.

Wrong, it gives you the total open orders on the server, not just the chart the EA is running on.

I would agree to disagree. The statement above is correct IF the "OrderSelect()" function is not used. If the "OrderSelect()" function is used, then the code block is searching for the ticket number contained within the "OrderSelect()" function.

You can't be sure because if the orderSelect failed, everything else is below is bogus. Also OrderClose can fail for many reasons, like server busy or connection lost.

Agreed and agreed.

Irrelevant. The new ticket number will NEVER be equal to i++, except in the tester and only if there is only one open order.

no comment.

Read the documentation, you open a buy at the Ask, the close is therefor a sell at the Bid. Your belief is WRONG and it will make a difference when the order doesn't close and you loose money

It would have been informative to me if you would have explained the reason why a buy order opens at Ask and closes at Bid is because of the requirement to pay spread to the broker. Simon clarified this in a separate post. Still, thank you for your response.

It will make a difference if you change brokers, or the broker changes from 4/5. Some brokers have different servers with different digits. The EA should automatically adjust

When I receive a spare moment, I will attempt to drop the code into an EA to witness the result in the tester to validate your statement of "The EA should automatically adjust".

You'll see the sense the moment you go live and put the EA on two different pairs, or open a manual trade with you EA or add another EA to some other chart. Please post how much you loose after stopping the EAs

no comment.


Thanks much William.

 

Simon,

1. "...and no,  it is not optional ;-)"

HA-HA! Sure it is. It is optional in "make believe land".

2. I have a choice to replace ask/bid with OrderClosePrice() assuming the code block is using the famous order select function. I'll check out the link "documentation" you provided me.

3. I'll check out the link "what are function return values? how do I use them?" you provided me.

Thank you.

Reason: