MT4 Expert Advisor: Reentry Trading with Stop Loss and Take Profit
extern int magicNumber = 92133;
extern double initialLots = 0.5;
extern double takeProfitAmount = 80;
extern int maxReentries = 4;
extern int reentryPips = 100;
extern int slippage = 4;
int totalOrders = 0;
double totalProfit = 0;
bool hasOpenPosition = false;
double entryPrice = 0; // Initialize entryPrice variable
void OnTick()
{
double currentPrice = Ask;
// Check for open position
if (hasOpenPosition)
{
// Check if take profit is reached
if (totalProfit >= takeProfitAmount)
{
CloseAllOrders();
DeleteAllPendings();
Sleep(60000); // Wait for 1 minute before restart
ResetVariables();
return;
}
// Check for reentry conditions
if (OrdersTotal() < maxReentries)
{
if (OrderSelect(totalOrders - 1, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol() && OrderMagicNumber() == magicNumber)
{
double stopLoss = OrderOpenPrice() - reentryPips * Point;
double takeProfit = OrderOpenPrice() + reentryPips * Point;
if (OrderType() == OP_BUY && currentPrice >= entryPrice + reentryPips * Point)
{
entryPrice = currentPrice;
double lots = OrderLots() * 2;
if (lots > MarketInfo(Symbol(), MODE_MINLOT))
{
int ticket = OrderSend(Symbol(), OP_BUYSTOP, lots, currentPrice, slippage, stopLoss, takeProfit, 'Reentry', magicNumber, 0, Blue);
if (ticket > 0)
totalOrders++;
}
}
else if (OrderType() == OP_SELL && currentPrice <= entryPrice - reentryPips * Point)
{
entryPrice = currentPrice;
double lots = OrderLots() * 2;
if (lots > MarketInfo(Symbol(), MODE_MINLOT))
{
int ticket = OrderSend(Symbol(), OP_SELLSTOP, lots, currentPrice, slippage, stopLoss, takeProfit, 'Reentry', magicNumber, 0, Red);
if (ticket > 0)
totalOrders++;
}
}
}
}
}
else
{
// Check for initial entry conditions
if (currentPrice >= currentPrice + 200 * Point)
{
entryPrice = currentPrice;
double stopLoss = currentPrice - 200 * Point;
double takeProfit = currentPrice + 200 * Point;
int ticket = OrderSend(Symbol(), OP_BUYSTOP, initialLots, currentPrice, slippage, stopLoss, takeProfit, 'Initial Entry', magicNumber, 0, Blue);
if (ticket > 0)
{
hasOpenPosition = true;
totalOrders++;
}
}
else if (currentPrice <= currentPrice - 200 * Point)
{
entryPrice = currentPrice;
double stopLoss = currentPrice + 200 * Point;
double takeProfit = currentPrice - 200 * Point;
int ticket = OrderSend(Symbol(), OP_SELLSTOP, initialLots, currentPrice, slippage, stopLoss, takeProfit, 'Initial Entry', magicNumber, 0, Red);
if (ticket > 0)
{
hasOpenPosition = true;
totalOrders++;
}
}
}
}
void OnTrade()
{
totalProfit = 0;
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == magicNumber && (OrderType() == OP_BUY || OrderType() == OP_SELL))
{
totalProfit += OrderProfit();
}
}
}
}
void CloseAllOrders()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == magicNumber)
{
bool result = OrderClose(OrderTicket(), OrderLots(), Bid, slippage, Red);
if (result)
totalOrders--;
}
}
}
}
void DeleteAllPendings()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() == Symbol() && (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP) && OrderMagicNumber() == magicNumber)
{
bool result = OrderDelete(OrderTicket());
if (result)
totalOrders--;
}
}
}
}
void ResetVariables()
{
hasOpenPosition = false;
totalProfit = 0;
totalOrders = 0;
}
This code implements a reentry trading strategy with dynamic lot sizing, stop-loss, and take-profit levels. The EA automatically places buy and sell orders based on pre-defined conditions, and adjusts the lot size based on the current trade's profit. The EA also includes features for closing all orders, deleting pending orders, and resetting variables.
Explanation:
- Magic Number: A unique identifier for the EA's trades.
- Initial Lots: The initial lot size for the first trade.
- Take Profit Amount: The desired profit target in pips.
- Max Reentries: The maximum number of reentry orders allowed.
- Reentry Pips: The number of pips to move the entry price for subsequent reentries.
- Slippage: The maximum acceptable slippage in pips.
Trading Logic:
- Initial Entry: The EA places a buy or sell order when the price moves 200 pips above or below the current price.
- Reentry: After the initial entry, the EA places reentry orders when the price moves the specified number of pips (reentryPips) in the direction of the initial trade.
- Dynamic Lot Sizing: The EA doubles the lot size for each reentry order.
- Stop Loss and Take Profit: The EA sets stop-loss and take-profit levels based on the entry price and the pre-defined parameters.
- Take Profit Exit: The EA closes all open orders and resets the variables when the total profit reaches the specified take profit amount.
- Close All Orders: The EA closes all open orders associated with the EA's magic number.
- Delete All Pendings: The EA deletes all pending orders associated with the EA's magic number.
- Reset Variables: The EA resets all variables to their default values.
Important Notes:
- This EA is for educational purposes only and should not be used for live trading without thorough backtesting and risk management.
- The EA's parameters should be adjusted based on your trading style, risk tolerance, and market conditions.
- It is essential to test the EA thoroughly on historical data before deploying it to a live account.
- Always use appropriate risk management techniques to minimize potential losses.
- This EA does not account for market volatility or news events. It is crucial to stay informed about potential market impacts that could affect your trades.
原文地址: https://www.cveoy.top/t/topic/dpY9 著作权归作者所有。请勿转载和采集!