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);
}}}}}
}