Cross MA indicator not auto update value

 

Hi, i am trying to learn to program indicator and EA. My indicator doesnt work well, it doesn't auto update the new price except i refresh chart or recompile. 
I dont know why. When the new candle generated, LSMA indicator (the red one) is falling down at 0 value. Can anyone point me in the right direction or how to fix it? I dont really want to make these noob mistakes again and I would highly appreciate it!

I have attached images below.

My code here:

#property copyright "Copyright 2021, Nomocp Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

//--- Drawing Plot

#property indicator_buffers 3
#property indicator_plots 2

//--- LSMA
#property indicator_type1 DRAW_LINE
#property indicator_label1 "LSMA"
#property indicator_color1 clrFireBrick
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2

//--- EMA
#property indicator_type2 DRAW_LINE
#property indicator_label2 "EMA"
#property indicator_color2 clrGreen
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2

//+------------------------------------------------------------------+
//| Custom parameters                                                |
//+------------------------------------------------------------------+
//--- Input parameters
input const string title1 = "__________EMA__________";
input ENUM_MA_METHOD EMA_Line = MODE_EMA;
input int EMA_Period = 10;
input ENUM_TIMEFRAMES EMATimeFrame = PERIOD_CURRENT;

input const string title2 = "__________LSMA__________";
input ENUM_MA_METHOD LSMA_Line = MODE_SMA;
input int LSMA_Period = 30;
input ENUM_TIMEFRAMES LSMATimeFrame = PERIOD_CURRENT;
input ENUM_APPLIED_PRICE LSMA_Applied_Price;

// Variables
double BufferEMA[];
double BufferLSMA[];

int EMAHandle;
int LSMAHandle;
int LongestPeriod;

MqlRates rates[];

//LSMA
double LengthVar;
double L2;
double FixPeriod;
//---

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
//--- indicator buffers mapping
    ArraySetAsSeries(rates, true);
    LongestPeriod = (int) MathMax(EMA_Period, LSMA_Period);
    LSMA();
    EMA();
//---
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom Modules                                                   |
//+------------------------------------------------------------------+


//--- LSMA
void LSMA() {
    LengthVar = (LSMA_Period + 1) / 3.0;
    L2 = LSMA_Period * LengthVar / 2.0;
    FixPeriod = LSMA_Period - LengthVar;

    SetIndexBuffer(0, BufferLSMA, INDICATOR_DATA);
    LSMAHandle = iMA(Symbol(), LSMATimeFrame, LSMA_Period, 0, LSMA_Line, PRICE_CLOSE); 

    ArraySetAsSeries(BufferLSMA, true);
    PlotIndexGetInteger(0, PLOT_DRAW_BEGIN, LongestPeriod); 
}

//--- EMA

void EMA() {
    SetIndexBuffer(1, BufferEMA, INDICATOR_DATA);
    ArraySetAsSeries(BufferEMA, true);
    EMAHandle = iMA(Symbol(), EMATimeFrame, EMA_Period, 0, EMA_Line, PRICE_CLOSE);
    PlotIndexGetInteger(1, PLOT_DRAW_BEGIN, LongestPeriod);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
    if(EMAHandle != INVALID_HANDLE) IndicatorRelease(EMAHandle);
    if(LSMAHandle != INVALID_HANDLE) IndicatorRelease(LSMAHandle);}
    
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[], 
                const double &low[], 
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) {
                
//--- 
    if(IsStopped()) return (0);
    if(rates_total < LongestPeriod) return (0); // checking the number of bars to be enough for calculation

    if(BarsCalculated(EMAHandle) < rates_total) return (0);
    if(BarsCalculated(LSMAHandle) < rates_total) return (0);

    int copyBars = 0;
    int startBar = 0;
    double sum;

// calculation of the starting number first for the bar recalculation loop
    if (prev_calculated > rates_total || prev_calculated <= 0) { // checking for the first start of calculation of an indicator
        startBar = LongestPeriod; // starting index for calculation of all bars
        copyBars = rates_total - LongestPeriod - 1;
    } else {
        startBar = prev_calculated - 1; // starting number for calculation of new bars
        copyBars = rates_total - prev_calculated;
        if(prev_calculated > 0) copyBars++;
    }
//---
    if(IsStopped()) return (0);
    if(CopyBuffer(EMAHandle, 0, 0, copyBars, BufferEMA) <= 0) return (0);
    
// Get Live Price
    GetPrice(LSMATimeFrame, 0, copyBars, rates);
    
//LSMA Calculation
    for (int barIndex = copyBars - LSMA_Period; barIndex >= 0 && !IsStopped(); barIndex--) {
        sum = 0.0;
        for (int periodIndex = 0; periodIndex < LSMA_Period; periodIndex++) {
            sum += rates[barIndex + periodIndex].close * (FixPeriod - periodIndex);
        }
        BufferLSMA[barIndex] = sum/L2;
    }
//--- return value of prev_calculated for next call
    return(rates_total);
}
//+------------------------------------------------------------------+

void GetPrice(  ENUM_TIMEFRAMES timeframe,
                int start_pos,              // start position
                int count,                  // data count to copy
                MqlRates& rates_array[]) {
    CopyRates(Symbol(), timeframe, start_pos, count, rates_array);
}
 

In your previous thread, I gave you a solution to correctly calculate the LSMA (using moving averages), and to properly use the Moving Average calculation functions, and to use the proper OnCalculate form for the applied price.

I dedicate my own time, to write that indicator, specifically for you, so that you could learn from it. However, it seems you have totally ignored my advice and my code, and as a result you continue to have difficulties.

What is the use of us offering help, or showing you proper code, if you are are just going to ignore it all?

 
Fernando Carreiro:

In your previous thread, I gave you a solution to correctly calculate the LSMA (using moving averages), and to properly use the Moving Average calculation functions, and to use the proper OnCalculate form for the applied price.

I dedicate my own time, to write that indicator, specifically for you, so that you could learn from it. However, it seems you have totally ignored my advice and my code, and as a result you continue to have difficulties.

What is the use of us offering help, or showing you proper code, if you are are just going to ignore it all?

Let me explain to you, the previous thread you help me and show me your code, but i dont know how to develop my ideal with it because it fundamental. My ideal is build an MA line and LSMA line base on the previous indicator's data. I tried to combine them with your way but it doesn't work and i dont know how to do that. Remember, I always appriciate your kindness, i dont ignore your code but also base on it. I hope you understand me.

 
Fernando Carreiro:

In your previous thread, I gave you a solution to correctly calculate the LSMA (using moving averages), and to properly use the Moving Average calculation functions, and to use the proper OnCalculate form for the applied price.

I dedicate my own time, to write that indicator, specifically for you, so that you could learn from it. However, it seems you have totally ignored my advice and my code, and as a result you continue to have difficulties.

What is the use of us offering help, or showing you proper code, if you are are just going to ignore it all?

i sympathize with you, in that case i would be unsatisfied with it

 
No one can help me?
 

I found the way to solve this problem for anyone else later has a similar issue
Just add this line into OnCalculate() function...

ChartSetSymbolPeriod(0, _Symbol, _Period);
Reason: