Who can help with the robot, why isn't it working? - page 4

 
GIM:

for(int i=0;i<OrdersTotal();i++)

Error: "i".

What is the error ?

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

input double  lots       = 0.01;   // Лот
input int     stop_loss      = 10; // Указываем в валюте депозита
input int     take_profit    = 20; // Указываем в валюте депозита
extern int    Slippage = 3;        // Допустимое проскальзываение цены в пунктах
input int     Magic = 16384;       // Уникальный номер эксперта

int            last_bar       = 0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
// ===== Пересчет под пятизнак =========
   if(Digits()==3 || Digits()==5)
     {
      Slippage           *= 10;
     }   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if (last_bar == Bars) {return;}
   last_bar = Bars;
   if (OrdersTotal() == 0){
          int ticketbuy = OrderSend(Symbol(), OP_BUY, lots ,Ask, Slippage, 0, 0,  "", Magic, 0, clrBlue);
            if(ticketbuy<0)
               Print(Symbol()," OpenPosition. OrderSend Buy fail #",GetLastError());
            else
               Print(Symbol()," OpenPosition. OrderSend Buy successfully");
          int ticketsell = OrderSend(Symbol(), OP_SELL, lots ,Bid, Slippage, 0, 0,  "", Magic, 0, clrRed);
            if(ticketsell<0)
               Print(Symbol()," OpenPosition. OrderSend Sell fail #",GetLastError());
            else
               Print(Symbol()," OpenPosition. OrderSend Sell successfully");      
}
 double profit=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==_Symbol && OrderMagicNumber()== Magic)
        {
         profit+=OrderProfit()+OrderSwap()+OrderCommission();
        }
     }
 int requot=0;
 if(profit>=take_profit || (-profit)>=stop_loss)
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==_Symbol && OrderMagicNumber()== Magic)
        {
         if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,clrRed))
               requot=0;
            else
              {
               requot++;
              }
         if(requot>10)
              {
               i++;
               requot=0;
              }
            i--;
        }
     }
 
  }
//+------------------------------------------------------------------+

 
GIM:
What do you mean, only with own orders....
If you place several copies of the EA on one account so that it does not confuse which EA opened the order, the identification number is used.
 
Could it be the number of trades or currency pairs?
 
GIM:
What do you mean, only with own orders....
If the EA will only work with own orders in the account, this is the correct way to write it. If there are several strategies and experts, they should not confuse their own orders with other people's orders.
 
Sergey Gritsay:
If you have several copies of the EA in one account, it does not confuse which EA opened the order and uses a unique identification number.
Everything is clear, from the heart!
 
Vladimir Zubov:

What is the mistake ?

The error here is that the variable i is initialized 2 times in the first for loop and in the second for loop, either the second initialization should be removed, or the variable name should be changed.
 
Thank you all!
 
Sergey Gritsay:
The error here is that the variable i is initialized twice in the first for loop and in the second for loop, either the second initialization should be removed, or the variable name should be changed.
That's right, I thought his compiler is fighting.
 
Vladimir Zubov:
That's right, I thought his compiler was fighting.
The compiler probably does, but it should give a warning, not an error.
 

and no, if you take it out.

#property strict

it says it's an error.

Reason: