6.2 Project: build your own robot
Now it is your turn. In this project you take a robot from the algo builder and add a new rule yourself.
The task
Create an MA crossover robot for MetaTrader 5 in the algo builder, and add an RSI filter: the robot should only buy when RSI(14) is below 70, and only sell when the RSI is above 30. That way it avoids buying when the market has already risen a lot.
Solution
Add a new setting at the top: ``mql5 input int InpRsiPeriod = 14; // RSI filter: periods ``
A new global variable next to the other handles: ``mql5 int hRsi = INVALID_HANDLE; ``
In OnInit(), after the other indicators: ``mql5 hRsi = iRSI(_Symbol, _Period, InpRsiPeriod, PRICE_CLOSE); if(hRsi == INVALID_HANDLE) return(INIT_FAILED); ``
In OnTick(), right after the line // Filters: ``mql5 double rsi1 = Value(hRsi, 0, 1); if(rsi1 == EMPTY_VALUE) return; if(rsi1 >= 70) buySignal = false; if(rsi1 <= 30) sellSignal = false; ``
And in OnDeinit(): ``mql5 if(hRsi != INVALID_HANDLE) IndicatorRelease(hRsi); ``
Compile (F7). If you get errors, check semicolons, curly brackets and that the names are spelled the same.
Test the robot
- Backtest without the RSI filter on 2 years with a realistic spread. Note the net result, number of trades, profit factor and maximum drawdown.
- Backtest with the filter on the same period.
- Test both on a different period that you have not used before.
- Did the robot get better in both periods – or only in one?
Checklist before demo
- The rulebook is written down in plain words.
- The code compiles without errors.
- Tested on at least two years and on a period the robot is not optimised on.
- Realistic spread and commission are included.
- Risk per trade is 1% or lower.
- I know what the largest drawdown in the test was, and I have a stop rule.
You have completed the course
You can now read, write and test a trading robot for MetaTrader and cTrader. Remember the most important lesson of the whole course: the robot is only as good as its rules and its risk management. Test thoroughly, start small – and let the numbers, not hope, decide.
Try it yourself
Complete the whole project: rules, code, backtest, one month on demo and an evaluation written in your trading journal.
Key takeaways
- Extend a robot with a new rule: setting, handle, signal and clean-up.
- Test with and without the change – and on a period you have not used before.
- Go through the checklist before the robot is allowed to run on demo.
How much do you remember from “Build your own robot”?
Recalling what you have read is one of the most effective ways to learn. Pick the answer you think is right – you get the explanation straight away.
1How should you change a robot from the algo builder?
Then you know what actually improved the robot.
2What must you do before the robot is allowed to run on demo?
The checklist catches the most common mistakes before they cost money.
65% of retail investor accounts lose money when trading CFDs with this provider.