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

 
Alexey Belyakov:

This way? Or do I need to declare something else in OnInit?

No,all lines in OnInitare hidden by default:

int OnInit() {
  hLine.Create(0, "hLine", 0, 0);
  hLine.Color(clrDarkViolet);
  lLine.Create(0, "lLine", 0, 0);
  lLine.Color(clrDodgerBlue);
  return(INIT_SUCCEEDED);
}
 
Mihail Matkovskij:

No,all lines are hidden by default inOnInit:

The condition is ignored for some reason. Even if I set a position opening.

PRL variable can't be yanked and put in a condition.


if (c0<PRL)  
{
MqlTradeRequest request ={0};
MqlTradeResult  result= {0};
      request.action   =TRADE_ACTION_DEAL;                         // тип торговой операции
      request.symbol   =Symbol();                                 // символ
      request.volume   =1;                                       // объем в 1 лот
      request.type     =ORDER_TYPE_SELL;                         // тип ордера
      request.price    =SymbolInfoDouble(Symbol(),SYMBOL_ASK); // цена для открытия
      request.deviation=3;
      request.sl    = NormalizeDouble(Bid+50*_Point,_Digits);
      request.tp    = NormalizeDouble(Bid-50*_Point,_Digits);
      if(!OrderSend(request,result))
         PrintFormat("OrderSend error %d",GetLastError());     // если отправить запрос не удалось, вывести код ошибки

I'm trying to pull it out somehow with GlobalVariableGet . But somehow it's doubtful...

Even when I put it inside the condition. The minimum price is already set. But it still stubbornly refuses to open the position.


//+------------------------------------------------------------------+
//|                                                  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=0;
  double   PRL=0;
  double   Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   double  Bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);

  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);
 
  
  
  // double a=GlobalVariableGet("i",PRL); 
Comment(" =======",PRL,"\n");

if (c0<PRL)  
{
MqlTradeRequest request={0};
MqlTradeResult  result={0};
      request.action   =TRADE_ACTION_DEAL;                         // тип торговой операции
      request.symbol   =Symbol();                                 // символ
      request.volume   =1;                                       // объем в 1 лот
      request.type     =ORDER_TYPE_SELL;                         // тип ордера
      request.price    =SymbolInfoDouble(Symbol(),SYMBOL_ASK); // цена для открытия
      request.deviation=3;
      request.sl    = NormalizeDouble(Bid+50*_Point,_Digits);
      request.tp    = NormalizeDouble(Bid-50*_Point,_Digits);
      if(!OrderSend(request,result))
         PrintFormat("OrderSend error %d",GetLastError());     // если отправить запрос не удалось, вывести код ошибки;
}
}
}
//+------------------------------------------------------------------+
Совершение сделок - Торговые операции - Справка по MetaTrader 5
Совершение сделок - Торговые операции - Справка по MetaTrader 5
  • www.metatrader5.com
Торговая деятельность в платформе связана с формированием и отсылкой рыночных и отложенных ордеров для исполнения брокером, а также с управлением текущими позициями путем их модификации или закрытия. Платформа позволяет удобно просматривать торговую историю на счете, настраивать оповещения о событиях на рынке и многое другое. Открытие позиций...
 
Alexey Belyakov:

The condition is ignored for some reason. Even if I set a position opening.

The PRL variable cannot be yanked and put in a condition.


PRL, you have Low at offset 1
PRL = iLow(NULL, PERIOD_CURRENT, 1);
PRH, this is High at offset 1
PRH = iHigh(NULL, PERIOD_CURRENT, 1);

We get the following picture:

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);  
  }
}

Therefore, instead ofPRL you can take iLow(NULL, PERIOD_CURRENT, 1) or l1.

I get the impression that you're not interested in writing code and don't understand what you're doing. A little patience and you can easily figure it all out. Clean up your code and many questions will disappear on their own. If you cannot figure out what went wrong, there is also debugging, it is a very powerful tool of a programmer. Get to the bottom of what you are doing and then ask questions.

 
Alexey Belyakov:

The condition is ignored for some reason. Even if I set a position opening.

Variable PRL cannot be yanked and put in a condition.


I'm trying to pull it out somehow with GlobalVariableGet . But somehow it's doubtful...

Even when I put it inside the condition. The minimum price is already set. I still do not want to open a position.


So, why do I have to write one message first and then edit it?

 
Alexey Belyakov:

The condition is ignored for some reason. Even if I set a position opening.

Variable PRL cannot be yanked and put in a condition.


I'm trying to pull it out somehow with GlobalVariableGet . But somehow it's doubtful...

Even when I put it inside the condition. The minimum price is already set. I still do not want to open a position.


The algorithm in your code was originally wrong, as well as the market entry algorithm. I have corrected it.

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

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


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

double   PRH = 0;
double   PRL = 0;

double entryPRL = 0;
double entryPRH = 0;

//+------------------------------------------------------------------+
//| 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   Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
  double  Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
  
  rt = MathAbs(c1 - o1) / Point(); //размер тела свечи
  rs1 = MathAbs(h1 - c1) / Point(); // размер верхней тени свечи
  rs2 = MathAbs(l1 - c1) / Point(); // размер нижней тени свечи
  
  if ((rs1 >= rt) && (c1 > o1)) { //условия для растущих свечей
    PRH = h1; // то это будет максимум
    hLine.Price(0, PRH);
  }
  
  if ((rs2 >= rt) && (c1 < o1)) { //условия для падающих свечей
    PRL = l1;
    lLine.Price(0, PRL);
    // double a=GlobalVariableGet("i",PRL);
    Comment(" =======", PRL, "\n");
  }
  
  if (PRL > 0 && c0 < PRL && PRL != entryPRL) {
     MqlTradeRequest request={0};
     MqlTradeResult  result={0};

     request.action   =TRADE_ACTION_DEAL;                     
     request.symbol   =Symbol();                          
     request.volume   =0.1;                                   
     request.type     =ORDER_TYPE_SELL;                       
     request.price    =SymbolInfoDouble(Symbol(),SYMBOL_BID); 
     request.deviation= 5;                                    
     request.magic    = 0;                         

     if(OrderSend(request,result))
       entryPRL = PRL;
     else
       PrintFormat("OrderSend error %d",GetLastError());     // если отправить запрос не удалось, вывести код ошибки

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

But this code will need a lot of revisions to be able to use it in real trading...

 
Mihail Matkovskij:

In your code, the algorithm itself was originally wrong, as well as the algorithm for entering the market. Corrected it.

But to use this code in real trading you will need a lot of revisions...

Very cool! Thank you! Looking here at the same time, and the multiple entry problem is solved. It's working as it should now.
 

How, in the tester, can a 4108 (invalid ticket) occur during a modification? MQL4

if(OrdersTotal() > 0 && OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
     {
      if(OrderType() == OP_BUY && SymbolInfoDouble(_Symbol,SYMBOL_BID) >= value_tp1 && !_tps) _tps = OrderClose(ticket,c_lot,SymbolInfoDouble(_Symbol,SYMBOL_BID),(int)slippage,clrNONE);
      if(OrderType() == OP_SELL && SymbolInfoDouble(_Symbol,SYMBOL_ASK) <= value_tp1 && !_tps) _tps = OrderClose(ticket,c_lot,SymbolInfoDouble(_Symbol,SYMBOL_ASK),(int)slippage,clrNONE);
      if(_tps && !sl_mod) sl_mod = OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE);
     };


 
Alexandr Sokolov:

How, in the tester, can a 4108 (invalid ticket) occur during a modification? MQL4


most likely you are trying to close an order that is already in the history, i.e. already closed order

look in the SELECT_BY_TICKET help file, it does this regardless of whether the order is already open or in the order history

SZZY: do a check for OrderCloseTime()

 
Igor Makanu:

you are most likely trying to close an order that is already in the history, i.e. already closed order

look through the SELECT_BY_TICKET help file. It does this regardless of whether the order is already open or in the order history

SZY: check OrderCloseTime()

I think it says that select in the market

OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)
OrderSelect - Trade Functions - MQL4 Reference
OrderSelect - Trade Functions - MQL4 Reference
  • docs.mql4.com
To find out from what list the order has been selected, its close time must be analyzed. If the order close time equals to 0, the order is open or pending and taken from the terminal open orders list. One can distinguish an opened order from a pending order by the order type. If the order close time does not equal to 0, the order is a closed...
 
Vitaly Muzichenko:

I think that says choose which one is on the market.

Didn't think I'd have to quote you on the documentation...

Note

The parameter pool is ignored if the order is selected using the ticket number. The ticket number is the unique identifier for the order.

To determine from which list an order is selected, we should analyse its closing time. If the time of order closing is 0, then the order is open or pending and is taken from the list of open orders of the terminal.