[Invalid Stops ] Using EA pending orders

 

Hello everyone

I am trying to place a pending buy limit when the current price of the tick becomes below the Initial price that the trade started with. Unfortunately i am getting the following error: 

2020.07.22 11:22:26.992 2020.07.01 00:00:00   failed modify #2 buy 0.1 XAUUSD sl: 0.000, tp: 0.000 -> sl: 0.000, tp: 1778.350 [Invalid stops]

Can anyone help me find out the reason ? Any help will  be appreciated 

Here is the code i am using : ( Most of its parts are from the documentation with minor modifications )

//+------------------------------------------------------------------+
//|                                                          ea1.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#define EXPERT_MAGIC 123456  
double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
    MqlTradeRequest request={0};
   MqlTradeResult  result={0};
//--- parameters of request
   request.action   =TRADE_ACTION_DEAL;                     // type of trade operation
   request.symbol   =Symbol();                              // symbol
   request.volume   =0.1;                                   // volume of 0.1 lot
   request.type     =ORDER_TYPE_BUY;                        // order type
   request.price    =SymbolInfoDouble(Symbol(),SYMBOL_ASK); // price for opening
   request.deviation=5;                                     // allowed deviation from the price
   request.magic    =EXPERT_MAGIC;                          // MagicNumber of the order
   
//--- send the request
   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
//--- information about the operation
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  double current_price = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
  if(current_price <= Ask )
  {
      int total = PositionsTotal();
      printf(total);
      for(int i=0;i<total;i++)
      {
          ulong  position_ticket=PositionGetTicket(i);// ticket of the position
      string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol 
      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // number of decimal places
      ulong  magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the position
      double volume=PositionGetDouble(POSITION_VOLUME);    // volume of the position
      double sl=PositionGetDouble(POSITION_SL);  // Stop Loss of the position
      double tp=PositionGetDouble(POSITION_TP);  // Take Profit of the position
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position
      //--- output information about the position
      PrintFormat("#%I64u %s  %s  %.2f  %s  sl: %s  tp: %s  [%I64d]",
                  position_ticket,
                  position_symbol,
                  EnumToString(type),
                  volume,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits),
                  DoubleToString(sl,digits),
                  DoubleToString(tp,digits),
                  magic);
                  if(magic==EXPERT_MAGIC && sl==0 && tp==0)
                  {
                      double price=PositionGetDouble(POSITION_PRICE_OPEN);
                      double ask=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
                      double price_level;
                      if(type==POSITION_TYPE_BUY)
                         {
                           sl=0;
                           tp=Ask - 3000*_Point;
                         }
                      ZeroMemory(request);
                      ZeroMemory(result);
                        request.action  =TRADE_ACTION_SLTP; // type of trade operation
         request.position=position_ticket;   // ticket of the position
         request.symbol=position_symbol;     // symbol 
         request.sl      =sl;                // Stop Loss of the position
         request.tp      =tp;                // Take Profit of the position
         request.magic=EXPERT_MAGIC;         // MagicNumber of the position
         //--- output information about the modification
         PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation   
         else PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
         Comment("Buy limit is activated ! ");
                  }
      
      }
      
  }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
 
before we go far you have OrderSend inside OnInit Function. That to me is a little bit off, but well i dont understand .

and your Code doesnt place a buylimit at all

whole Code Needs to be reWritten 

try 
#include <Trade\Trade.mqh>

CTrade m_trade


//Inside Code 
if(!m_trade.BuyLimit(/*Fill what Neds To beFilled*/))
   printf(/*What needs to be Reported*/);
 
metadev:

Hello everyone

I am trying to place a pending buy limit when the current price of the tick becomes below the Initial price that the trade started with. Unfortunately i am getting the following error: 

2020.07.22 11:22:26.992 2020.07.01 00:00:00   failed modify #2 buy 0.1 XAUUSD sl: 0.000, tp: 0.000 -> sl: 0.000, tp: 1778.350 [Invalid stops]

Can anyone help me find out the reason ? Any help will  be appreciated 

Here is the code i am using : ( Most of its parts are from the documentation with minor modifications )

MQL uses BID as price close (current price)

if(current_price <= Ask) => indeed the Bid is already under Ask.

The buy limit price must be below the Bid, not below the Ask.

Buy Limit TP must be above Ask, = > NOT => Ask - 3000 * Point

Buy Limit SL must be below the Bid.

 
Roberto Jacobs:

The buy limit price must be below the Bid, not below the Ask.

Buy Limit TP must be above Ask, = > NOT => Ask - 3000 * Point

Buy Limit SL must be below the Bid.

The buy limit entryprice must be below the Ask

Buy limit TP must be above the Buy limit entry price

Buy limit SL must be below the Buy limit entry price

 

Thank you !! this solved my problem.