Open position in specific time

 

Hi and regards. 
I want to open positions in specific times. is there any already written expert or a guide about how to insert some specific times and let the position operates in that times??

 
Hi, did you checked "CodeBase" section?. Regards Greg
 
Hi Greg. yah I took a look there but couldn't find something special for me. 
besides I might have lack of skills to search properly. do you think you might show me some useful links?
any already written scripts or experts to read and understand would be much appreciated too ^__^ thanks
 
If you don't find something you can also have it custom made over here: https://www.mql5.com/en/job
Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • www.mql5.com
Hello freelancers, I am looking for someone to code a new EA for me, that opens a trade when a fibonacci level has been crossed, but price closed above it. The segments will be defined by two zigzag indicators. Please have a look at the attached documentation and let me know if you have any questions Hi programmers, can someone share,rent...
 
babigaf:

Hi and regards. 
I want to open positions in specific times. is there any already written expert or a guide about how to insert some specific times and let the position operates in that times??

Opening and Сlosing on time v2
The EA opens and closes positions at a specified time. The condition is additionally checked: for Buy if the fast iMA on the first bar exceeds the slow iMA if (iMAGet(handle_iMAFast, 1 )>iMAGet(handle_iMASlow, 1 )) { if (! RefreshRates ()) return; price=m_symbol. Ask (); if (Extm_sl> 0.0 ) sl=m_symbol. Bid ()-Extm_sl; if (Extm_tp> 0.0 ) tp=m_symbol. Bid ()+Extm_tp; m_trade.Buy(m_lots,InpSymbol,price,sl,tp); } for Sell if the fast iMA on the first bar is less than the slow iMA if (iMAGet(handle_iMAFast, 1 )<iMAGet(handle_iMASlow, 1 )) { if (! RefreshRates ()) return; price=m_symbol. Bid (); if (Extm_sl> 0.0 ) sl=m_symbol. Ask ()+Extm_sl; if (Extm_tp> 0.0 ) tp=m_symbol. Ask ()-Extm_tp; m_trade.Sell(m_lots,InpSymbol,price,sl,tp); } Only HH:mm are considered in the inputs (time). Inputs: opening time (only HH:mm are considered) position open time closing time (only HH:mm are considered) position close time symbol traded symbol volume transaction position volume sell stop stop loss...
CodeBase | 2017.01.19 17:08 |  Vladimir Karputov | Experts | MetaTrader 5
Opening and Сlosing on time
The EA opens and closes positions at a specified time. Only HH:mm are considered in the inputs (time). Inputs: opening time (only HH:mm are considered) position open time closing time (only HH:mm are considered) position close time symbol traded symbol volume transaction position volume true -> Buy, false -> Sell if 'true ', only 'Buy ' positions are opened; if 'false ', only 'Sell ' ones magic number EA's magic number.
CodeBase | 2017.01.19 16:54 |  Vladimir Karputov | Experts | MetaTrader 5
Opening and Сlosing on time v2
Opening and Сlosing on time v2
  • www.mql5.com
The EA opens and closes positions at a specified time. The condition is additionally checked: for Buy —  if the fast iMA on the first bar exceeds the slow iMA                  {                   (!())                      ;                   price=m_symbol.();                   (Extm_sl>)                      sl=m_symbol.()-Extm_sl...
 
Greg my friend you are awesome
I already wrote this far:
#property copyright "Babak Jafarzadeh"
#property link      "https://goodlibrary.ir"
#property version   "1.00"
#include<Trade\Trade.mqh>
CTrade trade;
input string buyTime;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   string myTime;
   myTime=GetTime();
   
   if(PositionsTotal()==0)
     {
      if(myTime==buyTime)
        {
         trade.Buy(0.01,_Symbol,Ask,0,0,NULL);
        }
     }
   
   Comment(myTime);  
  }
//+------------------------------------------------------------------+
string GetTime()
{
 string times;
 times=TimeToString(TimeLocal(),TIME_DATE|TIME_MINUTES);
 return(times);
}
but the point is I need to insert more than one time specified in buyTime variable. I already guess I need array for that purpose. but I don't know how. can you head me to the right direction?
 

Hi. You can use a txt file as the source of your times. Here is an example code.

#property copyright "Armin Ahmadi"
#property link      "arminahmadi.official@gmail.com"
#property version   "1.00"

input int fault_interval = 2;       // Maximum offset from target time (Minutes)
input string source = "times.txt";  // Data source

struct Order
{
   datetime time;
   int type;
   bool placed;
};

Order orders[];
bool load = false;

int OnInit()
{
   load = LoadOrdersTimes();
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   if(!load)
   {
      return;
   }
   datetime current = TimeCurrent();
   int closest = 0;
   int remaining = 0;
   for(int i = 0; i < ArraySize(orders); i++)
   {
      if(orders[i].placed)
      {
         continue;
      }
      if(current - fault_interval * 60 > orders[i].time)
      {
         orders[i].placed = true;
         continue;
      }
      remaining++;
      if(orders[i].time < orders[closest].time)
      {
         closest = i;
      }
      if(orders[i].time < current + fault_interval * 60)
      {
         PlaceOrder(i);
      }
   }
   string comment = IntegerToString(remaining) + " orders remained";
   if(remaining > 0)
   {
      int minutes = NormalizeDouble((orders[closest].time - current) / 60, 0);
      comment += "\n" + IntegerToString(minutes) + " minutes until next order";
   }
   Comment(comment);
}

void PlaceOrder(int i)
{
   orders[i].placed = true;
   if(orders[i].type == 0)
   {
      // Place your buy order here
      Print("Buy order has been successfuly placed at " + TimeToString(TimeCurrent()));
   }
   if(orders[i].type == 1)
   {
      // Place your sell order here
      Print("Sell order has been successfuly placed at " + TimeToString(TimeCurrent()));
   }
}

bool LoadOrdersTimes()
{
   ArrayFree(orders);
   int handle = FileOpen(source, FILE_READ|FILE_ANSI|FILE_TXT);
   if(handle == INVALID_HANDLE)
   {
      Print("Failed to open the file : " + IntegerToString(GetLastError()));
   } 
   else
   {
      string data[];
      int split = StringSplit(FileReadString(handle), StringGetCharacter("|", 0), data);
      if(ArraySize(data) > 0)
      {
         if (ArraySize(data) % 2 == 0)
         {
            for(int i = 0; i < ArraySize(data) - 1; i += 2)
            {
               Order o;
               o.time = StringToTime(data[i]);
               o.type = StringToInteger(data[i + 1]);
               o.placed = false;
               int size = ArraySize(orders);
               ArrayResize(orders, size + 1);
               orders[size] = o;
            }
            Print("Orders has been loaded successfully.");
            return true;
         }
         else
         {
            Print("The source data has the wrong format.");
         }
      }
      else
      {
         Print("No order has been scheduled.");
      }
      FileClose(handle);
   }
   return false;
}

Just make sure to put the right data in the txt file and place it in the right place.

Format : TimeForOrder1|Order1Type|TimeForOrder2|Order2Type ...

Use | to separate the data and format the time like this : year-month-day hour:minute:second

Put the txt file in C:\Users\User\AppData\Roaming\MetaQuotes\Terminal\WeirdCode\MQL5\Files\times.txt

Here is an example for txt file.

2019-11-19 07:24:34|0|2019-11-17 14:56:05|1|2019-11-22 23:00:00|1
 
Do ypu know guys how to do it in Python mt5?
 
Travel #: Do ypu know guys how to do it in Python mt5?

The concept is the same as for MQL. Check the time and when the right time is reached, place the order.

Reason: