Open position in specific time
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
- www.mql5.com
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??
- www.mql5.com
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
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??