Trade Modification Failure

 

Hi every one. I have to strange things I have noticed

1. Why is it my code sometimes modify position (add stoploss and takeprofit) while other times it fails?

2.Also, sometimes it will place a buy order/sell order while other times it will not..?

Anyone experience the same? Any ideas on how to go about the problems?

Here goes the code:

// Input variables
input double TradeVolume=0.1;
input int StopLoss=250;
input int TakeProfit=500;
input int MAperiod21 = 21;
input int MAperiod55 = 55;
input int MAperiod89 = 89;
input int williamsperiod21 = 21;

// Global variables 
bool glBuyPlaced, glSellPlaced;

// OnTick() event handler
void OnTick()
{
        // Trade structures
        MqlTradeRequest request;
        MqlTradeResult result;
        ZeroMemory(request);

        // Moving average
        double MA21[];
        ArraySetAsSeries(MA21,true);
        double MA55[];
        ArraySetAsSeries(MA55,true);
        double MA89[];
        ArraySetAsSeries(MA89,true);
        
        int MAHandle21=iMA(_Symbol,0,MAperiod21,MODE_EMA,0,PRICE_CLOSE);
        CopyBuffer(MAHandle21,0,0,1,MA21);
        int MAHandle55=iMA(_Symbol,0,MAperiod55,MODE_EMA,0,PRICE_CLOSE);
        CopyBuffer(MAHandle55,0,0,100,MA55);
        int MAHandle89=iMA(_Symbol,0,MAperiod89,MODE_EMA,0,PRICE_CLOSE);
        CopyBuffer(MAHandle89,0,0,1,MA89);

        //williams %
        double williams[];
        ArraySetAsSeries(williams,true);
        
        int williamsHandle=iWPR(_Symbol,0,williamsperiod21);
        CopyBuffer(williamsHandle,0,0,1,williams);
        
        // Close price
        double close[];
        ArraySetAsSeries(close,true);
        CopyClose(_Symbol,0,0,1,close);
        
        
        // Get current market orders
        ulong buyTicket = 0, sellTicket = 0;
        for(int i = 0; i < PositionsTotal(); i++)
        {
           ulong ticket = PositionGetTicket(i);
           PositionSelectByTicket(ticket);
           
           if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
           {
              buyTicket = ticket;
              glBuyPlaced = true;
           }
           else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
           {
              sellTicket = ticket;
              glSellPlaced = true;
           }
        }
   
   
   // Open buy market order
   if(close[0] > MA21[0] && MA21[0]>MA55[0] && williams[0]>=-20 && glBuyPlaced == false)
   {
        // Close sell order if any
        if(sellTicket > 0)
        {
           PositionSelectByTicket(sellTicket);
           
           request.action = TRADE_ACTION_DEAL;
           request.type = ORDER_TYPE_BUY;
           request.symbol = _Symbol;
              request.position = sellTicket;
              request.type_filling = ORDER_FILLING_IOC; 
              request.volume = PositionGetDouble(POSITION_VOLUME);
                        request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
                        request.deviation = 50;
                        
                        bool sent = OrderSend(request, result);
        }
        
        // Open buy order
        request.action = TRADE_ACTION_DEAL;
                request.type = ORDER_TYPE_BUY;
                request.symbol = _Symbol;
                request.volume = TradeVolume;
                request.type_filling = ORDER_FILLING_IOC; 
                request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
                request.sl = 0;
                request.tp = 0;
                request.deviation = 50;
                
                bool sent = OrderSend(request,result);
                
                // Modify SL/TP
                if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
                {
                        request.action = TRADE_ACTION_SLTP;
                        do Sleep(6000); while(PositionSelect(_Symbol) == false);
                        request.position = result.order;
                        
                        PositionSelectByTicket(result.order);
                        double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
        
                        if(StopLoss > 0) request.sl = positionOpenPrice - (StopLoss * _Point);
                        if(TakeProfit > 0) request.tp = positionOpenPrice + (TakeProfit * _Point);
                        
                        if(request.sl > 0 && request.tp > 0) sent = OrderSend(request,result);
                        
                        glSellPlaced = false;
                } 
   }
   
   
   // Open sell market order
   else  if((close[0] < MA21[0]) && (MA21[0]<MA55[0]) && williams[0]<=-80 && (glSellPlaced == false))
   {
        // Close buy order if any
        if(buyTicket > 0)
        {
           PositionSelectByTicket(buyTicket);
           
           request.action = TRADE_ACTION_DEAL;
           request.type = ORDER_TYPE_SELL;
           request.symbol = _Symbol;
              request.position = buyTicket;
              request.type_filling = ORDER_FILLING_IOC; 
              request.volume = PositionGetDouble(POSITION_VOLUME);
                        request.price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
                        request.deviation = 50;
                        
                        bool sent = OrderSend(request, result);
        }
        
        // Open sell order
        request.action = TRADE_ACTION_DEAL;
                request.type = ORDER_TYPE_SELL;
                request.symbol = _Symbol;
                request.volume = TradeVolume;
                request.type_filling = ORDER_FILLING_IOC; 
                request.price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
                request.sl = 0;
                request.tp = 0;
                request.deviation = 50;
                
                bool sent = OrderSend(request,result);
                
                // Modify SL/TP
                if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
                {
                        request.action = TRADE_ACTION_SLTP;
                        do Sleep(6000); while(PositionSelect(_Symbol) == false);
                        request.position = result.order;
                        
                        PositionSelectByTicket(result.order);
                        double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);

                        if(StopLoss > 0) request.sl = positionOpenPrice + (StopLoss * _Point);
                        if(TakeProfit > 0) request.tp = positionOpenPrice - (TakeProfit * _Point);
                        
                        if(request.sl > 0 && request.tp > 0) sent = OrderSend(request,result);
                        
                        glBuyPlaced = false;
                } 
                
   } 
    
}


Thank you

Files:
report.png  319 kb
 

Always the same errors again and again, what is the point to have a forum with 100,000 topics !

1. Please read the documentation AND follow it.

2. Please check on the forum about your issues before creating new topic or posting. A lot of people had already the same issues.

 

Alain Verleyen thank you for the heads up. You got any link to such? I posted because i had checked and could not find any solutions to my issue. 

regards

 
Julius Mwangi:

Alain Verleyen thank you for the heads up. You got any link to such? I posted because i had checked and could not find any solutions to my issue. 

regards

Search better, there are dozens of topics about similar issue.

The forum is not a "search for me" service.

 
Alain Verleyen:

Search better, there are dozens of topics about similar issue.

The forum is not a "search for me" service.

You've realy driven your point home sir. Well noted

Reason: