Waddah Attar Win
Download scriptThis trading script is designed to automate a specific grid-based strategy on the MetaTrader platform. It manages both buy and sell positions simultaneously by placing pending orders at set intervals, aiming to capture market movements regardless of direction.
Core Operational Logic
1. Profit Monitoring and Reset The system continuously tracks your account balance. It is programmed with a specific profit target (defined as a minimum dollar amount). Once the account equity reaches this target, the script automatically closes all open positions and deletes any remaining pending orders. After hitting this goal, it resets its memory to begin a new cycle.
2. Initial Trade Placement When the script starts or after clearing previous trades, it identifies that no active orders exist for its specific ID. It then immediately places two "Limit" orders:
- A Buy Limit order placed a fixed distance below the current market price.
- A Sell Limit order placed a fixed distance above the current market price.
3. Dynamic Grid Expansion The script monitors the prices of the most recent buy and sell orders. If the market moves significantly toward these positions, it automatically places new pending orders at consistent intervals (the "Step").
- If the price approaches an existing Buy Limit, it places another one further down.
- If the price approaches an existing Sell Limit, it places another one further up.
This creates a "grid" of potential entries, ensuring that as the market fluctuates, the script maintains a presence in both directions.
4. Automated Management To ensure the system runs independently, it utilizes a unique "Magic Number." This allows the script to distinguish its own trades from any other manual trades or different automated systems you might be running on the same account. It specifically handles:
- Closing only the trades it opened.
- Deleting only the pending orders it created.
- Calculating the correct lot sizes based on the initial parameters provided.
Summary for the User
In simple terms, this is a grid trading bot. It places "safety nets" (limit orders) both above and below the current price. As the market moves, it builds a ladder of these orders. Once your account hits the profit goal you defined, it wipes the board clean and starts the process over again.
#property link "waddahattar@hotmail.com"
extern int Step=120;
extern double FirstLot=0.1;
extern double IncLot=0;
extern double MinProfit=450;
extern int Magic = 2008;
double gLotSell=0;
double gLotBuy=0;
double LSP,LBP;
int init()
{
Comment("Waddah Attar Win");
GlobalVariableSet("OldBalance",AccountBalance());
return(0);
}
int deinit()
{
Comment("");
return(0);
}
int start()
{
double i;
double sl,p;
if (AccountEquity()>=GlobalVariableGet("OldBalance")+MinProfit)
{
DeletePendingOrders(Magic);
CloseOrders(Magic);
GlobalVariableSet("OldBalance",0);
}
GlobalVariableSet("OldBalance",AccountBalance());
if (MyOrdersTotal(Magic)==0)
{
OrderSend(Symbol(),OP_BUYLIMIT,FirstLot,Ask-Step*Point,3,0,0,"",Magic,0,Green);
OrderSend(Symbol(),OP_SELLLIMIT,FirstLot,Bid+Step*Point,3,0,0,"",Magic,0,Red);
}
LSP=GetLastSellPrice(Magic);
LBP=GetLastBuyPrice(Magic);
if((LSP-Bid)<=5*Point)
{
OrderSend(Symbol(),OP_SELLLIMIT,gLotSell+IncLot,LSP+Step*Point,3,0,0,"",Magic,0,Red);
}
if((Ask-LBP)<=5*Point)
{
OrderSend(Symbol(),OP_BUYLIMIT,gLotBuy+IncLot,LBP-Step*Point,3,0,0,"",Magic,0,Red);
}
return(0);
}
int DeletePendingOrders(int Magic)
{
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >= 0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol() && (OrderType()!=OP_BUY || OrderType()!=OP_SELL))
{
OrderDelete(OrderTicket());
}
}
return(0);
}
int CloseOrders(int Magic)
{
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >= 0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol())
{
if (OrderType()==OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3);
}
if (OrderType()==OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3);
}
}
}
return(0);
}
int MyOrdersTotal(int Magic)
{
int c=0;
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol())
{
c++;
}
}
return(c);
}
double GetLastBuyPrice(int Magic)
{
int total=OrdersTotal()-1;
for (int cnt = total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol() && (OrderType()==OP_BUYLIMIT || OrderType()==OP_BUY))
{
gLotBuy=OrderLots();
return(OrderOpenPrice());
break;
}
}
return(0);
}
double GetLastSellPrice(int Magic)
{
int total=OrdersTotal()-1;
for (int cnt = total ; cnt >=0 ; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol() && (OrderType()==OP_SELLLIMIT ||OrderType()==OP_SELL))
{
gLotSell=OrderLots();
return(OrderOpenPrice());
break;
}
}
return(100000);
}
Profitability Reports
Expected payoff
Average expected payoff across the available backtests for each symbol.
Comments