Download MetaTrader 5

Random Number Help Plz

To add comments, please log in or register
Tony Chavez
236
Tony Chavez  

Trying to figure out how to get random number for use in random lot sizes.

Ex:

double randNumb = (MathRand()%9)/100; //randmon lot size

double LotSize = 0.1 + randNumb;

honest_knave
Moderator
2324
honest_knave  

Within what range? Let's say you want a random number between 1 and 10:

1 + 10*MathRand()/32768

Whatever you choose, remember to make sure your lot size is valid or you'll end up with 131 errors.

Forum on trading, automated trading systems and testing trading strategies

ERR_INVALID_TRADE_VOLUME 131 Invalid trade volume. error when trying to close order

honest_knave, 2017.01.18 09:09

Error 131 means the lot size is incorrect. Each broker sets their own parameters.

  • There is a minimum lot size (SYMBOL_VOLUME_MIN) e.g. 0.1
  • If you try to trade 0.01 your lot size is invalid
  • There is a maximum lot size (SYMBOL_VOLUME_MAX) e.g. 100
  • If you try to trade 150 your lot size is invalid
  • There is a lot step (SYMBOL_VOLUME_STEP) which your lot size must be an increment of e.g. 0.1
  • If you try to trade 0.15 your lot size is invalid. 

So, before placing any trade you must check these values using SymbolInfoDouble() and adjust your lot size accordingly.


Tony Chavez
236
Tony Chavez  
honest_knave:

Within what range? Let's say you want a random number between 1 and 10:

1 + 10*MathRand()/32768

Whatever you choose, remember to make sure your lot size is valid or you'll end up with 131 errors.


That doesn't seem to work for some reason.

double randNum = (1 + 9*MathRand()/32768)/100;

double LotSize = 0.1 + randNum;


Still buys and sells at lot size 0.1 but if I manually set it to 1.2, 1.3 or 1.4 etc it works.

Carl Schreiber
7354
Carl Schreiber  

double(MathRand()%10) * 0.1 + 0.1 will give you 0.1 .. 2.0 (9.0 will appear a bit less often than the other 0..8  due to the upper limit 32768.)

This will give you 0.1 .. 2.0 - may be you just want double(MathRand()%9) * 0.1 + 0.1 which will give you 0.1 .. 0.9.

whroeder1
16058
whroeder1  
  1. Write the general case once.
    // Returns a random number in the range [min..max) Max excluded.
    // Random(10,  0)   returns 0   .. 9      with equal probability.
    // Random(10.0,0.0) returns 0.0 .. 9.9999 with equal probability.
    template<typename T>
    T  Random(T max, T min=0){  
       return (T) MathRand()/32768.0*(max-min)+min);  
    }
  2. Normalize lots properly
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
Tony Chavez
236
Tony Chavez  
Thanks for all the help but in the end it was all for not, I read a forum that said different lot sizes would help get around the FIFO rule but not true.
To add comments, please log in or register