Re: Simple Expert Advisors (EA's) for MT4

391
Hi everyone.

I've been struggling to find a coder for an idea I have for an mt4 ea, and started meddling with chatgpt to create one, but it comes back with errors that MetaEditor can't compile.

I'm not sure if this is the right topic to post this on, but could someone please help correct My errors.

I'll post the code below. Thanks in advance.

//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
input double RiskPercentage = 5.0; // Percentage of account balance to risk
input double BreakEvenPercentage = 35.0; // Percentage of account balance for break-even
input double TrailingStopPercentage = 20.0; // Percentage of total profit to trail by
input int MagicNumber = 123456; // Unique magic number for orders

double initialAccountBalance;
double riskAmount;
double highestFloatingProfit = 0; // Track highest floating profit
bool tradeDirection = false; // False = not trading, True = trading
bool isBuy = false; // Determines if the current trading is Buy or Sell
bool trailingStopActive = false; // Tracks if the trailing stop is active

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
initialAccountBalance = AccountBalance();
riskAmount = (RiskPercentage / 100.0) * initialAccountBalance;

// Create Buy Button
ObjectCreate(0, "BuyButton", OBJ_BUTTON, 0, 0, 0);
ObjectSetInteger(0, "BuyButton", OBJPROP_CORNER, CORNER_LEFT_UPPER);
ObjectSetInteger(0, "BuyButton", OBJPROP_XDISTANCE, 10);
ObjectSetInteger(0, "BuyButton", OBJPROP_YDISTANCE, 10);
ObjectSetInteger(0, "BuyButton", OBJPROP_WIDTH, 60);
ObjectSetInteger(0, "BuyButton", OBJPROP_HEIGHT, 20);
ObjectSetString(0, "BuyButton", OBJPROP_TEXT, "Buy");

// Create Sell Button
ObjectCreate(0, "SellButton", OBJ_BUTTON, 0, 0, 0);
ObjectSetInteger(0, "SellButton", OBJPROP_CORNER, CORNER_LEFT_UPPER);
ObjectSetInteger(0, "SellButton", OBJPROP_XDISTANCE, 80);
ObjectSetInteger(0, "SellButton", OBJPROP_YDISTANCE, 10);
ObjectSetInteger(0, "SellButton", OBJPROP_WIDTH, 60);
ObjectSetInteger(0, "SellButton", OBJPROP_HEIGHT, 20);
ObjectSetString(0, "SellButton", OBJPROP_TEXT, "Sell");

return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Delete Buttons on Deinit
ObjectDelete("BuyButton");
ObjectDelete("SellButton");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Monitor user button input
if (ObjectGetInteger(0, "BuyButton", OBJPROP_STATE)) // Buy Button clicked
{
tradeDirection = true;
isBuy = true;
}
else if (ObjectGetInteger(0, "SellButton", OBJPROP_STATE)) // Sell Button clicked
{
tradeDirection = true;
isBuy = false;
}

// If trading is active, continue to manage trades
if (tradeDirection)
{
ManageMartingale();
}
}
//+------------------------------------------------------------------+
//| Martingale trading logic |
//+------------------------------------------------------------------+
void ManageMartingale()
{
double lotSize = 0.1; // Starting lot size
double totalLoss = 0;
double totalProfit = 0;

// Check existing orders and calculate total profit/loss
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber)
{
totalProfit += OrderProfit();
totalLoss += MathAbs(OrderProfit());
}
}

// Check if we need to close all trades due to reaching risk limit
if (totalLoss >= riskAmount)
{
CloseAllOrders();
tradeDirection = false;
return;
}

// Check if floating profit exceeds break-even percentage and activate trailing stop
if (totalProfit >= (BreakEvenPercentage / 100.0) * AccountBalance())
{
ActivateTrailingStop(totalProfit);
}

// Open new martingale orders if necessary
if (isBuy)
{
OpenMartingaleBuy(lotSize);
}
else
{
OpenMartingaleSell(lotSize);
}

// Close all trades when account is doubled
if (AccountBalance() >= 2 * initialAccountBalance)
{
CloseAllOrders();
tradeDirection = false;
}
}
//+------------------------------------------------------------------+
//| Open buy/sell orders using martingale logic |
//+------------------------------------------------------------------+
void OpenMartingaleBuy(double lotSize)
{
if (OrdersTotal() == 0 || CheckIfNextOrderRequired())
{
OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, 0, 0, "Martingale Buy", MagicNumber, 0, Blue);
}
}

void OpenMartingaleSell(double lotSize)
{
if (OrdersTotal() == 0 || CheckIfNextOrderRequired())
{
OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, 0, 0, "Martingale Sell", MagicNumber, 0, Red);
}
}

//+------------------------------------------------------------------+
//| Activate trailing stop |
//+------------------------------------------------------------------+
void ActivateTrailingStop(double totalProfit)
{
// Ensure trailing stop is only applied if it's not already active
if (!trailingStopActive)
{
trailingStopActive = true;
highestFloatingProfit = totalProfit;
}

// Update highest profit if new floating profit is higher
if (totalProfit > highestFloatingProfit)
{
highestFloatingProfit = totalProfit;
}

// Calculate the current trailing stop level (20% below highest profit)
double trailingStopLevel = highestFloatingProfit - (TrailingStopPercentage / 100.0 * highestFloatingProfit);

// If total profit drops below the trailing stop level, close all trades
if (totalProfit < trailingStopLevel)
{
CloseAllOrders();
tradeDirection = false;
trailingStopActive = false;
}
}
//+------------------------------------------------------------------+
//| Helper functions |
//+------------------------------------------------------------------+
void CloseAllOrders()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber)
{
if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, 3, Red);
if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, 3, Blue);
}
}
}

bool CheckIfNextOrderRequired()
{
// Logic to determine if a new martingale order needs to be opened
// For example, check if the last trade is in loss and apply Martingale
return true;
}