Good afternoon!!! Please tell me on the example code how to close an order in mql5???
For example, we have 7 open orders. Of these, 3 are for sale.
How to select from the total mass of these 3 orders and close them?
It is strange, but there is little information on this topic in the Internet… sooo little…
Thank you very much for your help!!!
Tell me PLIZ how to close an order on mql5?????
Share
-aleks-
Good advice. I just got used to discretion, so I asked if anyone had already dealt with these “rakes”.
fxsaber
Try the method of trial and error. I learned 99% of the information this way.
-aleks-
I conceived a system for changing the lot size depending on the growing fin result – not a certain number of traded positions is taken into account, it is necessary to identify the beginning of the calculation cycle – this requires a label, I can’t think of anything better than a comment.
fxsaber
comments from the order go to the deal?
No.
barabashkakvn
What’s the comment? What is it for? Do you want to catch a Stop loss or Take profit trigger?
-aleks-
Thanks! But, here’s how to understand that the transaction was related to a certain position?
By the way, do comments from the order go to the deal?
fxsaber
Forum on trading, automated trading systems and testing trading strategies
History Profit in MQL5 ?
fxsaber, 2017.08.26 19:16
double Profit( void )
{
double Res = 0;
if (HistorySelect(0, INT_MAX))
for (int i = HistoryDealsTotal() - 1; i >= 0; i--)
{
const ulong Ticket = HistoryDealGetTicket(i);
if((HistoryDealGetInteger(Ticket, DEAL_MAGIC) == MagicNumber) && (HistoryDealGetString(Ticket, DEAL_SYMBOL) == Symbol()))
Res += HistoryDealGetDouble(Ticket, DEAL_PROFIT);
}
return(Res);
}
#include //
double Profit( void )
{
double Res = 0;
for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderMagicNumber() == MagicNumber) && (OrderSymbol() == Symbol()))
Res += OrderProfit();
return(Res);
}
-aleks-
Please help! How to sort out the financial result of closed positions?
barabashkakvn
Example from the Autotrade Expert Advisor:
Connecting standard library trade classes and declaring objects of these trade classes
//+------------------------------------------------------------------+
//| Autotrade(barabashkakvn's edition).mq5 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, national :-)"
#property link "[email protected]"
#include
#include
#include
#include
CPositionInfo m_position; // trade position object
CTrade m_trade; // trading object
CSymbolInfo m_symbol; // symbol info object
COrderInfo m_order; // pending orders object
A function that implements the closing of all POSITIONS (not orders, but positions)
//+------------------------------------------------------------------+
/ / / Closing all positions by the current symbol and with the current Magic |
//+------------------------------------------------------------------+
void CloseAllPositions()
{
Print(__FUNCTION__);
for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
if(m_position.SelectByIndex(i))
if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
m_trade.PositionClose(m_position.Ticket());
return;
}
ds
alexhabba
please tell me how to close all open positions
master-mql4
And how to sort through all the positions? In mql4 it would be something like:
for(int i=0;i
{
OrderSelect(i,SELECT_BY_POS);
}
And in mql5 in the loop than to replace the function OrderSelect () …? Or positionselect is it?
barabashkakvn
Not an order, but a POSITION. The list of positions in the terminal can be obtained: PositionsTotal
And then go through the entire list of POSITIONS and, depending on the indicators, do anything with them. Getting position data can be seen in the positiongetinteger example:
//+------------------------------------------------------------------+
//| Trade function |
//+------------------------------------------------------------------+
void OnTrade()
{
/ / - - - check the position and print the time of its change
if(positionselect(_Symbol))
{
/ / - - - get the position ID for further work with it
ulong position_ID=PositionGetInteger(POSITION_IDENTIFIER);
Print(_Symbol," postion #",position_ID);
/ / - - - get the time of position formation in milliseconds from 01.01.1970
long create_time_msc=PositionGetInteger(POSITION_TIME_MSC);
PrintFormat("Position #%d POSITION_TIME_MSC = %i64 milliseconds => %s",position_ID,
create_time_msc,TimeToString(create_time_msc/1000));
/ / - - - get the time of the last position change in seconds from 01.01.1970
long update_time_sec=PositionGetInteger(POSITION_TIME_UPDATE);
PrintFormat("Position #%d POSITION_TIME_UPDATE = %i64 seconds => %s",
position_ID,update_time_sec,TimeToString(update_time_sec));
/ / - - - get the time of the last position change in milliseconds from 01.01.1970
long update_time_msc=PositionGetInteger(POSITION_TIME_UPDATE_MSC);
PrintFormat("Position #%d POSITION_TIME_UPDATE_MSC = %i64 milliseconds => %s",
position_ID,update_time_msc,TimeToString(update_time_msc/1000));
}
//---
}
You can close a position using the CTrade class: PositionClose.