Set signal expiry

To add comments, please log in or register
Nelson Wanyama
388
Nelson Wanyama  
Hello everyone. I'd like to learn how to set a signal's expiry in EA's. Such that the signal is nolonger traded after, say four bars. 
I will appreciate your help.
Mt5 user.
Vladimir Karputov
Moderator
169525
Vladimir Karputov  
Nelson Wanyama :
Hello everyone. I'd like to learn how to set a signal's expiry in EA's. Such that the signal is nolonger traded after, say four bars. 
I will appreciate your help.
Mt5 user.

I think it’s easier to describe the algorithm in words:

  • when you opened a position (you need to catch a position in OnTradeTransaction, catch the transaction TRADE_TRANSACTION_DEAL_ADD, catch the deal DEAL_ENTRY_IN) remember the time the bar opened (let it be the variable m_lasd_deal_IN).
  • in the signal module (if a signal appears) you are requesting four values using CopyTime . Compare the time m_lasd_deal_IN and the resulting array
Documentation on MQL5: Timeseries and Indicators Access / CopyTime
Documentation on MQL5: Timeseries and Indicators Access / CopyTime
  • www.mql5.com
The function gets to time_array history data of bar opening time for the specified symbol-period pair in the specified quantity. It should be noted that elements ordering If you know the amount of data you need to copy, it should better be done to a statically allocated buffer, in order to prevent the allocation of excessive memory. No matter...
Nelson Wanyama
388
Nelson Wanyama  
Vladimir Karputov:

I think it’s easier to describe the algorithm in words:

  • when you opened a position (you need to catch a position in OnTradeTransaction, catch the transaction TRADE_TRANSACTION_DEAL_ADD, catch the deal DEAL_ENTRY_IN) remember the time the bar opened (let it be the variable m_lasd_deal_IN).
  • in the signal module (if a signal appears) you are requesting four values using CopyTime . Compare the time m_lasd_deal_IN and the resulting array
I get the idea but it is still a little unclear. I'm good at following written codes. Please attach an example
Vladimir Karputov
Moderator
169525
Vladimir Karputov  
Nelson Wanyama :
Hello everyone. I'd like to learn how to set a signal's expiry in EA's. Such that the signal is nolonger traded after, say four bars. 
I will appreciate your help.
Mt5 user.

It is not clear here . Draw a picture.

Vladimir Karputov
Moderator
169525
Vladimir Karputov  
Nelson Wanyama :
I get the idea but it is still a little unclear. I'm good at following written codes. Please attach an example

Here is the code:

//+------------------------------------------------------------------+
//|                                           One deal in N bars.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//--- input parameters
input uchar    InpBars              = 4;           // Deal in N bars
input ulong    InpDeviation         = 10;          // Deviation, in points (1.00045-1.00055=10 points)
input ulong    InpMagic             = 200;         // Magic number
//---
datetime m_last_deal_IN = 0;                       // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- signal
   if(PositionsTotal()==0)
     {
      datetime time[];
      ArraySetAsSeries(time,true);
      int start_pos=0;        // start position
      int count=InpBars+1;    // data count to copy
      if(CopyTime(Symbol(),Period(),start_pos,count,time)!=count)
         return;
      //--- check
      if(time[InpBars]>m_last_deal_IN)
        {
         //--- open position
        }
     }
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      long     deal_ticket       =0;
      long     deal_order        =0;
      long     deal_time         =0;
      long     deal_time_msc     =0;
      long     deal_type         =-1;
      long     deal_entry        =-1;
      long     deal_magic        =0;
      long     deal_reason       =-1;
      long     deal_position_id  =0;
      double   deal_volume       =0.0;
      double   deal_price        =0.0;
      double   deal_commission   =0.0;
      double   deal_swap         =0.0;
      double   deal_profit       =0.0;
      string   deal_symbol       ="";
      string   deal_comment      ="";
      string   deal_external_id  ="";
      if(HistoryDealSelect(trans.deal))
        {
         deal_ticket       =HistoryDealGetInteger(trans.deal,DEAL_TICKET);
         deal_order        =HistoryDealGetInteger(trans.deal,DEAL_ORDER);
         deal_time         =HistoryDealGetInteger(trans.deal,DEAL_TIME);
         deal_time_msc     =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC);
         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_magic        =HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason       =HistoryDealGetInteger(trans.deal,DEAL_REASON);
         deal_position_id  =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);

         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_price        =HistoryDealGetDouble(trans.deal,DEAL_PRICE);
         deal_commission   =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);
         deal_swap         =HistoryDealGetDouble(trans.deal,DEAL_SWAP);
         deal_profit       =HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_comment      =HistoryDealGetString(trans.deal,DEAL_COMMENT);
         deal_external_id  =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID);
        }
      else
         return;
      ENUM_DEAL_ENTRY enum_deal_entry=(ENUM_DEAL_ENTRY)deal_entry;
      if(deal_symbol==Symbol() && deal_magic==InpMagic)
        {
         if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)
           {
            if(deal_entry==DEAL_ENTRY_IN)
              {
               m_last_deal_IN=iTime(Symbol(),Period(),0);
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
Files:
Nelson Wanyama
388
Nelson Wanyama  
Vladimir Karputov:

Here is the code:

Thank you so very much
To add comments, please log in or register