Closing order issue

 

Hi, i am very new to mql. I was trying to code a basic ma crossover, but i could open the order but not close it. Can someone help me where i am going wrong.

input double lot_size = 0.1;
input int slippage = 50;
input double stoploss = 5000;
input double takeprofit = 5000;

input int ma_period = 100;
input int ma_shift = 0;
input int ma_method = 0;
input int applied_price = 0;

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
//---

  }

void OnTick()
  {
   int signal, ticket;
   double lastclose_price = iClose(NULL, 0, 1);
   double close_price = iClose(NULL, 0, 0);

   double lastma = iMA(NULL, 0, ma_period, ma_shift, ma_method, applied_price, 1);
   double ma = iMA(NULL, 0, ma_period, ma_shift, ma_method, applied_price, 0);

   if(OrdersTotal()==0)
     {
      if(close_price>ma)
         signal = 1;
      else if(close_price<ma)
         signal=-1;

      if(signal ==1)
         int ticket = OrderSend(NULL, OP_BUY, lot_size, Ask, slippage, 0, 0, "Buy", 0, 0, clrGreen);
      else if(signal ==-1)
         int ticket = OrderSend(NULL, OP_SELL, lot_size, Bid, slippage, 0, 0, "Sell", 0, 0, clrRed);
     }

   if(signal ==1 && lastclose_price>lastma && close_price<ma)
      OrderClose(ticket, lot_size, Bid, slippage, clrNONE);
   else if(signal ==-1 && lastclose_price<lastma && close_price>ma)
      OrderClose(ticket, lot_size, Ask, slippage, clrNONE);
  }
 

Please EDIT your post and use the CODE button when you insert code.

Code button in editor

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 

Why arte you trying what already exist: https://www.mql5.com/en/search#!keyword=ma%20crossing%20EA&amp;module=mql5_module_codebase

Learn to search is faster then learning to code

 

Please don't post randomly in any section. Your question is not related to the section you posted.

MT4/mql4 has it's own section on the forum. I have moved your topic to the correct section.

 
Fernando Carreiro #:

Please don't post randomly in any section. Your question is not related to the section you posted.

MT4/mql4 has it's own section on the forum. I have moved your topic to the correct section.

Thanks...i ll make sure to post in the correct section henceforth
 
Carl Schreiber #:

Why arte you trying what already exist: https://www.mql5.com/en/search#!keyword=ma%20crossing%20EA&amp;module=mql5_module_codebase

Learn to search is faster then learning to code

Thanks Carl for the assist. 

I was just trying to learn from some basics.

But still is it possible for you to check and let me know my fault in saving the ticket number and closing it. It would be of great help.

 
  1.       int ticket = OrderSend(NULL,OP_BUY,lot_size,Ask,slippage,0,0,"Buy",0,0,clrGreen);
    

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
      MT5: create them.
    5. Cloud Protector Bug? - MQL4 programming forum (2020)

  2.    if(OrdersTotal()==0)
    

    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.
         If trading multiple timeframes, and filter by symbol requires use a range of MN (base plus timeframe).
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)

 
Sukhdeep Singh Panesar: Hi, i am very new to mql. I was trying to code a basic ma crossover, but i could open the order but not close it. Can someone help me where i am going wrong.
This is because your “signal” parameter only gets updated when OrdersTotal()==0. After you open first order, your “signal “ is always 0, so since it is a condition for your order close filters, to be either 1 or -1, it is never true. 
 
Sukhdeep Singh Panesar:

Hi, i am very new to mql. I was trying to code a basic ma crossover, but i could open the order but not close it. Can someone help me where i am going wrong.

I am a novice but I am mostly sure that you need to OrderSelect() aka (Select) the order before you can OrderClose() or Modify it. 

https://docs.mql4.com/trading/orderticket

  if(ticket>0)
        {
        OrderSelect(ticket,SELECT_BY_TICKET);
        if(signal ==1 && lastclose_price>lastma && close_price<ma)
        OrderClose(ticket, lot_size, Bid, slippage, clrNONE);
        else if(signal ==-1 && lastclose_price<lastma && close_price>ma)
        OrderClose(ticket, lot_size, Ask, slippage, clrNONE);
        }
}

There is many other methods you will discover but you will also want to learn how to GetLastError(). 


OrderTicket - Trade Functions - MQL4 Reference
OrderTicket - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderTicket - Trade Functions - MQL4 Reference
 
Daniel Cioca #:
He is saving the ticket number and he is using it in OrderClose() function .

Wrong.

void OnTick()
  {
   int signal, ticket;
Ticket number was lost on the first return.
 
William Roeder #:

Wrong.

Ticket number was lost on the first return.
It did work when I did select the order and tried closing it.
But how do I save a ticket number then.
Is it that, whenever I need the ticket number, I should select the order, extract the ticket number and use it.
Reason: