ema values are constant

 

Hello?


why am i getting the constant value of ema 20 50 and 100 as ema 20 = 10 ema 50 = 11 and ema 100 = 12??

//+------------------------------------------------------------------+
//|                                                         EMA Bot  |
//|                                      Copyright 2024, YourName    |
//|                                       https://www.yourwebsite.com|
//+------------------------------------------------------------------+
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Green

// Input parameters
input int EMA_Fast_Period = 20;
input int EMA_Medium_Period = 50;
input int EMA_Slow_Period = 100;
input double Risk_Reward_Ratio = 3.0; // 3% reward for 1% risk
input double Lot_Size = 0.01;
input double StopLoss_Factor = 2.0; // Factor to increase stop loss
input double TakeProfit_Factor = 2.0; // Factor to increase take profit
input double Min_Distance = 20.0; // Minimum distance between stop loss and take profit

// EMA variables
double EMA_Fast, EMA_Medium, EMA_Slow;

// EMA calculation variables
double EMA_Fast_Buffer[], EMA_Medium_Buffer[], EMA_Slow_Buffer[];

//+------------------------------------------------------------------+
//| Function to calculate Simple Moving Average                      |
//+------------------------------------------------------------------+
double SimpleMovingAverage(double &buffer[])
{
    double sum = 0.0;
    for (int i = 0; i < ArraySize(buffer); i++)
    {
        sum += buffer[i];
    }
    return sum / ArraySize(buffer);
}

//+------------------------------------------------------------------+
//| Function to calculate EMA                                       |
//+------------------------------------------------------------------+
void CalculateEMA(double &buffer[], int period, double price, double &result)
{
    // Add the current price to the buffer
    ArrayResize(buffer, ArraySize(buffer) + 1);
    buffer[ArraySize(buffer) - 1] = price;

    // Calculate smoothing factor
    double smoothingFactor = 2.0 / (period + 1);

    // Calculate EMA
    if (ArraySize(buffer) < period)
    {
        // If there are not enough elements in the buffer, use simple moving average
        result = SimpleMovingAverage(buffer);
    }
    else
    {
        // Calculate EMA based on previous EMA value and current price
        result = (price - result) * smoothingFactor + result;
    }
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Determine the maximum period among the three EMAs
    int maxPeriod = MathMax(EMA_Fast_Period, MathMax(EMA_Medium_Period, EMA_Slow_Period));

    // Initialize EMA buffers with enough space
    ArrayResize(EMA_Fast_Buffer, maxPeriod);
    ArrayResize(EMA_Medium_Buffer, maxPeriod);
    ArrayResize(EMA_Slow_Buffer, maxPeriod);
    
    // Rest of your code...

    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    // Cleanup code here
}

void OnTick()
{
    Print("OnTick() function is being executed.");

    // Get the current Ask price
    double Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

    // Calculate EMAs
    EMA_Fast = iMA(_Symbol, 0, EMA_Fast_Period, 0, MODE_EMA, PRICE_CLOSE);
    EMA_Medium = iMA(_Symbol, 0, EMA_Medium_Period, 0, MODE_EMA, PRICE_CLOSE);
    EMA_Slow = iMA(_Symbol, 0, EMA_Slow_Period, 0, MODE_EMA, PRICE_CLOSE);

    // Print EMA prices
    Print("EMA Fast: ", EMA_Fast, ", EMA Medium: ", EMA_Medium, ", EMA Slow: ", EMA_Slow);

    // Entry condition: EMA 20 crosses above EMA 50 and EMA 100
    if (EMA_Fast > EMA_Medium && EMA_Fast > EMA_Slow && EMA_Medium > EMA_Slow)
    {
        Print("Entry condition met. Placing buy order...");

        // Get minimum stop level
        double minStopLevel = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL) * _Point;

        // Calculate risk and reward levels
        double risk = EMA_Medium - Ask; // Calculate the risk (distance between 50 EMA and Ask)
        double reward = risk * Risk_Reward_Ratio; // Set reward as per risk-reward ratio

        // Increase stop loss and take profit levels by a factor
        risk *= StopLoss_Factor;
        reward *= TakeProfit_Factor;

        double stopLoss = NormalizeDouble(Ask - risk, _Digits); // Set stop loss below the current Ask price
        double takeProfit = NormalizeDouble(Ask + reward, _Digits); // Set take profit above the current Ask price

        // Calculate the minimum distance between stop loss and take profit
        double minDistance = Min_Distance * _Point; 

        // Ensure stopLoss and takeProfit are not equal or invalid
        if (stopLoss >= takeProfit || (takeProfit - stopLoss) < minDistance)
        {
            Print("Stop loss price is higher than or equal to take profit price, or minimum distance not met. Adjusting...");
            stopLoss = NormalizeDouble(Ask - minDistance / 2.0, _Digits); // Adjust stop loss
            takeProfit = NormalizeDouble(Ask + minDistance / 2.0, _Digits); // Adjust take profit
        }

        // Ensure stopLoss and takeProfit are not equal or invalid
        if (stopLoss >= Ask || takeProfit <= Ask)
        {
            Print("Calculated stop loss: ", stopLoss, ", take profit: ", takeProfit, ". Invalid stops.");
            return;
        }

        // Prepare trade request
        MqlTradeRequest request;
        ZeroMemory(request);
        request.action = TRADE_ACTION_DEAL; // Immediate order execution
        request.symbol = _Symbol;
        request.volume = Lot_Size;
        request.price = Ask;
        request.sl = stopLoss;
        request.tp = takeProfit;
        request.magic = 123456; // Unique identifier for the bot's trades
        request.type = ORDER_TYPE_BUY; // Buy order

        // Print trade request details
        Print("Sending buy order request...");
         Print("Ask: ", Ask, ", StopLoss: ", stopLoss, ", TakeProfit: ", takeProfit);

        // Send trade request
        MqlTradeResult result;
        ZeroMemory(result);
        if (OrderSend(request, result))
        {
            Print("Buy order placed successfully");
        }
        else
        {
            Print("Error placing buy order: ", GetLastError());
        }
    }
    else
    {
        Print("Entry condition not met. No action taken.");
    }
}
 
    EMA_Fast = iMA(_Symbol, 0, EMA_Fast_Period, 0, MODE_EMA, PRICE_CLOSE);
    EMA_Medium = iMA(_Symbol, 0, EMA_Medium_Period, 0, MODE_EMA, PRICE_CLOSE);
    EMA_Slow = iMA(_Symbol, 0, EMA_Slow_Period, 0, MODE_EMA, PRICE_CLOSE);

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)