How can I set expiration?

 
datetime SetOrderExpirationTime(int halfhours) {
    return(TimeCurrent() + halfhours * 1800); 
}

bool BuyStop_Func(double volume, double orderprice, ulong slippage, double stopLoss, double takeProfit, ulong magicnumber)
{
    MqlTradeRequest request = {};
    MqlTradeResult result = {};
    
    if((Ask - Bid) / Point() > Spread){
       Print("Buy Trade: High Spread");
       return false;
    }
    
    request.action = TRADE_ACTION_PENDING;
    request.symbol = _Symbol;
    request.volume = volume;
    request.type = ORDER_TYPE_BUY_STOP;
    request.price = orderprice;
    request.deviation = slippage;
    request.tp = takeProfit;
    request.sl = stopLoss;
    request.magic = magicnumber;
    request.type_filling = ORDER_FILLING_FOK;
    request.comment = "Buy Stop Trade #" + IntegerToString(magicnumber);
    request.expiration = SetOrderExpirationTime(21);
    if(TradingControl.OrderSend(request, result))
    {
        Print("BUY Stop order placed successfully");
        return true;
    }
    else
    {
        Print("Error placing BUY Stop order: ", GetLastError());
        return false;
    }
}
 
Kosei S:

You will have to elaborate a bit more on this, set to what? if you are referring to the expiration time, its in line 2. Im not sure what you want.

https://www.mql5.com/en/docs/constants/structures/mqltraderequest

datetime expiration=TimeTradeServer()+PeriodSeconds(PERIOD_M20);//Here you change the minutes

trade.BuyStop(0.10,Ask+100*_Point,_Symbol,buySl,Ask+300*_Point,ORDER_TIME_SPECIFIED,expiration,0);
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Trade Request Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Nardus Van Staden #:

You will have to elaborate a bit more on this, set to what? if you are referring to the expiration time, its in line 2. Im not sure what you want.

https://www.mql5.com/en/docs/constants/structures/mqltraderequest

Id like to make original buy stop function because I'd like to setting slippage

 
Kosei S #:

Id like to make original buy stop function because I'd like to setting slippage

Hi,

you can try this:

datetime SetOrderExpirationTime(int hourExpiry) 
{
   MqlDateTime today;
  //-- 
  TimeToStruct(iTime(_Symbol,PERIOD_M1,1),today);
  today.hour+=hourExpiry;
  //--
  return(StructToTime(today)); 
}


for example, expiration time is 1 hour:

request.expiration = SetOrderExpirationTime(1);
 
Yohana Parmi #:

Hi,

you can try this:


for example, expiration time is 1 hour:

Thank you for your help. I tried it but it doesnt work.
I have never got correct time on this code.

Print((datetime)OrderGetInteger(ORDER_TIME_EXPIRATION));
 
Kosei S #:

Thank you for your help. I tried it but it doesnt work.
I have never got correct time on this code.

What do you mean 'correct time' ?

Here is a result :


//+------------------------------------------------------------------+
//|                                               TimeExpiration.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//--- input parameters
input int      exp_hour=1;

bool firstcall;

//+------------------------------------------------------------------+
datetime SetOrderExpirationTime(int hourExpiry) 
{
   MqlDateTime today;
  //-- 
  TimeToStruct(iTime(_Symbol,PERIOD_M1,1),today);
  today.hour+=hourExpiry;
  //--
  return(StructToTime(today)); 
}
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   firstcall=true;
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
    if(firstcall)
    {
      firstcall=false;
      Print("Time bar 1 (M1): ",TimeToString(iTime(_Symbol,PERIOD_M1,1))," + ",exp_hour," hour = ",
             TimeToString(SetOrderExpirationTime(exp_hour))
           );
    
    }
  }
//+------------------------------------------------------------------+
 
Yohana Parmi #:

What do you mean 'correct time' ?

Here is a result :


No I mean if I use Buy Stop Function I couldn't return correct time.

request.expiration = SetOrderExpirationTime(1);

Probably that code is not correct.

SetOrderExpirationTime is returned correct time but if I use that code and if I order buy stop that is not returned expiration time.

Buy Stop Function is returned  this time "1970.01.01 00:00:00".

 
Kosei S #:

No I mean if I use Buy Stop Function I couldn't return correct time.

Probably that code is not correct.

SetOrderExpirationTime is returned correct time but if I use that code and if I order buy stop that is not returned expiration time.

Buy Stop Function is returned  this time "1970.01.01 00:00:00".


You may have forgotten this part:

https://www.mql5.com/en/docs/constants/tradingconstants/orderproperties#enum_order_type_time

struct MqlTradeRequest
  {
   ENUM_TRADE_REQUEST_ACTIONS    action;           // Trade operation type
   ulong                         magic;            // Expert Advisor ID (magic number)
   ulong                         order;            // Order ticket
   string                        symbol;           // Trade symbol
   double                        volume;           // Requested volume for a deal in lots
   double                        price;            // Price
   double                        stoplimit;        // StopLimit level of the order
   double                        sl;               // Stop Loss level of the order
   double                        tp;               // Take Profit level of the order
   ulong                         deviation;        // Maximal possible deviation from the requested price
   ENUM_ORDER_TYPE               type;             // Order type
   ENUM_ORDER_TYPE_FILLING       type_filling;     // Order execution type
   ENUM_ORDER_TYPE_TIME          type_time;        // Order expiration type 
   datetime                      expiration;       // Order expiration time (for the orders of ORDER_TIME_SPECIFIED type)
   string                        comment;          // Order comment
   ulong                         position;         // Position ticket
   ulong                         position_by;      // The ticket of an opposite position
  };

and you can also look for some examples at:

https://www.mql5.com/en/code

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Yohana Parmi #:


You may have forgotten this part:

https://www.mql5.com/en/docs/constants/tradingconstants/orderproperties#enum_order_type_time

and you can also look for some examples at:

https://www.mql5.com/en/code

Thank you so much. It is working.

request.type_time = ORDER_TIME_SPECIFIED;
request.expiration = SetOrderExpirationTime(1);
 
Kosei S #:

Thank you so much. It is working.

I'm glad to hear you've found a solution :)

 
Kosei S #:

Thank you so much. It is working.

If you struggle and no one can give you some advice, you may always look at the MQL5 reference manual, you will always find a solution there, there are so many ways to do things. I am happy you found a solution