When EA cross a new day, indicator return wrong data

 

Hi everyone

I'm trying to write an EA based on MACD indicator. Below is my framework. It works almost okay, but has a very strange bug I couldn't handle. I did much effort to search some posts but found nothing helpful.


#define MAX_NUM_HISTORY_MACD_DATA 40
ENUM_TIMEFRAMES MyTimeFrame = PERIOD_M5;
string MySymbol = "EURUSD";
int num_history_macd_data = 20;
bool MyInitialized = false;

void Process()
{
    //  Positions > 0
}

void MyInit()
{
    // Positions = 0
    MyInitialized = true;
    int someCondition;

    double macdData[MAX_NUM_HISTORY_MACD_DATA] = {0};
    int i;

    for(i=0;i < num_history_macd_data; i++)
    {
        macdData[num_history_macd_data - i - 1]=iMACD(MySymbol, MyTimeFrame, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, i);
    }

    // Some init ops
    if (someCondition == 1) {
        MyInitialized = true;
    }
    else {
        MyInitialized = false;
    }    
}

int OnInit()
{
    ChartSetSymbolPeriod(0, MySymbol, MyTimeFrame);
    return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{

}

void OnTick()
{

    if (MyInitialized == false) {
        MyInit();
    }
    else {
        Process();
    }
}


The code works normally in daytime. but when it across a new day(0:00, GMT), the value of macdData in MyInit() function appears to be very strange when no position opened(positions=0), which leads to a false signal activation. eg: right data(0.00021, 0.00020, 0.00019, ...) VS wrong data(0.0023, 0.0021, 0.0024). I try to find the relationship between these two series of data but failed.


Then I will stop the EA and restart it again. This time it works normally whole day, until the next new day (0:00, GMT), again and  again.


I write same framework in MQL5(like below),  it works perfectly, nothing bad happen when crossing a new day(0:00, GMT)

CopyBuffer(MacdHandler, 0, 1, num_history_macd_data, macdData);

Anyone can help me to figure out the problems.

Thank you so much.

MACD - Oscillators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
Moving Average Convergence/Divergence (MACD) is a trend-following dynamic indicator. It indicates the correlation between two Moving Averages of a...
 
  1.         macdData[num_history_macd_data - i - 1]=iMACD(MySymbol, MyTimeFrame, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, i);

    Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. Your code
        if (someCondition == 1) {
            MyInitialized = true;
        }
        else {
            MyInitialized = false;
        }    
    Simplified.
    MyInitialized = someCondition == 1;
  3. bin1496: The code works normally in daytime. but when it across a new day(0:00, GMT), the value of macdData in MyInit() function appears to be very strange 

    Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    Always post all relevant code (using Code button) or attach the source file.

  4. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
Sorry to bother you, I will post it in MQL4 section again. Thank you for your advice
 
bin1496 #:
Sorry to bother you, I will post it in MQL4 section again. Thank you for your advice
Don't do that. I moved your topic to the correct section.
 
bin1496 #: I will post it in MQL4 section again.

What part of “The moderators will likely move this thread there soon,” was unclear?

Reason: