mq4 file not working

 
Hey all,

I tried coding an EA with the following strategy:

1. BUY MARKET ORDER WHEN CANDLE PRICE CLOSES ABOVE THE 200 HIGH SMA
2. SELL MARKET ORDEN WHEN CANDLE PRICE CLOSES BELOW THE 200 LOW SMA
3. IGNORE SELL ORDERS WHEN PRICES IS ABOVE THE 100 EMA
4. IGNORE BUY ORDERS WHEN PRICE IS BELOW THE 100 EMA
5. OPTION FOR REVERSE POSITION AT THE OPPOSITE SIGNAL

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.


Files:
200smaEA.mq4  3 kb
 
  1. When you start learning MQL why the old and slower MQ4 instead of MQ5?
    Here the compare:

  2. 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
  • www.mql5.com
There are many strategies that can be used to filter generated signals based on any strategy, even by using the moving average itself which is the subject of this article. So, the objective of this article is to share with you some of Moving Average Strategies and how to design an algorithmic trading system.
 
  1. Yariel Burgos:
    1. BUY MARKET ORDER WHEN CANDLE PRICE CLOSES ABOVE THE 200 HIGH SMA
    2. SELL MARKET ORDEN WHEN CANDLE PRICE CLOSES BELOW THE 200 LOW SMA
    3. IGNORE SELL ORDERS WHEN PRICES IS ABOVE THE 100 EMA
    4. IGNORE BUY ORDERS WHEN PRICE IS BELOW THE 100 EMA
    5. OPTION FOR REVERSE POSITION AT THE OPPOSITE SIGNAL

    Don't SHOUT at us, that is very RUDE!

  2. void OnInit() {
    ⋮
    ReversePosition = ReversePosition;
    ⋮
    high200SMA = iHigh(Symbol, Timeframe, 200);

    What does V=V mean to you?

  3. 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:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. 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.

  5. 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)

  6. 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.