• Sign up >>

Newbie Coding Question regarding my Partial Close

To add comments, please log in or register
I-Kai Wu
I-Kai Wu  


I'm a newbie just starting out to learn MQL4..

I'm trying using an existing OrderClose function to close half of my trades once my order has traveled in my favor 1x the ATR of the bar before the OrderOpentime(I used a iBarshift function), I've get the code to print out the value I was looking for, and it is correct, however it is not doing the same on the chart.

Also I'm trying to make it Close half of my position only once... but it seems to close multiple times... I tried using a bool set as true at the start of void ontick, turn it as false after partial close and turn it back as true after opening an order, but it doesnt seem to work....

Could anyone help me out? Thank you....

void myOrderClose(int type, int volumepercent, string ordername) //close open orders for current symbol, magic number and "type" (OP_BUY or OP_SELL)
   if(!IsTradeAllowed()) return;
   if (type > 1)
      myAlert("error", "Invalid type in myOrderClose");
   bool success = false;
   int err = 0;
   string ordername_ = ordername;
   if(ordername != "")
      ordername_ = "("+ordername+")";
   int total = OrdersTotal();
   int orderList[][2];
   int orderCount = 0;
   for(int i = 0; i < total; i++)
      while(IsTradeContextBusy()) Sleep(100);
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderMagicNumber() != MagicNumber || OrderSymbol() != Symbol() || OrderType() != type) continue;
      ArrayResize(orderList, orderCount);
      orderList[orderCount - 1][0] = OrderOpenTime();
      orderList[orderCount - 1][1] = OrderTicket();
   if(orderCount > 0)
     ArraySort(orderList, WHOLE_ARRAY, 0, MODE_ASCEND);
   for (i = 0; i < orderCount; i++)
      if(!OrderSelect(orderList[i][1], SELECT_BY_TICKET, MODE_TRADES)) continue;
      while(IsTradeContextBusy()) Sleep(100);
      double price = (type == OP_SELL) ? Ask : Bid;
      double volume = NormalizeDouble(OrderLots()*volumepercent * 1.0 / 100, LotDigits);
      if (NormalizeDouble(volume, LotDigits) == 0) continue;
      success = OrderClose(OrderTicket(), volume, NormalizeDouble(price, Digits()), MaxSlippage, clrWhite);
         err = GetLastError();
         myAlert("error", "OrderClose"+ordername_+" failed; error #"+IntegerToString(err)+" "+ErrorDescription(err));
   string typestr[6] = {"Buy", "Sell", "Buy Limit", "Sell Limit", "Buy Stop", "Sell Stop"};
   if(success) myAlert("order", "Orders closed"+ordername_+": "+typestr[type]+" "+Symbol()+" Magic #"+IntegerToString(MagicNumber));

void OnTick()
   int ticket = -1;
   double price;   
   double TradeSize;
   double SL;
   double TP;
   bool isNewBar = NewBar();
   //Close Long Positions, instant signal is tested first
   if(isNewBar //Send order when new bar opens
    && Close[1] < iCustom(NULL, PERIOD_CURRENT, "Adaptive averages - mtf + alerts", "Current time frame", Periods, 0, Method, 2.0, 50, AtrAverageMethod, 0, 0, 0, 0, 0, 0, BarsShift, 0, Shift)
         myOrderClose(OP_BUY, 100, "");
      else //not autotrading => only send alert
         myAlert("order", "");
    if(OrderType() == OP_BUY && OrderStopLoss() < OrderOpenPrice() && MarketInfo(0,MODE_BID) >= (OrderOpenPrice()+ATR_Multiplier_ToBreakeven*iATR(NULL, PERIOD_CURRENT, ATR_Period, iBarShift(OrderSymbol(), 0, OrderOpenTime(), false)+1)) && OrderSymbol()==Symbol())
         Print("The ATR is "+ (OrderOpenPrice() + iATR(NULL, PERIOD_CURRENT, ATR_Period, iBarShift(OrderSymbol(), 0, OrderOpenTime(), false)+1)));
         myOrderClose(OP_BUY, 50, ""); 
ATR.JPG 282 kb
William Roeder
William Roeder  
I-Kai Wu: I'm trying to make it Close half of my position only once... but it seems to close multiple times... 

You have to have a way to tell if you have already closed it once. I move the SL to BE+ before closing and test that condition.

To add comments, please log in or register