what happens if i leave the take profit or stop loss fields empty in the ordersend function + error 4002

 

hi

i'm using the ordersend function to open new positions and i also use it to close positions when certain conditions are met by opening an opposite position to the one i already have and i was wondering what will happen if i don't define the sl and tp fields in the trade request structure when i open an opposite position to the one i have and if i do define them will they have any importance?

and another thing i keep getting a run time error when creating handles for the indicators it says "Error creating handle for adx - 4002" now i know where the problem is but i don't know how to fix it and heres the section where the problem is in my code :

    adx_hand=iADX(NULL,0,adx_per);
    ma_hand=iMA(NULL,PERIOD_H1,ma_per,0,MODE_EMA,PRICE_CLOSE);
    stoch_hand=iStochastic(NULL,0,k_per,d_per,slowing,MODE_EMA,STO_LOWHIGH);
    if (adx_hand<0)
    {
      Alert("Error creating handle for adx - ",GetLastError());
      return(1);
    }
    if (ma_hand<0)
    {
      Alert("Error creating handle for ma - ",GetLastError());
      return(1);
    }
    if (stoch_hand<0)
    {
      Alert("Error creating handle for stoch - ",GetLastError());
      return(1);
    }

 and it's the same with the other two indicators if i remove the first alert the second one will go and the same thing with the third.

any ideas about what i should do ?

thanks 

 
karemtalli:

hi

i'm using the ordersend function to open new positions and i also use it to close positions when certain conditions are met by opening an opposite position to the one i already have and i was wondering what will happen if i don't define the sl and tp fields in the trade request structure when i open an opposite position to the one i have and if i do define them will they have any importance?

You do not have to specify SL and/or TP, it is totally valid approach. Putting values "0.0" as SL or TP means there is no SL or TP at all. Regarding importance, for example:

1) if you send instant execution order for 2 lots long and then send instant execution order for 2 lots short - first order results with DEAL_ENTRY_IN, second with DEAL_ENTRY_OUT so there is no point in specifying SL or TP as you won't have a net position at all (you can specify it SL or TP in MqlTradeRequest, but it won't be used as there will be no position left after these 2 deals get executed)

2) if you send instant execution order for 2 lots long and then send instant execution order for 3 lots short - first order results with DEAL_ENTRY_IN, second with DEAL_ENTRY_INOUT (because it will be position reversal) so you will have a 1 lot short position, to which the SL or TP that you specify in MqlTradeRequest will be applied

 

Regarding the second issue:

ERR_WRONG_INTERNAL_PARAMETER

4002

Wrong parameter in the inner call of the client terminal function


Check what values are assigned to adx_per, ma_per, k_per, d_per, slowing. The problem can be as well in some other parameters that you specified for iADX, iMA, iStochastic.

 
Enigma71fx:

You do not have to specify SL and/or TP, it is totally valid approach. Putting values "0.0" as SL or TP means there is no SL or TP at all. Regarding importance, for example:

1) if you send instant execution order for 2 lots long and then send instant execution order for 2 lots short - first order results with DEAL_ENTRY_IN, second with DEAL_ENTRY_OUT so there is no point in specifying SL or TP as you won't have a net position at all (you can specify it SL or TP in MqlTradeRequest, but it won't be used as there will be no position left after these 2 deals get executed)

2) if you send instant execution order for 2 lots long and then send instant execution order for 3 lots short - first order results with DEAL_ENTRY_IN, second with DEAL_ENTRY_INOUT (because it will be position reversal) so you will have a 1 lot short position, to which the SL or TP that you specify in MqlTradeRequest will be applied

 

Regarding the second issue:

ERR_WRONG_INTERNAL_PARAMETER

4002

Wrong parameter in the inner call of the client terminal function


Check what values are assigned to adx_per, ma_per, k_per, d_per, slowing. The problem can be as well in some other parameters that you specified for iADX, iMA, iStochastic.

i fixed the error and everything is okay no errors at all but when i debug the code or test in the strategy tester it doesn't make any trades at all, i tried to compare it to samuel's code that in his first article ( https://www.mql5.com/en/articles/100 ) and everything in my code seems okay to me here's my code:
//input parameters:
input int take=50; // take profit
input int stop=50; // stop loss
input double adx_min=25; // adx minimum
input int ma_per=8; // moving average period
input int adx_per=8; // adx period
input int dev=100; // deviation
input double lot=0.1; // lots to trade
input double dimax=10; // plus or minus di maximum value
input double dimin=17; // plus or minus di minimum value
input int k_per=5; // stoch k period
input int d_per=3; // stoch d period
input int slowing=3; // stoch slowing
//input int trade_pct; // percent of margin to trade
input long ea_magic=1210; // magic number
//other parameters
int adx_hand , ma_hand , stoch_hand;
double adx[] , plsdi[] , mindi[] , ma[] , stoch[];
double price_close;
int stp , tkp;
//double pct;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
    adx_hand=iADX(NULL,0,adx_per);
    ma_hand=iMA(_Symbol,PERIOD_H1,ma_per,0,MODE_EMA,PRICE_CLOSE);
    stoch_hand=iStochastic(_Symbol,0,k_per,d_per,slowing,MODE_EMA,STO_LOWHIGH);
    if ( adx_hand<0 || ma_hand<0 || stoch_hand<0 )
    {
      Alert("Error creating handles - ",GetLastError());
      return(1);
    }
    stp=stop;
    tkp=take;
    if(_Digits==5 || _Digits==3)
    {
      stp=stp*10;
      tkp=tkp*10;
    }
//    pct=trade_pct/100;
    return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
    IndicatorRelease(adx_hand);
    IndicatorRelease(ma_hand);
    IndicatorRelease(stoch_hand); 
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
    MqlRates rates[];
    MqlTick latest_price;
    MqlTradeRequest request;
    MqlTradeResult result;
    ZeroMemory(request);
    ArraySetAsSeries(rates,true);
    ArraySetAsSeries(adx,true);
    ArraySetAsSeries(plsdi,true);
    ArraySetAsSeries(mindi,true);
    ArraySetAsSeries(ma,true);
    ArraySetAsSeries(stoch,true);
    if (CopyRates(_Symbol,_Period,0,10,rates)<0)
    {
      Alert("Error copying rates - ",GetLastError());
      return;
    }
    static datetime prev_time;
    datetime bar_time=rates[0].time;
    if (bar_time==prev_time)
    {
      return;
    }
    prev_time=bar_time;
    if (CopyBuffer(adx_hand,0,0,10,adx)<3 || CopyBuffer(adx_hand,1,0,10,plsdi)<3 || CopyBuffer(adx_hand,2,0,10,mindi)<3)
    {
      Alert("Error copying buffer for adx - ",GetLastError());
      return;
    }
    if (CopyBuffer(stoch_hand,0,0,10,stoch)<3)
    {
      Alert("Error copying buffer for stochastic - ",GetLastError());
      return;
    }
    if (CopyBuffer(ma_hand,0,0,10,ma)<3)
    {
      Alert("Error copying buffer for ma - ",GetLastError());
      return;
    }
    bool long_opened=false , short_opened=false;
    if (PositionSelect(_Symbol))
    {
      if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
      {
        long_opened=true;
        if (/*long position close conditions*/stoch[0]>80)
        {
          ZeroMemory(request);
          request.action = TRADE_ACTION_DEAL;
          request.symbol = _Symbol;
          request.volume = lot;
          request.price = NormalizeDouble(latest_price.bid,_Digits); 
          request.sl = NormalizeDouble(latest_price.bid - stp*_Point,_Digits); 
          request.tp = NormalizeDouble(latest_price.bid + tkp*_Point,_Digits); 
          request.deviation = dev;
          request.type = ORDER_TYPE_SELL;
          request.type_filling = ORDER_FILLING_FOK;
          request.magic = ea_magic;
          OrderSend(request,result);
          if(result.retcode==10009 || result.retcode==10008)
          {
            Alert("A long position was successfully closed");
          }
          else
          {
            Alert("The long position was not closed -error:",GetLastError());
            ResetLastError();           
            return;
          }
          long_opened=false;
        }
      }
      if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
      {
        short_opened=true;
        if (/*short position close conditions*/stoch[0]<20)
        { 
          ZeroMemory(request); 
          request.action = TRADE_ACTION_DEAL;
          request.symbol = _Symbol;
          request.volume = lot;
          request.price = NormalizeDouble(latest_price.ask,_Digits); 
          request.sl = NormalizeDouble(latest_price.ask - stp*_Point,_Digits); 
          request.tp = NormalizeDouble(latest_price.ask + tkp*_Point,_Digits); 
          request.deviation = dev;
          request.type = ORDER_TYPE_BUY;
          request.type_filling = ORDER_FILLING_FOK;
          request.magic = ea_magic;
          OrderSend(request,result);
          if(result.retcode==10009 || result.retcode==10008)
          {
            Alert("A short position was successfully closed");
          }
          else
          {
            Alert("The short position was not closed -error:",GetLastError());
            ResetLastError();           
            return;
          }
          short_opened=false;
        }
      }
    }
    if ( long_opened==false && short_opened==false )
    {
      if (/*buying conditions*/ adx[0]>adx_min && plsdi[0]>mindi[0])
      {
        ZeroMemory(request);
        request.action = TRADE_ACTION_DEAL;
        request.symbol = _Symbol;
        request.volume = lot;
        request.price = NormalizeDouble(latest_price.ask,_Digits); 
        request.sl = NormalizeDouble(latest_price.ask - stp*_Point,_Digits); 
        request.tp = NormalizeDouble(latest_price.ask + tkp*_Point,_Digits); 
        request.deviation = dev;
        request.type = ORDER_TYPE_BUY;
        request.type_filling = ORDER_FILLING_FOK;
        request.magic = ea_magic;
        OrderSend(request,result);
        if(result.retcode==10009 || result.retcode==10008)
        {
          Alert("A long position was successfully opened");
        }
        else
        {
          Alert("The long position was not opened -error:",GetLastError());
          ResetLastError();           
          return;
        }
      }
      if (/*selling conditions*/ adx[0]>adx_min && plsdi[0]>mindi[0])
      {
        ZeroMemory(request);
        request.action = TRADE_ACTION_DEAL;
        request.symbol = _Symbol;
        request.volume = lot;
        request.price = NormalizeDouble(latest_price.bid,_Digits); 
        request.sl = NormalizeDouble(latest_price.bid - stp*_Point,_Digits); 
        request.tp = NormalizeDouble(latest_price.bid + tkp*_Point,_Digits); 
        request.deviation = dev;
        request.type = ORDER_TYPE_SELL;
        request.type_filling = ORDER_FILLING_FOK;
        request.magic = ea_magic;
        OrderSend(request,result);
        if(result.retcode==10009 || result.retcode==10008)
        {
          Alert("A short position was successfully opened");
        }
        else
        {
          Alert("The short position was not opened -error:",GetLastError());
          ResetLastError();           
          return;
        }
      }
    }
  }
//+------------------------------------------------------------------+
can you tell me what's wrong with my code 
 
Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
  • 2010.06.09
  • Samuel
  • www.mql5.com
The Expert Advisors programming in MQL5 is simple, and you can learn it easy. In this step by step guide, you will see the basic steps required in writing a simple Expert Advisor based on a developed trading strategy. The structure of an Expert Advisor, the use of built-in technical indicators and trading functions, the details of the Debug mode and use of the Strategy Tester are presented.
 
ok i got it now everything is ok thanks 
 

Hi karemtalli,

If you intend to use GetLastError(), use ResetLatError() before function call or right after calling GetLastError(), so you have the right error report. Look at codes in MetaEditor 5 on how to use ResetLastError().

https://www.mql5.com/en/docs/common/resetlasterror

 

Documentation on MQL5: Common Functions / ResetLastError
Documentation on MQL5: Common Functions / ResetLastError
  • www.mql5.com
Common Functions / ResetLastError - Documentation on MQL5