Jade Gumede
Jade Gumede
I have 3 EA that I have completed coding. Debugged and error free. However upon backtesting it's not giving me any results. Can anyone who can maybe assist me and ensure the core is fully functional works
Jade Gumede
Jade Gumede
Hi everyone,

I've developed a Forex EA (Expert Advisor) in MQL4, and the code compiles cleanly without errors. However, when I try to backtest it on the MetaTrader platform, I'm not getting any results.

I'm a bit stuck and could really use some help from the community. I'm currently in a difficult financial situation and can't afford to pay for assistance.

If anyone with MQL4 expertise is willing to take a look at my code and offer some guidance pro bono, I would be incredibly grateful.

Thank you in advance for your time and consideration.

#forex #mql4 #backtesting #trading #help

//+------------------------------------------------------------------+
//| Leon EA.mq4 |
//| Copyright 2023, Tradecube Ltd. |
//| https://www.tradecube.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Tradecube Ltd."
#property link "https://www.tradecube.net"
#property version "2.1"
#property strict

//+------------------------------------------------------------------+
//| Input Parameters |
//+------------------------------------------------------------------+
input string p00 = "EA Settings"; //***** EA Settings *****************
input double Lot = 0.01; // Fixed lot size
input bool useAutoLot = false; // Use auto lot (risk-based)
input double m_risk = 1; // Risked amount % of balance
input int stoploss = 50; // Stop loss in pips
input int takeprofit = 100; // Take profit in pips
input bool trail = true; // Use trailing stops
input int t_start = 20; // Trailing starting point (in pips)
input int t_by = 10; // Trail by (in pips)
input int m_magic = 2560; // Magic number for EA
input double maxSpread = 3.0; // Maximum spread allowed (in pips)
input int breakEvenTrigger = 20; // Break-even trigger in pips

input string p01 = "Indicator Settings"; //***** Indicator Settings **********
input int maPer = 200; // Moving Average period
input int sto_k = 5; // Stochastic K period
input int sto_d = 3; // Stochastic D period
input int sto_s = 3; // Stochastic Slowing
input double stochb_lev = 50; // Buy above this level (Stochastic)
input double stochs_lev = 50; // Sell below this level (Stochastic)

input string p02 = "Trading Filters"; //***** Filters *********************
input int startHour = 9; // Start trading at 09:00
input int endHour = 17; // Stop trading at 17:00
input int atrPeriod = 14; // ATR period
input double minAtr = 0.001; // Minimum acceptable ATR

// Internal Variables
datetime lastTradeTime = 0; // Tracks the last trade time

//+------------------------------------------------------------------+
//| Custom Indicator Wrappers |
//+------------------------------------------------------------------+
double GetBPP(int buffer, int shift) {
double value = iCustom(_Symbol, _Period, "Market/Bruces Price Predictor", buffer, shift);
return (value != EMPTY_VALUE) ? value : 0;
}

double GetStochastic(int buffer, int shift) {
return iStochastic(_Symbol, _Period, sto_k, sto_d, sto_s, MODE_SMA, STO_LOWHIGH, buffer, shift);
}

double GetMA(int shift) {
return iMA(_Symbol, _Period, maPer, 0, MODE_SMA, PRICE_CLOSE, shift);
}

double GetATR() {
return iATR(_Symbol, _Period, atrPeriod, 0);
}

//+------------------------------------------------------------------+
//| Utility Functions |
//+------------------------------------------------------------------+
bool IsSpreadAcceptable() {
double spread = MarketInfo(_Symbol, MODE_SPREAD) / _Point;
return (spread <= maxSpread);
}

bool IsVolatilitySufficient() {
return (GetATR() >= minAtr);
}

bool IsTradingTime() {
int currentHour = Hour();
return (currentHour >= startHour && currentHour <= endHour);
}

//+------------------------------------------------------------------+
//| Signal Function |
//+------------------------------------------------------------------+
int GetSignal() {
int signal = 0;
if (GetBPP(0, 1) > 0 && Close[1] > GetMA(1) && GetStochastic(0, 1) > stochb_lev)
signal = 1; // Buy signal
else if (GetBPP(0, 1) > 0 && Close[1] < GetMA(1) && GetStochastic(0, 1) < stochs_lev)
signal = -1; // Sell signal
return signal;
}

//+------------------------------------------------------------------+
//| Expert Initialization |
//+------------------------------------------------------------------+
int OnInit() {
Print("Leon EA Initialized Successfully.");
return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert Deinitialization |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
Print("Leon EA Deinitialized. Reason: ", reason);
}

//+------------------------------------------------------------------+
//| Main Tick Function |
//+------------------------------------------------------------------+
void OnTick() {
// Skip trading if conditions are not met
if (!IsSpreadAcceptable() || !IsVolatilitySufficient() || !IsTradingTime()) return;

if (trail) ManageTrailingStops();

int signal = GetSignal();
if (signal != 0 && lastTradeTime != Time[0]) {
lastTradeTime = Time[0];
double lotSize = CalculateLotSize(signal);
if (signal == 1) OpenTrade(OP_BUY, lotSize);
if (signal == -1) OpenTrade(OP_SELL, lotSize);
}

ManageBreakEven();
}

//+------------------------------------------------------------------+
//| Calculate Lot Size |
//+------------------------------------------------------------------+
double CalculateLotSize(int direction) {
if (!useAutoLot) return Lot;
double riskAmount = AccountBalance() * m_risk * 0.01;
double price1 = (direction == 1) ? Ask : Bid;
double price2 = (direction == 1) ? Ask - stoploss * _Point : Bid + stoploss * _Point;
double lotSize = riskAmount / ((MathAbs(price1 - price2) / _Point) * SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE));
return NormalizeDouble(lotSize, 2);
}

//+------------------------------------------------------------------+
//| Open Trade Function |
//+------------------------------------------------------------------+
void OpenTrade(int type, double lotSize) {
double price = (type == OP_BUY) ? Ask : Bid;
double sl = (type == OP_BUY) ? price - stoploss * _Point : price + stoploss * _Point;
double tp = (type == OP_BUY) ? price + takeprofit * _Point : price - takeprofit * _Point;
color orderColor = (type == OP_BUY) ? clrBlue : clrRed;

int ticket = OrderSend(_Symbol, type, lotSize, price, 10, sl, tp, "Leon EA", m_magic, 0, orderColor);
if (ticket < 0) {
Print("OrderSend failed. Error: ", GetLastError());
} else {
Print("Trade successfully opened. Ticket: ", ticket);
}
}

//+------------------------------------------------------------------+
//| Manage Trailing Stops |
//+------------------------------------------------------------------+
void ManageTrailingStops() {
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (!OrderSelect(i, SELECT_BY_POS)) continue;
if (OrderSymbol() != _Symbol || OrderMagicNumber() != m_magic) continue;

double newStop = 0;
if (OrderType() == OP_BUY && Bid > OrderOpenPrice() + t_start * _Point) {
newStop = Bid - t_by * _Point;
if (newStop > OrderStopLoss()) {
if (!OrderModify(OrderTicket(), OrderOpenPrice(), newStop, OrderTakeProfit(), 0)) {
Print("Failed to modify BUY stop loss. Error: ", GetLastError());
}
}
}

if (OrderType() == OP_SELL && Ask < OrderOpenPrice() - t_start * _Point) {
newStop = Ask + t_by * _Point;
if (newStop < OrderStopLoss()) {
if (!OrderModify(OrderTicket(), OrderOpenPrice(), newStop, OrderTakeProfit(), 0)) {
Print("Failed to modify SELL stop loss. Error: ", GetLastError());
}
}
}
}
}

//+------------------------------------------------------------------+
//| Manage Break-Even |
//+------------------------------------------------------------------+
void ManageBreakEven() {
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (!OrderSelect(i, SELECT_BY_POS)) continue;
if (OrderSymbol() != _Symbol || OrderMagicNumber() != m_magic) continue;

if (OrderType() == OP_BUY && Bid >= OrderOpenPrice() + breakEvenTrigger * _Point) {
if (OrderStopLoss() < OrderOpenPrice()) {
if (!OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0)) {
Print("Failed to set break-even for BUY order. Error: ", GetLastError());
}
}
}

if (OrderType() == OP_SELL && Ask <= OrderOpenPrice() - breakEvenTrigger * _Point) {
if (OrderStopLoss() > OrderOpenPrice()) {
if (!OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0)) {
Print("Failed to set break-even for SELL order. Error: ", GetLastError());
}
}
}
}
}

//+------------------------------------------------------------------+
Jade Gumede
MQL5.커뮤니티에 등록됨