How to close the last trade EXACTLY at the open of the next trade? - page 4

 
marth tanaka:
@Mohamad Zulhairi Baba I modified your trade closer function to work with mine. I believe the idea you were implying was to use the tick.bid/tick.ask correct? I tried that and it did not work. Funny thing is that I don't get an error in the backtester Journal either. Any thoughts on this?
You didn’t loop it correctly.
 
Mohamad Zulhairi Baba:
You didn’t loop it correctly.
I know. That is why I deleted that comment because I quickly realized I hadnt called the tradecloser in my main script. I did now and it does the same thing as before. It does not close the last trade on the exact same price as the next open. Here is the code for the function reposted below:

void TradeCloser(){ //start

            int total = OrdersTotal();
            MqlTick tick;
            SymbolInfoTick(_Symbol,tick);
            if (total > 0)
            
            {
            for(int cnt=0;cnt<total;cnt++)
            {
               if(OrderSelect(cnt,SELECT_BY_POS))
               {
            
                     if ((Next_BUY_Order_Comment == "M1") && (OrderType() == OP_BUY)) // compare the commenting
               {
                  // do what you want.
                 // OrderCloseBy(Close1B,Close2B, CLR_NONE);
                  OrderClose(Close1B(),Order0_Lot,tick.bid,2,Red);

               }
                     if ((Next_BUY_Order_Comment == "M2") && (OrderType() == OP_BUY)) // compare the commenting
               {
                  // do what you want.
                  OrderClose(Close2B(),Order1_Lot,tick.bid,2,Red);
               }
                     if ((Next_BUY_Order_Comment == "M3") && (OrderType() == OP_BUY)) // compare the commenting
               {
                  // do what you want.
                  OrderClose(Close3B(),Order2_Lot,tick.bid,2,Red);
               }
                     if ((Next_BUY_Order_Comment == "M4") && (OrderType() == OP_BUY)) // compare the commenting
               {
                  // do what you want.
                  //myOrderModifyRel(Close6B(), 3 * myPoint, 0);
                 // OrderModify(Close5B(), OrderOpenPrice(), OrderOpenPrice()-sala, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
                  //OrderModify(Close5B(), OrderOpenPrice(), 3 * myPoint, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
                  OrderClose(Close4B(),Order3_Lot,tick.bid,2,Red);
               }
               
                     if ((Next_BUY_Order_Comment == "M5") && (OrderType() == OP_BUY)) // compare the commenting
               {
                  // do what you want.
                  
                  OrderClose(Close5B(),Order4_Lot,tick.bid,2,Red);
               }
               
               if ((Next_SELL_Order_Comment == "M1") && (OrderType() == OP_SELL)) // compare the commenting
               {
                  // do what you want.

                  OrderClose(Close1S(),Order0_Lot,tick.ask,2,Blue);

               }
                     if ((Next_SELL_Order_Comment == "M2") && (OrderType() == OP_SELL)) // compare the commenting
               {
                  // do what you want.
                  OrderClose(Close2S(),Order1_Lot,tick.ask,2,Blue);
               }
                     if ((Next_SELL_Order_Comment == "M3") && (OrderType() == OP_SELL)) // compare the commenting
               {
                  // do what you want.
                  OrderClose(Close3S(),Order2_Lot,tick.ask,2,Blue);
               }
                     if ((Next_SELL_Order_Comment == "M4") && (OrderType() == OP_SELL)) // compare the commenting
               {
                  // do what you want.
                  //myOrderModifyRel(Close6S(), 3 * myPoint, 0);
                  //OrderModify(Close5S(), OrderOpenPrice(), 3 * myPoint, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
                 // OrderModify(Close5S(), OrderOpenPrice(), OrderOpenPrice()+sala, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
                  OrderClose(Close4S(),Order3_Lot,tick.ask,2,Blue);
               }
               
                     if ((Next_SELL_Order_Comment == "M5") && (OrderType() == OP_SELL)) // compare the commenting
               {
                  // do what you want.
                  
                  OrderClose(Close5S(),Order4_Lot,tick.ask,2,Blue);
               }
            
         }
      }
   }



//end
}
 
marth tanaka: I truly appreciate your thoughts on this as a whole but I am not here to debate if the system as a whole will work or not. I am simply trying to learn and understand how to code this in (since it is possible manually). Thank you.

OK! But you will never be able to align two orders in the same direction in such a way that you will be able to close of the first trade at exactly the same price as the open of the second order, every time 100% of the time.

You will be only able to do it some of the times but not on every attempt. If there is a Spread, then that is simply impossible, even if you use pending orders!

The only way to achieve that is with Zero Spread, but such accounts then have commission associated with it.

So, you can attempt to code this as much as you want. It will never be possible as long as there is a spread. And slippage only makes it worst!

 
marth tanaka:
I know. That is why I deleted that comment because I quickly realized I hadnt called the tradecloser in my main script. I did now and it does the same thing as before. It does not close the last trade on the exact same price as the next open. Here is the code for the function reposted below:

Your code have many loopholes.
Learn to code the proper way.
And...as Fernando said, your system also have loopholes. Good luck.
 
Mohamad Zulhairi Baba:
Your code have many loopholes.
Learn to code the proper way.
And...as Fernando said, your system also have loopholes. Good luck.
Fernando Carreiro:

OK! But you will never be able to align two orders in the same direction in such a way that you will be able to close of the first trade at exactly the same price as the open of the second order, every time 100% of the time.

You will be only able to do it some of the times but not on every attempt. If there is a Spread, then that is simply impossible, even if you use pending orders!

The only way to achieve that is with Zero Spread, but such accounts then have commission associated with it.

So, you can attempt to code this as much as you want. It will never be possible as long as there is a spread. And slippage only makes it worst!

 Mohamad, I appreciate the positive feeback. I am a newbie at mql4 but I am trying to learn.

Fernando, I appreciate this comment! The problem is that, with spread, I was able to replicate this when trading manually on a live account. I sold one lot first then opened a pending order of 2 lots 3 pips above the first trade and it closed the first trade and opened the second trade at the exact same price--with spread being taken account of of course.
 
marth tanaka:
 Mohamad, I appreciate the positive feeback. I am a newbie at mql4 but I am trying to learn.

Fernando, I appreciate this comment! The problem is that, with spread, I was able to replicate this when trading manually on a live account. I sold one lot first then opened a pending order of 2 lots 3 pips above the first trade and it closed the first trade and opened the second trade at the exact same price--with spread being taken account of of course.

No, you were only able to replicate this because conditions were favorable to you during the test. Had the price continued against you indefinitely, you would never have been able to close the first order at the same price as the open of the second order. Again I repeat, your math is faulty.

EDIT: How can a pending order automatically close the first order? That is not possible on MT4!

 
Fernando Carreiro:

No, you were only able to replicate this because conditions were favorable to you during the test. Had the price continued against you indefinitely, you would never have been able to close the first order at the same price as the open of the second order. Again I repeat, your math is faulty.

EDIT: How can a pending order automatically close the first order? That is not possible on MT4!

This was not a test. It was a real market scenario. I don't believe the conditons were favorable otherwise it would have worked on test at least once. Thank you
 
marth tanaka: This was not a test. It was a real market scenario. I don't believe the conditons were favorable otherwise it would have worked on test at least once. Thank you

Sorry, but what you have described is simply not possible! A pending order can only trigger a market open, it cannot trigger the close of another order.

Plus, it cannot also do this at the same price at exactly the same time, because it is just impossible for orders in the same direction.

A Buy order opens at the Ask and closes at the Bid, and a Sell order opens at Bid and closes at Ask.

The only time two orders can open/close (one opens while the other closes) at the exact same price and time is if they are opposite orders, one Buy and the other Sell.

EDIT: Plus, you have already been told this on your other thread, but continue to insist on it:

Forum on trading, automated trading systems and testing trading strategies

How to avoid spread when trying to open a new trade at the close of the last one

Alain Verleyen, 2018.11.11 05:27

If your condition is the exact same price (close of first = open of second), then you are wrong it's not possible to do it systematically except with a fixed spread, not even taking into account possible slippage. Please don't repeat you did it manually or prove it (it has to work each time).

Why opening a second topic on the exact same issue ?

 
I have deleted the other topic
 

98
marth tanaka 2018.11.10 22:23      EN
nicholi shen:

So perhaps there is a language barrier issue here so please correct me if I am wrong... You intend to open a trade in the same direction with more volume as the price moves against the position. So for example 

1 lot Sell goes 3 pips underwater. 

You open a 2 lot sell and immediately close the one lot sell? By all accounts of your description, that is what you intend to do, correct?

Correct!

So what you're saying is that you want a position of 1 -- which you then open a different position of 2 while simultaneously closing 1 leaving you with 2. You just blew 1 spread and 1 commission for no reason. Why??? All you had to do was leave the original open and add 1 to it.