Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //+------------------------------------------------------------------+
- //| ProfitGuide.mq4 |
- //| Copyright 2015, Michal Mikoláš |
- //| http://www.michalm.cz |
- //| License: MIT (http://opensource.org/licenses/MIT) |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2015, Michal Mikoláš"
- #property link "http://www.michalm.cz"
- #property version "1.00"
- #property strict
- #property indicator_chart_window
- // Inputs
- input double lots = 0.01; // Value (lots to buy)
- input double profitInUsd = 10; // Profit to reach in USD
- input color lineColor = SteelBlue; // Line Color
- input int lineStyle = 2; // Line Style
- // Global variables
- int lotSize = 0;
- double pipValue = 0;
- double profitInUnit = 0;
- double pips = 0;
- string currency = "";
- int OnCalculate(const int rates_total,
- const int prev_calculated,
- const datetime &time[],
- const double &open[],
- const double &high[],
- const double &low[],
- const double &close[],
- const long &tick_volume[],
- const long &volume[],
- const int &spread[]
- ) {
- // Refresh calculated values
- recalculate();
- // Show comment
- showComment();
- // Show lines
- drawLines();
- return -1;
- }
- void recalculate()
- {
- lotSize = MarketInfo(Symbol(), MODE_LOTSIZE);
- pipValue = NormalizeDouble(
- lotSize * MarketInfo(Symbol(), MODE_TICKSIZE) * lots,
- 6
- );
- currency = StringSubstr(Symbol(), 3, 3);
- profitInUnit = currencyTransfer("USD", currency, profitInUsd);
- pips = profitInUnit / pipValue;
- }
- void showComment()
- {
- string comment = "";
- // Settings
- comment += "Value: " + (string)lots + "\n";
- comment += "Profit: " + (string)profitInUsd + " USD" +
- ((profitInUnit != profitInUsd)
- ? " -> " + DoubleToStr(profitInUnit, 2) + " " + currency
- : ""
- ) +
- "\n"
- ;
- comment += "------------------------------------------\n";
- // Currency info
- comment += "Lot size: " + formatNumber((string)lotSize) + "\n";
- comment += "------------------------------------------\n";
- // Computed values
- comment += "Pip value for " + (string)lots + " lots: \n" +
- " " + (string)pipValue + " " + currency +
- ((profitInUnit != profitInUsd)
- ? " -> " + DoubleToStr(currencyTransfer(currency, "USD", pipValue), 2) + " USD"
- : ""
- ) +
- "\n"
- ;
- comment += "Pips for reach " + (string)profitInUsd + " USD: " + formatNumber((string)MathRound(pips)) + "\n";
- Comment(comment);
- }
- void drawLines()
- {
- double price;
- string gridname;
- gridname = "ProfitGuide_Line_Top";
- price = Ask + (pips * Point);
- ObjectCreate(gridname, OBJ_HLINE, 0, 0, price);
- ObjectSet(gridname, OBJPROP_COLOR, lineColor);
- ObjectSet(gridname, OBJPROP_STYLE, lineStyle);
- ObjectSet(gridname, OBJPROP_PRICE1, price);
- ObjectSet(gridname, OBJPROP_BACK, true);
- gridname = "ProfitGuide_Line_Bottom";
- price = Ask - (pips * Point);
- ObjectCreate(gridname, OBJ_HLINE, 0, 0, price);
- ObjectSet(gridname, OBJPROP_COLOR, lineColor);
- ObjectSet(gridname, OBJPROP_STYLE, lineStyle);
- ObjectSet(gridname, OBJPROP_PRICE1, price);
- ObjectSet(gridname, OBJPROP_BACK, true);
- }
- double currencyTransfer(string currencyFrom, string currencyTo, double amount = 1.00)
- {
- double coef;
- if (currencyFrom == currencyTo) {
- coef = 1;
- } else {
- coef = MarketInfo(currencyFrom + currencyTo, MODE_ASK);
- if (coef == 0) { // pair not found, try find reverse pair
- coef = 1 / MarketInfo(currencyTo + currencyFrom, MODE_ASK);
- }
- }
- return (amount * coef);
- }
- string formatNumber(string numb, string delim=" ",string dec=".")
- {
- int pos = StringFind(numb, dec);
- string nnumb, enumb;
- if (pos == -1) {
- nnumb = numb;
- enumb = "";
- } else {
- nnumb = StringSubstr(numb, 0, pos);
- enumb = StringSubstr(numb, pos);
- }
- int cnt = StringLen(nnumb);
- if (cnt < 4) return(numb);
- int x = MathFloor(cnt / 3);
- int y = cnt - x * 3;
- string forma = "";
- if (y != 0) forma = StringConcatenate(StringSubstr(nnumb, 0, y), delim);
- for (int i=0;i<x;i++) {
- if (i != x - 1)
- forma = StringConcatenate(forma, StringSubstr(nnumb, y + i * 3, 3), delim);
- else
- forma = StringConcatenate(forma, StringSubstr(nnumb, y + i * 3, 3));
- }
- forma = StringConcatenate(forma, enumb);
- return forma;
- }
- int deinit()
- {
- Comment("");
- ObjectDelete("ProfitGuide_Line_Top");
- ObjectDelete("ProfitGuide_Line_Bottom");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment