Questions from Beginners MQL4 MT4 MetaTrader 4 - page 18

 
Babu Bonappan:

What if I get the exact value of margin at the time of order opening usingMarketInfo(OrderSymbol(),MODE_MARGINREQUIRED)*Lot- it will always have two decimal places, right? Then I will multiply it by 100 and save it as MagicNumber of this order. And if necessary, I will take it out of there and divide it by 100.0.

Will this be correct?

Do you want to assign a magic number to each order?
 
Babu Bonappan:

OrderOpenPrice, as I understand it, gives me exactly what I need. But only if the deposit currency is USD and the traded pair is EUR/USD. In this case, it is as if the OrderOpenPrice stores the exchange rate of the base currency to the currency of deposit at the moment of opening the order knowing which you can easily calculate the deposit.

But if at least one of these conditions is not met, how can we obtain the value of the deposit for an individual order? Where can we find the rate of the base currency of a quote relative to the currency of the deposit at the moment of its opening?

Yes, we have the time of order opening to the nearest second. But what can we get? At most - the parameters of the minute candle of the required symbol. But never the exact value of the rate used for calculating the deposit. But the AccountMargin function gets it somehow! It would be very interesting to understand how exactly it does it.

OrderOpenPrice - the opening price of the order, it (the price) is the ratio of one currency to another

AccountMargin - gives the total margin of the account for all open orders.

 
Vladimir Karputov:

Postponed:

Vasiliy Danilov, 2016.12.02 07:18

Can you please tell me what to do? I almostwrote a simple Expert Advisor using an external indicator, there is one hitch.

Closing of half of the lot does not work correctly and the order is modified for every tick.

Here is the block of modification to buy

if (CountBuy()>0) //In this function, the number of buy orders is calculated
{ for (int i = OrdersTotal() -1; i>=0; i--)
{ if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{ if (OrderMagicNumber()==Magic && OrderType()==OP_BUY && (Ask-OrderOpenPrice())*10000>MinMove) //if price has passed the required move from the indicator
SL=NormalizeDouble(Ask-(MinMove*10)*Point,Digits); //here I change Stop to Breakeven
if(!OrderClose(Ticket,Lots/2,Ask,Slippage,Black)) //I try to close half of the lot
Print("Error of closing half of the lot to buy");
if(!OrderModify(Ticket,OrderOpenPrice(),SL,TP,0)) //here I move the remaining part to Breakeven
Print("Error of modification to breakeven on purchase");

} } }


If SL != OrderStopLoss()) modify .... Then the order will be modified only if SL differs from the current OrderStopLoss.

And to understand what goes wrong at closing, use GetLastError(); in general, it would be good to check OrderClose ... bool testOrCls

testOrCls=OrderClose ......

if(!testOrCls) Print (GetLastError() );

i.e. if there is an error, we ask for the error code; if there is no error, we don't ask for the error code.


 
bablusut:

Thanks for the reply ... I searched through half of the Internet, there are very few examples of using theStringFind function, and from what I found I concluded that the parameters must be:

intStringFind(

stringcomment =OrderComment()// the string in which we are looking for
stringOrderStopLoss, OrderTakeProfit//what we are looking for
intstart_pos=0// from which position to start searching

);

... If I am wrong, please correct me ...

It does not work that way a bit. The arguments of this function are 1) the string you want to search; 2) the character combination you want to find; 3) The start of the search (by default, from the null character of the string according to step 1).

It returns the number of position in the string, at which the sought substring starts, or -1 if no substring is found.

In other words, write it like this:

if(StringFind(OrderComment(),"[tp]",0)>-1) {действие при нахождении признака закрытия по TP}
 
Renat Akhtyamov:

You have an error closing half of the lot, so it's not modifiable. Please correct it according to my post above.

If you want to do that only once, you should specify breakeven by a fixed number of points and add the condition of check of take profit of the order on the fact of its correspondence with this number

And when passing through such a condition in the order modification block, the half is closed.

Vasiliy Danilov:
Can you please tell me what to do? I have almostwritten a simple Expert Advisor using an external indicator but there is one hitch.

If there is OrederClose in the block, half of the lot will be closed immediately and OrderModify will not work any further.

If we remove OrederClose, then OrderModify modifies the order for each tick

Here is the block of modification to buy

   if (CountBuy()>0) //В этой функции считается кол-во ордеров на покупку
   { for (int i = OrdersTotal() -1; i>=0; i--)
     { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {  if (OrderMagicNumber()==Magic && OrderType()==OP_BUY && (Ask-OrderOpenPrice())*10000>MinMove) //Если цена прошла необходимое движение из индикатора
      SL=NormalizeDouble(Ask-(MinMove*10)*Point,Digits); //Тут меняю стоп на безубыток
       if(!OrderClose(Ticket,Lots/2,Ask,Slippage,Black)) //Пытаюсь закрыть половину лота
       Print("Ошибка закрытия половины лота на покупку");
      if(!OrderModify(Ticket,OrderOpenPrice(),SL,TP,0)) //Тут переставляю оставшуюся часть в безубыток
       Print("Ошибка модификации в безубыток на покупку");

   }    }   }
How to close half of the order when the price reaches MinMove, and the other half goes to Breakeven once?

By the way, I have just noticed that OrderSelect by position SELECT_BY_POS, but where is ticket selection?

OrderClose(OrderTicket()

 
Babu Bonappan:

Please advise how to use MQL4 to get a margin value for each open position in the terminal?

I used to do it this way:

margin = MarketInfo(Symbol(),MODE_LOTSIZE) * OrderOpenPrice() / AccountLeverage() * OrderLots();

When trading EUR/USD this construction worked fine and I was sure that its logic was correct.

But now I want to get the same result for EUR/JPY (or EUR/CHF). Obviously, instead ofOrderOpenPrice() I need to multiply the value of a standard lot by the rate of the base currency to the deposit currency (in my case, by EUR/USD). But what is this rate? The exchange rate that was at the time of position opening or the one that we have now (at the time when we want to know the amount of deposit for this position)?

No MODE_MARGINMAINTENANCE?

 
A1exPit:

By the way, I just noticed OrderSelect on SELECT_BY_POS position, but where is the selection ticket?

OrderClose(OrderTicket()

Don't you know anything at all in this thread?

If the order is selected, then the OrderTicket() returns the ticket of the selected order. And it doesn't matter how the order is selected - by index or by ticket.

There is a subtlety in the case of selection by ticket - pool is not taken into account, and we have to check from which list the order is selected, checking the time of its closing.

 
Vasiliy Danilov:
Could you please give me some pointers on how to understand this? I have almostwritten a simple Expert Advisor using an external indicator but I have hit upon one problem.

If there is an OrederClose in the block, half of the lot is closed immediately and OrderModify no longer works.

If we remove OrederClose, OrderModify modifies the order for each tick

Here is the modification block for buying

   if (CountBuy()>0) //В этой функции считается кол-во ордеров на покупку
   { for (int i = OrdersTotal() -1; i>=0; i--)
     { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {  if (OrderMagicNumber()==Magic && OrderType()==OP_BUY && (Ask-OrderOpenPrice())*10000>MinMove) //Если цена прошла необходимое движение из индикатора
      SL=NormalizeDouble(Ask-(MinMove*10)*Point,Digits); //Тут меняю стоп на безубыток
       if(!OrderClose(Ticket,Lots/2,Ask,Slippage,Black)) //Пытаюсь закрыть половину лота
       Print("Ошибка закрытия половины лота на покупку");
      if(!OrderModify(Ticket,OrderOpenPrice(),SL,TP,0)) //Тут переставляю оставшуюся часть в безубыток
       Print("Ошибка модификации в безубыток на покупку");

   }    }   }
How should I close one half of the order when the price reaches the MinMove level and the other half goes to Breakeven once?

When partial closing the ticket changes. First to breakeven, then to close.

Either change the logic.

 
Artyom Trishkin:

A1exPit:

By the way, I just noticed OrderSelect by SELECT_BY_POS , but where is the selection ticket?

OrderClose(OrderTicket()

Don't you know anything at all in this thread?

If an order is selected, then the OrderTicket() returns the ticket of the selected order. And it does not matter how the order is selected - by index or by ticket.

There is a nuance when selecting by ticket - pool is not taken into account, and you need to check from which list the order is selected by checking the time it closed.

And if we look at the code to which this comment was written? It's not so straightforward there...

{ if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {  if (OrderMagicNumber()==Magic && OrderType()==OP_BUY && (Ask-OrderOpenPrice())*10000>MinMove) //Если цена прошла необходимое движение из индикатора
      SL=NormalizeDouble(Ask-(MinMove*10)*Point,Digits); //Тут меняю стоп на безубыток
       if(!OrderClose(Ticket,Lots/2,Ask,Slippage,Black)) //Пытаюсь закрыть половину лота Какой тикет?
 
Vitalie Postolache:

And if you look at the code to which this comment was written? It's not so clear-cut there...

{ if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {  if (OrderMagicNumber()==Magic && OrderType()==OP_BUY && (Ask-OrderOpenPrice())*10000>MinMove) //Если цена прошла необходимое движение из индикатора
      SL=NormalizeDouble(Ask-(MinMove*10)*Point,Digits); //Тут меняю стоп на безубыток
       if(!OrderClose(Ticket,Lots/2,Ask,Slippage,Black)) //Пытаюсь закрыть половину лота Какой тикет?
Well... yeah... I didn't look closely. Just a glimpse. I'm not interested in this thread for some reason
Reason: