time trader
Download scriptThis automated trading script, known as the "Time Trader," is designed to execute trades based strictly on specific clock times rather than complex market indicators or price patterns. It functions as a scheduled execution tool, allowing you to automate entries at the exact hour and minute of your choosing.
Core Functionality
Time-Based Execution The primary logic of the script revolves around the system clock. You can define a specific hour and minute (e.g., 10:30 AM), and the script will monitor the market continuously. When the current time matches your predefined settings, the script triggers a trade automatically.
Trade Management & Constraints
- Buy/Sell Permissions: The script includes independent toggles that allow you to enable or disable "Buy" orders, "Sell" orders, or both.
- Lot Sizing: You can pre-define the volume (Lots) for each trade to ensure consistent position sizing.
- Unique Identification (Magic Numbers): To prevent the script from interfering with other trades or other automated systems you might be running, it uses unique identification numbers. This ensures that it only manages the specific trades it has opened.
Automated Risk Management
Once a trade is successfully opened at your specified time, the script takes over the management of that position by applying safety boundaries:
- Stop Loss & Take Profit: The script automatically calculates and attaches "Take Profit" (target profit) and "Stop Loss" (maximum loss) levels to every trade. It adjusts these values based on the specific decimal precision of the currency pair you are trading, ensuring the orders are valid.
- Active Monitoring: After a trade is placed, the script continuously checks if the profit targets or loss limits have been set correctly. If they are missing for any reason, the script will attempt to modify the active order to include these protections immediately.
Error Handling and Notifications
The script is built with a proactive feedback system to keep you informed of its status:
- Real-time Alerts: If the script encounters issues—such as a trade being rejected by the broker, market conditions changing too quickly (slippage), or the trading subsystem being busy—it will trigger a visual alert on your screen.
- Automatic Retries: In certain scenarios, such as when the "Stop Loss" levels are temporarily invalid due to price fluctuations, the script is programmed to refresh the market data and attempt to place the protection again automatically.
//+------------------------------------------------------------------+
//| time trader.mq4 |
//| Copyright © 2012, www.FxAutomated.com |
//| http://www.FxAutomated.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, www.FxAutomated.com"
#property link "http://www.FxAutomated.com"
//---- input parameters
extern string time_trader_v2="visit www.FxAutomated.com for more!!!";
extern double Lots=0.1;
extern int TakeProfit=20;
extern int StopLoss=20;
extern int Slip=5;
extern string TradeSettings="Mt4 time(min-max): hours 0-23, minutes 0-59, seconds 0-59";
extern bool AllowBuy=true;
extern bool AllowSell=true;
extern int TradeHour=0;
extern int TradeMinutes=0;
extern string MagicNumbers="To be changed in case of conflict with other EAs";
extern int BuyMagicNumber =10001;
extern int SellMagicNumber =10002;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Alert("Visit www.FxAutomated.com for more goodies!");
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert starts |
//+------------------------------------------------------------------+
int start()
{
//----
int StopMultd,Sleeper=1;
int digits=MarketInfo("EURUSD",MODE_DIGITS);
StopMultd=10;
double TP=NormalizeDouble(TakeProfit*StopMultd,Digits);
double SL=NormalizeDouble(StopLoss*StopMultd,Digits);
int Slippage=Slip*StopMultd;
// Calculate stop loss
double slb=NormalizeDouble(Ask-SL*Point,Digits);
double sls=NormalizeDouble(Bid+SL*Point,Digits);
// Calculate take profit
double tpb=NormalizeDouble(Ask+TP*Point,Digits);
double tps=NormalizeDouble(Bid-TP*Point,Digits);
//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
for(int i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
{
if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
{
if(OrderMagicNumber()==BuyMagicNumber) {int halt1=1;}
if(OrderMagicNumber()==SellMagicNumber) {int halt2=1;}
}
}
}
//-------------------------------------------------------------------+
if((halt1!=1)&&(AllowBuy==true)){// halt1
// Buy criteria
if ((TradeHour==Hour())&&(TradeMinutes==Minute())) //Signal Buy
{
int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"time trader buy order ",BuyMagicNumber,0,Blue);
if(openbuy<1){int buyfail=1;}
}
}// halt1
if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
// Sell criteria
if ((TradeHour==Hour())&&(TradeMinutes==Minute())) //Signal Sell
{
int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"time trader sell order ",SellMagicNumber,0,Green);
if(opensell<1){int sellfail=1;}
}
}// halt2
//-----------------------------------------------------------------------------------------------------
if(OrdersTotal()>0){
for(i=1; i<=OrdersTotal(); i++){ // Cycle searching in orders
if (OrderSelect(i-1,SELECT_BY_POS)==true){ // If the next is available
if((OrderMagicNumber()==BuyMagicNumber)&&(OrderTakeProfit()==0||OrderStopLoss()==0)) { OrderModify(OrderTicket(),0,slb,tpb,0,CLR_NONE); }
if((OrderMagicNumber()==SellMagicNumber)&&(OrderTakeProfit()==0||OrderStopLoss()==0)) { OrderModify(OrderTicket(),0,sls,tps,0,CLR_NONE); }
}
}
}
//-------------------------------------------------------------------+
// Error processing
//-------------------------------------------------------------------+
if(buyfail==1||sellfail==1){
int Error=GetLastError();
if(Error==130){Alert("Wrong stops. Retrying."); RefreshRates();}
if(Error==133){Alert("Trading prohibited.");}
if(Error==2){Alert("Common error.");}
if(Error==146){Alert("Trading subsystem is busy. Retrying."); Sleep(500); RefreshRates();}
}
//-------------------------------------------------------------------
return(0);
}
//+-----------------------------------
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments