Download MetaTrader 5
To add comments, please log in or register
Find and download Code Base apps via MetaTrader. It is fast!
FirstTimeTrader
7
FirstTimeTrader 2016.07.12 14:22 
Yes, I know this has been posted a couple of times now, but I'm really new to trading in general and want to be sure, that I understand how the calculation of the LotSize (with tolerated Risk and StopLoss-Distance) given.

This is the code I wrote (I would appreciate you to review this code and wrote a lot of explaining comment):

const extern int StopLoss   =   60;    // In Minipips (5 Digits for GKFX)
const extern double Risk    = 0.015;   // 3% of AccountBalance
const extern double MaxLot  = 50;      // Only allow to buy up to 50 lots (GKFX Limit)

int OnInit()
{
   // Broker           : GKFX (=> 5 Digits)
   // Symbol           : EURUSD
   // Account-Currency : EUR
   //
   // One standard lot is the equivalent to 100,000 units of the
   // base currency. => 100,000 EUR (I can assume this, right)
   //
   // When buying one lot. I'm essentially buying USD worth
   // 100,000 EUR
   //
   // StopLoss :   40 (5 Digits => 40 = 40 * 1/10 Pips = 4 Pips
   // Risk     : 0.03 (3% of current account balance)
   //
   // Let's pretend I have an 10,000 EUR Account.
   // With Risk = 0.03 I tolerate 10,000 * 0.03 = 30 EUR Loss.
   //
   //
   // double toleratedLoss = AccountBalance * Risk;                     // 30 EUR
   //
   // Okay, now I want to find the number of lots I need to buy,
   // so that the cost of StopLoss/10 (5 Digits) (= 4) pips matches
   // the toleratedLoss.
   //
   // toleratedLoss / (StopLoss/10) will give me the amount of
   // money I'm willing to loose per pip.
   //
   // 30 EUR / 3 pips = 10 EUR per pip
   //
   // double toleratedPricePerPip = toleratedLoss / (StopLoss / 10);    // 10 EUR per pip
   //
   // As said before one lot is equivalent to 100,000 units of the
   // base currency. Since one pip is equivalent to a difference of
   // 0.0001 in the currency pair. When buying 1 lot = 100,000 EUR
   // a change of one pip will result in 100,000 * 0.0001 = 10 EUR
   // gain/loss. I want to find the number of lots I need to buy so that
   // a change of one pip will result in toleratedPricePerPip gain/loss.
   //
   // numberOfLots * 100.000 * 0.0001 = toleratedPricePerPip.
   // numberOfLots * 10 = toleratedPricePerPip.
   // numberOfLots = toleratedPricePerPip / 10.
   //
   // double numberOfLots = toleratedPricePerPip / 10;                  // 1 lot
  
   double MinLot      = MarketInfo(Symbol(), MODE_MINLOT);          // Minimum volume
   double LotStep     = MarketInfo(Symbol(), MODE_LOTSTEP);         // Step to between valid amount of ltos
   double PricePerLot = MarketInfo(Symbol(), MODE_MARGINREQUIRED);  // Price of one lot
   double FreeMargin  = AccountFreeMargin();                        // Free Margin
  
   double toleratedTotalLoss, toleratedLossPerPip, lotsToBuy, validLotsToBuy;
  
   toleratedTotalLoss  = AccountBalance() * Risk;                 // Total amount of money I tolerate to loose
   toleratedLossPerPip = toleratedTotalLoss / (StopLoss / 10);    // Amount of money I tolerate to loose/gain per pip
   lotsToBuy = toleratedLossPerPip / (100000 * 0.0001);           // Optimal number of lot's I could buy
   validLotsToBuy = MathRound(lotsToBuy / MinLot) * MinLot;       // Valid number of lot's I could buy
  
   // Do I even have enough money to buy validLotsToBuy lots?
   if (FreeMargin < validLotsToBuy * validLotsToBuy) {
      lotsToBuy = FreeMargin / PricePerLot;
      validLotsToBuy = MathFloor(lotsToBuy / LotStep) * LotStep;
   }
  
   if (validLotsToBuy > MaxLot) {
      validLotsToBuy = MaxLot;
   }
  
   if (validLotsToBuy < MinLot) {
      Alert("Not enough Balance/FreeMargin to buy ", MinLot, " lots ...");
      return(INIT_FAILED);
   }
  
   Alert("You should buy: ", validLotsToBuy, " lots ...");
   return(INIT_SUCCEEDED);
}
Do I make any major calculation mistakes or did I understand the system properly? Where could this code break (different Currency-Pairs/Broker)? Are my thoughts in the comments right?


Thank you really much for the help!!

FirstTimeTrader
whroeder1
13644
whroeder1 2016.07.13 15:02  
  1. toleratedLossPerPip = toleratedTotalLoss / (StopLoss / 10);    // Amount of money I tolerate to loose/gain per pip
    lotsToBuy = toleratedLossPerPip / (100000 * 0.0001);           // Optimal number of lot's I could buy
    Don't hard code numbers. The code assumes you are trading EURUSD with standard lots account using USD as a base currency. Don't assume.
    • 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.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerlot + CommissionPerLot) (Note OOP-OSL includes the SPREAD)
    • Do NOT use TickValue by itself - DeltaPerlot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
FirstTimeTrader
7
FirstTimeTrader 2016.07.13 19:18  
Thank you for your help! But as I you can see in my code - I acutally checked min/max and FreeMargin. I think I'm also normalzing the lots with MathFloor(lotsToBuy / LotStep) * Lotstep don't I? And could you please explain your lines of code as I did with my code above? Like |OrderOpenPrice - OrderStopLoss| * DeltaPerlot + CommisionPerLot). And where do I use TickValue
/
To add comments, please log in or register