Error message: 'close' - invalid array access & 'ticket' - variable already defined

 

I am new to Forex trading and I know next to nothing about MQL4 coding but I would like to get this code working.
I will appreciate your help.

The errors from the MQL4 program are:

1. 'close' - invalid array access

2. 'ticket' - variable already defined.

I have made the lines the errors refer to bold and red for easy identification


Below is the code.

// Expert Advisor start function

void OnTick()

{

    double ema21 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE, 0);

    double sma50 = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);

    double stopLossPrice = iMA(NULL, 0, 21, 0, MODE_SMA, PRICE_CLOSE, 0);


    if (ema21 > sma50 && Close > ema21 && OrdersTotal() == 0) {

        int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 0, Ask - stopLossPrice, Ask + (2 * (Ask - stopLossPrice)), "Buy Order", 0, 0, Green);

        if (ticket > 0) {

            Print("Buy order opened successfully");

        } else {

            Print("Error opening buy order: ", GetLastError());

        }

    } else if (ema21 < sma50 && Close < ema21 && OrdersTotal() == 0) {

        int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 0, Bid + stopLossPrice, Bid - (2 * stopLossPrice), "Sell Order", 0, 0, Red);

        if (ticket > 0) {

            Print("Sell order opened successfully");

        } else {

            Print("Error opening sell order: ", GetLastError());

        }

 
  1. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

  2.  double ema21 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE, 0);

    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  3. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  4. Adenekan Ogungbola: 'ticket' - variable already defined.
    You did not use strict.

    Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  5. OrderSend(Symbol(), OP_BUY, LotSize, Ask, 0, Ask - stopLossPrice, Ask + (2 * (Ask - stopLossPrice)), …

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  6.  if (ema21 > sma50 && Close > ema21 && OrdersTotal() == 0) {

    Close is a predefined array int MT4.

  7. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

 
Dear William, thank you for your advice and also pointing out where my post should have been.
I apologize for posting it there. 
Reason: