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

 

Good afternoon. I'm learning to write code and I have a problem. The code in the tester runs, opens one trade and that's it. The price does not change, everything stops in place.

Who is not difficult, please advise.

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));
        {
         Print("Открыта Позиция BUY");
              {
               if(Forder > 0)
                 {
                  TP = NormalizeDouble((Ask+TakeProfit*_Point+OrderSwap()+OrderCommission()),_Digits);
                  Forder = OrderModify(Forder,OrderOpenPrice(),0,TP,0);
                 }Print("Ордер Модифицирован BUY ");
              }Print("Ошибка Модификации Ордера BUY");
        }
       }Print("Ошибка Открытия Позиции BUY");
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
//Открытие позиции Sell      
    if (body < 0)
       {
        Forder = (OrderSend(_Symbol,OP_SELL,Lot,Bid,Slippage,0,0,"Коммент Sell",Magic,0,Red));
        {
         Print("Открыта Позиция SELL");
              {
               if(Forder > 0)
                 {
                  TP = NormalizeDouble((Bid-TakeProfit*_Point+OrderSwap()+OrderCommission()),_Digits);
                  Forder = OrderModify(Forder,OrderOpenPrice(),0,TP,0);
                 }Print("Ордер Модифицирован SELL ");
              }Print("Ошибка Модификации Ордера SELL");
        }
       }Print("Ошибка Открытия Позиции Sell");
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
   }
 
Alexander Avksentyev #:

Good afternoon. I'm learning to write code and I have a problem. The code in the tester runs, opens one trade and that's it. The price does not change, everything stops in place.

Who doesn't mind, please advise.

Ask+TakeProfit*_Point+OrderSwap()+OrderCommission()

How do you add swap and commission to the price?

 
MakarFX #:

How do you add swap and commission to the price?

How do I do it?)

(((Ask+TakeProfit*_Point+OrderSwap()+OrderCommission()*_Point))

Do you think this is a problem?

 
How do I make Comment or Print in the log in the OnTimer function work in the tester during visualisation?
 
Alexander Avksentyev #:

How I know how to count.)

Do you think that's the problem?

First, when you open a swap, there is no swap.

Second, let's assume the commission of 1$ you open an order on the pound at 1.32500

you add (TakeProfit) 200*_Point and (commission) 1$ and you end up with a Take of 2.32700 ... In what decade would that be?

 
Ivan Butko Comment or print in the log in the OnTimer function work in the tester when rendering?
Add onTimer to onTick
 
MakarFX #:
Add onTimer to onTick

Thank you

 
Alexander Avksentyev #:

How I know how to count.)

Do you think that's the problem?

No, it's not right.


With OrderModify, that's more correct.

And better as a separate block.

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());
  }
 
Alexander Avksentyev #:

How I know how to count.)

Do you think that's the problem?

I don't think that's the problem, it's just not right. And as for the tester freezing, you'd better post the whole code and we'll see.

OrderCommission()*_Point

That's not right either.

_Points are decimal places and do not translate to points in any way
 
MakarFX #:

I don't think that's the problem, it's just not right. And as for the tester freezing, you'd better post all the code and see.

That's not right either.

_Point is decimal places and does not translate to points in any way
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;

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//


//+------------------------------------------------------------------+
//| 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//
     if(Digits == 3 || Digits == 5)
       {
        TakeProfit      *=10;
        TrailStart      *=10;
        TrailDist       *=10;
        TrailStep       *=10;
        TrailTakeProfit *=10;
        Step            *=10;
        Slippage        *=10;
       }
       return(INIT_SUCCEEDED);
    }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    double body = Close[1] - Open[1];
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
//Открытие позиции Buy
    if (body > 0)
       {
        Forder = (OrderSend(_Symbol,OP_BUY,Lot,Ask,Slippage,0,0,"Коммент",Magic,0,Blue));
        {
         Print("Открыта Позиция BUY");
              {
               if(Forder > 0)
                  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());
                     }
               }
         }Print("Ошибка Открытия Позиции BUY");
       }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
//Открытие позиции Sell      
    if (body < 0)
       {
        Forder = (OrderSend(_Symbol,OP_SELL,Lot,Bid,Slippage,0,0,"Коммент Sell",Magic,0,Red));
        {
         Print("Открыта Позиция SELL");
              if (Forder > 0)
                 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());
                   }
         }
        }Print("Ошибка Открытия Позиции BUY");}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
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);
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//


It's all there. Thanks for your help.

Reason: