2

To All Master / Coder

I have a question on how to find the Lowest Lots number in MQL4

Here's my starting code :

for (  int T = 0; T < OrdersTotal(); T++ ){
       if (  OrderSelect( T, SELECT_BY_POS ) == true ){
             if (  OrderSymbol() == Symbol() ){
                   // Find Lowest Lots Code Here . . . . . . . . . . . . .


                   // ....................................................
             }
       }
}

I do not know how to start finding the lowest lots, everytime I use OrderLots() it show only the latest opened Lots.

user3666197
  • 1
  • 6
  • 50
  • 92
user3869115
  • 41
  • 1
  • 6

3 Answers3

0

MQL4 works within certain "external" contexts
- Broker Terms & Conditions being one such
- Trade Management system being the second such ... to name just a few

Having said this, your code happens to re-read OrderLots() from Trade Management db.Pool, where the record has to be principally SELECT()-ed first, so if your code does not explicitly perform OrderSelect()-driven navigation throughout the db.Pool, your OrderLot()'s responses would remain randomly dependent on other db.Pool "current"-record-pointer activity ( which is more or less the know Russian Roulette, where you never know, when the arm will fire ).

So one shall rather remember to always explicitly control "current"-record-pointer navigation by adequately parametrised call to OrderSelect() before reading any piece of data from db.Pool, the more if going to try to modify anything.


So, how to attack the minimum amount of Lots?

Well, MarketInfo( _Symbol, < MODE_MINLOT | MODE_LOTSTEP | MODE_MAXLOT > ) is the set of function calls, that will save you.

Individual trading instruments have these values pre-set from Broker and these may differ also according to different Account Types / business offerings contracted between you, The Investor, and the Broker.

For this reason it is wise to systematically use a function, defined like this:

double   NormalizeLOTs( const double nLOTsREQUESTED, string aCcyPAIR = "" ){
         if ( aCcyPAIR == "" )   aCcyPAIR = _Symbol;
         return(  NormalizeDouble( MathMax( MathMin( ...
                                                     ),
                                            ),
                                   2   // --------------- BROKER_DEPENDENT, again
                                   )
                  );
   }

just to keep all Lot-sizing-specific operations to meet the Broker's Terms & Conditions, because, otherwise your OrderSend() will straight fail to file at the Server side.


Finally:

if not testing the lowest acceptable size of the Lots, but an actual value, of the smallest Order, already filed at the trading Server and still exposed to Market, the process has to traverse the records kept in the Trade Management system, in the db.Pool

double aSmallestLotSizeExposedAtMARKET = DBL_MAX;

for (  int aRecordPTR  = 0;
           aRecordPTR <  OrdersTotal();
           aRecordPTR++
           ){
       if (  OrderSelect( aRecordPTR, SELECT_BY_POS, MODE_TRADES ) )
             if (  _Symbol                         == OrderSymbol()
                && aSmallestLotSizeExposedAtMARKET >  OrderLots()
                )  aSmallestLotSizeExposedAtMARKET =  OrderLots();
}
if (               aSmallestLotSizeExposedAtMARKET >  DBL_MAX - 1 )
                   aSmallestLotSizeExposedAtMARKET =  EMPTY;

Print( ... );
user3666197
  • 1
  • 6
  • 50
  • 92
0

Assuming you are looking for the smallest OPENED order of the same SYMBOL of your chart:

#property copyright "Copyright 2016, joseph.lee@fs.com.my"
#property link      "https://www.facebook.com/joseph.fhlee"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()  {
    int viSmallestOrderLot = 0;
    int viSmallestOrderTicket = -1;
    for(int t=0; t<OrdersTotal(); t++ ) {
        if(OrderSelect(t, SELECT_BY_POS))
            if(OrderSymbol()==Symbol())
                if( (OrderType()==OP_BUY) || (OrderType()==OP_SELL) ) {
                    if( (viSmallestOrderLot == 0) || (OrderLots() < viSmallestOrderLot)) {
                        viSmallestOrderLot      = OrderLots();
                        viSmallestOrderTicket   = OrderTicket();
                    }
                }
    }
    if( viSmallestOrderTicket == -1)
        Print("No OPENED Order.");
    else
        Print( "Smallest OPENED Order: " + viSmallestOrderTicket + ", LotSize: " + viSmallestOrderLot + " lots");
}
//+------------------------------------------------------------------+

Will give you the smallest OPENED order of the same Symbol of the current chart. If there are multiple orders with the same smallest lot size, then the SmallestOrderLot will be correct, but the Order Ticket will be undetermined (usually the oldest, but is not guaranteed): enter image description here

jlee88my
  • 2,935
  • 21
  • 28
-1

Start with declaring the variable and give it a unrealistic high value

double lowestLot = 1234567890;

Then, in the loop, check if the OrderLots() is lower than this value and replace the lowestLot value by this new found value.

if ( OrderLots() < lowestLot ) lowestLot = OrderLots();

When loop is finished, you'll have found the lowest lot.

user3666197
  • 1
  • 6
  • 50
  • 92