how calculate volume position?

 

Hi i want create EA that calculete entry volume for each postion according the risk percentage.

i use belowcode:

 Input_Volume= (((UserBalance*(Risk_Money/100))/(Stoploss_point/100))   * (SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)))  / (100000/ AccountInfoInteger(ACCOUNT_LEVERAGE));

this code work correctly if I use my program on currency pair that USD is second part like EUR/USD.

but when i want is it on some currency pair that USD is not part of that or usd is first part the input volume result is different from "Forex volume Calculator" in across the internet?

how can i fix this? please help

 

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

  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. AccountBalance * 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 - MQL4 programming forum (2017)
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
              Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

  4. You must normalize lots properly and check against min and max.

  5. You must also check FreeMargin to avoid stop out

  6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
William Roeder #:

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

  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. AccountBalance * 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 - MQL4 programming forum (2017)
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
              Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

  4. You must normalize lots properly and check against min and max.

  5. You must also check FreeMargin to avoid stop out

  6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

thank for your help. but still i cannot calculate them correctly and do not understand part of the link you share. can you instead write the correct formula and code for me please?
 
justlink #: thank for your help. but still i cannot calculate them correctly and do not understand part of the link you share. can you instead write the correct formula and code for me please?

William has already done that for you in his post ...

2. AccountBalance * 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.)
Please read his post again carefully and follow up on the links and do some extra research of your own. Also read the documentation.
 
Fernando Carreiro #:

William has already done that for you in his post ...

2. AccountBalance * 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.)
Please read his post again carefully and follow up on the links and do some extra research of your own. Also read the documentation.

thank you. the formula you and William mention is very good on EA, and work perfectly. But i also need calculator as indicator.

just like the image 1 the online lot size calculator give instrument,deposit currency, stop loss,balance and risk. that mean you need input too many data for each position. and in this form you do not now what is open or TP price like the EA.

i try create something like image 2, so i only need write risk and stop loss in point (i Think each 100 point is 1 pip). in this form the other data like instrument and balance cash automatically and the risk always is same so i need only change the stop loss each time. So I no longer need to leave the program environment and computation is done faster.

the formula i found for this calculator is image 3 and i writhe this code for that: (i mention it on first post too)

 Input_Volume= (((UserBalance*(Risk_Money/100))/(Stoploss_point/100))   * (SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)))  / (100000/ AccountInfoInteger(ACCOUNT_LEVERAGE));

However, on some sites there was no leverage parameter in the formula.

this code work correctly if I use my program on currency pair that USD is second part like EUR/USD.

but when i want is it on some currency pair that USD is not part of that or usd is first part the input volume result is different from "Forex volume Calculator" in across the internet?

thank you for your help but as i said i need the correct formula for the indicator too.

please help.  :)

Files:
1.jpg  117 kb
2.png  70 kb
3.jpg  27 kb
 
justlink #: the formula you and William mention is very good on EA, and work perfectly. But i also need calculator as indicator.

It does not matter if you are using the formula in an EA or an Indicator. It is the same. It only depends on the Stop-Loss size and current tick value.

Your formular using leverage is incorrect. Don't use it.  Use the formular that William provided.
 

Hi i finally do it but i now have to other question:

1.I can't find the equivalent of the following function(mql4) in mql 5.

MarketInfo(Symbol(), MODE_LOTSTEP))

2. how can i pass my value to the trade panel? (image 1)

Files:
Img1.png  15 kb
 
justlink #: 1.I can't find the equivalent of the following function(mql4) in mql 5. MarketInfo(Symbol(), MODE_LOTSTEP))

Reference: SymbolInfoDouble, ENUM_SYMBOL_INFO_DOUBLE

// Variables for Symbol Volume Conditions
double
   dbLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
   dbLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
   dbLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );
justlink #: 2. how can i pass my value to the trade panel? (image 1)

Not possible! Create your own trading panel as an EA. Indicators cannot manage trades.

 

Also read the following thread ...

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 17:41

You can! These are the steps I take. I supply the function with a lot size equal to the “Max Lot Size” allowed for the symbol in question, then calculate the ratio needed to achieve the fractional risk that I wish to apply, to get the correct volume for the order. I then align that with the “Lot Step” and finally check it against both the maximum and minimum allowed lots for the symbol.

The reason I use the “maximum” lots instead of just “1.0” lots as a reference value is because there is no guarantee that the value of 1.0 is within the minimum and maximum values allowed. Given that using 1.0, or the maximum, gives equivalent results anyway (by using the ratio method), I choose to use the “max lots” as the reference point which also offers the most precision for the calculation.

Something like this ...

// This code will not compile. It is only a example reference

if( OrderCalcProfit( eOrderType, _Symbol, dbLotsMax, dbPriceOpen, dbPriceStopLoss, dbProfit ) )
{
   dbOrderLots = fmin( fmax( round( dbRiskMax * dbLotsMax / ( -dbProfit * dbLotsStep ) )
               * dbLotsStep, dbLotsMin ), dbLotsMax ); 
      
   // the rest of the code ...
};

 

hi

did you check contract size?

 Input_Volume= (((UserBalance*(Risk_Money/100))/(Stoploss_point/100))   * (SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)))  / (100000/ AccountInfoInteger(ACCOUNT_LEVERAGE));

 Input_Volume= (((UserBalance*(Risk_Money/100))/(Stoploss_point/100))   * (SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)))  / (SymbolInfoDouble(_Symbol, SYMBOL_TRADE_CONTRACT_SIZE)/ AccountInfoInteger(ACCOUNT_LEVERAGE));
Reason: