ORDER_TYPE_CLOSE_BY with Ctrade Standard Library

Davide Gribaudo  

Hello, 


I can't understand how to close an open buy with a ORDER_TYPE_CLOSE_BY.

my code : 

for(int i=0; i<tcount; i++)
        {
         if(PositionSelectByTicket(ticket_chain[i]) && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
           {
            long stoplevel = SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) > 0 ? SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) : info.Spread();
            if(!trade.PositionOpen(_Symbol,ORDER_TYPE_CLOSE_BY,PositionGetDouble(POSITION_VOLUME),info.Bid()-(stoplevel*_Point),0,0,"Close Buy"))
               Alert("Close Error code ", _LastError);
           }
         else
            Print(_LastError);
        }
Close Error code 0

this is what I get on the Journal on the strategy Tester..

Fernando Carreiro  
arimbur: I can't understand how to close an open buy with a ORDER_TYPE_CLOSE_BY. my code : this is what I get on the Journal on the strategy Tester..

I think you may be misunderstanding something. "ORDER_TYPE_CLOSE_BY" is only used when you have opposing positions on a "hedging" account, and you want to close them simultaneously against each other and save on trading costs (which does not seem to be your case).

PositionCloseBy

Closes a position with the specified ticket by an opposite position

To close a normal single position by using the CTrade class, then use the following method:

PositionClose

Closes a position for the specified symbol

Documentation on MQL5: Standard Library / Trade Classes / CTrade / PositionClose
Documentation on MQL5: Standard Library / Trade Classes / CTrade / PositionClose
  • www.mql5.com
PositionClose(const string,ulong) - CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5