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

 
Alekseu Fedotov #:

No, that's not right.


With OrderModify, that's more correct.

And better as a separate block.

Ok, thank you!

 
Alexander Avksentyev #:

All there is. Thank you for your help.

#property strict
//--- input parameters
input int      Magic            =12345;
input int      iTakeProfit      =10;
input int      iTrailStart      =10;
input int      iTrailDist       =10;
input int      iTrailStep       =10;
input int      iTrailTakeProfit =10;
input int      iStep            =10;
input int      iSlippage        =5;
input double   Lot              =0.01;

int      Login = 4950287;
datetime time  = D'2022.01.01 00:00:00';
string   name  = "AutoP_v.3.0";
datetime time1 = 0;
double   price, TP, lastlot, minlot;
int      Forder;
int      TakeProfit,TrailStart,TrailDist,TrailStep,TrailTakeProfit,Step,Slippage;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
     if(AccountNumber() != Login)
       {
        int close = MessageBox("Неверный номер счёта","Ошибка",MB_OK|MB_ICONSTOP);
        return(INIT_FAILED);
       }else (AccountNumber() == Login);
             {
              int close = MessageBox("Проверка прошла успешно.Экcперт может приступить к работе.","Инициализация",MB_OK|MB_ICONEXCLAMATION);
             }
 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//  
     if (TimeCurrent() > time)
        {
         int close = MessageBox("Время работы закончилось!!!","Время работы",MB_OK|MB_ICONSTOP);
         return(INIT_FAILED);
        }else(TimeCurrent() < time);
             {
              int close = MessageBox("Проверка времени работы прошла успешно.Эксперт может приступить к работе!","Время работы",MB_OK|MB_ICONEXCLAMATION);
             }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
      TakeProfit     =iTakeProfit;
      TrailStart     =iTrailStart;
      TrailDist      =iTrailDist;
      TrailStep      =iTrailStep;
      TrailTakeProfit=iTrailTakeProfit;
      Step           =iStep;
      Slippage       =iSlippage;
     if(Digits == 3 || Digits == 5)
       {
        TakeProfit      *=10;
        TrailStart      *=10;
        TrailDist       *=10;
        TrailStep       *=10;
        TrailTakeProfit *=10;
        Step            *=10;
        Slippage        *=10;
       }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if (CountTrades() == 0)
      {
      double body = Close[1] - Open[1];
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
//Открытие позиции Buy
      if (body > 0)
         {
         Forder = (OrderSend(_Symbol,OP_BUY,Lot,Ask,Slippage,0,0,"Коммент",Magic,0,Blue));
         if(Forder > 0)
           {
            Print("Открыта Позиция BUY");
            if(OrderSelect(Forder, SELECT_BY_TICKET))
               {
               TP = NormalizeDouble((Ask+TakeProfit*_Point),_Digits);
               if (OrderModify(OrderTicket(),OrderOpenPrice(),0,TP,0))
                  Print("Ордер Модифицирован BUY "); 
               else 
                  Print("Ошибка Модификации Ордера BUY = ",GetLastError());
               }
           }
         else
            Print("Ошибка Открытия Позиции BUY");
       }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
//Открытие позиции Sell      
      if (body < 0)
         {
         Forder = (OrderSend(_Symbol,OP_SELL,Lot,Bid,Slippage,0,0,"Коммент Sell",Magic,0,Red));
         if (Forder > 0)
            {
            Print("Открыта Позиция SELL");
            if(OrderSelect(Forder, SELECT_BY_TICKET))
               {
               TP = NormalizeDouble((Bid-TakeProfit*_Point),_Digits);
               if (OrderModify(OrderTicket(),OrderOpenPrice(),0,TP,0))
                  Print("Ордер Модифицирован SELL "); 
               else 
                  Print("Ошибка Модификации Ордера SELL = ",GetLastError());
               }
            }
         else
            Print("Ошибка Открытия Позиции BUY");
        }
      }
//---
  }
//+------------------------------------------------------------------+
//| Подсчет открытых ордеров                                         |
//+------------------------------------------------------------------+
int CountTrades() 
  {
   int count =0;
   int i=OrdersTotal()-1;
   for(int pos=i;pos>=0;pos--)
     {
      if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol()==_Symbol)
           {
            if(OrderMagicNumber()==Magic) count ++;
           }
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
 
MakarFX #:

Thank you

 
MakarFX #:

Only he started to open both buy and sell at once. I assumed that

int CountTrades() 
  {
   int count =0;
   int i=OrdersTotal()-1;
   for(int pos=i;pos>=0;pos--)
     {
      if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol()==_Symbol)
           {
            if(OrderMagicNumber()==Magic) count ++;
           }
        }
     }
   return(count);
  }

This part would check for an open position on magic and

if (CountTrades() == 0)
      {

This part will not allow to open both ways.

 
Alexander Avksentyev #:

Only he started to open both buy and sell at once. I assumed that

This part would check for an open position on magic and

This part will not allow to open both ways.

Unlikely, show the log or write when, on which pair and what time frame, so I can check.


It is.

This part will not allow to open if there are already open orders.

 
MakarFX #:

Unlikely, show the log or write when, on which pair and what time, so I can check.


It is.

This part doesn't let you open if there are already open orders.

I'm sorry.

I have already made these rules, and it has turned out that the order opens on every candle, in the direction of the previous one's close.

My mistake.

 
MakarFX #:

Unlikely, show the log or write when, on which pair and what time, so I can check.


It is.

This part does not allow to open if there are already open orders.

I have 10 different editors, I change something everywhere, I'm confused.

 
MakarFX #:

Unlikely, show the log or write when, on which pair and what time, so I can check.


It is.

This part does not allow to open if there are already open orders.

Another question.

How seriously should we pay attention to the warnings?

 
Alexander Avksentyev #:

Another question.

How seriously should you pay attention to the warnings?

Always. Many warnings are due to inattention.
 
MakarFX #:

Check the size of your terminal


P.S. it's not a fact that your "32 bit library" can work on 64 vin.

There is a setting for that in Visual Studio

It will. Yes there is a setting there, when you create a console application you select the 32 version of the program and it will run. The pointer in this case is 4 bytes.

Reason: