Skip to content

Instantly share code, notes, and snippets.

@vhcandido
Last active January 23, 2017 22:43
Show Gist options
  • Select an option

  • Save vhcandido/883d13d813e60a6555b68fdb24c0f156 to your computer and use it in GitHub Desktop.

Select an option

Save vhcandido/883d13d813e60a6555b68fdb24c0f156 to your computer and use it in GitHub Desktop.
#property copyright ""
#property link "https://www.mql5.com"
#property version "1.00"
#include <myFunctions.mqh>
// Magic number
#define MAGIC_NUMBER 1123581321
// 5min before actual opening time
input datetime _mkt_open = D'10:00';
// 5min before actual closing time
input datetime _mkt_close = D'17:50';
input string input_file = "today_order.dat";
input int n_contracts = 1;
MqlDateTime mkt_open = {0}, mkt_close = {0};
int OnInit() {
TimeToStruct(_mkt_open, mkt_open);
TimeToStruct(_mkt_close, mkt_close);
EventSetTimer(300);
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason) {
//--- destroy timer
EventKillTimer();
}
void OnTimer() {
datetime curTime = TimeLocal();
MqlDateTime curTimeStruct = {0};
TimeToStruct(curTime, curTimeStruct);
bool enterTime = curTimeStruct.hour == mkt_open.hour &&
(curTimeStruct.min >= mkt_open.min && curTimeStruct.min < ( mkt_open.min+5));
bool exitTime = curTimeStruct.hour == mkt_close.hour &&
(curTimeStruct.min >= mkt_close.min && curTimeStruct.min < (mkt_close.min+5));
if(!enterTime && !exitTime) return;
if(TimeToString(curTime, TIME_DATE) > TimeToString(TimeCurrent(), TIME_DATE)) return;
if(exitTime) {
bool closed_any = 0;
Print("Closing position");
closed_any = CloseAllPositions(MAGIC_NUMBER, Symbol(), POSITION_TYPE_BUY);
closed_any = closed_any || CloseAllPositions(MAGIC_NUMBER, Symbol(), POSITION_TYPE_SELL);
if(closed_any) return;
} else if(enterTime) {
// Read integer defining today's operation
ENUM_ORDER_TYPE today_order;
string order = readTodayOrder(input_file);
if(order == "BUY") today_order = ORDER_TYPE_BUY;
else if(order == "SELL") today_order = ORDER_TYPE_SELL;
else {
Print("No orders to open");
return;
}
Print("Opening position");
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_DEAL;
request.magic = MAGIC_NUMBER;
request.symbol = Symbol();
request.volume = n_contracts;
request.type_time = ORDER_TIME_SPECIFIED;
request.expiration = TimeLocal() + 3600; // 1 hour to expirate
request.price = SymbolInfoDouble(Symbol(), today_order == ORDER_TYPE_BUY ? SYMBOL_ASK : SYMBOL_BID);
request.type = today_order;
if(!OrderSend(request, result)) Print("Couldn't place order: ", GetLastError());
}
}
string readTodayOrder(string filename) {
ResetLastError();
string order = "";
// Reading order from file
int file_handle = FileOpen(filename, FILE_READ|FILE_ANSI);
if(file_handle != INVALID_HANDLE) {
if(!FileIsEnding(file_handle)) {
order = FileReadString(file_handle);
}
}
FileClose(file_handle);
// Deleting file's content
file_handle = FileOpen(filename, FILE_WRITE|FILE_ANSI);
if(file_handle != INVALID_HANDLE) FileWriteString(file_handle, "");
FileClose(file_handle);
return order;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment