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

 

@STARIJ Thanks, I took my time writingObjectSet! It has long been written, not commented, and the code does not interfere! You wroteStringSubstr(str,20,7) , I didn't think it was all literal! After dinner, I realised it was all about the spaces! I should have counted every character and space! Thanks friend!!!!


 

Hello, dear friends. Another question from an μl4 newbie. I use the Fun_New_Bar() function recommended in the tutorial to define a new bar:


void Fun_New_Bar()                              // Ф-ия обнаружения нового бара   
  {                                             
   static datetime New_Time=0;                  // Время текущего бара  
   New_Bar=false;                               // Нового бара нет  
   if(New_Time!=Time[0])                        // Сравниваем время  
     {  
      New_Time=Time[0];                         // Теперь время такое  
      New_Bar=true;                             // Поймался новый бар  
     }  
  }
However, for some reason it does not always work. I've put in a print check:
if(Hour()>=8 && Hour()<20) 
      {
      Fun_New_Bar();
      if(New_Bar==false) Print("Нового бара нет");
      if(New_Bar==true )      
       {     
       RefreshRates();
        ........... Fun_Open_Sell();          
        ........... Fun_Open_Buy();
       } 
      }

So, it opens a position on a new bar, then prints "No new bar" and opens a position on the current bar. I cannot understand why. Thank you.

 
novichok2018:

Hello, dear friends. Another question from an μl4 newbie. I use the Fun_New_Bar() function recommended in the tutorial to define a new bar:

However, it somehow does not always work. I put in a print check:

So, it opens a position on a new bar, then prints "No new bar" and opens a position on the current bar. I cannot understand why. Thank you.

How do you distinguish between New bar and Current bar? How often is this part of the code executed - every tick or only when a new bar is formed?

 
STARIJ:

how do you distinguish between New bar and Current bar? How often is this part of the code executed - every tick or only when a new bar is formed?

New bar - by function. The Current bar - in which a position has been closed, but the bar time has not expired. The code must be executed at the moment of anew bar forming.Or rather, not so. The part of the code where the conditions for closing the position are defined is executed every tick, while those for opening the position are executed in a new bar.

 
novichok2018:

New bar - by function. Current - where the position has been closed but the bar time has not expired. The code must be executed at the moment when thenew bar is formed.Or rather, it is not. The part of the code where the conditions for closing the position are defined is executed every tick, while the condition for opening the position is executed in a new bar.

So, everything is clear here. But when you are writing and see that you should write it more precisely, move the cursor back and correct. Otherwise it is hard to read it this way and that way.

The code is executed with every tick. I.e. the question "Is there a new bar? Is there a new bar? Is there a new bar? and each time a message is printed that there is no new bar. And then there is a new bar. The order opens? First a command is sent to the server. And then the ticks come in and there's a new bar? There is a new bar and every time there is no new bar, the server finally opens an order and you look at the time and wonder what time it is. You can use Print(TimeCurrent()) before the opening of an order, 4 lines below the first one. Or better yet, use Alert - right on the screen. You can see at a glance.

One more thing: instead of if(New_Bar==false) if(New_Bar==true), you may use if(!New_Bar) if(New_Bar) otherwise you get buttery oil

 
STARIJ:

In short, everything is clear here. But when you write and see that you need to write more precisely, bring the cursor back and correct it. It's hard to read it this way and that way.

The code is executed with every tick. I.e. the question "Is there a new bar? Is there a new bar? Is there a new bar? and each time a message is printed that there is no new bar. And then there is a new bar. The order opens? First a command is sent to the server. And then the ticks come in and there's a new bar? There is a new bar and every time there is no new bar, the server finally opens an order and you look at the time and wonder what time it is. You can use Print(TimeCurrent()) before the opening of an order, 4 lines below the first one. Or better yet, use Alert - right on the screen. You can see at a glance.

And one more thing: instead of if(New_Bar==false) if(New_Bar==true) you may use if(!New_Bar) if(New_Bar) otherwise you will get oil

Ok, everything is clear with the message about absence of a new bar. The time of order opening displayed in the journal by the platform is sufficient for me even without an additional print. However, I still do not understand why an order is opened on a new bar once and on the current one the second time, although according to the code, a new bar should appear before looking for conditions to open the position.

 
novichok2018:

OK, everything is clear with the message about the lack of a new bar. The time of order opening printed in the log by the platform is sufficient for me even without the additional print. However, I still do not understand why the order opens on a new bar once and on the current one the second time, although according to the code, a new bar should appear before looking for conditions to open the position.

First, a command is sent to the server. In the meantime, the ticks come and Is there a new bar? There is a new bar and each time there is no new bar, the server finally opens an order and you look at the time and wonder. You can use Print(TimeCurrent( )) before the opening of an order, 4 lines below the first one. Or better yet, use Alert - straight to the screen. You can see at a glance... the command to open the order was sent earlier, on the new bar

 

Can you tell me how to make it possible to have a shading colour between the 2 indicator lines?

As in ishimoku

 
Roman Sharanov:

Can you tell me how to make it possible to have a shading colour between the 2 lines of the indicator? As in the ishimoku

Take a look at how it's donethere... And the terminal probably has...

 

Hello, I have started to study MQL4 using the 2013 video tutorials, I have been rewriting the code exactly from the video, adding some minor changes from another one, as I need them. As a result I get 25 errors. I have tried to look for solution to these errors, but since build of terminal has changed and some changes in coding have occurred. I will be grateful if someone can help me to edit the code or at least give me some useful advice, thanks in advance.

//+------------------------------------------------------------------+
//|                                                           ea.mq4 |
//|                                                             mql4 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Intruder"
#property link      ""
#property version   "1.00"
#property strict

extern double Lots         = 0.1;
extern int    TakeProfit   = 50;
extern int    Step         = 50;
extern double Multiplier   = 2;
extern int    Slippage     = 5;
extern int    Magic        = 123;

extern int    MA_1_Period  = 21;
extern int    MA_1_Shift   = 0;

extern int    MA_2_Period  = 3;
extern int    MA_2_Shift   = 0;


int ticket;
double price, TP, lastlot;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int Init()
{
   if (Digits == 3 || Digits == 5)
   {
       TakeProfit *= 10;
       Step       *= 10;
       Slippage   *= 10;  
   }
   return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert start function                                             |
//+------------------------------------------------------------------+
int start()
{
   if (CountTrades() == 0)
   {
      double ima_1 = iMA(Symbol(), PERIOD_CURRENT, MA_1_Period, MA_1_Shift, MODE_SMA, PRICE_CLOSE, 1);
      double ima_2 = iMA(Symbol(), PERIOD_CURRENT, MA_2_Period, MA_2_Shift, MODE_SMA, PRICE_CLOSE, 1);
      
      if (ima_1 > ima_2)
      {
         ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "", Magic, 0, Blue);
         if(ticket > 0)
         {
            TP = NormalizeDouble(Ask + TakeProfit * Point, Digits);
            OrderModify(ticket, OrderOpenPrice(), 0, TP, 0);
         }
      }
      else if(ima_1 < ima_2)
      {
         ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "", Magic, 0, Red);
         if(ticket > 0)
         {
            TP = NormalizeDouble(Bid - TakeProfit * Point, Digits);
            OrderModify(ticket, OrderOpenPrice(), 0, TP, 0);
         }
      }
      
   }
   else
   {
      int order_type = FindLastType();
      if (order_type == OP_BUY)
      {
         price = FindLastOrderPrice(OP_BUY);
         if(Ask <= price - Step * Point)
         {
            Lastlot = FindLastLots (OP_BUY);
            lastlot = NormalizeDouble(lastlot * Multiplier, 2);
            ticket = OrderSend(Symbol(), OP_BUY, lastlot, Ask, Slippage, 0, 0, "", Magic, 0, Blue);
            if (ticket > 0)
               ModifiOrders(OP_BUY);
         }
      }
      else if (order_type == OP_SELL)
      {
          price = FindLastOrderPrice(OP_SELL);
         if(Bid <= price + Step * Point)
         {
            Lastlot = FindLastLots (OP_SELL);
            lastlot = NormalizeDouble(lastlot * Multiplier, 2);
            ticket = OrderSend(Symbol(), OP_BUY, lastlot, Bid, Slippage, 0, 0, "", Magic, 0, Red);
            if (ticket > 0)
               ModifiOrders(OP_SELL);
         }        
      }
   }
   
   return(0);
}
//+------------------------------------------------------------------+
void ModifiOrders(int otype)
{
   double avgprice = 0,
          order_lots= 0;
   
   price = 0;
   for (int i=OrdersTotal()-1; i>=0; i--)
   {
      if (OrderSelecti(i, SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
         {
            price += OrderOpenPrice() * OrderLots();
            order_lots += OrderLots();
         }
      }
   }
   avgprice = NormalizeDouble(price / order_lots, Digits);
   
   if (otype == OP_BUY) TP = NormalizeDouble(avgprice + TakeProfit*Point, Digits);
   if (otype == OP_SELL) TP = NormalizeDouble(avgprice - TakeProfit*Point, Digits);
   
   for (i=OrdersTotal()-1; i>=0; i--)
   {
      if (OrderSelect(i SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
            OrderModify(OrderTicket(), OrderOpenPrice(), 0, TP, 0)    
      } 
   }
}
//+------------------------------------------------------------------+
double FindLastLots(int otype)
{
   double oldlots;
   int oldticket;
   
   ticket = 0;
   for(int i = OrdersTotal ()-1; i>=0; i--)
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() ++ otype)
         {
            oldticket = OrderTicket();
            if(oldticket > ticket)
            {
               oldlots = OrderLots();
               ticket = oldticket;
            }
         }
      }
   }
   return(oldlots);
}
//+------------------------------------------------------------------+
double FindLastOrderPrice(int otype)
{
   double oldopenprice;
   int    oldticket;
   
   ticket = 0;
   
   for(int i=OrdersTotal()-1; i>=0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype
         {
            oldticket = OrderTicket();
            if(oldticket > ticket)
            {
               oldopenprice = OrderOpenPrice();
               ticket = oldticket;
            }
         }
      }
   }
   return(oldopenprice);
}
//+------------------------------------------------------------------+
int FindLastOrderType()
{
   for (int i= OrdersTotal()-1; i>=0; i--)
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderSymbol() == Symbol() && OrderMagicNumber () == Magic)
              return(OrderType());
      }
   }
   return(-1);   
}
//+------------------------------------------------------------------+
int CountTrades()
{
   int count = 0;
   for (int i=OrdersTotal()-1 i>=0; i--)
   {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
               count++;
        }
   }
   
   return(count);

//-------------------------------------------------------------------+

Errors