Position sizing calculation, TICK_VALUE = 0

 

Hi,

The below Script I'm using to calculate the position size, it works fine across all symbols but I got some problems with some CFDs, "The Nikkei JPN225" for example,  TICK_VALUE = 0 which causes the position size to be Max Volume of that symbol or never breaking out of that While loop.

So my question is, is there a work around that, when TICK_VALUE = 0, for calculating the appropriate position size ? 

double OptimalLotSize(string CurrentSymbol, double EntryPrice, double StopLoss)
{
   //Set symbol string and calculate point value
   double TickSize      = SymbolInfoDouble(CurrentSymbol,SYMBOL_TRADE_TICK_SIZE);
   double TickValue     = SymbolInfoDouble(CurrentSymbol,SYMBOL_TRADE_TICK_VALUE);
   double PointAmount   = SymbolInfoDouble(CurrentSymbol,SYMBOL_POINT);
   double TicksPerPoint = TickSize/PointAmount;
   double PointValue    = TickValue/TicksPerPoint;

   //Calculate risk based off entry and stop loss level by pips
   double RiskPoints = MathAbs((EntryPrice - StopLoss)/TickSize);
      
   //Set risk model - Fixed or compounding
   if(RiskCompounding)
      CurrentEquityRisk = AccountInfoDouble(ACCOUNT_EQUITY);
   else
      CurrentEquityRisk = StartingEquity; 

   //Calculate total risk amount in dollars
   double RiskAmount = CurrentEquityRisk * RiskPercent/2;
   double MinLots    = SymbolInfoDouble(CurrentSymbol, SYMBOL_VOLUME_MIN);
   double MaxLots    = SymbolInfoDouble(CurrentSymbol, SYMBOL_VOLUME_MAX);
   double TempLots   = MinLots;
   
   //Calculate lot size
   double RiskLots   = 0.01*(MathFloor(100*NormalizeDouble(RiskAmount/(RiskPoints*PointValue),4))); 
   
   while(TempLots+MinLots <= RiskLots)
   {
      TempLots = TempLots + MinLots;
   }
   
   RiskLots = TempLots;
   
   RiskLots = MathMax(RiskLots, MinLots);
   RiskLots = MathMin(RiskLots, MaxLots);
   
   //Print values in Journal to check if operating correctly
   PrintFormat("TickSize=%f,TickValue=%f,PointAmount=%f,TicksPerPoint=%f,PointValue=%f,",
                  TickSize,TickValue,PointAmount,TicksPerPoint,PointValue);   
   PrintFormat("EntryPrice=%f,StopLoss=%f,RiskPoints=%f,RiskAmount=%f,RiskLots=%f,",
                  EntryPrice,StopLoss,RiskPoints,RiskAmount,RiskLots);   

   //Return optimal lot size
   return RiskLots;
}


 
Djope Ben: So my question is, is there a work around that, when TICK_VALUE = 0, for calculating the appropriate position size ? 

You tell your broker to fix their server.

 
Djope Ben:

Hi,

The below Script I'm using to calculate the position size, it works fine across all symbols but I got some problems with some CFDs, "The Nikkei JPN225" for example,  TICK_VALUE = 0 which causes the position size to be Max Volume of that symbol or never breaking out of that While loop.

So my question is, is there a work around that, when TICK_VALUE = 0, for calculating the appropriate position size ? 


Yes, calculate it yourself. Search on the forum, it has already been discussed and coded.
 
Djope Ben:

Hi,

The below Script I'm using to calculate the position size, it works fine across all symbols but I got some problems with some CFDs, "The Nikkei JPN225" for example,  TICK_VALUE = 0 which causes the position size to be Max Volume of that symbol or never breaking out of that While loop.

So my question is, is there a work around that, when TICK_VALUE = 0, for calculating the appropriate position size ? 


https://www.mql5.com/en/code/28029

also,

https://www.mql5.com/en/forum/441959/page6#comment_45207526

Forex Calculators
Forex Calculators
  • www.mql5.com
Margin Calculator, Point Value Calculator, Position Size Calculator, Profit Calculator and Swap Calculator.