Download MetaTrader 5

script closing orders one at a time

To add comments, please log in or register
babymt4
7
babymt4  

Hi:

I wrote a script to close all orders at the same time.  But I don't know why it closes orders one at a time.  I need to run the script repeatedly in order to close all open orders.  Does anyone know what I might have done wrong?

The script I wrote is as follows:

#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "http://www.mql4.com"
#property version   "1.00"
#property strict

bool selectresult=false,closeresult=false,deleteresult=false;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   for(int z=OrdersTotal()-1;z>=0;z--)
      {
      selectresult=OrderSelect(z,SELECT_BY_POS,MODE_TRADES);
      if (OrderType()==OP_BUY) closeresult=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_BID),10,clrNONE);
      if (OrderType()==OP_SELL) closeresult=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_ASK),10,clrNONE);
      if (OrderType()==OP_BUYLIMIT ||
          OrderType()==OP_BUYSTOP ||
          OrderType()==OP_SELLLIMIT ||
          OrderType()==OP_SELLSTOP) deleteresult=OrderDelete(OrderTicket());

      }

  }

whroeder1
15117
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop and check OrderSelect when dealing with multiple orders. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. You must RefreshRates after sleep and between multiple server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead.
  4. Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
babymt4
7
babymt4  
Thanks WHRoader !!
To add comments, please log in or register