Calculating StopLoss?

 

When I make deal, then I know (risk) sum of money what I can loose (in program (OrderCalcProfit()) that is Profit with minus of course). But when I want to put that sum in EA I need StopLoss Price. I thought I can calculate it with cycling:

double CalculateStopLoss(double Price_Open, double Side, ENUM_ORDER_TYPE Order_Type) {
   double Price_Close=0.0,Profit=0.0,SL=0.0; uint i;Increment=10e-6;
   if(Side=="Up") Increment=-Increment;
   FlagPrint(0,"   Open Price: " + DoubleToString(Price_Open,7));
   for(i=0;i<100000;i++) {
     Price_Close=Price_Open+i*Increment;
     OrderCalcProfit(Order_Type,SymbolName,InitMaxLoss,Price_Open,Price_Close,Profit);
     if(MathAbs(Profit)>InitMaxLoss) {SL=Price_Close;break;};}
   if(i=100000)  FlagPrint(0,"   Warning: StopLoss index reached to extreme limit, Profit:"          + DoubleToString(Profit) );
   else              FlagPrint(0,"   Calculate StopLoss Result: " + DoubleToString(SL,7) + ", Profit: " + DoubleToString(Profit) );

   return(SL);}

There InitMaxLoss is sum what I can loose, (Deal)Side is "Up" or "Down" accordingly with "BUY" or "SELL". That function almost always reaches to maximum index 100000 and overall was yet unable to give reliable result. (So I need turned around formula of OrderCalcProfit().)

Maybe in some good day I can get good result with that, but can anybody give me better way to calculate StopLoss (Depending of the loss (or risk) sum)?

 
You should also adding lot volume in your calculation. And then calculate it with tick value from symbolinfo() function
 

Really Working SL Calculation Sub-program:

double CalculateStopLoss(double Price_Open, ENUM_ORDER_TYPE Order_Type) {
   double Digits=-SymbolInfoInteger(SymbolName,SYMBOL_DIGITS),
              Lots=InitVolume*100000,SL=0,Pips=MathPow(10,Digits);
   LastDeal.PipSize=Lots*Price_Open*(Pips/Price_Open);
   if(Order_Type==ORDER_TYPE_BUY) SL=Price_Open-Pips*InitMaxLoss/LastDeal.PipSize;
   else                           SL=Price_Open+Pips*InitMaxLoss/LastDeal.PipSize;
   FlagPrint(5," Pips: " + DoubleToString(Pips) +
               " Price_Open: "         + DoubleToString(Price_Open) +
               " Lots: "               + DoubleToString(Lots,0) +
               " PipSize: "            + DoubleToString(LastDeal.PipSize,2) +
               " SL: "                 + DoubleToString(SL));
   return(SL);}

I hope that one who is interested understands here undeclared variables e.t.c.

 
SteelAce:

Really Working SL Calculation Sub-program:

double CalculateStopLoss(double Price_Open, ENUM_ORDER_TYPE Order_Type) {
   double Digits=-SymbolInfoInteger(SymbolName,SYMBOL_DIGITS),
              Lots=InitVolume*100000,SL=0,Pips=MathPow(10,Digits);
   LastDeal.PipSize=Lots*Price_Open*(Pips/Price_Open);
   if(Order_Type==ORDER_TYPE_BUY) SL=Price_Open-Pips*InitMaxLoss/LastDeal.PipSize;
   else                           SL=Price_Open+Pips*InitMaxLoss/LastDeal.PipSize;
   FlagPrint(5," Pips: " + DoubleToString(Pips) +
               " Price_Open: "         + DoubleToString(Price_Open) +
               " Lots: "               + DoubleToString(Lots,0) +
               " PipSize: "            + DoubleToString(LastDeal.PipSize,2) +
               " SL: "                 + DoubleToString(SL));
   return(SL);}

I hope that one who is interested understands here undeclared variables e.t.c.

Your code is simply unreadable. Can you explain why you are still coding on 80 columns in 2015 ?
 
These programs are Sub-programs, so there are really of course insufficient data. They are meaned more as formulas for calculating SL. Meaning of variables can one who is interested guess by variable names simply.