Guest User

ProfitGuide.mq4

a guest
Feb 18th, 2015
292
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //+------------------------------------------------------------------+
  2. //|                                                  ProfitGuide.mq4 |
  3. //|                                   Copyright 2015, Michal Mikoláš |
  4. //|                                            http://www.michalm.cz |
  5. //|                License: MIT (http://opensource.org/licenses/MIT) |
  6. //+------------------------------------------------------------------+
  7. #property copyright "Copyright 2015, Michal Mikoláš"
  8. #property link      "http://www.michalm.cz"
  9. #property version   "1.00"
  10. #property strict
  11. #property indicator_chart_window
  12.  
  13. // Inputs
  14. input double lots = 0.01;            // Value (lots to buy)
  15. input double profitInUsd = 10;       // Profit to reach in USD
  16. input color  lineColor = SteelBlue;  // Line Color
  17. input int    lineStyle = 2;          // Line Style
  18.  
  19. // Global variables
  20. int lotSize = 0;
  21. double pipValue = 0;
  22. double profitInUnit = 0;
  23. double pips = 0;
  24. string currency = "";
  25.  
  26.  
  27.  
  28. int OnCalculate(const int rates_total,
  29.                      const int prev_calculated,
  30.                      const datetime &time[],
  31.                      const double &open[],
  32.                      const double &high[],
  33.                      const double &low[],
  34.                      const double &close[],
  35.                      const long &tick_volume[],
  36.                      const long &volume[],
  37.                      const int &spread[]
  38. ) {
  39.     // Refresh calculated values
  40.     recalculate();
  41.  
  42.     // Show comment
  43.     showComment();
  44.  
  45.     // Show lines
  46.     drawLines();
  47.    
  48.     return -1;
  49. }
  50.  
  51.  
  52.  
  53. void recalculate()
  54. {
  55.     lotSize = MarketInfo(Symbol(), MODE_LOTSIZE);
  56.     pipValue = NormalizeDouble(
  57.         lotSize * MarketInfo(Symbol(), MODE_TICKSIZE) * lots,
  58.         6
  59.     );
  60.    
  61.     currency = StringSubstr(Symbol(), 3, 3);
  62.     profitInUnit = currencyTransfer("USD", currency, profitInUsd);
  63.    
  64.     pips = profitInUnit / pipValue;
  65. }
  66.  
  67.  
  68.  
  69. void showComment()
  70. {
  71.     string comment = "";
  72.    
  73.     // Settings
  74.     comment += "Value: " + (string)lots + "\n";
  75.     comment += "Profit: " + (string)profitInUsd + " USD" +
  76.         ((profitInUnit != profitInUsd)
  77.             ? " -> " + DoubleToStr(profitInUnit, 2) + " " + currency
  78.             : ""
  79.         ) +
  80.         "\n"
  81.     ;
  82.     comment += "------------------------------------------\n";
  83.    
  84.     // Currency info
  85.     comment += "Lot size: " + formatNumber((string)lotSize) + "\n";
  86.     comment += "------------------------------------------\n";
  87.    
  88.     // Computed values
  89.     comment += "Pip value for " + (string)lots + " lots: \n" +
  90.         "    " + (string)pipValue + " " + currency +
  91.         ((profitInUnit != profitInUsd)
  92.             ? " -> " + DoubleToStr(currencyTransfer(currency, "USD", pipValue), 2) + " USD"
  93.             : ""
  94.         ) +
  95.         "\n"
  96.     ;
  97.     comment += "Pips for reach " + (string)profitInUsd + " USD: " + formatNumber((string)MathRound(pips)) + "\n";
  98.    
  99.     Comment(comment);
  100. }
  101.  
  102.  
  103.  
  104. void drawLines()
  105. {
  106.     double price;
  107.     string gridname;
  108.  
  109.     gridname = "ProfitGuide_Line_Top";
  110.     price = Ask + (pips * Point);
  111.     ObjectCreate(gridname, OBJ_HLINE, 0, 0, price);
  112.     ObjectSet(gridname, OBJPROP_COLOR, lineColor);
  113.     ObjectSet(gridname, OBJPROP_STYLE, lineStyle);
  114.     ObjectSet(gridname, OBJPROP_PRICE1, price);
  115.     ObjectSet(gridname, OBJPROP_BACK, true);
  116.    
  117.  
  118.     gridname = "ProfitGuide_Line_Bottom";
  119.     price = Ask - (pips * Point);
  120.     ObjectCreate(gridname, OBJ_HLINE, 0, 0, price);
  121.     ObjectSet(gridname, OBJPROP_COLOR, lineColor);
  122.     ObjectSet(gridname, OBJPROP_STYLE, lineStyle);
  123.     ObjectSet(gridname, OBJPROP_PRICE1, price);
  124.     ObjectSet(gridname, OBJPROP_BACK, true);
  125. }
  126.  
  127.  
  128.  
  129. double currencyTransfer(string currencyFrom, string currencyTo, double amount = 1.00)
  130. {
  131.     double coef;   
  132.     if (currencyFrom == currencyTo) {
  133.         coef = 1;
  134.     } else {
  135.         coef = MarketInfo(currencyFrom + currencyTo, MODE_ASK);
  136.        
  137.         if (coef == 0) {  // pair not found, try find reverse pair
  138.             coef = 1 / MarketInfo(currencyTo + currencyFrom, MODE_ASK);
  139.         }
  140.     }
  141.    
  142.     return (amount * coef);
  143. }
  144.  
  145.  
  146.  
  147. string formatNumber(string numb, string delim=" ",string dec=".")
  148. {
  149.     int pos = StringFind(numb, dec);
  150.    
  151.     string nnumb, enumb;
  152.     if (pos == -1) {
  153.         nnumb = numb;
  154.         enumb = "";
  155.     } else {
  156.         nnumb = StringSubstr(numb, 0, pos);
  157.         enumb = StringSubstr(numb, pos);
  158.     }
  159.    
  160.     int cnt = StringLen(nnumb);
  161.     if (cnt < 4) return(numb);
  162.    
  163.     int x = MathFloor(cnt / 3);
  164.     int y = cnt - x * 3;
  165.    
  166.     string forma = "";
  167.     if (y != 0) forma = StringConcatenate(StringSubstr(nnumb, 0, y), delim);
  168.     for (int i=0;i<x;i++) {
  169.         if (i != x - 1)
  170.             forma = StringConcatenate(forma, StringSubstr(nnumb, y + i * 3, 3), delim);
  171.         else
  172.             forma = StringConcatenate(forma, StringSubstr(nnumb, y + i * 3, 3));
  173.     }
  174.     forma = StringConcatenate(forma, enumb);
  175.    
  176.     return forma;
  177. }  
  178.  
  179.  
  180.  
  181. int deinit()
  182. {
  183.     Comment("");
  184.    
  185.     ObjectDelete("ProfitGuide_Line_Top");
  186.     ObjectDelete("ProfitGuide_Line_Bottom");
  187.    
  188.     return 0;
  189. }
RAW Paste Data