Questions from Beginners MQL4 MT4 MetaTrader 4 - page 17

 
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! I would like very much to understand how exactly it does it.

You need three formulas to calculate the margin.

Depending on the currency of your deposit, you will calculate the margin for any currency pair.

It is a fact that there may be an error in the calculation. But it will be much smaller than the one that will appear when rounding the result of the calculation to one hundredth of a unit of the base currency of the deposit, i.e. if it is a dollar, then I mean cents.

PS

AccountMargin is the current, i.e. last value

 

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?

 
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?

Again, that's not what the MagicNumber is for. It would be better to write it into a log-file and then read it.

It all depends on the purpose of such calculation and the number of orders the program is designed for.

It seems to me that such precise calculations are necessary only for a broker or a brokerage company.

From this point of view, the optimal solution would be logging.

 
Vitaly Muzichenko:

Why are you lying about the number of signs? Don't mislead people.

Ha.

Have you never heard of high-precision counting?

 

Postponed:

wrote a simple Expert Advisor using an external indicator, but there is one hitch.

Closing half lot does not work correctly and order is modified on every tick.

Here is the block of modification to buy

if (CountBuy()>0) //This function counts the number of buy orders
{ 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 modifying to breakeven on purchase");

} } }


 
Vladimir Karputov:

Reposted:

Vasiliy Danilov, 2016.12.02 07:18

For starters, try using NormalizeDouble(OrderLot()/2,2) instead of Lots/2

And paste the code into the post via the "SRC" button to make it readable

 
Can you please give me some tips on how to deal with this issue. I have almostwritten a simple Expert Advisor using an external indicator but I have hit upon one hitch.

If the block has OrederClose - 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 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 should I close one half of the order when the price reaches the MinMove level and the other half goes to breakeven once?
 
Vasiliy Danilov:
Can you please give me some tips on how to deal with this? I have almostwritten a simple Expert Advisor using an external indicator but I have hit upon one problem.

If you have OrederClose in the block, it immediately closes half of the lot and OrderModify no longer works.

If you remove OrederClose, OrderModify will modify the order at every 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 should I close one half of the order and make the other half Breakeven once if the price reaches MinMove?

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

And to do it once, you have to set breakeven to a fixed number of points and add the condition of check of take profit of the order to the fact of compliance with this number

And when the price passes through such a condition in the order modification block, half of the order should be closed

 
Alexey Kozitsyn:
No. Try to understand what you're doing. What you've written is just a prototype of the function (i.e. just a description of what it does). So you just ripped it out of the documentation. You need to use it. So you have to substitute your own values for arguments. And the function will return the result. Then this result must be processed.

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 where we are looking for
stringOrderStopLoss, OrderTakeProfit//what we are looking for
intstart_pos=0// at which position to start search

);

... If I'm wrong, correct me ...

 
Vitaly Muzichenko:

Why are you lying about the number of signs? Don't mislead people.

Every broker's point is different
Reason: