" A position cannot be reversed in the hedging system. In this case, the current position is closed and a new one with the remaining volume is opened. "
Is there any way to disable it so that I can simultaneously hold open Buy & Sell positions on a currency pair?
I think you need to ask your question on the forum - here is a thread for those who ask questions about PROGRAMMING in MQL5, but you generally have zero knowledge. You manage to misinterpret the documentation. Please do not post any more in this thread - this thread is for those who program.
Example - there is a line (Horizontal line or Trend line) and you need to track when the price crosses the line.
To do this, you first need to get the price of the line - but first of all we check if there is an object with that name at all? If there is such an object, then, depending on the type of object, we get the price:
Objective: Several positions are open in the market. You need to get the number of positions (both BUY and SELL) for each symbol.
Suggested solution: the 'STRUCT_CALCULATE_POSITIONS' structure
//+------------------------------------------------------------------+//| Structure Calculate Positions |//+------------------------------------------------------------------+struct STRUCT_CALCULATE_POSITIONS
{
string symbol; // position symbolint count_buys; // count position BUYint count_sells; // count position SELL//--- Constructor
STRUCT_CALCULATE_POSITIONS()
{
symbol = "";
count_buys = 0;
count_sells = 0;
}
};
is created and this structure is filled in the 'CalculateAllPositions'
//+------------------------------------------------------------------+//| Calculate all positions Buy and Sell |//+------------------------------------------------------------------+void CalculateAllPositions(STRUCT_CALCULATE_POSITIONS &SCalculatePositions[])
{
ArrayFree(SCalculatePositions);
for(int i=PositionsTotal()-1; i>=0; i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
{
string pos_symbol=m_position.Symbol();
int size=ArraySize(SCalculatePositions);
int find=false;
for(int j=0; j<size; j++)
{
if(SCalculatePositions[j].symbol==pos_symbol)
{
if(m_position.PositionType()==POSITION_TYPE_BUY)
SCalculatePositions[j].count_buys=SCalculatePositions[j].count_buys+1;
if(m_position.PositionType()==POSITION_TYPE_SELL)
SCalculatePositions[j].count_sells=SCalculatePositions[j].count_sells+1;
find=true;
break;
}
}
if(!find)
{
ArrayResize(SCalculatePositions,size+1);
SCalculatePositions[size].symbol=pos_symbol;
if(m_position.PositionType()==POSITION_TYPE_BUY)
SCalculatePositions[size].count_buys=SCalculatePositions[size].count_buys+1;
if(m_position.PositionType()==POSITION_TYPE_SELL)
SCalculatePositions[size].count_sells=SCalculatePositions[size].count_sells+1;
}
}
//---return;
}
function.
For example, a test advisor has been created: first, it opens four positions and then (after calling 'CalculateAllPositions') unpacks the 'STRUCT_CALCULATE_POSITIONS' structure'
Another example of how to use the 'STRUCT_CALCULATE_POSITIONS' structure. The EA opens a position on a new bar. There is no Stop Loss or Take Profit. Nobody closes open positions - this is a demonstration of how to use and how to work with the structure 'STRUCT_CALCULATE_POSITIONS'.
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
STRUCT_CALCULATE_POSITIONS SCPos[];
CalculateAllPositions(SCPos);
int size=ArraySize(SCPos);
//--- Symbol 0if(m_symbol_0)
{
datetime time_0=iTime(InpSymbol_0,Period(),0);
if(time_0!=m_prev_bars_0)
{
m_prev_bars_0=time_0;
//--- search for trading signals only at the time of the birth of new barint count_buys=0,count_sells=0;
for(int i=0; i<size; i++)
{
if(SCPos[i].symbol==InpSymbol_0)
{
count_buys=SCPos[i].count_buys;
count_sells=SCPos[i].count_sells;
break;
}
}
if(count_buys+count_sells<InpMaxPositions)
{
double lot=SymbolInfoDouble(InpSymbol_0,SYMBOL_VOLUME_MIN);
if(lot>0.0)
{
MqlRates rates[];
ArraySetAsSeries(rates,true);
int start_pos=0,count=3;
if(CopyRates(InpSymbol_0,Period(),start_pos,count,rates)==count)
{
if(rates[1].open<rates[1].close)
m_trade.Buy(lot,InpSymbol_0);
else
m_trade.Sell(lot,InpSymbol_0);
}
}
}
}
}
//--- Symbol 1if(m_symbol_1)
{
datetime time_0=iTime(InpSymbol_1,Period(),0);
if(time_0!=m_prev_bars_1)
{
m_prev_bars_1=time_0;
//--- search for trading signals only at the time of the birth of new barint count_buys=0,count_sells=0;
for(int i=0; i<size; i++)
{
if(SCPos[i].symbol==InpSymbol_1)
{
count_buys=SCPos[i].count_buys;
count_sells=SCPos[i].count_sells;
break;
}
}
if(count_buys+count_sells<InpMaxPositions)
{
double lot=SymbolInfoDouble(InpSymbol_1,SYMBOL_VOLUME_MIN);
if(lot>0.0)
{
MqlRates rates[];
ArraySetAsSeries(rates,true);
int start_pos=0,count=3;
if(CopyRates(InpSymbol_1,Period(),start_pos,count,rates)==count)
{
if(rates[1].open<rates[1].close)
m_trade.Buy(lot,InpSymbol_1);
else
m_trade.Sell(lot,InpSymbol_1);
}
}
}
}
}
//--- Symbol 2if(m_symbol_2)
{
datetime time_0=iTime(InpSymbol_2,Period(),0);
if(time_0!=m_prev_bars_2)
{
m_prev_bars_2=time_0;
//--- search for trading signals only at the time of the birth of new barint count_buys=0,count_sells=0;
for(int i=0; i<size; i++)
{
if(SCPos[i].symbol==InpSymbol_2)
{
count_buys=SCPos[i].count_buys;
count_sells=SCPos[i].count_sells;
break;
}
}
if(count_buys+count_sells<InpMaxPositions)
{
double lot=SymbolInfoDouble(InpSymbol_2,SYMBOL_VOLUME_MIN);
if(lot>0.0)
{
MqlRates rates[];
ArraySetAsSeries(rates,true);
int start_pos=0,count=3;
if(CopyRates(InpSymbol_2,Period(),start_pos,count,rates)==count)
{
if(rates[1].open<rates[1].close)
m_trade.Buy(lot,InpSymbol_2);
else
m_trade.Sell(lot,InpSymbol_2);
}
}
}
}
}
//--- Symbol 3if(m_symbol_3)
{
datetime time_0=iTime(InpSymbol_3,Period(),0);
if(time_0!=m_prev_bars_3)
{
m_prev_bars_3=time_0;
//--- search for trading signals only at the time of the birth of new barint count_buys=0,count_sells=0;
for(int i=0; i<size; i++)
{
if(SCPos[i].symbol==InpSymbol_3)
{
count_buys=SCPos[i].count_buys;
count_sells=SCPos[i].count_sells;
break;
}
}
if(count_buys+count_sells<InpMaxPositions)
{
double lot=SymbolInfoDouble(InpSymbol_3,SYMBOL_VOLUME_MIN);
if(lot>0.0)
{
MqlRates rates[];
ArraySetAsSeries(rates,true);
int start_pos=0,count=3;
if(CopyRates(InpSymbol_3,Period(),start_pos,count,rates)==count)
{
if(rates[1].open<rates[1].close)
m_trade.Buy(lot,InpSymbol_3);
else
m_trade.Sell(lot,InpSymbol_3);
}
}
}
}
}
}
Task: find the OBJ_VLINE object and get the time of the object.
There are several stages of checks and protections in the code. Stage one - searching for an object by name. Stage two - we get the type of object. The third stage is without checking, we just get the time of the object.
Francisco Carlos Sobral Ribeiro : Is it possible to move the identifier part of the OnInit () section to the function, whose call is made in OnInit () ? Was there a problem?
You can't. Why? Because you will start calling this function from OnTick.
306178
***
" A position cannot be reversed in the hedging system. In this case, the current position is closed and a new one with the remaining volume is opened. "Is there any way to disable it so that I can simultaneously hold open Buy & Sell positions on a currency pair?
I think you need to ask your question on the forum - here is a thread for those who ask questions about PROGRAMMING in MQL5, but you generally have zero knowledge. You manage to misinterpret the documentation. Please do not post any more in this thread - this thread is for those who program.
306178
Example - there is a line (Horizontal line or Trend line) and you need to track when the price crosses the line.
To do this, you first need to get the price of the line - but first of all we check if there is an object with that name at all? If there is such an object, then, depending on the type of object, we get the price:
306178
Calculate positions for all symbols
Code: 'Calculate positions for all symbols.mq5
Objective: Several positions are open in the market. You need to get the number of positions (both BUY and SELL) for each symbol.
Suggested solution: the 'STRUCT_CALCULATE_POSITIONS' structure
is created and this structure is filled in the 'CalculateAllPositions'
function.
For example, a test advisor has been created: first, it opens four positions and then (after calling 'CalculateAllPositions') unpacks the 'STRUCT_CALCULATE_POSITIONS' structure'
Result:
306178
No more than N positions for each symbol Simple
Another example of how to use the 'STRUCT_CALCULATE_POSITIONS' structure. The EA opens a position on a new bar. There is no Stop Loss or Take Profit. Nobody closes open positions - this is a demonstration of how to use and how to work with the structure 'STRUCT_CALCULATE_POSITIONS'.
306178
Hello
I didn't understand anything at all. What's the top? What's the bottom? Ask your question in the form of a picture.
306178
Search for the most recent position and display information about it.
The code: 'Last position info.mq5'
Result:
306178
Working with the OBJ_VLINE graphic object
Task: find the OBJ_VLINE object and get the time of the object.
There are several stages of checks and protections in the code. Stage one - searching for an object by name. Stage two - we get the type of object. The third stage is without checking, we just get the time of the object.
The handle MUST ONLY be created ONCE. The most convenient place to create an identifier is OnInit ().
306178
Is it possible to move the identifier part of the OnInit () section to the function, whose call is made in OnInit () ? Was there a problem?
You can't. Why? Because you will start calling this function from OnTick.