Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1128

 

Hello, I have a strange behavior of the system function OrderCalcProfit, I have a feeling that the volume parameter is ignored in the calculation, i.e. the profit value is always calculated for the volume 1.0

I am trying to check it in the tester for different currency pairs on different demo accounts.

1. Calculation is correct for 1.0, see calc_profi101.png.

2. The second and any subsequent calls of the function return a profit result based on a volume of 1.0, the value of volume is ignored, see a screenshot (calc_profit02.png) - passed volume 0.5, but the result is still the same as for 1.0

Can you tell me what the problem might be. Thanks

Files:
 
aveshoff:

Hello, I have a strange behavior of the system function OrderCalcProfit, I have a feeling that the volume parameter is ignored in the calculation, i.e. the profit size is always calculated for the volume 1.0

I am trying to check it in the tester for different currency pairs on different demo accounts.

1. Calculation is correct for 1.0, see calc_profi101.png.

2. The second and any subsequent calls of the function return a profit result based on a volume of 1.0, the value of volume is ignored, see a screenshot (calc_profit02.png) - passed volume 0.5, but the result is still the same as for 1.0

Can you tell me what the problem might be. Thanks

I think the answer is in the help:

"... profit

[out] Variable that will hold the calculated profit value if the function succeeds. The value of the profit estimate depends on many factors and can change as the market environment changes. ... "


Added: although this is only a guess.

 
aveshoff:

Hello, I have a strange behavior of the system function OrderCalcProfit, I have a feeling that the volume parameter is ignored in the calculation, i.e. the profit is always calculated for the volume 1.0

I am trying to check it in the tester for different currency pairs on different demo accounts.

1. Calculation is correct for 1.0, see calc_profi101.png.

2. The second and any subsequent calls of the function return a profit result based on a volume of 1.0, the value of volume is ignored, see a screenshot (calc_profit02.png) - passed volume 0.5, but the result is still the same as for 1.0

Can you tell me what the problem might be. Thanks

Here's the test examiner - everything works correctly:

//+------------------------------------------------------------------+
//|                                             OrderProfitCheck.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
#include <Trade\AccountInfo.mqh>
CAccountInfo      m_account;                    // object of CAccountInfo class
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   string symbol=Symbol();
   ENUM_ORDER_TYPE trade_operation=ORDER_TYPE_BUY;
   double volume=1.0;
   double price_open=1.09350;
   double price_close=1.0930;
   double profit=m_account.OrderProfitCheck(symbol,trade_operation,volume,price_open,price_close);
   Print("Volume 1.0, profit -> ",DoubleToString(profit,2));
   volume=0.5;
   profit=m_account.OrderProfitCheck(symbol,trade_operation,volume,price_open,price_close);
   Print("Volume 0.5, profit -> ",DoubleToString(profit,2));
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+

and the result:

2019.09.30 07:47:21.688 OrderProfitCheck (EURUSD,H1)    Volume 1.0, profit -> -50.00
2019.09.30 07:47:21.688 OrderProfitCheck (EURUSD,H1)    Volume 0.5, profit -> -25.00
Files:
 
Can you tell me. Is it possible and how can the language of the terminal in use be known programmatically?
 
Uladzimir Izerski:
Can you give me a hint. Is it possible and how can I programmatically find out the language of the terminal in use?

I determine this either the Russian terminal language or if another - the output is in English.

   if(InpTrailingStop!=0 && InpTrailingStep==0)
     {
      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?
                      "Трейлинг невозможен: параметр \"Trailing Step\" равен нулю!":
                      "Trailing is not possible: parameter \"Trailing Step\" is zero!";
      //--- when testing, we will only output to the log about incorrect input parameters
      if(MQLInfoInteger(MQL_TESTER))
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);
         return(INIT_FAILED);
        }
      else // if the Expert Advisor is run on the chart, tell the user about the error
        {
         Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);
         return(INIT_PARAMETERS_INCORRECT);
        }
     }


Added: thought I saved all the languages from the TERMINAL_LANGUAGE enumeration - but can't find it. Must have lost ...

 
Good afternoon everyone, can someone help add here that at SL it would not just flip, but also add a lot or more. And when TP is triggered it would go back by one lot.
input double   Lot=1;
input int      TakeProfit = 6;
input int      Stoploss   = 6;

int TP;
int SL;

CTrade trader;
bool Invertor;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   TP = TakeProfit;
   SL = Stoploss;

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   double points;

   if(!PositionSelect(_Symbol))
     {
      if(Invertor)
         trader.Buy(Lot);
      else trader.Sell(Lot);
     }
   else
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         points=(SymbolInfoDouble(_Symbol,SYMBOL_BID)-PositionGetDouble(POSITION_PRICE_OPEN))/_Point;
         if(points>=TP)
           {
            trader.PositionClose(_Symbol);
            Invertor=true;
           }

         if(points<=-SL)
           {
            trader.PositionClose(_Symbol);
            Invertor=false;
           }
        }
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
        {
         points=(PositionGetDouble(POSITION_PRICE_OPEN)-SymbolInfoDouble(_Symbol,SYMBOL_ASK))/_Point;
         if(points>=TP)
           {
            trader.PositionClose(_Symbol);
            Invertor=false;
           }
         if(points<=-SL)
           {
            trader.PositionClose(_Symbol);
            Invertor=true;
           }
        }
     }   
  }
 
Andrey990:
Good day everyone, can someone help to add here that at SL it would not just be a rollover but also add a lot or more. And when TP is triggered it would return one lot.

1. I categorically advise against using functions which select a position by a character. Use a function that selects a position by an index in a list.

2. To know how a position was closed, theENUM_DEAL_REASON enumeration must be viewed in OnTradeTransaction.

Example with ENUM_DEAL_REASON:

Stop Loss Take Profit

If closing by Stop loss - double the volume, if by Take Profit - set the minimum volume. To detect that the trade occurred as a result of Stop Loss or Take Profit triggering, we use OnTradeTransaction. From build 1625 there is a nice enumeration ENUM_DEAL_REASON: ENUM_DEAL_REASON Description DEAL_REASON_SL Operation made as a result of Stop Loss triggering DEAL_REASON_TP Operation made as a result of Take Profit triggering... that can be tracked in OnTradeTransaction. In other words, now you can easily and most importantly, reliably determine that this trade was the result of Take Profit or Stop Loss triggering. At the moment (build 1626), this EA can be tested only live - either on a chart or in the debug mode using real data (F5 in MetaEditor). I also applied a workaround for now: Determine what triggered: Take Profit or Stop Loss in the OnTradeTransaction procedure: if (deal_symbol==m_symbol...

CodeBase | 2017.07.13 07:42 |Vladimir Karputov| EAs | MetaTrader 5

 

Is it possible to enter account login details into a third-party programme and manage the account and, you know, open/close positions?

Or is it only through a robot that is installed on the account?

 

I'm transferring indicators from MQL4 to MQL5, question about buffers and "plots". The indicator has 2 buffers and 1 plot:

#property indicator_buffers 2
#property indicator_plots 1

SetIndexBuffer(0,Array1,INDICATOR_CALCULATIONS);
SetIndexBuffer(1,Array2,INDICATOR_DATA);


If the first buffer is used for calculations, etc., and the second one should be plotted, then

first, is it necessary to set DRAW_NONE for the first buffer if it is already set to INDICATOR_CALCULATIONS in the SetIndexBuffer() function ?

PlotIndexSetInteger(?,PLOT_DRAW_TYPE,DRAW_NONE);

second, what index should I set in PlotIndexSetInteger() instead of question mark (?) if only second buffer should be drawn?

PlotIndexSetInteger(?,PLOT_DRAW_TYPE,DRAW_HISTOGRAM);


Third, if I use two styles of DRAW_HISTOGRAM type, I may set colour for each of them and get histogram like in the Volumes indicator, where green and red bars alternate, but if I use DRAW_HISTOGRAM2, may I set two colours for one bar too or only one colour is set for this style?

 
The_Sheikh:

I'm transferring indicators from MQL4 to MQL5, question about buffers and "plots". The indicator has 2 buffers and 1 plot:

#property indicator_buffers 2
#property indicator_plots 1

SetIndexBuffer(0,Array1,INDICATOR_CALCULATIONS);
SetIndexBuffer(1,Array2,INDICATOR_DATA);


If the first buffer is used for calculations, etc., and the second one should be plotted, then

first, is it necessary to set DRAW_NONE for the first buffer if it is already set to INDICATOR_CALCULATIONS in the SetIndexBuffer() function ?

PlotIndexSetInteger(?,PLOT_DRAW_TYPE,DRAW_NONE);

second, what index should I set in PlotIndexSetInteger() instead of question mark (?) if only second buffer should be drawn?

PlotIndexSetInteger(?,PLOT_DRAW_TYPE,DRAW_HISTOGRAM);


Third, if I use two styles of DRAW_HISTOGRAM type, I may set colour for each of them and get histogram like in the Volumes indicator, where green and red bars alternate, but if I use DRAW_HISTOGRAM 2, may I set two colours for one bar too or only one colour is set for this style?

Why are you being so hard on yourself? What's the problem with setting the buffers you want to display first, and then the auxiliary buffers?

For DRAW_HISTOGRAМ2 3 buffers are defined and the sequence is mandatory, 2 value buffers first, followed by a colour buffer. But there is a difference between DRAW_HISTOGRAM and DRAW_HISTOGRAM2 in that DRAW_HISTOGRAM is drawn from zero to the value in the buffer, while DRAW_HISTOGRAM2 is drawn from the value of one buffer to the value of another buffer.

Reason: