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

 
Mohamad Zulhairi Baba:


modified some of your code, to select highest lot first.

Correct me if I am wrong but I dont think this code will work as it reverses the order—which I am not doing. I am simply placing another sell with double the lots at every 3 pip interval.

 
Stanislav Korotky:
If you just want to add volume to your trades in the same direction - no need to close previous trade, but just open a new one with additional volume.

I need to close the trade because it reduces risk. It is better to close the last trade than let all run because it significantly reduces the overall loss should the total SL of 18 pips be hit.

 
marth tanaka:

Correct me if I am wrong but I dont think this code will work as it reverses the order—which I am not doing. I am simply placing another sell with double the lots at every 3 pip interval.

If you're opening orders in the same direction immediately after closing them then you're just wasting money and need to rethink that strategy. 

 
Mohamad Zulhairi Baba:


modified some of your code, to select highest lot first.

There's no need to select the highest volume order first since smaller orders always split off larger ones which results in the need to send the ordercloseby command the same amount of times no matter how they are ordered. That's the reason for the recursive call as opposed to just continuing the loop. The only time I've seen it make sense to add more logic is for someone like a signal provider, who wants to arrange the orders to be reconciled in such a way will yield the smoothest possible equity curve. That's an algorithm far more advanced than what we're discussing ITT however...  


Additionally, the code doesn't do anything different. The first order that meets the criteria will always be > 0 lots, and every recursive call resets the lots var back to zero. If you wanted to closeby larger orders first then you'd need to sort the order pool by size @ each call to multiple_closeby() 

 
nicholi shen:

If you're opening orders in the same direction immediately after closing them then you're just wasting money and need to rethink that strategy. 

Yes, but theyre spaced 3 pips apart as I mentioned. Therefore, this isnt wasting money. It’s better than traditional martingale where the losses are gigantic when SL hits. I’m trying to minimize that by closing the last trade at the open of the next trade spaced 3 pips apart.

 
marth tanaka:

Yes, but theyre spaced 3 pips apart as I mentioned. Therefore, this isnt wasting money. It’s better than traditional martingale where the losses are gigantic when SL hits. I’m trying to minimize that by closing the last trade at the open of the next trade spaced 3 pips apart.

That exactly proves my point. Just keep the original position open and reduce the volume of the next accordingly. Your net position is the same and you didn't pay the broker twice! 

 
marth tanaka:

Yes, but theyre spaced 3 pips apart as I mentioned. Therefore, this isnt wasting money. It’s better than traditional martingale where the losses are gigantic when SL hits. I’m trying to minimize that by closing the last trade at the open of the next trade spaced 3 pips apart.

3pips (30point) is too narrow, imho.
anyway, quick code, didn't test.

int gMagic1 = 1;
int gMagic2 = 2;

void tradeCloser(int pip, int firstMagic = 0) {
   int ticket;
   bool x = false;
   double pipDist = pip*_Point;
   for(int order = OrdersTotal()-1; order >= 0; order--) {
      bool select = OrderSelect(order,SELECT_BY_POS);
      if(select && OrderMagicNumber()==firstMagic && OrderSymbol()==_Symbol) {
         MqlTick tick;
         SymbolInfoTick(_Symbol,tick);
         
         if(OrderType()==OP_BUY && tick.bid>=OrderOpenPrice()+pipDist) x = true;
         else if(OrderType()==OP_SELL && tick.ask<=OrderOpenPrice()-pipDist) x = true;
         
         if(x) {
            bool closeorder = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,clrRed);
            double price = tick.bid;
            color arrow  = clrRed;
            if(OrderType()==OP_BUY) {price = tick.ask;arrow = clrGreen;}
            if(OrderMagicNumber()!=gMagic1 && countOrder(gMagic1)==0) ticket = OrderSend(_Symbol,OrderType(),OrderLots()*2,price,10,0,0,"",gMagic1,0,arrow);
            else if(OrderMagicNumber()!=gMagic2 && countOrder(gMagic2)==0) ticket = OrderSend(_Symbol,OrderType(),OrderLots()*2,price,10,0,0,"",gMagic2,0,arrow);
         }
      }
   }
}

int countOrder(int magic) {
   int count = 0;
   for(int order = OrdersTotal()-1; order >= 0; order--){
      bool select = OrderSelect(order,SELECT_BY_POS);
      if(select && OrderSymbol()==_Symbol) {
         if(OrderType()==OP_BUY) count++;
         else if(OrderType()==OP_SELL) count++;
      }
   }
   return count;
}

call it on OnTick() 

void OnTick(){
   tradeCloser(30);
   tradeCloser(30,gMagic1);
   tradeCloser(30,gMagic2);
}
ps: edit some code
 
nicholi shen:

That exactly proves my point. Just keep the original position open and reduce the volume of the next accordingly. Your net position is the same and you didn't pay the broker twice! 

Correct me if I’m wrong (I’m trying to best understand) but I believe you have the wrong idea.


I do not open an order in the opposite direction if price moves 3 pips against my original sell. Instead, I open a sell if price moves 3 pips against me that has twice the volume of the last sell so that if it retraces 3 pips down to the first sell, it covers the losses of the first trade and profits.

 
nicholi shen:

There's no need to select the highest volume order first since smaller orders always split off larger ones which results in the need to send the ordercloseby command the same amount of times no matter how they are ordered. That's the reason for the recursive call as opposed to just continuing the loop. The only time I've seen it make sense to add more logic is for someone like a signal provider, who wants to arrange the orders to be reconciled in such a way will yield the smoothest possible equity curve. That's an algorithm far more advanced than what we're discussing ITT however...  

thank you @nicholi shen

 
Mohamad Zulhairi Baba:

3pips (30point) is too narrow, imho.
anyway, quick code, didn't test.

call it on OnTick() 

Thank you so much! I will throw this into my original code, give it a test, and let you know ASAP!