The script is designed to automate trading based on a weekly breakout strategy. Here's how it works:
-
Weekly Setup: The script looks for specific days and times of the week to initiate trades. You can define the 'start day' (like Tuesday) and 'end day' (like Friday), along with corresponding 'start time' and 'end time' in Greenwich Mean Time (GMT). This establishes a window of time for trading activity.
-
Order Placement: At the beginning of the defined weekly trading window (specified start day and time), the script checks if it has already placed orders. If not, it places two pending orders:
-
A "Buy Stop" order, which is an order to buy if the price rises to a certain level above the current market price. The script calculates this price by adding a small amount (50 "Points," a unit of price movement) to the current ask price (the price you would pay to buy). A stop loss order is also defined for this trade which would execute if price starts to move in the wrong direction.
-
A "Sell Stop" order, which is an order to sell if the price falls to a certain level below the current market price. The script calculates this price by subtracting a small amount (50 Points) from the current bid price (the price you would receive to sell). A stop loss order is also defined for this trade which would execute if price starts to move in the wrong direction.
The key idea is that if the price "breaks out" of its recent range during the week, one of these orders will be triggered, initiating a trade.
-
-
Break-Even Adjustment: The script includes an optional feature to move the stop-loss order of an open trade to the "break-even" point. This means that if the price moves favorably by a certain amount (defined by the 'breakEven' parameter), the stop loss is adjusted to the original entry price. This reduces the risk of the trade by ensuring that, at worst, you won't lose money on the trade (excluding any potential broker fees or slippage).
-
Weekly Closure: At the end of the defined weekly trading window (specified end day and time), the script closes all open orders associated with it. This ensures that any remaining trades are exited before the weekend, regardless of their current profit or loss.
//+------------------------------------------------------------------+
//| WeeklyBreakout.mq4 |
//| Copyright ?2007, Arui |
//| http://www.ea-performance.com |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2007, Arui"
#property link "http://www.ea-performance.com"
// This ea is based on SIBKIS's weekly breakout system
extern int startTime = 0; // GMT time
extern int startDay = 2; // Tuesday
extern int endTime = 19;
extern int endDay = 5; // Close all orders at the end of Friday
extern int stopLoss = 100;
extern double lots = 0.1;
extern int breakEven = 0;
extern int magicNumber= 2007411;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
if(DayOfWeek()==startDay && TimeCurrent() > StrToTime(startTime+":00")
&& TimeCurrent()< StrToTime(startTime+":10")
)
{
if(currentOrders_By_Type(OP_BUYSTOP)==0) // Send BuyStop order
{
double sellPrice = NormalizeDouble(Bid - 50*Point,Digits);
double sellStopPrice = NormalizeDouble(sellPrice + stopLoss*Point,Digits);
OrderSend(Symbol(),OP_SELLSTOP,lots,sellPrice,3,sellStopPrice,0,"weekly breakout 1.0",magicNumber,0,Yellow);
/*
double buyPrice = NormalizeDouble(Ask + 50*Point,Digits);
double buyStopPrice = NormalizeDouble(buyPrice - stopLoss*Point,Digits);
OrderSend(Symbol(),OP_BUYSTOP,lots,buyPrice,3,buyStopPrice,0,"weekly breakout 1.0",magicNumber,0,Green);
*/
}
if(currentOrders_By_Type(OP_SELLSTOP)==0)
{
double buyPrice = NormalizeDouble(Ask + 50*Point,Digits);
double buyStopPrice = NormalizeDouble(buyPrice - stopLoss*Point,Digits);
OrderSend(Symbol(),OP_BUYSTOP,lots,buyPrice,3,buyStopPrice,0,"weekly breakout 1.0",magicNumber,0,Green);
/*
double sellPrice = NormalizeDouble(Bid - 50*Point,Digits);
double sellStopPrice = NormalizeDouble(sellPrice + stopLoss*Point,Digits);
OrderSend(Symbol(),OP_SELLSTOP,lots,sellPrice,3,sellStopPrice,0,"weekly breakout 1.0",magicNumber,0,Yellow);
*/
}
}
if(breakEven > 0) moveToBreakeven();
if(DayOfWeek()==endDay && TimeCurrent()>=StrToTime(endTime+":00")&& TimeCurrent()<=StrToTime(endTime+":10"))
{
closeAll();
}
//----
return(0);
}
//+------------------------------------------------------------------+
int currentOrders_By_Type(int type)
{
int cur_orders = 0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==magicNumber && OrderType()==type)
{
cur_orders++;
}
}
return(cur_orders);
}
void closeAll(){
for(int i=0; i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol() == Symbol() && OrderMagicNumber() == magicNumber)
{
if(OrderType()==OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
}
else if(OrderType()==OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
}
else
{
OrderDelete(OrderTicket());
}
}
}//end for
}// end closeAll
void moveToBreakeven()
{
for (int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if ( OrderSymbol()==Symbol() && OrderMagicNumber()==magicNumber)
{
if (OrderType()==OP_BUY)
{
if ( breakEven > 0 && Bid-OrderOpenPrice()> breakEven*Point && OrderOpenPrice()!= OrderStopLoss() )
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Orange);
}
}//end buy
if (OrderType()==OP_SELL)
{
if ( breakEven > 0 && OrderOpenPrice()-Ask>breakEven*Point && OrderOpenPrice()!= OrderStopLoss())
{
OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Orange);
}
}//end sell
}
}//end for
}// end trailing
Comments