TAYU Academy course material · MQL5 EA Programming Intro
Text walkthrough
Syntax and structure
A self-paced text walkthrough, without video or audio. See the article below for worked examples and materials.
Course Notes
1. Understand the core idea of “Syntax and structure” and explain it in your own words
2. Master this module's focus: MQL5 basics
3. Complete the assignment on your own and check it against the reference answer
MQL5 programs organize code by events: OnInit for initialization, OnTick for handling quotes, and OnDeinit for releasing resources. The type, scope, and return status of variables affect execution; MQL4 code cannot be directly used in MQL5 without modification.
The main event functions of an Expert Advisor (EA) include OnInit (initialization), OnTick (on each quote trigger), and OnDeinit (release resources on exit). Indicators need to first create a handle (such as iMA), then use CopyBuffer to read the values, and check if the return value was successful.
Example: In OnInit, handle = iMA(_Symbol, PERIOD_H1, 20, 0, MODE_SMA, PRICE_CLOSE); In OnTick, use CopyBuffer(handle, 0, 0, 3, buf) to read the most recent 3 values, and return directly if unsuccessful.
Read the accompanying mq5 → input/global/local variables → list event entry → check initialization failure path → record compilation version in MetaEditor.
Explain why indicator handles must not be rebuilt unconditionally on every tick, and what should be done on exit.
Rebuilding repeatedly wastes resources and may not be ready with the data; initialize, establish, and check INVALID_HANDLE, then release. Reading a file does not equal compilation success; a local log must prove it.
Assuming the `CopyBuffer` request returns 1 value out of 3, it cannot read the last two elements to calculate a cross. Using a shift of `bar.shift = 1` can avoid treating the current bar, which is still being calculated, as the final value. However, it is still important to clarify the order of the array and the meaning of the indicator buffer.
Don't rebuild indicator handles on every tick; by default the current bar and closed bars are not the same information set.
The companion .mq5 file only logs signals and never submits orders. Below is a terminal checklist; having the source on the web does not mean it has compiled or run in your terminal. Without a terminal, complete the state machine on paper and record which steps were not run.
1. Download AcademySmaSignal.mq5
2. Open and compile it in MT5 MetaEditor; save the version and diagnostics
3. Watch new-bar signal logs on a demo chart or in the Strategy Tester only
4. Check CopyBuffer failures, duplicate new-bar events and resource release on exit
5. Do not enable live trading; verify the order state machine on paper1. Master the three main event functions
2. Create the indicator handle first, then read the buffer
3. Check every return value
Risk warning: leveraged trading in forex, precious metals and similar products carries a high level of risk and you may lose all of your capital, or more. This course is for education only. It is not investment advice and does not promise any return. Example prices and contract specifications are teaching assumptions; always use what your own platform discloses. Past performance and backtests do not indicate future results.
All values, prices, accounts and cases in this course are teaching assumptions; the fixed materials can be reproduced offline. Marking a lesson complete only records your study. It is not a certification, a service approval or a promise of returns. Workbooks and answer files are currently provided in Chinese.
Key Takeaways
Completion records your own study, not verified competence. Exercises use fictional data; no real account or deposit is required.