Are you learning MQL5 and want to have a couple of very simple EAs? Here you have my two micro robots!
Your code is much better than the last one.
Here's some error, though ;(
1. This is the same error like the last one, you use input for symbol, and yet you don't use it
input string pair = "EURUSD";
2. Try not comparing string
if (... flag == "sell") else if (... flag == "buy")
There's nothing wrong comparing string, but string takes much more memory, and processor have to compare each character in a string. So in your code, use directive #define with integer value on top of your code.
#define FLAG_BUY 1 #define FLAG_NO_TRADE 0 #define FLAG_SELL -1
And change the type of variable flag from string to integer and use it like this
if (... flag == FLAG_SELL || flag == FLAG_NO_TRADE) else if (... flag == FLAG_BUY || flag == FLAG_NO_TRADE)
3. You Risk-Reward-Ratio (3R) calculation is incorrect. Your 3R is based on calculation of SL and TP and also not changing lot size. Your 3R should change the lot size based calculation of SL, and Margin call (see AccountInfoDouble) . I'll explain this another time, coz it will be long explanation and calculation.
4. Your lot calculation is also incorrect - and this is a fatal error which makes your EA may will only open buy. In MT5, there's only one position for each symbol, and opening opposite symbol will close the other symbol (see it here buy and sel another cancel each other). In your code, if you have 1 lot buy, and then open 1 lot sell, you will have no trade at all, yet you flag it as opened sell. If buy position exist and you want to open sell position with the same lot as buy position, then you have to send open sell with twice lot size than open buy , or close that buy position first (using CTrade::PositionCLose) then open sell with same lot as previous buy.
Use this code to check the lot size of current opened position
int pos, total_position; total_position = PositionsTotal(); flag = FLAG_NO_TRADE; for (pos = 0; pos < total_position; pos ++) { if (PositionGetSymbol (pos) == _Symbol) { if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { flag = FLAG_BUY; size = PositionGetDouble(POSITION_VOLUME); break; } else { flag = FLAG_SELL; size = PositionGetDouble(POSITION_VOLUME); break; } } }
5. Your code does NOT using CheckResult to check if order send is successful or not,
6. You should not use SL and TP to stop your position. In MT5, we should use opposite type pending order with the same lot as opened position to stop our positions (for buy position use sell limit as TP and sell stop as SL with the same lot as that buy position and vice versa). That because MT5 is a position based trade platform, and so, SL and TP may not be well executed (there's plenty report about that in forum) and SL and TP may will be deleted if we open new position with the same type with the previous one.
So after order send is successful (which you don't check - see number 5), immediately send opposite pending orders with the same lot as opened position, as SL and TP, using CTrade::OrderOpen.
7. Have fun.
Your code is much better than the last one.
Here's some error, though ;(
1. This is the same error like the last one, you use input for symbol, and yet you don't use it
2. Try not comparing string
There's nothing wrong comparing string, but string takes much more memory, and processor have to compare each character in a string. So in your code, use directive #define with integer value on top of your code.
And change the type of variable flag from string to integer and use it like this
3. You Risk-Reward-Ratio (3R) calculation is incorrect. Your 3R is based on calculation of SL and TP and also not changing lot size. Your 3R should change the lot size based calculation of SL, and Margin call (see AccountInfoDouble) . I'll explain this another time, coz it will be long explanation and calculation.
4. Your lot calculation is also incorrect - and this is a fatal error which makes your EA may will only open buy. In MT5, there's only one position for each symbol, and opening opposite symbol will close the other symbol (see it here buy and sel another cancel each other). In your code, if you have 1 lot buy, and then open 1 lot sell, you will have no trade at all, yet you flag it as opened sell. If buy position exist and you want to open sell position with the same lot as buy position, then you have to send open sell with twice lot size than open buy , or close that buy position first (using CTrade::PositionCLose) then open sell with same lot as previous buy.
Use this code to check the lot size of current opened position
5. Your code does NOT using CheckResult to check if order send is successful or not,
6. You should not use SL and TP to stop your position. In MT5, we should use opposite type pending order with the same lot as opened position to stop our positions (for buy position use sell limit as TP and sell stop as SL with the same lot as that buy position and vice versa). That because MT5 is a position based trade platform, and so, SL and TP may not be well executed (there's plenty report about that in forum) and SL and TP may will be deleted if we open new position with the same type with the previous one.
So after order send is successful (which you don't check - see number 5), immediately send opposite pending orders with the same lot as opened position, as SL and TP, using CTrade::OrderOpen.
7. Have fun.
hello dear I'm writing an EA as an amature...
here is my problem
this EA dont care about charts.. it just open a file an read trade signals from, and open/modify them
so it should select a symbol each time a new signal arrive to open a trade on
how do i exactly select a symbol by SymbolSelect() ?
i tried this but that doesnt work
string pair="CADJPY";
SymbolSelect(pair,true);
Alert (Symbol());
please help me by writing the exactly code which select for example EURUSD
please note that im writing in mql4

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello there,
I share with you my two first EAs tought for learning purposes! Any feedback from experts is welcome. How would you improve them?
The first one is RSIExpectator, the very simple EA that works exclusively on RSI. The results calculated by this robot give an idea of the effectiveness of RSI understood as an overbought/oversold indicator. In other words, this EA tries to refute the popular a priori reasoning which states that RSI itself is an overbought/oversold indicator. I mean, it answers the question Can you get rich by selling an asset when RSI equals 80, buy it when equals 20, and so on?
Here it is RSIExpectator.mq5:
The second one is MACDWaterlineCrossExpectator. This is the classical trading system which consists in buying when MACD crosses above the waterline line and selling when crosses below it. This EA works, as the previous one, along with a monetary management system which has a positive mathematical expectation.
Here it is MACDWaterlineCrossExpectator.mq5: