Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1402

 

Hi there,

I am trying to make a very simple EA that compares two closing prices and based on that it either opens a buy or a sell order.

All goes well on the first order, but as soon as the condition switches from sell to buy, it places multiple orders at once. 

m_trade is a Ctrade object.

// Current position information

bool Buy_openend = false;
bool Sell_openend = false;

double currentVolume = 0;

if(PositionSelect(_Symbol) == true)
  {
   if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
     {
      Buy_openend = true;
     }
      if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
     {
      Sell_openend = true;
      }   
        currentVolume = PositionGetDouble(POSITION_VOLUME);   
  } 

// Check for trade conditions

bool SellCondition = close <= closeprev && bar[1].time == startTime;
bool BuyCondition = close >= closeprev && bar[1].time == startTime;


// Entering trade

if(SellCondition)
  {
   if(Sell_openend)
   {
   Print("We already have a sell open");
   return;
   }
   m_trade.Sell(TradeVolume+currentVolume,_Symbol,m_symbol.Ask(),NULL,NULL,NULL);
  }
if(BuyCondition)
  {
   if(Buy_openend)
     {
      Print("We already have a buy open");
   return;
     }
     m_trade.Buy(TradeVolume+currentVolume,_Symbol,m_symbol.Bid(),NULL,NULL,NULL);
  }
 
Divania111 # :

Hi there,

I am trying to make a very simple EA that compares two closing prices and based on that it either opens a buy or a sell order.

All goes well on the first order, but as soon as the condition switches from sell to buy, it places multiple orders at once. 

m_trade is a Ctrade object .

You forgot that the 'startTime' time needs to be reset.

 
Vladimir Karputov #:

You forgot that the 'startTime' time needs to be reset.

Thank you Vladimir, I will try to fix it based on your hint.

Yet, I am still suprised. Because in the if statement for entering a trade, we check for an existing order placement of the same type. If this is true, then the order should not be placed. So I thought with this statement only 1 type of order can be entered at the same time.

Is this thought incorrect? 

 
Divania111 # :

Thank you Vladimir, I will try to fix it based on your hint.

Yet, I am still suprised. Because in the if statement for entering a trade, we check for an existing order placement of the same type. If this is true, then the order should not be placed. So I thought with this statement only 1 type of order can be entered at the same time.

Is this thought incorrect? 

You do not control the ORDERS in any way, you check the number and type of the POSITION in the code. Besides - you incorrectly work with POSITIONS (error to use 'PositionSelect(_Symbol)' if your account type is hedging). Your algorithm has serious design errors.

What You Should Do:

1. Work EXCLUSIVELY at the time of the birth of a new bar. If there is no new bar - do nothing, do not make unnecessary movements.

2. This item will be disclosed after item 1 is completed.

 
Hello. Can you tell me if the Comment in void OnTick() is slowing down the EA. Put it in int OnInit() works much faster.
 
Marco Nicholas the comment in void OnTick() is slowing down the EA. Put it in int OnInit() works much faster.

Yes, in MT5 it slows down the operation considerably.

In OnInit it only works once during initialisation and no more.

 
Marco Nicholas #: Hello. Can you tell me if the Comment in void OnTick() is slowing down the EA. Put it in int OnInit() works much faster.

Are you talking about the Comment() function?

If yes, then that is obvious. In the OnInit(), the comment will only update once, while on OnTick() the comment might end up being updated on every tick.

The solution is to only update the comment when needed. If it is static, then updated in only on OnInit() and clear it it in OnDeinit(), but if it is dynamic, then in OnTick() only update the comment when it needs to be changed.

EDIT: This post was originally done on the English forum.

 
Hello teachers, when I use the client-side SocketConnect() function, I can't establish a connection with the server even after filling in the parameters correctly according to the user instructions, but the server is normally available after testing.
 

Good day to all.

I have a question

how to make a query to sqlite to select a row from a ticket.

The structure of the database is as follows:

symbol-text

ticket - text

pirce- real

s_l - real

t_p - real

need to get the whole ticket string into the structure

 
Hello, can you use Comment() in switch() instead of the same Print.