Questions from Beginners MQL4 MT4 MetaTrader 4 - page 254

 
Aleksei Stepanenko #:
Well, there you have it, beautiful.

No one was quarrelling. If you don't like the answer, go ahead... What's there to be offended about? People are all different. And in a previous post I said this about all programmers. I myself sometimes ask questions in topics for newbies. What's the point of creating a whole thread because of a question that can be answered with just one post?

 

wrote a robot... The tester gives an OrderSend error 148. What's the problem ?


//+------------------------------------------------------------------+
//|                                               эксперт пустой.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
int pu;
 
   
//делаем сделку один раз при октрытии бара
int i=1;//номер ордера
int posup=0,possell=0;
int pos_sum=0;
int posa_close=0;
double posa_close_summ=0.03;//объем закрытой позиции
 
datetime bar_otkr=Time[0];
if (Time[0]>bar_otkr)

 while ( i<OrdersTotal())
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 
 pu=pu+OrderProfit();
 if (OrderType()== OP_BUY) posup++;
 if (OrderType()== OP_SELL) possell++;
 if (posup>0)pos_sum=1;//направление бай
 if (possell>0)pos_sum=0;//направление селл
 i++;
 }
 ;
 
 printf(posup,possell);
   if    (Close[0]>Open[0]&&pu==0) OrderSend(Symbol(),OP_BUY,0.03,Bid,2,Ask+15,0,0,0,0,0);//свеча бай при начале работы
   if (Close[0]<Open[0]&&pu==0) OrderSend(Symbol(),OP_SELL,0.03,Bid,2,Ask+15,0,0,0,0,0);//свеча селл при начале работы
      
   if(Close[0]>Open[0]&&pu>0&&pos_sum==1) OrderSend(Symbol(),OP_BUY,0.03,Bid,2,Ask+15,0,0,0,0,0);//свеча бай при прибыли при покупках - покупаем 0.03
   if (Close[0]<Open[0]&&pu>0&&pos_sum==1)//свеча селл при прибыли при покупках- кроем 0.01
   while (i<OrdersTotal())
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 if (OrderLots()>=0.01&&posa_close==0) OrderClose(i,0.01,Bid,3);posa_close=1 ;
 if (posa_close==1)break ; else posa_close=0;
 

 
 i++;
   
  }
   
   
;
 
 
 
  if (Close[0]<Open[0]&&pu<0&&pos_sum==1)//свеча селл при убытках при покупках- кроем 0.03
   while (i<OrdersTotal())
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 if (OrderLots()==0.03&&posa_close==0) OrderClose(i,0.03,Bid,3);posa_close=1 ;
 if (posa_close==1)break ; else posa_close=0;
 
 if (posa_close==0&&OrderLots()==0.02)OrderClose(i,OrderLots(),Bid,3);posa_close_summ=posa_close_summ-0.02;if (posa_close_summ==0)posa_close=1;
 
 if (posa_close==0&&OrderLots()==0.01)OrderClose(i,OrderLots(),Bid,3);posa_close_summ=posa_close_summ-0.01;if (posa_close_summ==0)posa_close=1;
 
 
 
 i++;
   
  }
   
   
;
 
 
 
 
 
 
 
 
 
 
 
  
  if(Close[0]<Open[0]&&pu>0&&pos_sum==0)OrderSend(Symbol(),OP_SELL,0.03,Bid,2,Ask+15,0,0,0,0,0);// свеча селл при продаже и прибыли - покупаем 0.03
  
  
  
  
  
   if (Close[0]>Open[0]&&pu>0&&pos_sum==0)//свеча бай при продаже и прибыли- кроем 0.01
   while (i<OrdersTotal())
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 if (OrderLots()>=0.01&&posa_close==0) OrderClose(i,0.01,Bid,3);posa_close=1 ;
 if (posa_close==1)break ; else posa_close=0;
 
 
 i++;
   
  }
   
   
;
 
 
  
  
  
// свеча селл при продаже и убытке - ничего не делаем
    

  if (Close[0]>Open[0]&&pu<0&&pos_sum==0)// свеча бай при продаже и убытке - кроем 0.03

   while (i<OrdersTotal())
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 if (OrderLots()==0.03&&posa_close==0) OrderClose(i,0.03,Bid,3);posa_close=1 ;
 if (posa_close==1)break ; else posa_close=0;
 
 if (posa_close==0&&OrderLots()==0.02)OrderClose(i,OrderLots(),Bid,3);posa_close_summ=posa_close_summ-0.02;if (posa_close_summ==0)posa_close=1;
 
 if (posa_close==0&&OrderLots()==0.01)OrderClose(i,OrderLots(),Bid,3);posa_close_summ=posa_close_summ-0.01;if (posa_close_summ==0)posa_close=1;
 
 
 
 i++;
   
  }
   
   
;
  
  
  
  }
//+------------------------------------------------------------------+
 

People ... help! there is a new bar...

how do i open an order only when a new bar opens?

 
Roman Epifanov a new bar...

how do i open an order only when a new bar opens?

void OnTick() {
  datetime cTime;
  static datetime time = 0;
  cTime = iTime(Symbol(), Period(), 0);
  if (time != cTime)
    time = cTime;
  else
    return;
  ...
}
 

I don't want to open trades properly... what could be the problem... No errors during compilation

//+------------------------------------------------------------------+
//|                                               эксперт пустой.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

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

//делаем сделку один раз при октрытии бара
int i=1;//номер ордера
int posup=0,possell=0;
int pos_sum=0;
int posa_close=0;
double posa_close_summ=0.03;//объем закрытой позиции
 

static datetime New_Time=0;
  bool New_Bar=false;
  if(New_Time!=Time[0])
  {
  New_Time=Time[0];
  New_Bar=true;
}

if (New_Bar)
{
 Print(New_Bar);
 
 
 while ( i<OrdersTotal())
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 
 pu=pu+OrderProfit();
 if (OrderType()== OP_BUY) posup++;
 if (OrderType()== OP_SELL) possell++;
 if (posup>0)pos_sum=1;//направление бай
 if (possell>0)pos_sum=0;//направление селл
 i++;
 }
 ;
 

   if    (Close[0]>Open[0]&&pu==0) OrderSend(Symbol(),OP_BUY,0.03,Bid,2,Ask+15,0,0,0,0,0);//свеча бай при начале работы
   if (Close[0]<Open[0]&&pu==0) OrderSend(Symbol(),OP_SELL,0.03,Bid,2,Ask+15,0,0,0,0,0);//свеча селл при начале работы
      
   if(Close[0]>Open[0]&&pu>0&&pos_sum==1) OrderSend(Symbol(),OP_BUY,0.03,Bid,2,Ask+15,0,0,0,0,0);//свеча бай при прибыли при покупках - покупаем 0.03
   if (Close[0]<Open[0]&&pu>0&&pos_sum==1)//свеча селл при прибыли при покупках- кроем 0.01
   while (i<OrdersTotal())
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 if (OrderLots()>=0.01&&posa_close==0) OrderClose(i,0.01,Bid,3);posa_close=1 ;
 if (posa_close==1)break ; else posa_close=0;
 

 
 i++;
   
  }
   
   
;
 
 
 
  if (Close[0]<Open[0]&&pu<0&&pos_sum==1)//свеча селл при убытках при покупках- кроем 0.03
   while (i<OrdersTotal())
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 if (OrderLots()==0.03&&posa_close==0) OrderClose(i,0.03,Bid,3);posa_close=1 ;
 if (posa_close==1)break ; else posa_close=0;
 
 if (posa_close==0&&OrderLots()==0.02)OrderClose(i,OrderLots(),Bid,3);posa_close_summ=posa_close_summ-0.02;if (posa_close_summ==0)posa_close=1;
 
 if (posa_close==0&&OrderLots()==0.01)OrderClose(i,OrderLots(),Bid,3);posa_close_summ=posa_close_summ-0.01;if (posa_close_summ==0)posa_close=1;
 
 
 
 i++;
   
  }
   
   
;
 
 
 
 
 
 
 
 
 
 
 
  
  if(Close[0]<Open[0]&&pu>0&&pos_sum==0)OrderSend(Symbol(),OP_SELL,0.03,Bid,2,Ask+15,0,0,0,0,0);// свеча селл при продаже и прибыли - покупаем 0.03
  
  
  
  
  
   if (Close[0]>Open[0]&&pu>0&&pos_sum==0)//свеча бай при продаже и прибыли- кроем 0.01
   while (i<OrdersTotal())
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 if (OrderLots()>=0.01&&posa_close==0) OrderClose(i,0.01,Bid,3);posa_close=1 ;
 if (posa_close==1)break ; else posa_close=0;
 
 
 i++;
   
  }
   
   
;
 
 
  
  
  
// свеча селл при продаже и убытке - ничего не делаем
    

  if (Close[0]>Open[0]&&pu<0&&pos_sum==0)// свеча бай при продаже и убытке - кроем 0.03

   while (i<OrdersTotal())
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 if (OrderLots()==0.03&&posa_close==0) OrderClose(i,0.03,Bid,3);posa_close=1 ;
 if (posa_close==1)break ; else posa_close=0;
 
 if (posa_close==0&&OrderLots()==0.02)OrderClose(i,OrderLots(),Bid,3);posa_close_summ=posa_close_summ-0.02;if (posa_close_summ==0)posa_close=1;
 
 if (posa_close==0&&OrderLots()==0.01)OrderClose(i,OrderLots(),Bid,3);posa_close_summ=posa_close_summ-0.01;if (posa_close_summ==0)posa_close=1;
 
 
 
 i++;
   
  }
   
   
;
  
  
  
  }
}
//+------------------------------------------------------------------+
Files:
Lentyai2.mq4  10 kb
 

Hi all, I need a piece of code to close all purchases by a certain symbol.

 
Roman Epifanov #:

wrote a robot... The tester gives an OrderSend error 148. What's the problem ?


There are no internal functions in MQL, only external.

Do you know how to fix it or do you need a qualified help? )))

 
Алексей Тарабанов #:

There are no internal functions in MQL, only external ones.

Do you know how to fix it, or do you need expert help? )))

error 148 has already been solved

 

It says wrong take profit - what's wrong?


if (Close[1]<Open[1]) {OrderSend(Symbol(),OP_SELL,lot,Bid,2,Ask+sl,Bid-tp,"Candle sell, Close[0]<MA");};
   

I put zero instead of Bid-tp - it works!

 
Roman Epifanov take profit - what's wrong?


How do you initialise the tp?

Reason: