Help me find the logical error in this sample code from a book.

 

I am still learning to code and this code was given as an introductory sample...

It used ORDER_FILLING_FOK, but with both ORDER_FILLING_FOK and ORDER_FILLING_RETURN it gave an error as "unsupported filling mode".

So i tried with ORDER_FILLING_IOC. Now it opened the order but didn't seem to close it. In the journal it seems to be unable to change sl/tp saying failed to modify position(details) & position doesn't exist.

i tried increasing the sleep delay and deviation but still no help.

Pls help me find the error.

Here is the original code with ORDER_FILLING_FOK

// Input variables
input double TradeVolume=0.1;
input int StopLoss=1000;
input int TakeProfit=1000;
input int MAPeriod=10;
// Global variables
bool glBuyPlaced, glSellPlaced;
// OnTick() event handler
void OnTick()
  {
// Trade structures
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
// Moving average
   double ma[];
   ArraySetAsSeries(ma,true);
   int maHandle=iMA(_Symbol,0,MAPeriod,MODE_SMA,0,PRICE_CLOSE);
   CopyBuffer(maHandle,0,0,1,ma);
// Close price
   double close[];
   ArraySetAsSeries(close,true);
   CopyClose(_Symbol,0,0,1,close);
// Current position information
   bool openPosition = PositionSelect(_Symbol);
   long positionType = PositionGetInteger(POSITION_TYPE);
   double currentVolume = 0;
   if(openPosition == true)
      currentVolume = PositionGetDouble(POSITION_VOLUME);
// Open buy market order
   if(close[0] > ma[0] && glBuyPlaced == false
      && (positionType != POSITION_TYPE_BUY || openPosition == false))
     {
      request.action = TRADE_ACTION_DEAL;
      request.type = ORDER_TYPE_BUY;
      request.symbol = _Symbol;
      request.volume = TradeVolume + currentVolume;
      request.type_filling = ORDER_FILLING_FOK;
      request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      request.sl = 0;
      request.tp = 0;
      request.deviation = 50;
      OrderSend(request,result);
      // Modify SL/TP
      if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
        {
         request.action = TRADE_ACTION_SLTP;
         do
            Sleep(100);
         while(PositionSelect(_Symbol) == false);
         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)
            OrderSend(request,result);
         glBuyPlaced = true;
         glSellPlaced = false;
        }
     }
// Open sell market order
   else
      if(close[0] < ma[0] && glSellPlaced == false && positionType != POSITION_TYPE_SELL)
        {
         request.action = TRADE_ACTION_DEAL;
         request.type = ORDER_TYPE_SELL;
         request.symbol = _Symbol;
         request.volume = TradeVolume + currentVolume;
         request.type_filling = ORDER_FILLING_FOK;
         request.price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
         request.sl = 0;
         request.tp = 0;
         request.deviation = 50;
         OrderSend(request,result);
         // Modify SL/TP
         if((result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
            && (StopLoss > 0 || TakeProfit > 0))
           {
            request.action = TRADE_ACTION_SLTP;
            do
               Sleep(100);
            while(PositionSelect(_Symbol) == false);
            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)
               OrderSend(request,result);
            glBuyPlaced = false;
            glSellPlaced = true;
           }
        }
  }


 
saurabhsuman003 :


The indicator handle in MQL5 SHOULD BE RECEIVED ONCE - IN OnInit ()!

 
Vladimir Karputov:

The indicator handle in MQL5 SHOULD BE RECEIVED ONCE - IN OnInit ()!

You mean that those double ma[] & double close[] declarations should be in OnInit() fn?

If not then pls elaborate.

 
saurabhsuman003 :
You mean that those double ma[] & double close[] declarations should be in OnInit() fn?

If not then pls elaborate.

Reference Guide:  iMA

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
//|                                                     Demo_iMA.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                              https://www.mql5.com | "The method of creation of the handle is set through the 'type' parameter (function type...
 
saurabhsuman003:

I am still learning to code and this code was given as an introductory sample...

It used ORDER_FILLING_FOK, but with both ORDER_FILLING_FOK and ORDER_FILLING_RETURN it gave an error as "unsupported filling mode".

So i tried with ORDER_FILLING_IOC. Now it opened the order but didn't seem to close it. In the journal it seems to be unable to change sl/tp saying failed to modify position(details) & position doesn't exist.

i tried increasing the sleep delay and deviation but still no help.

Pls help me find the error.

The position id is missing in your modify request.

 
Ty @vladimir karputov
 
lippmaje:

The position id is missing in your modify request.

Ty...i'll try modifying it.
 
saurabhsuman003:
Ty...i'll try modifying it.
request.position = PositionGetInteger(POSITION_TICKET);

Code samples see here:

https://www.mql5.com/en/docs/constants/tradingconstants/enum_trade_request_actions

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trading is done by sending orders to open positions using the OrderSend() function, as well as to place, modify or delete pending orders. Each trade order refers to the type of the requested operation. Trading operations are described in the ENUM_TRADE_REQUEST_ACTIONS enumeration...
Reason: