Download MetaTrader 5
To add comments, please log in or register
Do you have Linux? Find out how to install MetaTrader on it!
Roland
34
Roland 2014.06.26 22:08 

Hi,

I see so many different Moneymanagement calculation and some of them are very complicated.

So, is this right, as I wanted to calculate the lotsize:

NormalizeDouble(AccountBalance()*(RiskOfBalance/100.0)/(StopLossPrice/pips2dbl*MarketInfo(Symbol(),MODE_TICKVALUE)),LotDigit);

Lot's of thanks!!

whroeder1
13602
whroeder1 2014.06.27 01:40  
  1. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  2. StopLossPrice/pips2dbl is wrong. How can price (in pips) * tickvalue have meaning.
  3. AccountBalance * percent = Risk = ( OrderOpenPrice - OrderStopLoss )*DIR * DeltaPerLot * lotsize. Solve for lotsize and normalize 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. Note that OrderOpenPrice - OrderStopLoss includes the spread.
Roland
34
Roland 2014.06.27 04:35  

Hi WHRoeder,

thanks für your tipps! Have I understood everything correctly?

double  DeltaValuePerLot(string pair=""){
    if (pair == "") pair = Symbol();
    return(  MarketInfo(pair, MODE_TICKVALUE)
           / MarketInfo(pair, MODE_TICKSIZE) ); // Not Point.
}
double NormalizeLots(double lots, string pair=""){
    if (pair == "") pair = Symbol();
    double  lotStep     = MarketInfo(pair, MODE_LOTSTEP),
            minLot       = MarketInfo(pair, MODE_MINLOT);
    lots            = MathRound(lots/ls) * ls;
    if (lots < minLot) lots = 0;    // or minLot
    return(lots);
}

if(OrderDIR == OP_BUY)
  lotsize = NormalizeLots((AccountBalance * percent) / (( OrderStopLoss - OrderOpenPrice ) * DeltaPerLot))
else 
  lotsize = NormalizeLots((AccountBalance * percent) / (( OrderOpenPrice - OrderStopLoss ) * DeltaPerLot))
Boeing747
241
Boeing747 2014.06.27 10:39  
apfel:

Hi WHRoeder,

thanks für your tipps! Have I understood everything correctly?



//+------------------------------------------------------------------+
//|                                                         SIZE.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//#property strict
#property show_inputs
extern double Risk       = 5.0;           // The risk of the position as a percentage of the balance. 
extern color Clr_Line    = DarkGray;      // Color line. 
extern color Clr_Text    = DarkGray;      // Color text.
extern int Shift_X_axis  = 1087;          // The offset of the text horizontally in pixels.
extern int Shift_Y_axis  = 10;            // The offset of the text vertically in pixels.
extern int Density_lines = 20;            // The density of lines.
extern int Distance=150;                  // Adjusting the distance between characters.
string symb;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   string symbol=Symbol();
   double lotstep=MarketInfo(symbol,MODE_LOTSTEP);
   int digit=(int)MarketInfo(symbol,MODE_DIGITS);
   double tick=MarketInfo(symbol,MODE_TICKSIZE);
   double pt=MarketInfo(symbol,MODE_TICKVALUE);
   double ask=MarketInfo(symbol,MODE_ASK);
   double ar=0.25*(High[1]-Low[1]+High[2]-Low[2]+High[3]-Low[3]+High[4]-Low[4]);
   ObjectCreate("LINE",OBJ_TREND,0,2*Time[0]-Time[4],ask-ar,Time[0],ask);
   ObjectSet("LINE",OBJPROP_COLOR,Clr_Line);
   ObjectSet("LINE",OBJPROP_RAY,0);
   ObjectSet("LINE",OBJPROP_BACK,1);
   string text;
   string name[]={"SIZE OF POSITION","RISK FROM BALANCE","RISK IN CURRENCY","PRICE_1","PRICE_2","SIZE","PERCENTAGE","CURRENCY","UP","DOWN","LINE"};
   int i,y;
   int e=0;
   int d=0;
   double z[13],dis,x,size;

   for(i=0; i<10; i++)
     {
      if(i==5)
        {
         e=0;
         d=Distance;
        }
      ObjectCreate(name[i],OBJ_LABEL,0,0,0,0,0,0,0);
      ObjectSet(name[i],OBJPROP_XDISTANCE,Shift_X_axis+d);
      ObjectSet(name[i],OBJPROP_YDISTANCE,5+e*Density_lines);
      e++;
     }
//----

   z[8]=ObjectGet("LINE",OBJPROP_PRICE1);
   z[9]=ObjectGet("LINE",OBJPROP_PRICE2);
//----
   if(tick>0)dis=MathAbs(z[8]-z[9])/tick;
   x=100*dis*pt*lotstep;
   if(x>0)y=MathFloor(Risk*AccountBalance()/x);
   size=lotstep*y;
   z[5]=size;
   z[6]=Risk;
   if(Risk==0) size=lotstep;
   z[7]=dis*pt*size;
//----
   for(i=0; i<10; i++)
     {
      text=name[i];
      if(i>4 && i<8) text=DoubleToStr(z[i],2);
      if(i>7) text=DoubleToStr(z[i],digit);
      ObjectSetText(name[i],text,10,"Arial",Clr_Text);
     }

  }
//+------------------------------------------------------------------+
whroeder1
13602
whroeder1 2014.06.27 13:33  
apfel: thanks für your tipps! Have I understood everything correctly?
  1. (OrderDIR == OP_BUY)
      lotsize = NormalizeLots((AccountBalance * percent) / (( OrderStopLoss - OrderOpenPrice
    If it's a buy the SL is below the OP so OSL - OOP would be negative. Those are backwards.
  2. z[8]=ObjectGet("LINE",OBJPROP_PRICE1);
    z[9]=ObjectGet("LINE",OBJPROP_PRICE2);
    if(tick>0)dis=MathAbs(z[8]-z[9])/tick;
    Your prices from "LINE" are bid prices. On a buy the OOP includes the spread, on a sell the SL includes the spread. dis must include the spread.
/
To add comments, please log in or register