Pips to USD Conversion Formula?

 

Does anyone have a piece of code representing a Pips to USD Conversion Formula?


What I'm trying to do is let my EA determine lot size dynamically. My EA already knows where to set the Take Profit and Stop Loss levels. What I'd like to do is have it change the lot size so that the Stop Loss level it has chosen represents one percent of free margin.


Thanks for your help!

 
double calcPositionSize(double amountToRisk, int pipRange, string currentSymbol, string myCurrency)
{
   string baseCurrency = StringSubstr(currentSymbol,0,3);
   string quoteCurrency = StringSubstr(currentSymbol,3,3);
   double lotSize;
   
   double standardLotSize = MarketInfo(currentSymbol, MODE_LOTSIZE);
   double tickSize = MarketInfo(currentSymbol, MODE_POINT);
   double singlePipValue;
   
   if(quoteCurrency == myCurrency){
      singlePipValue = standardLotSize * tickSize;
   }
   
   if(baseCurrency == myCurrency){
      singlePipValue = standardLotSize * tickSize / MarketInfo(currentSymbol, MODE_ASK);
   }
   
   if(baseCurrency != myCurrency && quoteCurrency != myCurrency){
      singlePipValue = standardLotSize * tickSize * MarketInfo(baseCurrency + myCurrency, MODE_ASK) / MarketInfo(currentSymbol, MODE_ASK);
   }
   
   return(NormalizeDouble( (amountToRisk * standardLotSize) / (pipRange * singlePipValue) / standardLotSize , 2));
}
 

Thanks!


That works perfectly.

Reason: