Calculating Pip Size and and Questions about Tick/Lot Size Values - page 2

 
Dominik Egert:

I do have a similar functionset in my library. SInce your code shows to be valid only for certain types of calculation modes, I was wondering if my code takes this into respect or if I need to adjust it maybe to have it generalized.

May I ask to take a look at this code and share improvements to it, please.

    // Calculate profit
    return(fabs(open_price - close_price) * c_digit_factor * c_tick_value);

This is not valid either. The profit is not related to the number of digits used.

 
Thank you all for adding such valuable input.
 

I wrote my own function because I trade few different types include forex, crypto and stock.

Base on your broker there may be different in DIGITS so you may need to add some as exception as my example.

That may be replacable with SYMBOL_CATEGORY from SymbolInfoString function but I was too lazy and not willing to dig into anymore since it was not necessary for me. Do it on your own :)


P/S: I mean 1 pip equal to $10 per lot in profit. This should be the best meaning definition for PIP.
double pricePerPip(string sym="")
  {
   if(StringFind(sym,"XAUUSD",0)!=-1) // exception for 1 pip = 0.1, like aug, xau...
      return 0.1;
   if(StringFind(sym,"BTCUSD",0)!=-1 || StringFind(sym,"ETHUSD",0)!=-1) // exception for 1pip = 10, like crypto and stock
      return 10;
///// common for all forex pairs, some broker may trim to 4 or 2 digits, so convert them back to 5 and 3 standard
   int deciCount = SymbolInfoInteger(sym,SYMBOL_DIGITS);
   if(deciCount==4)
      deciCount=5;
   if(deciCount==2)
      deciCount=3;
// now 1 pip is 10 points with forex 5 or 3 digits standard
   return MathPow(10,1-deciCount);
  }
  
// unsigned pip range calculator, you could remove absolute function if needed
double calPips(string sym="", double open_price=0,double close_price=0)
  {
   if(sym=="") sym=Symbol();
   return MathAbs(open_price-close_price)/pricePerPip(sym);
  }