LotSizing Issue on none fixed pip amount but variable stoploss levels

 

Hi,

 I am new to MQL4, I haven't had a problem coding lotsizes based on a fixed amount of pips but cannot seem to make it work where I am calculating the lots size base on entry and stoploss levels. Please advise... Code is below... 

void EnterTrade(int TypeOfTrade){

   err=0;

   double Equity = AccountEquity();

   double RiskedAmount = Equity*(RiskedPercent*0.01);

   double LowOfBar1    = Low[1];// BuyOrder StopLoss Level

   double HighOfBar1    = High[1];// SellOrder StopLoss Level

  

   double StopLossLevel = HighOfBar1;

   double Price = Bid;//Sets Bid for OrderSend to use but below if statement allows that to change

   double StopLoss = (StopLossLevel - Bid)*Point;

   if (TypeOfTrade == OP_BUY){

   Price = Ask;//Helps OrderSend Identify if it enters Ask or Bid

   StopLossLevel = LowOfBar1;

   StopLoss = (Ask - StopLossLevel)*Point;

   }

   double Lots = (RiskedAmount/StopLoss);


   int ticket = OrderSend(Symbol(),TypeOfTrade,Lots,Price,SlippageAllowed,0,0,"Test1V1",MagicNumber,0,clrGreen);

      if(ticket > 0){

         if(OrderSelect(ticket,SELECT_BY_TICKET)){

            if(!OrderModify(ticket,0,StopLossLevel,0,0,clrBlue)){

            err = GetLastError();

            Print ("Encountered an error during OrderSend modification! "+(string)err+" "+ErrorDescription(err));

            }//!OrderModify Fails Above Line Will Be Placed  

         }//OrderSelect If Statement Ends. else need to be placed.

         else{//Fails To Select An Order

         Print ("Failed To Select An Order: ",ticket);

         err = GetLastError();

         Print ("Encountered an error during selecting an order! "+(string)err+" "+ErrorDescription(err));

         }

      }//(Ticket > 0) End

      else{//Fails To Open Order And Return Ticket

      err = GetLastError();

      Print ("Encountered an error during order placement! "+(string)err+" "+ErrorDescription(err));

      }//Fails To Open Order And Return Ticket - End

}
 
sunnysahdev: I am new to MQL4, I haven't had a problem coding lotsizes based on a fixed amount of pips but cannot seem to make it work where I am calculating the lots size base on entry and stoploss levels. Please advise... Code is below...
Please do a search! That question has been asked and answered hundreds of times. In fact, the user "@whroeder1" has a boilerplate post for this very question. Just click on the "Magnifying Glass" in the top right.
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

    • 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, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • 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
 

Theres 7 undeclared identifiers so what are their values?

Do i have to add them and assume a value ?
 
void OnTick()
{
  EnterTrade(OP_BUY);
  
  EnterTrade(OP_SELL);
}


void EnterTrade(int TypeOfTrade){

   int err=0;
   int RiskedPercent = 10;
   double Stoploss;
   int SlippageAllowed=3;
   int MagicNumber=1234;

   double Equity = AccountEquity();

   double RiskedAmount = Equity*(RiskedPercent*0.01);

   double LowOfBar1    = Low[1];// BuyOrder StopLoss Level

   double HighOfBar1    = High[1];// SellOrder StopLoss Level

  

   double StopLossLevel = HighOfBar1;

   double Price = Bid;//Sets Bid for OrderSend to use but below if statement allows that to change

   double StopLoss = (StopLossLevel - Bid)*Point;

   Print(" Stoploss ?: ",DoubleToString(Stoploss));

   if (TypeOfTrade == OP_BUY){

   Price = Ask;//Helps OrderSend Identify if it enters Ask or Bid

   StopLossLevel = LowOfBar1;

   StopLoss = (Ask - StopLossLevel)*Point;
  
   Print(" Stoploss OP_BUY: ",DoubleToString(Stoploss));
   }

   double Lots = (RiskedAmount/StopLoss);
   Print(" LOTS: ",DoubleToString(Lots));


   int ticket = OrderSend(Symbol(),TypeOfTrade,Lots,Price,SlippageAllowed,0,0,"Test1V1",MagicNumber,0,clrGreen);

      if(ticket > 0){

         if(OrderSelect(ticket,SELECT_BY_TICKET)){

            if(!OrderModify(ticket,0,StopLossLevel,0,0,clrBlue)){

            err = GetLastError();

            //Print ("Encountered an error during OrderSend modification! "+(string)err+" "+ErrorDescription(err));

            }//!OrderModify Fails Above Line Will Be Placed  

         }//OrderSelect If Statement Ends. else need to be placed.

         else{//Fails To Select An Order

         Print ("Failed To Select An Order: ",ticket);

         err = GetLastError();

         //Print ("Encountered an error during selecting an order! "+(string)err+" "+ErrorDescription(err));

         }

      }//(Ticket > 0) End

      else{//Fails To Open Order And Return Ticket

      err = GetLastError();

      //Print ("Encountered an error during order placement! "+(string)err+" "+ErrorDescription(err));

      }//Fails To Open Order And Return Ticket - End

}
0       17:05:56.159    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:56.159    Calculate lotsize EURUSD,H1:  Stoploss OP_BUY: 0.00000000
0       17:05:56.159    Calculate lotsize EURUSD,H1:  LOTS: 37864120681818.52343750
0       17:05:56.159    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:56.159    Calculate lotsize EURUSD,H1:  LOTS: 66640852399997.46093750
0       17:05:56.473    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:56.473    Calculate lotsize EURUSD,H1:  Stoploss OP_BUY: 0.00000000
0       17:05:56.473    Calculate lotsize EURUSD,H1:  LOTS: 37864120681818.52343750
0       17:05:56.473    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:56.473    Calculate lotsize EURUSD,H1:  LOTS: 65763999078939.23437500
0       17:05:56.902    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:56.902    Calculate lotsize EURUSD,H1:  Stoploss OP_BUY: 0.00000000
0       17:05:56.902    Calculate lotsize EURUSD,H1:  LOTS: 38153157862597.67968750
0       17:05:56.902    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:56.902    Calculate lotsize EURUSD,H1:  LOTS: 65763995789465.55468750
0       17:05:57.674    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:57.674    Calculate lotsize EURUSD,H1:  Stoploss OP_BUY: 0.00000000
0       17:05:57.674    Calculate lotsize EURUSD,H1:  LOTS: 37864101742424.58593750
0       17:05:57.674    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:57.674    Calculate lotsize EURUSD,H1:  LOTS: 65763966184202.39062500
0       17:05:58.180    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:58.180    Calculate lotsize EURUSD,H1:  Stoploss OP_BUY: 0.00000000
0       17:05:58.180    Calculate lotsize EURUSD,H1:  LOTS: 37579408120305.51562500
0       17:05:58.180    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:58.180    Calculate lotsize EURUSD,H1:  LOTS: 65763964210518.17968750
0       17:05:58.528    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:58.528    Calculate lotsize EURUSD,H1:  Stoploss OP_BUY: 0.00000000
0       17:05:58.528    Calculate lotsize EURUSD,H1:  LOTS: 37298968507465.54687500
0       17:05:58.528    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:58.528    Calculate lotsize EURUSD,H1:  LOTS: 66640823733330.78906250
0       17:05:58.826    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:58.826    Calculate lotsize EURUSD,H1:  Stoploss OP_BUY: 0.00000000
0       17:05:58.826    Calculate lotsize EURUSD,H1:  LOTS: 37298981940301.36718750
0       17:05:58.826    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:58.826    Calculate lotsize EURUSD,H1:  LOTS: 68466624383550.42968750
0       17:05:59.143    Calculate lotsize EURUSD,H1:  Stoploss ?: 0.00000000
0       17:05:59.143    Calculate lotsize EURUSD,H1:  Stoploss OP_BUY: 0.00000000
0       17:05:59.143    Calculate lotsize EURUSD,H1:  LOTS: 37579425413538.59375000
You need a lot of capital for that.
 

However you calculate your lot size (and I'd politely suggest your method above is not the best...), you must finish by checking it against minlot, maxlot and lotstep.

double LotCalculator(double lots)
  {
   double minlot  = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN),
          maxlot  = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX),
          lotstep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
   lots = MathFloor(lots/lotstep)*lotstep; // or MathRound
   lots = MathMax(lots,minlot);
   lots = MathMin(lots,maxlot);
   return(lots);
  }
Reason: