Algorithm

 
//+------------------------------------------------------------------+
//| GoldenCross_EA.mq5 |
//| Copyright 2026, Trading Bot |
//+------------------------------------------------------------------+
#property copyright "Trading Bot"
#property version "1.00"
#include <Trade\Trade.mqh>

CTrade trade;

// Input parameters
input double InpRiskPercent = 1.0; // Risk Management (% of Balance)
input int InpStopLossPips = 30; // Stop Loss in Pips
input int InpTakeProfitPips = 60; // Take Profit in Pips
input int InpFastEMA = 50; // Fast EMA Period
input int InpSlowEMA = 200; // Slow EMA Period

// Global variables
int handleFastEMA;
int handleSlowEMA;
datetime lastBarTime;

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Initialize EMA Indicators
   handleFastEMA = iMA(_Symbol, _Period, InpFastEMA, 0, MODE_EMA, PRICE_CLOSE);
   handleSlowEMA = iMA(_Symbol, _Period, InpSlowEMA, 0, MODE_EMA, PRICE_CLOSE);

   if(handleFastEMA == INVALID_HANDLE || handleSlowEMA == INVALID_HANDLE)
     {
      Print("Error creating EMA indicator handles!");
      return(INIT_FAILED);
     }

   lastBarTime = 0;
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(handleFastEMA);
   IndicatorRelease(handleSlowEMA);
  }

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
  {
   // Check for new bar to avoid multiple entries on same candle
   datetime currentBarTime = iTime(_Symbol, _Period, 0);
   if(currentBarTime == lastBarTime) return;

   // Check if we already have an open position
   if(PositionsTotal() > 0) return;

   // Copy EMA values for last 2 closed bars
   double fastEMA[];
   double slowEMA[];
   ArraySetAsSeries(fastEMA, true);
   ArraySetAsSeries(slowEMA, true);

   if(CopyBuffer(handleFastEMA, 0, 1, 2, fastEMA) < 2 ||
      CopyBuffer(handleSlowEMA, 0, 1, 2, slowEMA) < 2)
     {
      return;
     }

   // Golden Cross Logic: Fast EMA crossed above Slow EMA on completed bar
   bool isGoldenCross = (fastEMA[1] > slowEMA[1]) && (fastEMA[2] <= slowEMA[2]);

   if(isGoldenCross)
     {
      lastBarTime = currentBarTime; // Update bar time
      ExecuteBuyTrade();
     }
  }

//+------------------------------------------------------------------+
//| Function to calculate Lot Size based on Risk Percentage |
//+------------------------------------------------------------------+
double CalculateLotSize(double slPips)
  {
   double balance = AccountInfoDouble(ACCOUNT_BALANCE);
   double riskAmount = balance * (InpRiskPercent / 100.0);
   
   double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
   double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
   
   if(tickValue == 0 || point == 0) return SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);

   double slPoints = slPips * 10; // Convert Pips to Points
   double riskPerLot = (slPoints * point / tickSize) * tickValue;
   
   double lotSize = riskAmount / riskPerLot;

   // Normalize lot size to broker limits
   double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
   double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);

   lotSize = MathFloor(lotSize / lotStep) * lotStep;
   
   if(lotSize < minLot) lotSize = minLot;
   if(lotSize > maxLot) lotSize = maxLot;

   return lotSize;
  }

//+------------------------------------------------------------------+
//| Function to Open Buy Trade |
//+------------------------------------------------------------------+
void ExecuteBuyTrade()
  {
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);

   double sl = ask - (InpStopLossPips * 10 * point);
   double tp = ask + (InpTakeProfitPips * 10 * point);
   double lotSize = CalculateLotSize(InpStopLossPips);

   trade.Buy(lotSize, _Symbol, ask, sl, tp, "Golden Cross Buy");
  }
//+------------------------------------------------------------------+
 
Som Rat:
Decent Educational starter needs work . 
 
Som Rat:
whats the question?