AutoTraderRus

Download script
Author: Copyright 2016, AM2

This software serves as an automated trading assistant designed to manage the operational windows of your trading platform. Its primary role is to ensure that your trading activity remains within specific pre-defined hours and to automatically handle the opening and closing of trades based on those boundaries.

Core Functionalities

1. Time-Based Session Management The system allows you to set a specific start time and end time for your trading day. It intelligently monitors the current market time and determines whether you are currently within your active "trading session." It is designed to handle standard schedules as well as sessions that span across midnight.

2. Automated Position Liquidation One of the primary features of this script is its safety mechanism. If the current time falls outside of your designated trading window, the system will automatically scan all open positions and close them. This ensures that no trades remain active during times when you do not wish to be exposed to the market, helping to manage risk during off-hours.

3. Automated Trading Permissions (Hotkeys) The script includes a unique automation feature that interacts with your trading terminal's permissions:

  • Auto-Enable: When your designated trading session begins, if the platform’s manual trading permissions are turned off, the script will simulate a keyboard command to enable them automatically.
  • Auto-Disable: When your trading session ends, if the platform's trading permissions are still active, the script will simulate a keyboard command to disable them.

Summary of Workflow

Essentially, the tool acts as an automated "gatekeeper." You define your preferred working hours, and the script ensures that the platform is active only during those times. It proactively closes your trades when your day is over and toggles the terminal's trading capabilities on and off to match your schedule, providing a streamlined, hands-off experience for managing your trading environment.

11 Views
0 Downloads
0 Favorites
AutoTraderRus
//+------------------------------------------------------------------+
//|                                                   AutoTrader.mq5 |
//|                                              Copyright 2016, AM2 |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, AM2"
#property link      "http://www.forexsystems.biz"
#property version   "1.00"

#define VK_CONTROL 0x11 //CTRL key
#define KEY_CODE   'E'

// Ïîäêëþ÷àåì áèáëèîòåêó Windows äëÿ ðàñïîçíàâàíèÿ íàæàòèÿ êëàâèø
#import "user32.dll"
void  keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo);
#import

// Èñïîëüçóåì êëàññ CTrade
#include<Trade\Trade.mqh>

//Îáúåêò êëàññà CTrade
CTrade trade;

// Îáúåêò êëàññà CPositionInfo
CPositionInfo position;

// Âõîäíûå ïåðåìåííûå
input int StartHour   = 9;   // ×àñ íà÷àëà òîðãîâëè
input int StartMinute = 30;  // Ìèíóòà íà÷àëà òîðãîâëè
input int StopHour    = 23;  // ×àñ îêîí÷àíèÿ òîðãîâëè
input int StopMinute  = 30;  // Ìèíóòà îêîí÷àíèÿ òîðãîâëè
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//|  Íàæèìàåì êëàâèøè Ctrl+E                                         |
//+------------------------------------------------------------------+
void Key()
  {
   keybd_event(VK_CONTROL,0,0,0);
   Sleep(10);
   keybd_event(KEY_CODE,0,0,0);
   Sleep(10);
   keybd_event(KEY_CODE,0,2,0);
   Sleep(10);
   keybd_event(VK_CONTROL,0,2,0);
  }
//+------------------------------------------------------------------+
//|   Ïðîâåðêà âðåìåíè òîðãîâëè                                      |
//+------------------------------------------------------------------+
bool TimeSession(int aStartHour,int aStartMinute,int aStopHour,int aStopMinute,datetime aTimeCur)
  {
//--- âðåìÿ íà÷àëà ñåññèè
   int StartTime=3600*aStartHour+60*aStartMinute;
//--- âðåìÿ îêîí÷àíèÿ ñåññèè
   int StopTime=3600*aStopHour+60*aStopMinute;
//--- òåêóùåå âðåìÿ â ñåêóíäàõ îò íà÷àëà äíÿ
   aTimeCur=aTimeCur%86400;
   if(StopTime<StartTime)
     {
      //--- ïåðåõîä ÷åðåç ïîëíî÷ü
      if(aTimeCur>=StartTime || aTimeCur<StopTime)
        {
         return(true);
        }
     }
   else
     {
      //--- âíóòðè îäíîãî äíÿ
      if(aTimeCur>=StartTime && aTimeCur<StopTime)
        {
         return(true);
        }
     }
   return(false);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
// çàêðûâàåì âñå ïîçèöèè ïî âðåìåíè
   if(!TimeSession(StartHour,StartMinute,StopHour,StopMinute,TimeCurrent()))
     {
      for(int i=0; i<PositionsTotal(); i++)
        {
         if(position.Select(PositionGetSymbol(i)))
           {
            // çàêðûòü îòêðûòóþ ïîçèöèþ ïî ýòîìó ñèìâîëó
            trade.PositionClose(PositionGetSymbol(i));
           }
        }
     }

// âêëþ÷àåì ïî âðåìåíè
   if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)==0 && TimeSession(StartHour,StartMinute,StopHour,StopMinute,TimeCurrent())) Key();
// âûêëþ÷àåì ïî âðåìåíè
   if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)==1 && !TimeSession(StartHour,StartMinute,StopHour,StopMinute,TimeCurrent())) Key();

   Comment("\n Enable: ",TerminalInfoInteger(TERMINAL_TRADE_ALLOWED),
           "\n Time: ",TimeCurrent());
  }
//+------------------------------------------------------------------+

Comments