First you have to select a position to do something with it.
use m_position.SelectByIndex() function.
Example1:
CTrade m_trade; CPositionInfo m_position; if ( PositionsTotal() > 0) { for (int i=0; i < PositionsTotal() ; i++) { if(m_position.SelectByIndex(i)) { if (m_position.Symbol()==Symbol() && m_position.Magic()==m_magic) { if (m_position.PositionType() == POSITION_TYPE_SELL || m_position.PositionType() == POSITION_TYPE_BUY ) { m_trade.PositionClose(m_position.Ticket()); // Close the selected position } } } } }
Another example:
//+------------------------------------------------------------------+ //| Close all positions | //+------------------------------------------------------------------+ void CloseAllPositions() { for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current positions if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic) m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol }
how can i close sell position and open buy position?
example:
/ 0 - MAIN_LINE, 1 - SIGNAL_LINE.
double ma_ema[],ma_lwma[],wpr[];
ArraySetAsSeries(ma_ema,true);
ArraySetAsSeries(ma_lwma,true);
ArraySetAsSeries(wpr,true);
int buffer=0,start_pos=0,count=3;
if(!iGetArray(handle_iMA_EMA,0,start_pos,count,ma_ema) ||
!iGetArray(handle_iMA_LWMA,0,start_pos,count,ma_lwma) ||
!iGetArray(handle_iWPR,0,start_pos,count,wpr))
{
PrevBars=0; return;
}
if(ma_ema[0]<ma_lwma[0] && ma_ema[1]>ma_lwma[1] &&wpr[1]<wpr[0]>=50.0) //Buy
{
m_need_open_buy=true;
I want to close sell position by openiing this position
how can i close sell position and open buy position?
I want to close sell position by openiing this position
Please edityour post and use the code button (Alt+S) to post your code.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi.
How can i get the ticket of an position and close a the position?
whats wrong in Code ?
InThe Code, I Open a position and immediately (after 10 seconds) i want to close it.