Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1124

 
macleta:
Thanks,ndd is what?

Didn't follow, didn't replace everything. It's a macro.

#define  ndd(A) NormalizeDouble(A, _Digits)
 
Alexey Viktorov:

Didn't keep track, didn't replace everything. It's a macro.

Thank you. (chuckles)
 
Can you please tell me how to know the last open position by its magic number, maybe there is a function that returns the lot value of the last open position? I need it on mql5
 
astrologer Аполонов:
Please advise how to find out the last open position by magic number, maybe there is a function that returns the lot value of the last open position? I need it on mql5

There are several ways, so make it clear: is the trading account type hedge or netting, in which case do you need to know about the position: does the advisor open a position and does he need to know the parameters of his last open position?

 
astrologer Аполонов:
Can you please tell me how to know the last open position by magic number, maybe there is a function that returns the lot value of the last open position? I need it on mql5

Example #1:

//+------------------------------------------------------------------+
//|                                             Trading engine 3.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.000"
/*
   barabashkakvn Trading engine 3.042
*/
#include <Trade\SymbolInfo.mqh>
//---
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input ulong    InpMagic             = 200;         // Magic number
//---
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

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

  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      long     deal_ticket       =0;
      long     deal_order        =0;
      long     deal_time         =0;
      long     deal_time_msc     =0;
      long     deal_type         =-1;
      long     deal_entry        =-1;
      long     deal_magic        =0;
      long     deal_reason       =-1;
      long     deal_position_id  =0;
      double   deal_volume       =0.0;
      double   deal_price        =0.0;
      double   deal_commission   =0.0;
      double   deal_swap         =0.0;
      double   deal_profit       =0.0;
      string   deal_symbol       ="";
      string   deal_comment      ="";
      string   deal_external_id  ="";
      if(HistoryDealSelect(trans.deal))
        {
         deal_ticket       =HistoryDealGetInteger(trans.deal,DEAL_TICKET);
         deal_order        =HistoryDealGetInteger(trans.deal,DEAL_ORDER);
         deal_time         =HistoryDealGetInteger(trans.deal,DEAL_TIME);
         deal_time_msc     =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC);
         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_magic        =HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason       =HistoryDealGetInteger(trans.deal,DEAL_REASON);
         deal_position_id  =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);

         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_price        =HistoryDealGetDouble(trans.deal,DEAL_PRICE);
         deal_commission   =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);
         deal_swap         =HistoryDealGetDouble(trans.deal,DEAL_SWAP);
         deal_profit       =HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_comment      =HistoryDealGetString(trans.deal,DEAL_COMMENT);
         deal_external_id  =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID);
        }
      else
         return;
      ENUM_DEAL_ENTRY enum_deal_entry=(ENUM_DEAL_ENTRY)deal_entry;
      if(deal_symbol==m_symbol.Name() && deal_magic==InpMagic)
        {
         if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)
           {
            if(deal_entry==DEAL_ENTRY_IN)
              {
               Print("Last DEAL IN volume: ",DoubleToString(deal_volume,2));
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+

In OnTradeTransaction we catch a transaction with the type

TRADE_TRANSACTION_DEAL_ADD

Add the transaction to the history. This is done as a result of order execution or account balance transactions.

   if(type==TRADE_TRANSACTION_DEAL_ADD)


make sure it is our symbol and our magic:

      if(deal_symbol==m_symbol.Name() && deal_magic==InpMagic)


Look for a BUY or SELL deal:

         if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)


make sure that this trade is

DEAL_ENTRY_IN.

Market entry

            if(deal_entry==DEAL_ENTRY_IN)
Files:
Test.mq5  10 kb
 
astrologer Аполонов:
Please advise how to find out the last position opened by magic number, maybe there is a function that returns the lot value of the last position opened? I need it on mql5

Option #2 - bypass the item list:

//+------------------------------------------------------------------+
//|                                             Trading engine 3.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.000"
/*
   barabashkakvn Trading engine 3.042
*/
#include <Trade\PositionInfo.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input ulong    InpMagic             = 200;         // Magic number
//---
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   ulong    last_time   = 0;
   double   last_volume = 0.0;

   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
           {
            if(m_position.TimeMsc()>last_time)
              {
               last_time=m_position.TimeMsc();
               last_volume=m_position.Volume();
              }
           }
   if(last_volume>0.0)
      Print("Last POSITIONS volume: ",DoubleToString(last_volume,2));
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {

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


Go through the list of positions

   for(int i=PositionsTotal()-1; i>=0; i--)


look for the longest position open time in milliseconds

TimeMsc

Getsthe time of opening a position in milliseconds since 01.01.1970

            if(m_position.TimeMsc()>last_time)
              {
               last_time=m_position.TimeMsc();
               last_volume=m_position.Volume();
              }


check - if the volume is higher than "0.0" - the position has been found:

   if(last_volume>0.0)
      Print("Last POSITIONS volume: ",DoubleToString(last_volume,2));
Files:
Test.mq5  6 kb
 
Alexey Viktorov:

For loss size, here is the function

Simply replace SL with TP

Apparently there is double risk as a percentage of available funds. How do I set a specific amount of profit in money, e.g. 1.5?
double RiskLots(double risk, int TP)
{
  double RiskMony, Lot;
  double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
  double margin = SymbolInfoDouble(_Symbol, SYMBOL_MARGIN_INITIAL);
  double FreeMargin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
    long accountLeverage = AccountInfoInteger(ACCOUNT_LEVERAGE);
    RiskMony = floor(FreeMargin*risk/100);
     Lot = NormalizeDouble(RiskMony*_Point/NormalizeDouble(TP*_Point*tickValue, _Digits), 2);
     //
  return(Lot);
}
 
Благодарю Vladimir Karputov:

Example #1:

In OnTradeTransaction we catch a transaction with the type

TRADE_TRANSACTION_DEAL_ADD

Add the transaction to the history. This is done as a result of order execution or account balance transactions.


make sure it is our symbol and our magic:


look at whether it is a BUY or SELL trade:


make sure that this trade is

DEAL_ENTRY_IN.

entering the market

 
macleta:
Apparently, this is double risk as a percentage of available funds. How do I set a specific amount of profit in money, e.g. 1.5?

Well, here's the line

    RiskMony = floor(FreeMargin*risk/100);
Just put a number instead of a formula and ...
 

How to convert, mouse position, during, but without throwing?

ChartTimeOnDropped
Reason: