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

 
Порт-моне тв:

@MakarFX,thank you.... I get these errors when compiling

'CounterOrderTradeType' - function not defined test for many expt trade.mq4 48 19

'CounterOrderTradeType' - function declarations are allowed on global, namespace or class scope only test for many expt trade.mq4 141 5


correct the yellow highlighting


 //+------------------------------------------------------------------+
//|                                                   sova_2bars.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int Magic = 64777;
extern double Lot = 0.01;
extern int Slippage = 3;
extern int TakeProfit = 1600;
extern int StopLoss = 800;
extern int Experation = 0; 

int OrderOfSymbol;
//+------------------------------------------------------------------+
....
 //+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  { 
  OrderOfSymbol=CounterOrderTradeType(-1);

...

//-------------функции---------------------------------------

if ( Ask>0 && Bid >0)
   {

 if (OrderOfSymbol<1)
    if (Close[1]>Open[1])
    if(Bears < -0.00035 && MiddleBB > Ask)
         {
    ticket = OrderSend(Symbol(),OP_BUY, Lot, Ask, Slippage, SLbuy, TPbuy, "открыт ордер на покупку", Magic, 0, Blue);
         }
         
   
if (OrderOfSymbol==1)
   if(Bulls > 0.00035 && MiddleBB < Ask && Bears > 0 && Opentwo < Closetwo && Openthree < Closethree && BigTrand < Ask)
    CloseBuyPositions1();
   } 
//+------------------------------------------------------------------+
//| Подсчет ордеров потекущему символу                               |
//+------------------------------------------------------------------+
int CounterOrderTradeType(ENUM_ORDER_TYPE order_type)
   {
   int cnt=0;
   //----
   for(int pos=OrdersTotal()-1;pos>=0;pos--)
      {
      if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)==false) continue;
      if(OrderSymbol()!=_Symbol) continue;
      if(order_type == OrderType() || order_type == -1) cnt++;
      }
   //----
   return(cnt);
   }
 
MakarFX:

Correct what you highlighted in yellow.


I realised it was about these segments and tried to re-define the variables, didn't help. I'm in mql4 (I'm not familiar with 5, if there is a difference), so I'm asking for help again
 
Порт-моне тв:
I realised it was about these segments and tried to re-define the variables, didn't help. I'm in mql4 (I'm not familiar with mql5, if it's different), so please help me again.
Give me the code you got, I'll see what's wrong.
 
MakarFX:
Give me the code you got, I'll see what's wrong.
//+------------------------------------------------------------------+
//| sova_2bars.mq4 |
//| Copyright 2020, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp.
#property link "https://www.mql5.com"
#property version "1.00"
#property strict

extern int Magic = 6477;
extern double Lot = 0.01;
extern int Slippage = 3;
extern int TakeProfit = 1600;
extern int StopLoss = 800;
extern int Experation = 0;

double Openone, //opening price of the first candle
Opentwo, //opening price of the second candle
Closeone, //close price of the first candle
Closetwo, //the scene of the second candle
Lowone, //first candle's minimum price
Lowtwo, //first candle's price
Lowthree, //the scene of the third candle's minimum
Openthree, //open price of a third candle
Closethree, //close price of the third candle
CloseOrder, //close order
Highone, //maximal price of the first candle
Hightwo, //the price maximum of the second candle
Highhree, //the price maximum of the second candle
Highfour, //the price is the maximum of the second candle
Highfive, //price of a maximum of a second candle
Highsix, //price of the maximum second candle.
Highseven, //price of the maximum second candle
Highheight, //price of a maximum of a second candle
Lowfour, //the price of the fourth candle's minimum
Lowfive, //first price of a fifth candle
Lowsix, //the scene of a sixth candle's minimum
Lowseven, //the scene of a minimum of a seventh candle
Loweight, //the scene of the eighth candle's minimum
Closefour, //the closing price of the fourth candle
Openfour,
Low15,
Low25,
SLbuy, TPbuy, SLsell, TPsell, //open price of the fourth candle
OrderOpenPrice;
int ticket, ticketclose;
int OrderOfSymbol;

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
OrderOfSymbol=CounterOrderTradeType(-1);
//--- determining prices of required bars
Openone = NormalizeDouble(iOpen(Symbol(), Period(), 0), Digits);
Highone = NormalizeDouble(iHigh(Symbol(), Period(), 0), Digits);
Hightwo = NormalizeDouble(iHigh(Symbol(), Period(), 1), Digits);
Highthree = NormalizeDouble(iHigh(Symbol(), Period(), 2), Digits);
Highfour = NormalizeDouble(iHigh(Symbol(), Period(), 3), Digits);
Highfive = NormalizeDouble(iHigh(Symbol(), Period(), 4), Digits);
Highsix = NormalizeDouble(iHigh(Symbol(), Period(), 5), Digits);
Highseven = NormalizeDouble(iHigh(Symbol(), Period(), 6), Digits);
Higheight = NormalizeDouble(iHigh(Symbol(), Period(), 7), Digits);
Opentwo = NormalizeDouble(iOpen(Symbol(), Period(), 1), Digits);
Openthree = NormalizeDouble(iOpen(Symbol(), Period(), 2), Digits);
Openfour = NormalizeDouble(iOpen(Symbol(), Period(), 3), Digits);
Closeone = NormalizeDouble(iClose(Symbol(), Period(), 0), Digits);
Closetwo = NormalizeDouble(iClose(Symbol(), Period(), 1), Digits);
Closethree = NormalizeDouble(iClose(Symbol(), Period(), 2), Digits);
Closefour = NormalizeDouble(iClose(Symbol(), Period(), 3), Digits);
Lowone = NormalizeDouble(iLow(Symbol(), Period(), 0), Digits);
Lowtwo = NormalizeDouble(iLow(Symbol(), Period(), 1), Digits);
Lowthree = NormalizeDouble(iLow(Symbol(), Period(), 2), Digits);
Lowfour = NormalizeDouble(iLow(Symbol(), Period(), 3), Digits);
Lowfive = NormalizeDouble(iLow(Symbol(), Period(), 4), Digits);
Lowsix = NormalizeDouble(iLow(Symbol(), Period(), 5), Digits);
Lowseven = NormalizeDouble(iLow(Symbol(), Period(), 6), Digits);
Low15 = NormalizeDouble(iLow(Symbol(), Period(), 14), Digits);
Low25 = NormalizeDouble(iLow(Symbol(), Period(), 24), Digits);

//----индикатор - условия--------------------------------------


double BigTrand = iMA(NULL, 0,50,0,MODE_SMA, PRICE_CLOSE, 1);
double SmallTrand = iMA(NULL, 0,5,0,MODE_SMA, PRICE_CLOSE, 1);
double LowerBB = iBands(_Symbol,_Period,17,1,0,PRICE_CLOSE,MODE_LOWER,1);
double UpperBB = iBands(_Symbol,_Period,17,1,0,PRICE_CLOSE,MODE_UPPER,1);
double MiddleBB = iBands(_Symbol,_Period,17,1,0,PRICE_CLOSE,MODE_MAIN,1);
double Bears = iBearsPower(_Symbol,_Period, 5, PRICE_CLOSE,0);
double Bulls = iBullsPower(_Symbol,_Period, 5, PRICE_CLOSE,0);
double Parab = iSAR(_Symbol,_Period,0.002, 1, 0);

SLbuy = NormalizeDouble(Ask - StopLoss*Point, 5);
TPbuy = NormalizeDouble(Ask + TakeProfit*Point, 5);
SLsell = NormalizeDouble(Bid + StopLoss*Point, 5);
TPsell = NormalizeDouble(Bid - TakeProfit*Point, 5);


//-------------функции---------------------------------------




if ( Ask>0 && Bid >0)
{

if (OrdersTotal()<1)
if (Close[1]>Open[1])
if(Ask>LowerBB)
{
ticket = OrderSend(Symbol(),OP_BUY, Lot, Ask, Slippage, SLbuy, TPbuy, "open buy order", Magic, 0, Blue)
}
if(OrdersTotal()==1)
if(Bid>UpperBB)
CloseBuyPositions1();
}

//+------------------------------------------------------------------+
//| Order counting |
//+------------------------------------------------------------------+
int CounterOrderTradeType(ENUM_ORDER_TYPE order_type)
{
int cnt=0;
//----
for(int pos=OrdersTotal()-1;pos>=0;pos--)
{
if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)==false) continue;
if(OrderSymbol()!=_Symbol) continue;
if(order_type == OrderType() || order_type == -1) cnt++;
}
//----
return(cnt);
}


//---- end of function
}
//---------------------------------------------------------------------
void CloseBuyPositions1()
{
for(int i = OrdersTotal() - 1; i >= 0; i--)
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if(OrderMagicNumber() == Magic)
if(OrderSymbol() == Symbol())
{


if(OrderType()==OP_BUY)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 3, NULL);
}
}
}




Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
  • www.mql5.com
One Click Close The script allows users to easily close positions if their profit/loss reaches or exceeds a value specified in pips. Please set slippage value first. Sometimes some positions do not close due to high volatility of the market. Please set larger slippage or restart the script. The free demo version is: ...
 
Порт-моне тв:

the wrong parenthesis ...catch.

 //+------------------------------------------------------------------+
//|                                                   sova_2bars.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int Magic = 64777;
extern double Lot = 0.01;
extern int Slippage = 3;
extern int TakeProfit = 1600;
extern int StopLoss = 800;
extern int Experation = 0;

double Openone,    //цена открытия первой свечи
Opentwo,    //цена открытия второй свечи
Closeone,   //цена закрытия первой свечи
Closetwo,   //цена закрытия второй свечи
Lowone,     //цена минимальная первой свечи
Lowtwo,     //цена минимальная второй свечи
Lowthree,   //цена минимальная третьей свечи
Openthree,  //цена открытия третьей свечи
Closethree, //цена закрытия третьей свечи
CloseOrder, //закрыть ордер
Highone,    //цена максимальная первой свечи
Hightwo,    //цена максимальная второй свечи
Highthree,    //цена максимальная второй свечи
Highfour,    //цена максимальная второй свечи
Highfive,    //цена максимальная второй свечи
Highsix,    //цена максимальная второй свечи
Highseven,    //цена максимальная второй свечи
Higheight,    //цена максимальная второй свечи
Lowfour,    //цена минимальная четвертой свечи
Lowfive,    //цена минимальная пятой свечи
Lowsix,    //цена минимальная шестой свечи
Lowseven,    //цена минимальная седьмой свечи
Loweight,    //цена минимальная восьмой свечи
Closefour,  //цена закрытия четвертой свечи
Openfour,
Low15,
Low25, 
SLbuy, TPbuy,  SLsell, TPsell,   //цена открытия четвертой свечи
OrderOpenPrice;
int ticket, ticketclose; 
int OrderOfSymbol;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  OrderOfSymbol=CounterOrderTradeType(-1); 
//--- определение цен необходимых баров
   Openone = NormalizeDouble(iOpen(Symbol(), Period(), 0), Digits);
   Highone = NormalizeDouble(iHigh(Symbol(), Period(), 0), Digits);
   Hightwo = NormalizeDouble(iHigh(Symbol(), Period(), 1), Digits);
   Highthree = NormalizeDouble(iHigh(Symbol(), Period(), 2), Digits);
   Highfour = NormalizeDouble(iHigh(Symbol(), Period(), 3), Digits);
   Highfive  = NormalizeDouble(iHigh(Symbol(), Period(), 4), Digits);
   Highsix  = NormalizeDouble(iHigh(Symbol(), Period(), 5), Digits);
   Highseven = NormalizeDouble(iHigh(Symbol(), Period(), 6), Digits);
   Higheight = NormalizeDouble(iHigh(Symbol(), Period(), 7), Digits);
   Opentwo = NormalizeDouble(iOpen(Symbol(), Period(), 1), Digits);
   Openthree = NormalizeDouble(iOpen(Symbol(), Period(), 2), Digits);
   Openfour = NormalizeDouble(iOpen(Symbol(), Period(), 3), Digits);
   Closeone = NormalizeDouble(iClose(Symbol(), Period(), 0), Digits);
   Closetwo = NormalizeDouble(iClose(Symbol(), Period(), 1), Digits);
   Closethree = NormalizeDouble(iClose(Symbol(), Period(), 2), Digits);
   Closefour = NormalizeDouble(iClose(Symbol(), Period(), 3), Digits);
   Lowone = NormalizeDouble(iLow(Symbol(), Period(), 0), Digits);
   Lowtwo = NormalizeDouble(iLow(Symbol(), Period(), 1), Digits);
   Lowthree = NormalizeDouble(iLow(Symbol(), Period(), 2), Digits);
   Lowfour = NormalizeDouble(iLow(Symbol(), Period(), 3), Digits);
   Lowfive = NormalizeDouble(iLow(Symbol(), Period(), 4), Digits);
   Lowsix = NormalizeDouble(iLow(Symbol(), Period(), 5), Digits);
   Lowseven = NormalizeDouble(iLow(Symbol(), Period(), 6), Digits);
   Low15 = NormalizeDouble(iLow(Symbol(), Period(), 14), Digits);
   Low25 = NormalizeDouble(iLow(Symbol(), Period(), 24), Digits);

     
//----индикатор - условия--------------------------------------


double BigTrand = iMA(NULL, 0,50,0,MODE_SMA, PRICE_CLOSE, 1);
double SmallTrand = iMA(NULL, 0,5,0,MODE_SMA, PRICE_CLOSE, 1);
double LowerBB = iBands(_Symbol,_Period,17,1,0,PRICE_CLOSE,MODE_LOWER,1);
double UpperBB = iBands(_Symbol,_Period,17,1,0,PRICE_CLOSE,MODE_UPPER,1);
double MiddleBB = iBands(_Symbol,_Period,17,1,0,PRICE_CLOSE,MODE_MAIN,1);
double Bears = iBearsPower(_Symbol,_Period, 5, PRICE_CLOSE,0);
double Bulls = iBullsPower(_Symbol,_Period, 5, PRICE_CLOSE,0);
double Parab = iSAR(_Symbol,_Period,0.002, 1, 0);

SLbuy = NormalizeDouble(Ask - StopLoss*Point, 5); 
TPbuy = NormalizeDouble(Ask + TakeProfit*Point, 5);
SLsell = NormalizeDouble(Bid + StopLoss*Point, 5); 
TPsell = NormalizeDouble(Bid - TakeProfit*Point, 5);


//-------------функции---------------------------------------
            
if ( Ask>0 && Bid >0)
   {

 if (OrdersTotal()<1)
    if (Close[1]>Open[1])
    if(Ask>LowerBB)
         {
    ticket = OrderSend(Symbol(),OP_BUY, Lot, Ask, Slippage, SLbuy, TPbuy, "открыт ордер на покупку", Magic, 0, Blue);
         }
         
   
if (OrdersTotal()==1)
   if(Bid>UpperBB)
    CloseBuyPositions1();
   }
}
//----конец функции
//---------------------------------------------------------------------
void CloseBuyPositions1()
      {
      
       for(int i = OrdersTotal() - 1; i >= 0; i--)
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if(OrderMagicNumber() == Magic)
            if(OrderSymbol() == Symbol())
              {


          
          if(OrderType()==OP_BUY)
             {
              if(OrderClose(OrderTicket(), OrderLots(), Bid, 3, NULL)){Print("Order Close");}
             }
          }
       } 
//+------------------------------------------------------------------+
//| Подсчет ордеров потекущему символу                               |
//+------------------------------------------------------------------+
int CounterOrderTradeType(ENUM_ORDER_TYPE order_type)
   {
   int cnt=0;
   //----
   for(int pos=OrdersTotal()-1;pos>=0;pos--)
      {
      if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)==false) continue;
      if(OrderSymbol()!=_Symbol) continue;
      if(order_type == OrderType() || order_type == -1) cnt++;
      }
   //----
   return(cnt);
   }   
       
 
Порт-моне тв:

That's what I don't understand.

if ( Ask>0 && Bid >0)

why?

 
MakarFX:

That's what I don't understand.

why?

I do this so that I don't have to write additional functions with all the brackets and then experiment with the indicators on this function by changing this common-understood value
 
Порт-моне тв:
to avoid having to write additional functions with all the brackets, I also add a general one, and then experiment with the indicators on this function

I would do this

//-------------функции---------------------------------------
            
 if (OrderOfSymbol<1)
    if (Close[1]>Open[1])
      if(Ask>LowerBB)
         {
         if(OrderSend(Symbol(),OP_BUY, Lot, Ask, Slippage, SLbuy, TPbuy, "открыт ордер на покупку", Magic, 0, Blue)){Print("Buy Order Open");}
         }
         
   for(int pos=0;pos<OrdersTotal();pos++)
      if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==_Symbol)
            if(Bid>UpperBB)
               CloseBuyPositions1();
   }
//----конец функции
 
MakarFX:

Мне вот это не понятно

зачем?


The problem is not solved, opened order on one chart, on the second does not want to, counts OrdersTotal apparently again on all (if anything the magician has changed, even compiled a separate EA)

 
Порт-моне тв:

The problem is not solved, opened order on one chart, on the second does not want to, counts OrdersTotal apparently again on all (if anything the magician has changed, even compiled a separate advisor)

Put the function from my post. It works for me
Reason: