Download MetaTrader 5

Dynamic Risk Calculation

To add comments, please log in or register
Track Market top sellers!
Matthias 2015.08.14 13:55 

Hi Guys,


im trying to calculate my risk dynamically but i dont really "like" my code and im pretty sure there has to be a better method of doing it but i cant really think of one.

// Risk Calculation
            double LongEntryPrice = NormalizeDouble(iHigh(Symbol(), 0, 1), Digits);
            double ShortEntryPrice = NormalizeDouble(iLow(Symbol(), 0, 1), Digits);
            double StopLossPrice = NormalizeDouble(LongEntryPrice - ShortEntryPrice, Digits);
            Print("StopLossPrice: ", StopLossPrice);
            double StopLoss = StopLossPrice * 100000; // Convert 0.00234 to 234
            if(NormalizeDouble(StopLoss, 0) < 100)
               StopLoss = StopLoss * 10; // If some random stop loss is smaller than 100 (0.0024 e.g.) * 10 => 240
            Print("StopLoss: ", StopLoss);
            double Lots = NormalizeDouble((AccountEquity() * MaxRisk / StopLoss * PipValue), 1);
            Print("Lots: ", Lots);


Any Tips?


Thanks in Advance,


whroeder1 2015.08.14 18:35  
  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 = RISK = (OrderOpenPrice - OrderStopLoss)*DIR * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  3. Do NOT use TickValue by itself - DeltaPerlot
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out
burgie 2015.08.15 12:56  
MarketInfo( SymName , MODE_TICKVALUE ) / ( MarketInfo( SymName , MODE_TICKSIZE ) / Point

I use MarketInfo to calculate the value of a (fractional) pip in the currency of the account. That way you can risk a percentage of your account balance per trade. There is one problem with it though. MODE_TICKVALUE and MODE_TICKSIZE are not stored in the history data. They hold simply the latest value received from the server. So when you backtest your lotsize calculations can be off depending on the currency pair you're testing on. A work around to this problem is to set the currency of the account to the base currency of the pair. So if you're testing EUR/USD you set the currency of the account in the Strategy Tester to USD.

Matthias 2015.08.16 18:00  
Hey guys, thanks for your answers, ill definitely experiment with your answers and let you know how it went!
Matthias 2015.08.16 22:13  
  1. Account Balance * percent = RISK = (OrderOpenPrice - OrderStopLoss)*DIR * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)



whroeder1 2015.08.17 11:00  
+1 for Buy -1 for sell. (OOP - OSL)*DIR > 0 or there is no RISK.
Ali Akcaagac
Ali Akcaagac 2015.08.18 12:31  
I have written a dynamic risk calculator by using the events handler system, which got recently announced in MT4. Wherever I point the mouse (e.g. where a stop loss has actually to be placed within the chart), it then calculates the distance between where the mouse pointer is and the BID/ASK price to figure out the points between it. This value is then taken into account to calculate the LOT size required to enter the trade. For my personal needs i've set a fixed risk for 1% of the account balance. The calculation isn't 100% precise but enough for my trading needs.
Matthias 2015.08.18 15:01  

The actual problem that i have is, when i try to calculate my stoploss i subtract the lowest price of the last candle from the highest price which gives me e.g. 0.00265 Points.

 How do i convert this 5 digit double value to ~27 pips?  Or can i just take the value of Low[1] as StopLoss?

Ali Akcaagac
Ali Akcaagac 2015.08.18 15:09  
string marketSymbol = Symbol();
int marketPrecision = MarketInfo(marketSymbol, MODE_DIGITS);

double marketDivisor[] = {0.0, 0.1, 0.01, 0.001, 0.0001, 0.00001};

double marketPips = MathAbs((marketBid - marketPrice) / marketDivisor[marketPrecision]);

// in your case place the variable holding 0.00265 here

// double marketPips = MathAbs((0.00265) / marketDivisor[marketPrecision]);

Basicly this is copied 1:1 from my own code. There are probably better solutions. Hope this helps.

BTW: I calculate everything with points. 

Matthias 2015.08.18 15:34  
Thanks a lot, im sure this will help! 
To add comments, please log in or register