How to code partial close function in MT4

1
Hi, everyone , i got a problem when i thinking about coding a partial close function. My rule is when the price hits a target like 50 pips at the first time, then close 50% of the opened position. The problem is how to set the condition: price could hit the 50 pips target many times but i only want to close part of order at the first time when it hits target. So is there any advice or example code i can learn from? Thank you.


Re: How to code partial close function in MT4

2
raycharles0403 wrote: Wed Jan 22, 2020 9:19 pm Hi, everyone , i got a problem when i thinking about coding a partial close function. My rule is when the price hits a target like 50 pips at the first time, then close 50% of the opened position. The problem is how to set the condition: price could hit the 50 pips target many times but i only want to close part of order at the first time when it hits target. So is there any advice or example code i can learn from? Thank you.
Check this, if it could be of help

//| Partial Close |
//+------------------------------------------------------------+
void PartialClose()
{
if(!PartialClosing) return;
double PartialLot; bool action;

for(int cnt=0; cnt < OrdersTotal(); cnt++)
{
bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
int ticket=OrderTicket(), OrderTy=OrderType();
double OrderOP=OrderOpenPrice(), Orderlots=OrderLots();
string comment=OrderComment();

if(OrderSymbol()==Symbol()&& (OrderMagicNumber()==MN || MN==0))
{
if(OrderTy==OP_BUY)
{
if(Bid>OrderOP+ClosePips1*point() && ClosePips1>0 && ClosePercent1>0 && Lots() == Orderlots)
{
PartialLot=NormalizeDouble((ClosePercent1*Orderlots)/100,lotdigit);
if(PartialLot<MarketInfo(Symbol(),MODE_MINLOT))
{
Print("Can't Close Partial Fill , Lot Less than Min Lot");
continue;
}
action=OrderClose(ticket,PartialLot,NormalizeDouble(Bid,Digits),30);
if(ClosePercent1==100){
int tick=OrderSend(Symbol(),OP_BUY,Orderlots,Ask,30,0,0,"",MN,0,Blue);
}}}
if(OrderTy==OP_SELL)
{
if(Ask<OrderOP-ClosePips1*point() && ClosePips1>0 && ClosePercent1>0 && Lots() == Orderlots)
{
PartialLot=NormalizeDouble((ClosePercent1*Orderlots)/100,lotdigit);
if(PartialLot<MarketInfo(Symbol(),MODE_MINLOT))
{
Print("Can't Close Partial Fill , Lot Less than Min Lot");
continue;
}
action=OrderClose(ticket,PartialLot,NormalizeDouble(Ask,Digits),30);
if(ClosePercent1==100){
int tick=OrderSend(Symbol(),OP_SELL,Orderlots,Bid,30,0,0,"",MN,0,Red);
}}}}}
}
These users thanked the author OLAYEMI8 for the post:
raycharles0403

Re: How to code partial close function in MT4

3
OLAYEMI8 wrote: Wed Jan 22, 2020 9:41 pm

Check this, if it could be of help

//| Partial Close |
//+------------------------------------------------------------+
void PartialClose()
{
if(!PartialClosing) return;
double PartialLot; bool action;

for(int cnt=0; cnt < OrdersTotal(); cnt++)
{
bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
int ticket=OrderTicket(), OrderTy=OrderType();
double OrderOP=OrderOpenPrice(), Orderlots=OrderLots();
string comment=OrderComment();

if(OrderSymbol()==Symbol()&& (OrderMagicNumber()==MN || MN==0))
{
if(OrderTy==OP_BUY)
{
if(Bid>OrderOP+ClosePips1*point() && ClosePips1>0 && ClosePercent1>0 && Lots() == Orderlots)
{
PartialLot=NormalizeDouble((ClosePercent1*Orderlots)/100,lotdigit);
if(PartialLot<MarketInfo(Symbol(),MODE_MINLOT))
{
Print("Can't Close Partial Fill , Lot Less than Min Lot");
continue;
}
action=OrderClose(ticket,PartialLot,NormalizeDouble(Bid,Digits),30);
if(ClosePercent1==100){
int tick=OrderSend(Symbol(),OP_BUY,Orderlots,Ask,30,0,0,"",MN,0,Blue);
}}}
if(OrderTy==OP_SELL)
{
if(Ask<OrderOP-ClosePips1*point() && ClosePips1>0 && ClosePercent1>0 && Lots() == Orderlots)
{
PartialLot=NormalizeDouble((ClosePercent1*Orderlots)/100,lotdigit);
if(PartialLot<MarketInfo(Symbol(),MODE_MINLOT))
{
Print("Can't Close Partial Fill , Lot Less than Min Lot");
continue;
}
action=OrderClose(ticket,PartialLot,NormalizeDouble(Ask,Digits),30);
if(ClosePercent1==100){
int tick=OrderSend(Symbol(),OP_SELL,Orderlots,Bid,30,0,0,"",MN,0,Red);
}}}}}
}
Thank you very much, i will test it later.

Re: How to code partial close function in MT4

4
OLAYEMI8 wrote: Wed Jan 22, 2020 9:41 pm

Check this, if it could be of help

//| Partial Close |
//+------------------------------------------------------------+
void PartialClose()
{
if(!PartialClosing) return;
double PartialLot; bool action;

for(int cnt=0; cnt < OrdersTotal(); cnt++)
{
bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
int ticket=OrderTicket(), OrderTy=OrderType();
double OrderOP=OrderOpenPrice(), Orderlots=OrderLots();
string comment=OrderComment();

if(OrderSymbol()==Symbol()&& (OrderMagicNumber()==MN || MN==0))
{
if(OrderTy==OP_BUY)
{
if(Bid>OrderOP+ClosePips1*point() && ClosePips1>0 && ClosePercent1>0 && Lots() == Orderlots)
{
PartialLot=NormalizeDouble((ClosePercent1*Orderlots)/100,lotdigit);
if(PartialLot<MarketInfo(Symbol(),MODE_MINLOT))
{
Print("Can't Close Partial Fill , Lot Less than Min Lot");
continue;
}
action=OrderClose(ticket,PartialLot,NormalizeDouble(Bid,Digits),30);
if(ClosePercent1==100){
int tick=OrderSend(Symbol(),OP_BUY,Orderlots,Ask,30,0,0,"",MN,0,Blue);
}}}
if(OrderTy==OP_SELL)
{
if(Ask<OrderOP-ClosePips1*point() && ClosePips1>0 && ClosePercent1>0 && Lots() == Orderlots)
{
PartialLot=NormalizeDouble((ClosePercent1*Orderlots)/100,lotdigit);
if(PartialLot<MarketInfo(Symbol(),MODE_MINLOT))
{
Print("Can't Close Partial Fill , Lot Less than Min Lot");
continue;
}
action=OrderClose(ticket,PartialLot,NormalizeDouble(Ask,Digits),30);
if(ClosePercent1==100){
int tick=OrderSend(Symbol(),OP_SELL,Orderlots,Bid,30,0,0,"",MN,0,Red);
}}}}}
}
Sorry,it doesn't solved my problem. I think my problem is how to set the conditions to avoid executing the partial close function above after the price hits the target at first time. i see on other place that when partial close an order, the ticket number would change which can be used to avoiding executing partial close after the first time the target being hit, but i don't know how to code this function.

Re: How to code partial close function in MT4

5
The following code close 50% of the running trade after it has achieved 50Pips.
But you have option to set number of pips you want it to close partial profit, but you have to set the lot size of the EA to be the same as the on of the running trade.
Currently a default lotsize is 0.05
Both Pips and Lotsize are extern you can find then on input



//+------------------------------------------------------------------+
//| BE & Partial Close.mq4 |
//| Jats Junior |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Jats Junior"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property show_inputs
extern double lotSize = 0.05;
extern double PartialClosePips = 50;
double Pips = Point;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
Alert("");
Alert("starting Jats SL BE & Partial Close");
if(Digits==3 || Digits ==5)
Pips*=10;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Alert("Closing Jats SL BE & Partial Close");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(OrdersTotal()<1)
if(!OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
Print("unable to select an order");
double op = OrderOpenPrice();
int type = OrderType();
double lots = OrderLots();
int ticket = OrderTicket();
if(lots == lotSize)
{
if(type == OP_BUY && Bid-op > (PartialClosePips*Pips))
{
if(!OrderClose(ticket,lots/2,Bid,3,clrWhite))
Print("failure to close the Order");
}
else if(type == OP_SELL && op-Ask > (PartialClosePips*Pips))
if(OrderClose(ticket,lots/2,Ask,3,clrYellow))
Print("failure to close the Order");
}
}


Who is online

Users browsing this forum: No registered users and 17 guests