-
When you start learning MQL why the old and slower MQ4 instead of MQ5?
Here the compare:
- Here s an article with working code where a system is developed and decribes incl. working(!) code:
https://www.mql5.com/en/articles/3040
Using and changing this is faster than to step into every beginner mistake.
![Learn how to design different Moving Average systems Learn how to design different Moving Average systems](https://c.mql5.com/36/69/learn-how-to-design-different-moving.jpg)
- www.mql5.com
- Yariel Burgos:
1. BUY MARKET ORDER WHEN CANDLE PRICE CLOSES ABOVE THE 200 HIGH SMA2. SELL MARKET ORDEN WHEN CANDLE PRICE CLOSES BELOW THE 200 LOW SMA3. IGNORE SELL ORDERS WHEN PRICES IS ABOVE THE 100 EMA4. IGNORE BUY ORDERS WHEN PRICE IS BELOW THE 100 EMA5. OPTION FOR REVERSE POSITION AT THE OPPOSITE SIGNALDon't SHOUT at us, that is very RUDE!
-
void OnInit() { ⋮ ReversePosition = ReversePosition; ⋮ high200SMA = iHigh(Symbol, Timeframe, 200);
What does V=V mean to you?
- Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
-
That is not a SMA That is the high of a bar, 200 periods in the past. It doesn't update unless you assign to it.
-
high100EMA = iMA(Symbol, Timeframe, 100, MODE_EMA, PRICE_CLOSE); ⋮ void OnTick() { ⋮ if (!isOpen && currentBar.Close > high200SMA) {
This is an MT5 call. Bogus in MT4.
Perhaps you should read the manual, especially the examples.
How To Ask Questions The Smart Way. (2004)
How To Interpret Answers.
RTFM and STFW: How To Tell You've Seriously Screwed Up.They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
How to call indicators in MQL5 - MQL5 Articles (2010) - Yariel Burgos: Please hope someone can help me spot the error and fix it
Stop using ChatGPT. Learn to code.
Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (2023)Do not post code that will not even compile.
MT4: Learn to code it.
MT5: Begin learning to code it.If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.
sorry for caps i didn't mean to be rude. thanks for the articles link. will go through it throughly and learn the mql5 language, hopefully then i will be able to come up with a more specific question.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I tried coding an EA with the following strategy:
1. BUY MARKET ORDER WHEN CANDLE PRICE CLOSES ABOVE THE 200 HIGH SMA
not sure where i went wrong but when i saved the code as an mq4 and tried testing it on a demo account the expert advisor is not working at all.
Here is the code for the following and i will attach the mq4 file as well
Please hope someone can help me spot the error and fix it
// Expert Advisor that buys market orders when candle price closes above the 200 high SMA, sells market orders when candle price closes below the 200 low SMA, ignores sell orders when prices is above the 100 EMA, ignores buy orders when price is below the 100 EMA, and has an option for reversing position at the opposite signal.
// Input parameters: // - symbol: The symbol to trade. // - timeframe: The timeframe to trade. // - reversePosition: Whether to reverse position at the opposite signal.
// Variables: // - currentBar: The current bar. // - high200SMA: The 200 high SMA. // - low200SMA: The 200 low SMA. // - high100EMA: The 100 EMA. // - low100EMA: The 100 EMA. // - isOpen: Whether a trade is open. // - openOrder: The open order.
// Initialization function: // This function is called when the EA is first loaded.
void OnInit() {
// Set the symbol and timeframe. SetSymbol(Symbol); SetTimeframe(Timeframe);
// Set the reverse position option. ReversePosition = ReversePosition;
// Initialize the 200 high SMA. high200SMA = iHigh(Symbol, Timeframe, 200);
// Initialize the 200 low SMA. low200SMA = iLow(Symbol, Timeframe, 200);
// Initialize the 100 EMA. high100EMA = iMA(Symbol, Timeframe, 100, MODE_EMA, PRICE_CLOSE);
// Initialize the 100 EMA. low100EMA = iMA(Symbol, Timeframe, 100, MODE_EMA, PRICE_OPEN); }
// Tick function: // This function is called every tick.
void OnTick() {
// Get the current bar. currentBar = MarketInfo(Symbol, MODE_BID, PERIOD_CURRENT);
// If a trade is not open and the market price closes above the 200 high SMA, // then place a buy market order. if (!isOpen && currentBar.Close > high200SMA) { OpenOrder(Symbol, OP_BUY, 1, Ask); }
// If a trade is not open and the market price closes below the 200 low SMA, // then place a sell market order. if (!isOpen && currentBar.Close < low200SMA) { OpenOrder(Symbol, OP_SELL, 1, Bid); }
// If a trade is open and the market price is above the 100 EMA, // then ignore the sell order. if (isOpen && currentBar.Close >= high100EMA) { return; }
// If a trade is open and the market price is below the 100 EMA, // then ignore the buy order. if (isOpen && currentBar.Close <= low100EMA) { return; }
// If a trade is open and the market price closes at the opposite signal, // then reverse the position. if (isOpen && (currentBar.Close > high200SMA && ReversePosition) || (currentBar.Close < low200SMA && !ReversePosition)) { CloseOrder(openOrder); OpenOrder(Symbol, OP_BUY if (currentBar.Close > high200SMA) else OP_SELL, 1, Ask if (currentBar.Close > high200SMA) else Bid); } }
This Expert Advisor will buy market orders when candle price closes above the 200 high SMA, sell market orders when candle price closes below the 200 low SMA, ignore sell orders when prices is above the 100 EMA, ignore buy orders when price is below the 100 EMA, and has an option for reversing position at the opposite signal.