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( ... );