//+------------------------------------------------------------------+
//| Signal-based Trading EA |
//| Converts Indicator Signals to Trades |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.0"
input double Lots = 0.1; // Adjustable lot size
input int Slippage = 3; // Slippage in points
input int MagicNumber = 12345; // Unique identifier for EA trades
// Indicator configuration
string IndicatorName = "MT4-LevelStop-Reverse";
int SignalBufferUp = 1; // Buffer for buy signals
int SignalBufferDown = 2; // Buffer for sell signals
// Indicator parameters
input bool UseVTDefaultOptimal = false;
input bool UseATRMode = true;
input int NonATRStopPips = 40;
input int ATRPeriod = 14;
input double ATRMultiplier = 3.0;
input color UpArrowColor = DodgerBlue;
input color DnArrowColor = OrangeRed;
input int ArrowDistance = 25;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Verify indicator is available
if (iCustom(NULL, 0, IndicatorName, UseVTDefaultOptimal, UseATRMode,
NonATRStopPips, ATRPeriod, ATRMultiplier, UpArrowColor, DnArrowColor,
ArrowDistance) == INVALID_HANDLE)
{
Print("Error: Indicator not found - ", IndicatorName);
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
static datetime lastSignalTime = 0;
// Get the current candle time
datetime currentCandleTime = iTime(NULL, 0, 0);
// Skip processing if signal already acted on this candle
if (currentCandleTime == lastSignalTime)
return;
// Read indicator signals
double buySignal = iCustom(NULL, 0, IndicatorName, UseVTDefaultOptimal,
UseATRMode, NonATRStopPips, ATRPeriod, ATRMultiplier, UpArrowColor, DnArrowColor,
ArrowDistance, SignalBufferUp, 0);
double sellSignal = iCustom(NULL, 0, IndicatorName, UseVTDefaultOptimal,
UseATRMode, NonATRStopPips, ATRPeriod, ATRMultiplier, UpArrowColor, DnArrowColor,
ArrowDistance, SignalBufferDown, 0);
// Check for Buy Signal
if (buySignal != EMPTY_VALUE)
{
CloseOrders(OP_SELL); // Close all sell orders
OpenOrder(OP_BUY);
lastSignalTime = currentCandleTime;
}
// Check for Sell Signal
if (sellSignal != EMPTY_VALUE)
{
CloseOrders(OP_BUY); // Close all buy orders
OpenOrder(OP_SELL);
lastSignalTime = currentCandleTime;
}
}
//+------------------------------------------------------------------+
//| Function to open orders |
//+------------------------------------------------------------------+
void OpenOrder(int type)
{
double price = (type == OP_BUY) ? Ask : Bid;
double stopLoss = 0; // Add SL logic if needed
double takeProfit = 0; // Add TP logic if needed
int ticket = OrderSend(Symbol(), type, Lots, price, Slippage, stopLoss,
takeProfit, "EA Trade", MagicNumber, 0, clrGreen);
if (ticket < 0)
{
Print("Error opening order: ", GetLastError());
}
else
{
Print((type == OP_BUY ? "Buy" : "Sell"), " order placed successfully.
Ticket: ", ticket);
}
}
//+------------------------------------------------------------------+
//| Function to close all orders of a specific type |
//+------------------------------------------------------------------+
void CloseOrders(int type)
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber() == MagicNumber &&
OrderSymbol() == Symbol() && OrderType() == type)
{
double price = (type == OP_BUY) ? Bid : Ask;
if (!OrderClose(OrderTicket(), OrderLots(), price, Slippage, clrRed))
{
Print("Error closing order: ", GetLastError());
}
else
{
Print((type == OP_BUY ? "Buy" : "Sell"), " order closed
successfully. Ticket: ", OrderTicket());
}
}
}
}