Questions from Beginners MQL5 MT5 MetaTrader 5 - page 903

 
Vladimir Pavlov:

Stupidly made a script to copy inside the computer.... Doesn't copy!!!

So where is the error description? What operating system? What build of MetaTrader 5?

Do you have write rights to the folder?


Added: it looks like if there is no directory, it needs to be created first.


When copying to existing directory - no problem. MetaTrader 5 will work without errors.

 

How can I be sure that the position with the given id has closed?

The obvious solution - when PositionSelectByTicket returns false, but this can probably happen in a situation where the open positions have not had time to be loaded into the terminal when connected to the trading account?

A slightly more complicated solution - PositionSelectByTicket returns false and HistorySelectByPosition returns true. In this case, if the history for this position is loaded, then the list of open positions must be loaded.

Совершение сделок - Торговые операции - MetaTrader 5
Совершение сделок - Торговые операции - MetaTrader 5
  • www.metatrader5.com
Торговая деятельность в платформе связана с формированием и отсылкой рыночных и отложенных ордеров для исполнения брокером, а также с управлением текущими позициями путем их модификации или закрытия. Платформа позволяет удобно просматривать торговую историю на счете, настраивать оповещения о событиях на рынке и многое другое. Открытие позиций...
 
. ... Rick D. ... .:

Wait for the terminal to synchronise. For example, while(HistorySelect).

 
fxsaber:

Wait for the terminal to synchronise. For example, while (HistorySelect).

I'm not an expert in the inner workings of the terminal. But maybe it's gaoanted that when OnInit comes, the table of open positions (and possibly trading history) is already loaded?

 
. ... Rick D. ... .:

I am not an expert in the inner workings of the terminal.

Also far from an expert. It's just that several options come to mind to try out at once. And what will work will have to be experimented with.

 
Vladimir Karputov:

The easiest solution: open a chart of the desired financial instrument in MetaTrader and place the Expert Advisor on it.

This is known. But I wonder how to set the pair in the program? Is there a function for this?
 
cepreu1:
This is known. But I want to know how to set the pair in the application ? Is there a function for this ?

Easily.

For example, trade operations. SeeCTrade trade class,Buy method:

bool  Buy( 
   double        volume,          // объем позиции 
   const string  symbol=NULL,     // символ 
   double        price=0.0,       // цена исполнения 
   double        sl=0.0,          // цена Stop Loss 
   double        tp=0.0,          // цена Take Profit 
   const string  comment=""       // комментарий 
   )

I have highlighted the place where you can specify the required symbol.


Or for example receiving data about opening and closing prices... -CopyRates:

int  CopyRates( 
   string           symbol_name,       // имя символа 
   ENUM_TIMEFRAMES  timeframe,         // период 
   int              start_pos,         // откуда начнем  
   int              count,             // сколько копируем 
   MqlRates         rates_array[]      // массив, куда будут скопированы данные 
   );
 
Vladimir Karputov:

Easily.

For example, trade operations. SeeCTrade trade class,Buy method:

I have highlighted the place where you can specify the required symbol.


Or for example receiving data about opening, closing prices ... -CopyRates:

Yes, ok. But I haven't specified that I want to assign a pair at startup, but

without opening position. And Buy does it by opening a position,

andCopyRates giveshistorical data of the pair without assigning it.

 
cepreu1:

Yes, okay. But I didn't specify that I want to assign a couple when starting up, but

without opening a position. And Buy does this by opening a position,

andCopyRates giveshistorical data of pair without assigning it.

You are being florid, but I'll try to guess: you need an input parameter with the symbol name? Then here's the code - you specify the desired symbol in the"InpSymbol" parameter. This symbol is checked in OnInit() - if there is no such a symbol, then the Expert Advisor will be unloaded and an error message will appear in the "Experts" tab of the terminal (or in the "Journal" tab of the Strategy Tester).

//+------------------------------------------------------------------+
//|                                                   Set Symbol.mq5 |
//|                              Copyright © 2018, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2018, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.000"
//---
#include <Trade\SymbolInfo.mqh>  
CSymbolInfo    m_symbol;                     // symbol info object
//--- input parameters
input string   InpSymbol="ASDWER";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(!m_symbol.Name(InpSymbol)) // sets symbol name
      return(INIT_FAILED);
   RefreshRates();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(!RefreshRates())
      return;
   string text=m_symbol.Name()+"\n"+
               "Ask "+DoubleToString(m_symbol.Ask(),m_symbol.Digits())+"\n"+
               "Bid "+DoubleToString(m_symbol.Bid(),m_symbol.Digits());
   Comment(text);

  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates(void)
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      Print("RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
      return(false);
//---
   return(true);
  }
//+------------------------------------------------------------------+

If a symbol specified in the"InpSymbol" parameter exists, then the following information will be displayed on the chart

  • Symbol name
  • Ask price
  • Price Bid


Here is how it works: we start it on EURUSD and specify the USDJPY symbol in the settings. So, it works with the EURUSD symbol, but the data (prices) comes from USDJPY


Files:
 
Vladimir Karputov:

You're being a bit florid, but let me guess: you need an input parameter with a symbol name? Then here is the code - you specify the desired symbol in the"InpSymbol" parameter. This symbol is checked in OnInit() - if there is no such a symbol, then the Expert Advisor will be unloaded and an error message will appear in the "Experts" tab of the terminal (or in the "Journal" tab of the Strategy Tester).

If a symbol specified in the"InpSymbol" parameter exists, then the following information will be displayed on the chart

  • Symbol name
  • Ask price
  • Price Bid


Here is how it works: we start it on EURUSD and specify the USDJPY symbol in the settings. So, it works with EURUSD symbol, but the information (prices) comes from US

Vladimir Karputov:

You're being florid, but let me guess: you need an input parameter with the symbol name? Then here's the code - you specify the required symbol in the"InpSymbol" parameter. This symbol is checked in OnInit() - if there is no such a symbol, then the Expert Advisor will be unloaded and an error message will appear in the "Experts" tab of the terminal (or in the "Journal" tab of the Strategy Tester).

If a symbol specified in the"InpSymbol" parameter exists, then the following information will be displayed on the chart

  • Symbol name
  • Ask price
  • Price Bid


Here is how it works: we start it on EURUSD and specify the USDJPY symbol in the settings. So, it turns out that it works with the EURUSD symbol, but receives information (prices) from the USDJPY symbol


Yes, but in this example, the Expert Advisor runs on EURUSD, but the information (prices) is obtained from the USDJPY symbol.

Why complicate things, I mean, if we take this example, then we launch the Expert Advisor on EURUSD ( or any other

needed currency pair) and that's it, we observe this pair, and then, if necessary, we open a position.

Reason: