AGAIN: Calculate lot size wit fix risk and fix points

 

i scrolled to many pages but the moor i read them the less i understand.

Basicli it is simple math

Balance in USD

Distance 500 Points

myBalance is 10000 USD myRisk 2% myRiskMoney 200 USD 

myLot is then 200USD / 500 Points

Job done! (Help me if the code is wrong so far:)

double      myBalance            = 0;
double      myRiskMoney          = 0;
uint        myRiskPoints         = 500;
double      myPercent            = 2;
string      AccountCurrency      = AccountInfoString(ACCOUNT_CURRENCY);
double      myLot                = 0;

void OnTick()
  {
   myBalance   = AccountInfoDouble(ACCOUNT_BALANCE);
   myRiskMoney = (myBalance/100)*myPercent;
   myLot = NormalizeDouble(myRiskMoney/myRiskPoints,2);
   
   
   MessageBox(
   "Balance: " + DoubleToString(myBalance) +"\n"+
   DoubleToString(myPercent,2) + "% Risk is: " + DoubleToString(myRiskMoney,2) + " " + AccountCurrency + "\n"
   "Distance: " + IntegerToString(myRiskPoints,0) +" -> Lot: " + DoubleToString(myLot,2) );
}

As long i trade USDXXX - but now i wont to trade EURCHF or so.

How i adjust EURCHF wenn i have 200USD risk how much EUR will it be and how can i then end up with the lots

there must be a easy codeline or a multiplicator ore something - how is the math done??

Scrolled many pages but ended up confused ?!

 
 

Thank you Fladimir


i tried to understand -    m_money.Percent(PercentRisk); // 10% risk

and - #include <Expert\Money\MoneyFixedRisk.mqh>

But it is not clear wat if i want to calculate for a pending order for example? I never

used the cTrade Library i do not feel good when i am not untherstand wath the code does...


Can u please describe me the Idea how you turn the 200USD risk in ammount for new currency in some words so i may find other ideas arround ?


Wath about

   F0   =  SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);

   F1   =  SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE_PROFIT);

   F2   =  SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE_LOSS);


Is there a usable relation? how is TickValue calculated ??

 

An interesting problem - I always managed to calculate through the percentage of risk. I'll try to solve the calculation when the loss is set in money.

To calculate, you need two parameters: a loss (set only positive numbers) and a StopLoss level.

//+------------------------------------------------------------------+
//|                                             Money Money Risk.mq5 |
//|                              Copyright © 2017, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2017, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//---
#include <Trade\SymbolInfo.mqh>  
#include <Trade\AccountInfo.mqh>
CSymbolInfo    m_symbol;                     // symbol info object
CAccountInfo   m_account;                    // account info wrapper
//---
#property script_show_inputs
//--- input parameters
input double   InpMaxLossMoney   = 200.0;    // Max Loss Money (Set only positive numbers)
input ushort   InpStopLoss       = 50;       // Stop Loss (in pips)
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- Step 1: Get the current price
   m_symbol.Name(Symbol());                  // sets symbol name
   if(!RefreshRates())
     {
      Print("Error RefreshRates. Bid=",DoubleToString(m_symbol.Bid(),Digits()),
            ", Ask=",DoubleToString(m_symbol.Ask(),Digits()));
      return;
     }
   m_symbol.Refresh();

//--- Step 2: Tuning for 3 or 5 digits
   double         m_adjusted_point;             // point value adjusted for 3 or 5 points

   int digits_adjust=1;
   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
      digits_adjust=10;
   m_adjusted_point=m_symbol.Point()*digits_adjust;

//--- Step 3: Calculate
   double volume=m_symbol.LotsMin();
   double price=m_symbol.Ask();
   double stop_loss=m_symbol.NormalizePrice(price-InpStopLoss*m_adjusted_point);
   double profit=0.0;
   while(-profit<InpMaxLossMoney)
     {
      profit=m_account.OrderProfitCheck(m_symbol.Name(),ORDER_TYPE_BUY,volume,price,stop_loss);

      if(profit==EMPTY_VALUE)
        {
         Print("Error calculate, volume: ",DoubleToString(volume,2),
               ", price: ",DoubleToString(price,m_symbol.Digits()),
               ", stop_loss: ",DoubleToString(stop_loss,m_symbol.Digits()));
         return;
        }

      volume+=m_symbol.LotsMin();
     }
//--- Step 4: Result
   Print("Is given: ",DoubleToString(InpMaxLossMoney,2),
         ", calculate profit: ",DoubleToString(profit,2),
         ", volume: ",DoubleToString(volume,2),
         ", price: ",DoubleToString(price,m_symbol.Digits()),
         ", stop_loss: ",DoubleToString(stop_loss,m_symbol.Digits()));
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
      return(false);
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
      return(false);
//---
   return(true);
  }
//+------------------------------------------------------------------+

The problem is solved by a simple cycle with a search of all values :)

Files:
 
Not quite right. I did not take into account the level of margin.
 

@Zar88 You need to add "SYMBOL_TRADE_TICK_VALUE" in the calculation:

myLot = NormalizeDouble( myRiskMoney  / (myRiskPoints * SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE)), 2);
 

Cool this looks much easyser

but ther is a followup question on this:

(myRiskPoints * SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE)

but i want to adjust myRiskMoney from 200USD to XXXGBP //--(example)

mathenaticly it is:

myRiskMoney *(1/SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE))

if this is right , then i am OK??

 
All calculations must be in same currency.
"SYMBOL_TRADE_TICK_VALUE" is the value in the currency of your account.

And your risk needs to be measured in the currency of your account.

So you do not need to change anything, just use the account currency for all calculations.

 

Then this is it:

double      myBalance            = 0;
double      myRiskMoney          = 0;
uint        myRiskPoints         = 500;
double      myPercent            = 2;
string      AccountCurrency      = AccountInfoString(ACCOUNT_CURRENCY);
double      myLot                = 0;

void OnTick()
  {
   myBalance   = AccountInfoDouble(ACCOUNT_BALANCE);
   myRiskMoney = (myBalance/100)*myPercent;
   myLot = NormalizeDouble( myRiskMoney  / (myRiskPoints * SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE)), 2);
   
   MessageBox(
   "Balance: " + DoubleToString(myBalance) +"\n"+
   DoubleToString(myPercent,2) + "% Risk is: " + DoubleToString(myRiskMoney,2) + " " + AccountCurrency + "\n"
   "Distance: " + IntegerToString(myRiskPoints,0) +" -> Lot: " + DoubleToString(myLot,2) );
}


@Vlad - looks much easier right :-D

 
Zar88:

Then this is it:


@Vlad - looks much easier right :-D

Except this is not correct, a tick is not the same as a point.

Forum on trading, automated trading systems and testing trading strategies

Calculating a trade lotsize by Max Loss in base currency

Alain Verleyen, 2017.03.05 16:54

You don't need to use the same approach as WHRoeder (or anyone) but understand it and apply it for your needs.

To calculate a lot size from a risk I am using such code :

//--- compute from sl
   double tickSize      = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
   double tickValue     = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
   double capital       = AccountInfoDouble(ACCOUNT_EQUITY);
   double tickCount     = sldistance/tickSize;
   double valueToRisk   = capital*risk/100;
   double lots          = (tickCount*tickValue)!=0 ? valueToRisk/(tickCount*tickValue) : 0;

‌Obviously you can use Balance or whatever you want for "capital".

"sldistance" is a decimal value like 0.0012 (12 pips). "Risk" is for example 2 (for 2% of capital to use).‌


 

@Alain Verleyen
As far as I know for Forex enough "SYMBOL_TRADE_TICK_VALUE".

"SYMBOL_TRADE_TICK_SIZE" is needed for CFDs and Futures - symbols with different margins in different directions.

Reason: