Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1202

 
Alexey Belyakov:

Good afternoon!

I am struggling with one question. Could you please tell me how to solve it?

The idea is the following: I draw a line at the previous maximum of the candle. I want the line to be shifted depending on the previous maximum.

Who prevents me from changing the line price on ticks when I need it?

//+------------------------------------------------------------------+
//|                                                  HiBkExample.mq5 |
//|                                      Copyright 2020, © Cyberdev. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, © Cyberdev."
#property version   "1.00"

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#include <ChartObjects\ChartObjectsLines.mqh>

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CChartObjectHLine hLine;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
  hLine.Create(0, "hLine", 0, 0);
  return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
//---
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
  double   o1 = iOpen(NULL, PERIOD_CURRENT, 1);
  double   h1 = iHigh(NULL, PERIOD_CURRENT, 1);
  double   l1 = iLow(NULL, PERIOD_CURRENT, 1);
  double   c1 = iClose(NULL, PERIOD_CURRENT, 1);
  double   c0 = iClose(NULL, PERIOD_CURRENT, 0);
  double   rt = 0;
  double   rs1 = 0;
  double   PRH;

  rt=MathAbs(c1 - o1) / Point(); //размер тела свечи
  rs1=MathAbs(h1 - c1) / Point();// размер верхней тени свечи

  if (rs1 >= rt) // условие для построения уровней (если верхняя тень предыдущей свечи, больше, или равна телу этой же всечи, то...   
  {
    PRH = iHigh(NULL, PERIOD_CURRENT, 1); // то это будет максимум
    hLine.Price(0, PRH);
    Comment("ЦЕНА МАКС =======", PRH, "\n"); // комментарий пред. максимума
  }
}
//+------------------------------------------------------------------+
Files:
 
Can you please tell me how MT5 knows from the history the price and time of the first trade when opening a position and the price and time of the first trade when closing that position? The purpose is to get an array of entry points from the history, because I sometimes equity when trading manually, so I want to understand if it is effective or not. I don't know anything about trading - I'm using a class, where these functions have not been implemented :(
 
Aleksei Beliakov:
There are 2 options
1. Delete an object before creating it
2. Set a price after the object is created

Hi namesake!)


void OnTick()
  {
//---
   double   o1 = iOpen(Symbol(),Period(),1);
   double   h1 = iHigh(Symbol(),Period(),1);
   double   l1 = iLow(Symbol(),Period(),1);
   double   c1 = iClose(Symbol(),Period(),1);
   double   c0 = iClose(Symbol(),Period(),0);
   double   rt = 0;
   double   rs1 = 0;
   double   rs2 = 0;
   
rt=MathAbs(c1-o1)/_Point; //размер тела свечи
rs1=MathAbs(h1-c1)/_Point;// размер верхней тени свечи
rs2=MathAbs(l1-c1)/_Point; // размер нижней тени свечи

ObjectDelete(0,"HL");

if ((rs1>=rt)&&(c1>o1)) //[для ростовых свечей] условие для построения уровней (если верхняя тень предыдущей свечи, больше, или равна телу этой же свечи, то...   
{
double PRH=iHigh(Symbol(),_Period,1); // то это будет максимум
ObjectCreate(0,"HL",OBJ_HLINE,0,_Period,PRH); // и строим по этому максимуму, горизонтальную линию
}

ObjectDelete(0,"lL");

if ((rs2>=rt)&&(c1<o1)) // [для падающих свечей]условие для построения уровней (если нижняя тень предыдущей свечи, больше, или равна телу этой же свечи, то...   
{
double PRL=iLow(Symbol(),_Period,1); // то это будет минимум
ObjectCreate(0,"lL",OBJ_HLINE,0,_Period,PRL); // и строим по этому минимум, горизонтальную линию
}
}

Considered option 1, something close. But not correct somehow happens. At first stages according to plan, then in a tailspin.

 
Mihail Matkovskij:

And who prevents you from simply changing the line price on the ticks when you need to?


//+------------------------------------------------------------------+
//|                                                  HiBkExample.mq5 |
//|                                      Copyright 2020, © Cyberdev. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, © Cyberdev."
#property version   "1.00"

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#include <ChartObjects\ChartObjectsLines.mqh>

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CChartObjectHLine hLine;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
  hLine.Create(0, "hLine", 0, 0);
  return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
//---
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
  double   o1 = iOpen(NULL, PERIOD_CURRENT, 1);
  double   h1 = iHigh(NULL, PERIOD_CURRENT, 1);
  double   l1 = iLow(NULL, PERIOD_CURRENT, 1);
  double   c1 = iClose(NULL, PERIOD_CURRENT, 1);
  double   c0 = iClose(NULL, PERIOD_CURRENT, 0);
  double   rt = 0;
  double   rs1 = 0;
  double   rs2 = 0;
  double   PRH;
  double   PRL;

  rt=MathAbs(c1 - o1) / Point(); //размер тела свечи
  rs1=MathAbs(h1 - c1) / Point();// размер верхней тени свечи
  rs2=MathAbs(l1 - c1) / Point();// размер нижней тени свечи

  if ((rs1>= rt)&&(c1>o1)) //условия для растущих свечей
  {
    PRH = iHigh(NULL, PERIOD_CURRENT, 1); // то это будет максимум
    hLine.Price(0, PRH);
  }
  
   if ((rs2>= rt)&&(c1<o1)) //условия для падающих свечей 
  {
    PRL = iLow(NULL, PERIOD_CURRENT, 1);
    hLine.Price(0, PRL);  
  }
  
  
}
//+------------------------------------------------------------------+

Well, sort of, yes. If you add conditions just a little bit, there's a problem. It draws one line. What it needs is: if there is no new high/low, it is saved.

 
Alexey Belyakov:


Well, sort of, yes. If you add conditions just a little bit, there's a problem. It draws one line. What it needs is: if there is no new high/low, it is saved.

I don't know, who's stopping you from adding another line for Low...?

//+------------------------------------------------------------------+
//|                                                  HiBkExample.mq5 |
//|                                      Copyright 2020, © Cyberdev. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, © Cyberdev."
#property version   "1.00"

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#include <ChartObjects\ChartObjectsLines.mqh>

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CChartObjectHLine hLine, lLine;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
  hLine.Create(0, "hLine", 0, 0);
  hLine.Color(clrDarkViolet);
  lLine.Create(0, "lLine", 0, 0);
  lLine.Color(clrDodgerBlue);
  return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
//---
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
  double   o1 = iOpen(NULL, PERIOD_CURRENT, 1);
  double   h1 = iHigh(NULL, PERIOD_CURRENT, 1);
  double   l1 = iLow(NULL, PERIOD_CURRENT, 1);
  double   c1 = iClose(NULL, PERIOD_CURRENT, 1);
  double   c0 = iClose(NULL, PERIOD_CURRENT, 0);
  double   rt = 0;
  double   rs1 = 0;
  double   rs2 = 0;
  double   PRH;
  double   PRL;

  rt=MathAbs(c1 - o1) / Point(); //размер тела свечи
  rs1=MathAbs(h1 - c1) / Point();// размер верхней тени свечи
  rs2=MathAbs(l1 - c1) / Point();// размер нижней тени свечи

  if ((rs1 >= rt) && (c1 > o1)) //условия для растущих свечей
  {
    PRH = iHigh(NULL, PERIOD_CURRENT, 1); // то это будет максимум
    hLine.Price(0, PRH);
  }
  
  if ((rs2 >= rt) && (c1 < o1)) //условия для падающих свечей 
  {
    PRL = iLow(NULL, PERIOD_CURRENT, 1);
    lLine.Price(0, PRL);  
  }
}
//+------------------------------------------------------------------+

You can add a whole array of lines or other objects and mark every Low and High with it. What's the problem...?

 
Aleksey Vyazmikin:
Please, advise how to get the price and time of the first deal when opening a position and the price and time of the first deal to close this position from the history. My purpose is to get an array of entry points from the history, because I sometimes equity when trading manually, so I want to know if it is effective or not. I don't know anything about trading operations, I use a class, where these functions have not been implemented :(

HistorySelectByPosition will help you

Then there are several options:

  • Take trades, check theDEAL_ENTRY_IN or DEAL_ENTRY_OUT property to enter or exit the market and take the price and time of that trade.
  • Take orders. With index 0 this is an entry into the market, respectively OrdersTotal()-1 an exit from the market.
  • The best way is to take both. The point is that if the position is opened with a slippage, the deal price ≠ the order price. Accordingly, take what you need.
Документация по MQL5: Торговые функции / HistorySelectByPosition
Документация по MQL5: Торговые функции / HistorySelectByPosition
  • www.mql5.com
Не следует путать между собой ордера из торговой истории и действующие отложенные ордера, которые отображаются на вкладке "Торговля" в панели "Инструменты". Список ордеров, которые были отменены или привели к проведению торговой операции, можно посмотреть  в закладке "История" на панели "Инструменты" клиентского терминала. Функция...
 
Is there any way to declare a bunch of buffers compactly in mt5?
 
Mihail Matkovskij:

I don't know, who's stopping you from adding another line for Low...?

You can add a whole array of lines or other objects and mark every Low and High with them. What's the problem...?

Yes! It's working properly! Thanks Mihail!


And if you have pierced the bottom line and need to delete it after the piercing, like:

if (c0<PRL)  
  {
  lLine.Delete(0,"lLine"); 
  }

Right?

 
Alexey Belyakov:

Yes! It works like it should! Thanks Mihail!


And if the bottom line is punched and you have to delete on the fact of punching, like:

Right?

The program only uses two lines to highlight levels. Why delete and then create it again if you can just set it to 0...?

lLine.Price(0, 0.0);

The line will not be visible on the chart... And then you can show it again:

lLine.Price(0, PRL);
 
Mihail Matkovskij:

The programme only uses two lines to highlight the levels. Why delete and then recreate when you can just set it to 0...?

The line will not be visible on the chart... And then you can show it again:

Like this? Or is it necessary to declare something else in OnInit?

if (c0<PRL)  
{
lLine.Price(0,0.0);
}