/*
   Generated by EX4 TO MQ4 decompile Service 
   Website: http://www.ex4Tomq4.net 
   E-mail : info@ex4Tomq4.net 
*/
#property copyright "Copyright © 2011, AZM."
#property link      "http://strategy4you.ru"

#property indicator_chart_window

extern int Loss = 50;
extern double Percent = 2.0;
extern double Percent_2 = 5.0;
extern double Percent_3 = 10.0;
extern color Color = Gray;
extern int размер_шрифта = 10;
string gs_calc_112 = "Calc";
string gs_calc1_120 = "Calc1";
string gs_calc2_128 = "Calc2";
int gi_136;
double g_digits_140;
double gd_148;
double gd_156;
double gd_164;
string g_text_172;
double g_tickvalue_180;

int init() {
   g_digits_140 = MarketInfo(Symbol(), MODE_DIGITS);
   if (g_digits_140 == 5.0 || g_digits_140 == 3.0) gi_136 = 10;
   if (g_digits_140 == 4.0 || g_digits_140 == 2.0) gi_136 = 1;
   Print("http://strategy4you.ru");
   return (0);
}

int deinit() {
   ObjectDelete(gs_calc_112);
   ObjectDelete(gs_calc1_120);
   ObjectDelete(gs_calc2_128);
   ObjectDelete("logo");
   ObjectDelete("Loss");
   return (0);
}

int start() {
   string ls_4;
   string ls_12;
   string ls_20;
   int y_0 = 15;
   int li_28 = размер_шрифта + 10;
   double tickvalue_32 = MarketInfo(Symbol(), MODE_TICKVALUE);
   if (tickvalue_32 != g_tickvalue_180) {
      g_tickvalue_180 = tickvalue_32;
  //    if (ObjectFind("logo") == -1) {
  //       ObjectCreate("logo", OBJ_LABEL, 0, 0, 0);
  //       ObjectSet("logo", OBJPROP_CORNER, 0);
  //       ObjectSet("logo", OBJPROP_XDISTANCE, 15);
  //       ObjectSet("logo", OBJPROP_YDISTANCE, y_0);
  //    }
  //  ObjectSetText("logo", "http://strategy4you.ru/", размер_шрифта, "Arial", MediumBlue);
      y_0 += li_28;
      if (Percent != 0.0) {
         gd_148 = f0_0(gi_136 * Loss, Percent);
         gd_156 = gi_136 * gd_148 * tickvalue_32;
         gd_164 = gd_156 * Loss;
         if (Percent < 10.0) ls_4 = "% 0" + DoubleToStr(Percent, 2);
         else ls_4 = "% " + DoubleToStr(Percent, 2);
         if (gd_148 < 10.0) ls_12 = "  Lot 0" + DoubleToStr(gd_148, 2);
         else ls_12 = "  Lot " + DoubleToStr(gd_148, 2);
         if (gd_156 < 10.0) ls_20 = "  стоимость пункта 0" + DoubleToStr(gd_156, 2);
         else ls_20 = "  стоимость пункта " + DoubleToStr(gd_156, 2);
         g_text_172 = ls_4 + ls_12 + ls_20 + "  SL " + DoubleToStr(gd_164, 2);
         if (ObjectFind(gs_calc_112) == -1) {
            ObjectCreate(gs_calc_112, OBJ_LABEL, 0, 0, 0);
            ObjectSet(gs_calc_112, OBJPROP_CORNER, 0);
            ObjectSet(gs_calc_112, OBJPROP_XDISTANCE, 15);
            ObjectSet(gs_calc_112, OBJPROP_YDISTANCE, y_0);
         }
         ObjectSetText(gs_calc_112, g_text_172, размер_шрифта, "MS Sans Serif", Color);
         y_0 += li_28;
      }
      if (Percent_2 != 0.0) {
         gd_148 = f0_0(gi_136 * Loss, Percent_2);
         gd_156 = gi_136 * gd_148 * tickvalue_32;
         gd_164 = gd_156 * Loss;
         if (Percent_2 < 10.0) ls_4 = "% 0" + DoubleToStr(Percent_2, 2);
         else ls_4 = "% " + DoubleToStr(Percent_2, 2);
         if (gd_148 < 10.0) ls_12 = "  Lot 0" + DoubleToStr(gd_148, 2);
         else ls_12 = "  Lot " + DoubleToStr(gd_148, 2);
         if (gd_156 < 10.0) ls_20 = "  стоимость пункта 0" + DoubleToStr(gd_156, 2);
         else ls_20 = "  стоимость пункта " + DoubleToStr(gd_156, 2);
         g_text_172 = ls_4 + ls_12 + ls_20 + "  SL " + DoubleToStr(gd_164, 2);
         if (ObjectFind(gs_calc1_120) == -1) {
            ObjectCreate(gs_calc1_120, OBJ_LABEL, 0, 0, 0);
            ObjectSet(gs_calc1_120, OBJPROP_CORNER, 0);
            ObjectSet(gs_calc1_120, OBJPROP_XDISTANCE, 15);
            ObjectSet(gs_calc1_120, OBJPROP_YDISTANCE, y_0);
         }
         ObjectSetText(gs_calc1_120, g_text_172, размер_шрифта, "MS Sans Serif", Color);
         y_0 += li_28;
      }
      if (Percent_3 != 0.0) {
         gd_148 = f0_0(gi_136 * Loss, Percent_3);
         gd_156 = gi_136 * gd_148 * tickvalue_32;
         gd_164 = gd_156 * Loss;
         if (Percent_3 < 10.0) ls_4 = "% 0" + DoubleToStr(Percent_3, 2);
         else ls_4 = "% " + DoubleToStr(Percent_3, 2);
         if (gd_148 < 10.0) ls_12 = "  Lot 0" + DoubleToStr(gd_148, 2);
         else ls_12 = "  Lot " + DoubleToStr(gd_148, 2);
         if (gd_156 < 10.0) ls_20 = "  стоимость пункта 0" + DoubleToStr(gd_156, 2);
         else ls_20 = "  стоимость пункта " + DoubleToStr(gd_156, 2);
         g_text_172 = ls_4 + ls_12 + ls_20 + "  SL " + DoubleToStr(gd_164, 2);
         if (ObjectFind(gs_calc2_128) == -1) {
            ObjectCreate(gs_calc2_128, OBJ_LABEL, 0, 0, 0);
            ObjectSet(gs_calc2_128, OBJPROP_CORNER, 0);
            ObjectSet(gs_calc2_128, OBJPROP_XDISTANCE, 15);
            ObjectSet(gs_calc2_128, OBJPROP_YDISTANCE, y_0);
         }
         ObjectSetText(gs_calc2_128, g_text_172, размер_шрифта, "MS Sans Serif", Color);
         y_0 += li_28;
      }
      if (ObjectFind("loss") == -1) {
         ObjectCreate("loss", OBJ_LABEL, 0, 0, 0);
         ObjectSet("loss", OBJPROP_CORNER, 0);
         ObjectSet("loss", OBJPROP_XDISTANCE, 15);
         ObjectSet("loss", OBJPROP_YDISTANCE, y_0);
      }
      ObjectSetText("loss", "стоп лосс = " + Loss + " пунктов", размер_шрифта, "MS Sans Serif", Color);
      y_0 = 15;
   }
   return (0);
}

double f0_0(int ai_0, double ad_4) {
   double ld_12 = AccountBalance();
   double tickvalue_20 = MarketInfo(Symbol(), MODE_TICKVALUE);
   double ld_28 = MarketInfo(Symbol(), MODE_MINLOT);
   double ld_36 = MarketInfo(Symbol(), MODE_MAXLOT);
   double lotstep_44 = MarketInfo(Symbol(), MODE_LOTSTEP);
   double ld_ret_52 = MathFloor(ld_12 * ad_4 / 100.0 / (ai_0 * tickvalue_20) / lotstep_44) * lotstep_44;
   if (ld_ret_52 < ld_28) ld_ret_52 = ld_28;
   if (ld_ret_52 > ld_36) ld_ret_52 = ld_36;
   return (ld_ret_52);
}