Certainly! Below is an example of an expert advisor that uses the W%R indicator on a 4-hour timeframe in MetaTrader 5 (MQL5 language). It opens a long position when the indicator crosses into the overbought area, closes the position when it exits the overbought zone, and does the same for a short position in the oversold area. The lot size is based on a certain percentage of the current equity in the account. Please note that this code is for educational purposes only and should be thoroughly tested before using on a live trading account.
// Expert Advisor input parameters
input double LotPercentage = 0.01; // Percentage of equity to trade
input int StopLoss = 100; // Stop Loss in pips
input int TakeProfit = 200; // Take Profit in pips
// Expert Advisor initialization function
int OnInit()
{
// Set the W%R indicator for the 4-hour timeframe
int indicatorHandle = iWPR(_Symbol, PERIOD_H4, 14, 0);
if(indicatorHandle == INVALID_HANDLE)
Print("Failed to load the W%R indicator!");
return(INIT_SUCCEEDED);
}
// Expert Advisor deinitialization function
void OnDeinit(const int reason)
{
// Unload the W%R indicator
IndicatorRelease(indicatorHandle);
}
// Expert Advisor start function
void OnTick()
{
// Check if there is already an open position
if(PositionSelect(_Symbol))
return; // Exit if there is an open position
// Check the W%R indicator value
double wrValue = iWPR(_Symbol, PERIOD_H4, 14, 0);
// Calculate lot size based on the account equity
double lotSize = AccountEquity() * LotPercentage / 100.0 / _Point;
// Open a long position when the W%R indicator crosses into the overbought area
if(wrValue > -20 && iWPR(_Symbol, PERIOD_H4, 14, 1) <= -20)
{
Print("Opening a long position");
OrderSend(_Symbol, OP_BUY, lotSize, Ask, 10, Ask - StopLoss * _Point, Ask + TakeProfit * _Point);
}
// Open a short position when the W%R indicator crosses into the oversold area
if(wrValue < -80 && iWPR(_Symbol, PERIOD_H4, 14, 1) >= -80)
{
Print("Opening a short position");
OrderSend(_Symbol, OP_SELL, lotSize, Bid, 10, Bid + StopLoss * _Point, Bid - TakeProfit * _Point);
}
}
// Expert Advisor stop function
void OnStop()
{
// Close any open positions at EA stop
for(int i=PositionsTotal()-1; i>=0; i--)
{
if(PositionGetTicket(i) > 0)
OrderClose(PositionGetTicket(i), PositionGetDouble(POSITION_VOLUME));
}
}
Please note that this is a basic example and may require further improvements depending on your specific requirements and trading strategy.