This automated trading script is designed to execute a single trade based on the previous day's price movement. It follows a systematic logic to determine whether to enter a "Buy" or "Sell" position at a specific time of day.
Core Trading Logic
The script operates by analyzing the relationship between the opening and closing prices of the previous trading day:
- Sell Signal: If the previous day’s opening price was higher than its closing price (indicating a downward trend or a "down" day), the system prepares to open a Sell position.
- Buy Signal: If the previous day’s opening price was lower than its closing price (indicating an upward trend or an "up" day), the system prepares to open a Buy position.
Execution Controls
To ensure the script behaves predictably, it incorporates several automated rules:
- Time Window Management: The script does not trade constantly. It is programmed to activate only within a specific 5-minute window starting at a user-defined time (set in Greenwich Mean Time). Once this window passes or if the current time is outside of it, the script remains inactive.
- Single Trade Enforcement: The logic includes a "one-time" execution rule. It tracks whether a trade has already been successfully opened during its active cycle; if a trade is live, it prevents the system from opening duplicate positions.
- Risk Management: Users can define specific parameters for every trade, including:
- Lot Size: The volume of the trade.
- Stop Loss: A safety net to automatically close a trade if it moves against the position by a certain amount.
- Take Profit: A target price at which the system will automatically close the trade to lock in gains.
- Slippage: A tolerance level for variations in price during high-speed market movement.
Market Interaction
The script is flexible regarding the assets it monitors. It can be set to trade on the current chart being viewed or can be specifically pointed toward a different currency pair (such as EURUSD). When a signal is triggered, it automatically calculates the correct pricing levels for the "Stop Loss" and "Take Profit" based on the current market bid/ask prices and executes the order immediately.
//+------------------------------------------------------------------+
//| SHE_kanskigor.mq4 |
//| Copyright © 2006, Shurka |
//| shforex@narod.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Shurka"
#property link "shforex@narod.ru"
#define MAGIC 130306
extern double Lots=0.1; // Êîëè÷åñòâî ëîòîâ äëÿ òîðãîâ
extern int Profit=100; // Âåëè÷èíà òåéêïðîôèòà, åñëè 0 - èãðàåì áåç ïðîôèòà
extern int Stop=0; // Âåëè÷èíà ñòîïëîññà, åñëè 0 - èãðàåì áåç ñòîïà
extern int Slippage=5; // Ïðîñêàëüçûâàíèå
extern string Symb="*"; // Ñèìâîë äëÿ òîðãîâ. Åñëè * òî ïî òåêóùåìó ñèìâîëó ãðàôèêà
// èíà÷å íóæíî âïèñàòü èíñòðóìåíò òèïà EURUSD
extern string StartTime="00:05";// Âðåìÿ ñòàðòà ïî ãðèíâè÷ó
datetime TimeStart;
double stoplevel,profitlevel;
string SMB;
bool trade=false;
//+------------------------------------------------------------------+
//| Îñíîâíàÿ ôóíêöèÿ |
//+------------------------------------------------------------------+
int start()
{
int i,b;
// Ïåðåâîäèì âðåìÿ èç ñòðî÷íîé âåëè÷èíû StartTime âî âðåìåííÓþ TimeStart
TimeStart=StrToTime(StartTime);
// Åñëè òåêóùåå âðåìÿ ìåíüøå ñòàðòîâîãî èëè áîëüøå åãî íà 5 ìèíóò, òî âûõîäèì è íè÷åãî íå äåëàåì.
// Íî ïðåäâàðèòåëüíî äåëàåì ïåðåìåííóþ trade ëîæíîé. Ïðîñòî ñáðàñûâàåì èíôîðìàöèþ î òîì, ÷òî óæå îòêðûâàëèñü.
if(CurTime()<TimeStart || CurTime()>TimeStart+300) { trade=false; return(0); }
// Åñëè trade èñòèííà, çíà÷èò óæå óñïåëè îòêðûòüñÿ.
if(trade) return(0);
// Åñëè öåíà îòêðûòèÿ â÷åðà áûëà áîëüøå öåíû çàêðûòèÿ, çíà÷èò ïîêóïàåì èíà÷å ïðîäà¸ì
if(iOpen(SMB,PERIOD_D1,1)>iClose(SMB,PERIOD_D1,1)) b=OP_BUY; else b=OP_SELL;
// Åñëè ïîêóïàåì
if(b==OP_BUY)
{
/*
dbTakeProfit=Ask+TakeProfit*Point;
dsTakeProfit=Bid-TakeProfit*Point;
numorder = OrderSend(Symbol(), OP_BUY, dLots, Ask, nSlippage, dbStopLoss, dbTakeProfit, sNameExpert, MAGIC, 0, colorOpenBuy);
*/
if(Stop==0) stoplevel=0;
else stoplevel= Bid-Stop*Point; //MarketInfo(SMB,MODE_ASK)-Stop*MarketInfo(SMB,MODE_POINT);
// Òî æå è ñ ïðîôèò óðîâíåì
if(Profit==0) profitlevel=0; else profitlevel=Ask+Profit*Point; //MarketInfo(SMB,MODE_ASK)+Profit*MarketInfo(SMB,MODE_POINT);
// Îòêðûâàåìñÿ â ïîêóïêó îò öåíû Ask ñî ñòîïîì stoplevel è ïðîôèòîì profitlevel
i=OrderSend(SMB,OP_BUY,Lots,Ask,Slippage,stoplevel,profitlevel,NULL,MAGIC,0,Red);
// Åñëè îðäåð óäà÷íî îòêðûëñÿ, òî èíäèêàòîð torg âçâîäèì â èñòèíó, ÷òîáû áîëüøå ïîêà íå òîðãîâàòü
if(i!=-1) trade=true;
else Print("OrderSend failed with error #", GetLastError());
}
// Ñ ïðîäàæåé òî æå ñàìîå, ÷òî è ñ ïîêóïêîé.
if(b==OP_SELL)
{
if(Stop==0) stoplevel=0;
else stoplevel= Ask+Stop*Point;// MarketInfo(SMB,MODE_BID)+Stop*MarketInfo(SMB,MODE_POINT);
if(Profit==0) profitlevel=0;
else profitlevel=Bid-Profit *Point;//MarketInfo(SMB,MODE_BID)-Profit*MarketInfo(SMB,MODE_POINT);
i=OrderSend(SMB,OP_SELL,Lots,MarketInfo(SMB,MODE_BID),Slippage,stoplevel,profitlevel,NULL,MAGIC,0,Blue);
if(i!=-1) trade=true;
else Print("OrderSend failed with error #", GetLastError());
}
return(0);
}
//+------------------------------------------------------------------+
//| Ôóíêöèÿ èíèöèàëèçàöèè ñîâåòíèêà |
//+------------------------------------------------------------------+
int init()
{
int i;
// Îïðåäåëÿåì ïàðó äëÿ òîðãîâëè
if(Symb=="*") SMB=Symbol(); else SMB=Symb;
return(0);
}
//+------------------------------------------------------------------+
//| Ôóíêöèÿ äåèíèöèàëèçàöèè ñîâåòíèêà |
//+------------------------------------------------------------------+
int deinit() { return(0); }
//+------------------------------------------------------------------+
Comments