Algo/Scalping EA with ZLSMA and Chandelier Exit
farhadkrup 1f8e563214 Add Scalping EA with ZLSMA and Chandelier Exit
Signed-off-by: farhadkrup <farhadkrup@noreply.mql5.com>
2025-11-30 23:09:43 +00:00

208 lines
No EOL
12 KiB
Text

//+------------------------------------------------------------------+
//| ScalpingEA.mq5 |
//| Copyright 2024, Your Name Here |
//| https://www.yoursite.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, Your Name Here"
#property link "https://www.yoursite.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Input parameters |
//+------------------------------------------------------------------+
input double LotSize = 0.01; // Lot size
input int StopLoss = 50; // Stop Loss in points
input int TakeProfit = 100; // Take Profit in points
input int MagicNumber = 12345; // Magic Number
input int Slippage = 3; // Slippage
// ZLSMA parameters
input int ZLSMA_Period = 50; // ZLSMA Period
input int ZLSMA_Price = PRICE_CLOSE; // ZLSMA Price type
// Chandelier Exit parameters
input int CE_Period = 22; // Chandelier Exit Period
input double CE_Multiplier = 3.0; // Chandelier Exit Multiplier
// Additional indicators for better signals
input int RSI_Period = 14; // RSI Period
input int ATR_Period = 14; // ATR Period for volatility
//+------------------------------------------------------------------+
//| Global variables |
//+------------------------------------------------------------------+
int zlsma_handle, ce_handle, rsi_handle, atr_handle;
double zlsma[], ce_high[], ce_low[], rsi[], atr[];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create indicator handles
zlsma_handle = iCustom(Symbol(), Period(), "Zero Lag Smoothed MA", ZLSMA_Period, ZLSMA_Price);
ce_handle = iCustom(Symbol(), Period(), "Chandelier Exit", CE_Period, CE_Multiplier);
rsi_handle = iRSI(Symbol(), Period(), RSI_Period, PRICE_CLOSE);
atr_handle = iATR(Symbol(), Period(), ATR_Period);
if(zlsma_handle == INVALID_HANDLE || ce_handle == INVALID_HANDLE)
{
Print("Error creating indicator handles");
return(INIT_FAILED);
}
// Set array as series
ArraySetAsSeries(zlsma, true);
ArraySetAsSeries(ce_high, true);
ArraySetAsSeries(ce_low, true);
ArraySetAsSeries(rsi, true);
ArraySetAsSeries(atr, true);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Get current indicator values
if(CopyBuffer(zlsma_handle, 0, 0, 3, zlsma) < 3) return;
if(CopyBuffer(ce_handle, 0, 0, 3, ce_high) < 3) return; // Upper exit
if(CopyBuffer(ce_handle, 1, 0, 3, ce_low) < 3) return; // Lower exit
if(CopyBuffer(rsi_handle, 0, 0, 3, rsi) < 3) return;
if(CopyBuffer(atr_handle, 0, 0, 3, atr) < 3) return;
double current_price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
double ask_price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
// Check for buy signal
if(IsBuySignal())
{
if(CountPositions() == 0)
{
double sl = current_price - StopLoss * _Point;
double tp = current_price + TakeProfit * _Point;
OpenTrade(ORDER_TYPE_BUY, LotSize, ask_price, sl, tp);
}
}
// Check for sell signal
if(IsSellSignal())
{
if(CountPositions() == 0)
{
double sl = current_price + StopLoss * _Point;
double tp = current_price - TakeProfit * _Point;
OpenTrade(ORDER_TYPE_SELL, LotSize, current_price, sl, tp);
}
}
}
//+------------------------------------------------------------------+
//| Buy signal condition |
//+------------------------------------------------------------------+
bool IsBuySignal()
{
// Price above ZLSMA and ZLSMA trending up
bool zlsma_bullish = (zlsma[0] > zlsma[1]) && (zlsma[1] > zlsma[2]);
// Price above Chandelier Exit lower band
double current_price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
bool above_ce_low = current_price > ce_low[0];
// RSI not overbought
bool rsi_ok = rsi[0] < 70;
return (zlsma_bullish && above_ce_low && rsi_ok);
}
//+------------------------------------------------------------------+
//| Sell signal condition |
//+------------------------------------------------------------------+
bool IsSellSignal()
{
// Price below ZLSMA and ZLSMA trending down
bool zlsma_bearish = (zlsma[0] < zlsma[1]) && (zlsma[1] < zlsma[2]);
// Price below Chandelier Exit upper band
double current_price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
bool below_ce_high = current_price < ce_high[0];
// RSI not oversold
bool rsi_ok = rsi[0] > 30;
return (zlsma_bearish && below_ce_high && rsi_ok);
}
//+------------------------------------------------------------------+
//| Open trade function |
//+------------------------------------------------------------------+
bool OpenTrade(ENUM_ORDER_TYPE type, double lot, double price, double sl, double tp)
{
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_DEAL;
request.symbol = Symbol();
request.volume = lot;
request.type = type;
request.price = price;
request.sl = sl;
request.tp = tp;
request.deviation = Slippage;
request.magic = MagicNumber;
if(OrderSend(request, result))
{
Print("Trade opened: ", result.comment);
return true;
}
else
{
Print("Error opening trade: ", GetLastError());
return false;
}
}
//+------------------------------------------------------------------+
//| Count open positions |
//+------------------------------------------------------------------+
int CountPositions()
{
int count = 0;
for(int i = 0; i < PositionsTotal(); i++)
{
if(PositionGetSymbol(i) == Symbol() && PositionGetInteger(POSITION_MAGIC) == MagicNumber)
count++;
}
return count;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(zlsma_handle != INVALID_HANDLE) IndicatorRelease(zlsma_handle);
if(ce_handle != INVALID_HANDLE) IndicatorRelease(ce_handle);
if(rsi_handle != INVALID_HANDLE) IndicatorRelease(rsi_handle);
if(atr_handle != INVALID_HANDLE) IndicatorRelease(atr_handle);
}
//+------------------------------------------------------------------+
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}