why all open positions getting close after some time, without running my EA codes?

 

I write some codes in MT5 with MQL5 language and create EA, after some times all open positions getting close without running any of my codes.

I did not set take profit or stop loss for any of positions, because I want to handle it by my self and my account leverage is 1:500.

 
Analyze the logfile.
 
Marco vd Heijden:
Analyze the logfile.

for one of the open position:

deal #48 buy 0.01 EURUSD at 1.20456 done (based on order #48)

deal performed [#48 buy 0.01 EURUSD at 1.20456]

order performed buy 0.01 at 1.20456 [#48 buy 0.01 EURUSD at 1.20456]

market sell 0.01 EURUSD, close #48 (1.20437/ 1.20439/ 1.20437)

CTrade:: orderSend : market sell 0.01 position #48 EURUSD [done at 1.20437]

 
b51338:

for one of the open position:

deal #48 buy 0.01 EURUSD at 1.20456 done (based on order #48)

deal performed [#48 buy 0.01 EURUSD at 1.20456]

order performed buy 0.01 at 1.20456 [#48 buy 0.01 EURUSD at 1.20456]

market sell 0.01 EURUSD, close #48 (1.20437/ 1.20439/ 1.20437)

CTrade:: orderSend : market sell 0.01 position #48 EURUSD [done at 1.20437]

you post/share your code here 


so that someone can fix it for you

 
Hakim Precious:

you post/share your code here 


so that someone can fix it for you

MqlTick currentPrice;
double last_price;

void OnTick()
        {
        
                if(!SymbolInfoTick(Symbol(), currentPrice)){
                        Alert(" error getting the lastest price quote");
                        return;
                }       
        
                bool isPositionOpened = false;
                int total = PositionsTotal();
                if(total > 0) {
                        isPositionOpened = true;
                } else {
                        isPositionOpened = false;
                }
                
                if(isPositionOpened == false) {
                        orderBuy(_Symbol, 0.03, currentPrice.ask);                      
                }else if(currentPrice.ask >= last_price + 200) {
                        closeOpenOrder();
                }
	}


void orderBuy( string symbol,
                        double volume,
                        double price){

                MqlTradeRequest request = { 0 };
                MqlTradeResult = { 0 };

                request.action = TRADE_ACTION_DEAL;
                request.price = NormalizeDouble(price, _Digits);
                request.symbol = symbol;
                request.volume = volume;
                request.magic = 12345;
                request.type = ORDER_TYPE_BUY;
                request.type_filling = ORDER_FILLING_FOK;
                
                OrderSend(request, result);
                if(result.retcode == 10009 || result.retcode == 10008){
                        Alert(" buy order has been successfully placed with ticket #:", result.order);
                        last_price = price;
                }else {
                        Alert("error");
                        ResetLastError();
                }
}

void closeOpenOrder() {
        
        CTrade trade;
        int i = PositionsTotal() - 1;
        while ( i >= 0 ){
                if(trade.PositionClose(PositionGetTicket(i))){
                        i--;
                }
        }
}               

 
else if(currentPrice.ask >= last_price + 200) {
                        closeOpenOrder();

 
Marco vd Heijden:


what is the problem in this line? I want to close open position when current price reach last order price + 20 pip.

 

okay so you can do:

Print(last_price)
double price=last_price+200;
Print(price);
 
Marco vd Heijden:

okay so you can do:

you want to see what is the amount of last_price and last_price + 200 is?