Lot Size, Lot Size, Lot Size

 
Hey, the Bee is back again with questions.

I've read a few threads, where traders take into account the percent of risk they're ready to take. (e.g. %5 of Free Margin or so)
which is understandable.

then I read some EA codes and some scripts, which try to calculate the lot size, based on where StopLoss is and also considering the risk percent.

and then this procedure :
Max lot size is calculated based on a percent of available free margin.

OrderCalcMargin(ORDER_TYPE_BUY, _Symbol, 1, Bid_Price, Cost_of_1_lot);
MAX_LOT = FreeMargin / Cost_of_1_lot; //or 0.05*FreeMargin...
So, it considers the risk preferred by user, but StopLoss price is ignored ?!!!!
the code means that at max, 5 percent of free margin is used for that lot size. right?
so why SL is not important here ?

Also, Why PipValue is not taken into account ? (is it ?)

Also, using these variables below, the other one can be calculated, right ? :

  • FreeMargin , StopLoss, %Risk  >  LotSize
  • %Risk, FreeMargin > LotSize
  • FreeMargin > MaxLotSize
 
Free margin has nothing to do with risk.
  1. In code: Risk depends on your initial stop loss, lot size, and the value of the pair.
    1. 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.
    2. 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.)
    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out
    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.
  2. Use a GUI EA like mine (for MT4): Indicators: 'Money Manager Graphic Tool' indicator by 'takycard' Forum - Page 6
 

whroeder1:

  1. In code: Risk depends on your initial stop loss, lot size, and the value of the pair. for each position i want the EA to open, the SL_pip and OpenPrice and Risk_percent are known.
    1. 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. Done
    2. 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.)
    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    4. You must normalize lots properly and check against min and max. Done
    5. You must also check FreeMargin to avoid stop out  . Thanks, Done

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum. That's Exactly what I need to know. how much a pip of any pair is worth ?
 
Code2219 or probably 2319:
That's Exactly what I need to know. how much a pip of any pair is worth ?
MQL4/5
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);

MQL4

double tickValue = MarketInfo(_Symbol, MODE_TICKVALUE);

Symbol Properties

 
Konstantin Nikitin:

Reread #2.1.3 It's lots * distance * tick_value/tick_size.

 
Thanks four answers.
Ok, I think i got where i was wrong. risk is not about free margin.

@Konstantin Nikitin, using that function gives reasonable lot sizes when i checked, seems to be working.
but if that's enough and reliable, why @whroeder1 is shouting in every single thread : "Don't trust TICK_VALUE..."  :lol:
or maybe those warnings are for MQ4 only ?

and now help me with these questions please :
  1. what is that DeltaPerLot ? it equals to 100000 for EURUSD and many other pairs, and equals to 915.73 for USDJPY right now. and so...
    also saw it being multiplied by your pips2dbl somewhere (which by the way, I don't know what that is.)
    I imagine you can see how much confused i am right now  :S
  2. what is that pips2dbl ? (pip to double coefficient ?!!! not that)

A little background on my EA, if you like to know about it (not vital for answering my questions) :
I've been working on this EA for months now, it has three cores to say.
one detects signals, and doesn't care about anything else. it spits out any detected pattern in different time frames of a symbol. and gives that signals to the second core. (position handler)
the second core is considering the signals and decides whether to act based on them, compute lot size, sl, tp .etc and open/close positions and so...
another part is a learning core (somewhat). this core tries to make the EA's decisions better and more reliable. it helps the second core (position handling core) to act better.
NOW, the signals provided by the first core, have a certainty factor attached to them.
like this : SIGNAL_DIRECTION = UP, SIGNAL_CERTAINTY=84 (%), SIGNAL_TYPE=3 (reverse prediction) .etc...
almost all cores are completely written (still need to be tested), but this part, the position handling part, is killing me.

I want the lot size to be calculated based on :
1. the constant Risk (percent of balance) >>>>>>> MAX_SAFE_VOLUME (which should consider the SL. sl is known.)
2. the certainty of received signal.   >>>>>> CERTAINTY
3. some other factors , which are not important here. (those coefficients given by third core.) >>>> IA_Co1, IA_Co2, ...
FINAL_LOT_SIZE = MAX_SAFE_VOLUME * CERTAINTY/100 * IA_Co1 * IA_Co2 ....

My problem is at the first step : MAX_SAFE_VOLUME ( probably the most stupid step to be stuck at :D )

 

Describe it easier. I think you will find an application.

     double    tickValue;
     SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE, tickValue);
     for(int cnt=0; cnt<OrdersTotal(); cnt++)
     {
          if( !OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) ) continue;
          if( OrderSymbol() != _Symbol)                     continue;
          if( OrderMagicNumber() != 12345 )                 continue;
          
          double slProfit = 0,
                 tpProfit = 0;
          switch(OrderType())
          {
               case ORDER_TYPE_BUY:
                    if( OrderStopLoss()   > 0 )
                         slProfit = MathFloor((OrderStopLoss()   - OrderOpenPrice())/_Point)*tickValue*OrderLots()+OrderSwap()+OrderCommission();
                    if( OrderTakeProfit() > 0 )
                         tpProfit = MathFloor((OrderTakeProfit() - OrderOpenPrice())/_Point)*tickValue*OrderLots()+OrderSwap()+OrderCommission();
                    break;
               case ORDER_TYPE_SELL:
                    if( OrderStopLoss()   > 0 )
                         slProfit = MathFloor((OrderOpenPrice() - OrderStopLoss()  )/_Point)*tickValue*OrderLots()+OrderSwap()+OrderCommission();
                    if( OrderTakeProfit() > 0 )
                         tpProfit = MathFloor((OrderOpenPrice() - OrderTakeProfit())/_Point)*tickValue*OrderLots()+OrderSwap()+OrderCommission();
                    break;
          }
     }


If neither of which is confused, then so

 
Konstantin Nikitin:

Describe it easier. I think you will find an application.

If neither of which is confused, then so

thanks again, but you misunderstood my question.
all i want to do is to calculate a proper lot size (prior to order send) based on the following : Account balance, Risk percent, StopLoss in pips.

I know the general formula for lot size , considering the risk and having the sl , is :
Number of Lots = (Account Balance in dollars x Risk %) / (Stop Loss in pips x Pip Value in dollars)


this is the same formula @whroeder1 mentioned. BUT the difference he made is that delta thing.
ok, if it's safer to use deltaperlot than tick_value alone, but how do you replace a value in formula by another variable just like that ? ( Delta has a much bigger value than TickValue !!!)

@whroeder1 please explain this delta thing, I've read your comments about it, probably others like me, stuck at this lot_size calculation, may find a simpler version of explanation useful.

THANKS

 
I answered this question
Code2219 or probably 2319 :
That's Exactly what I need to know. how much a pip of any pair is worth ?
Calculation of risk in percent
risk in% = margin * lot * leverage / deposit
risk in% = margin * lot * leverage / equity
or
risk in% = margin * lot / deposit
risk in% = margin * lot / equity
 
  1. Konstantin Nikitin: I answered this question Calculation of risk in percent
    You did and it's wrong. For a given (fixed) open price/SL/lotsize combination, how can your risk change with a change in margin or leverage? It can't. Leverage determines how large an order your broker allows, has nothing to do with your risk.

  2. Code2219 or probably 2319 why @whroeder1 is shouting in every single thread : "Don' t trust TICK_VALUE..."  :lol:or maybe those warnings are for MQ4 only ?
    TickValue is supposed to be in the account currency. Brokers sometimes, gets it wrong. What part of " MODE_TICKVALUE is not reliable on non-fx instruments with many brokers" is unclear? Not MT4, BROKERS on NON-FX symbols! That is SHOUTING.

  3. Code2219 or probably 2319@whroeder1 please explain this delta thing, I've read your comments about it, probably others like me, stuck at this lot_size calculation, may find a simpler version of explanation useful.
    The formula is RISK = OrderLots * |OrderOpenPrice - OrderStopLoss| * DeltaPerLot where DPL is TV/TS. put DPL into a variable and solve for what you are looking for. Usually you have to divide. Keep DPL separate and you will forget to invert TS/TV. Yes DPL is a large number but not when you multiply it by change in price (OOP-OSL) 5-100 ten thousandths (PIP = 0.0001)

  4. Code2219 or probably 2319 : That's Exactly what I need to know. how much a pip of any pair is worth ?
    You keep asking that, but there is no general answer. It depends on the pair and your account currency. If the pair is xxxACC where ACC is your account currency it is exactly ¤10/PIP/lot. Any other combination requires rate conversions. ACCxxx is ¤10/PIP/lot/ACCxxx. xxxyyy is ¤10/PIP/xxxACC or ¤10/PIP*yyyACC, etc.
Reason: