questions about one infinite loop.

 

Hi, please I need some help. I.m writing a code that has to take orders to buy or sell based on the price of the last candle is above or below the upper or lower IMA. I have a big problem with my code, because this send to the market hundreds of buy or send orders depending on whether the trend is up or down. I have this questions: I put the code then to give him a look to see what you think, but I have some doubts:

-How can I do that my EA only send one order to buy o sell when the market will be up or down?

- How can I send an order to buy or sell without you establish a stoploss or TakeProfit?

- When I bought or sold is better to use OrderClose order (order_id, 1, Ask, 3, Red) as this closes my current operation?

-If I have in the market one order by the command ordersend(), I can't to close this order by the command orderclose(), because I dont know the order_id, no?
- I send you the algorithm I want to reflect on MQL, a greeting.

 
jdcabezas:

Hi, please I need some help. I.m writing a code that has to take orders to buy or sell based on the price of the last candle is above or below the upper or lower IMA. I have a big problem with my code, because this send to the market hundreds of buy or send orders depending on whether the trend is up or down. I have this questions: I put the code then to give him a look to see what you think, but I have some doubts:

-How can I do that my EA only send one order to buy o sell when the market will be up or down?

- How can I send an order to buy or sell without you establish a stoploss or TakeProfit?

- When I bought or sold is better to use OrderClose order (order_id, 1, Ask, 3, Red) as this closes my current operation?

-If I have in the market one order by the command ordersend(), I can't to close this order by the command orderclose(), because I dont know the order_id, no?
- I send you the algorithm I want to reflect on MQL, a greeting.


this are the basics in MQL.

Best will be you take some of the EAs are comming with your MetaTrader and try to analyse them.

Next Step will be here: https://book.mql4.com//

 
jdcabezas:

Hi, please I need some help. I.m writing a code that has to take orders to buy or sell based on the price of the last candle is above or below the upper or lower IMA. I have a big problem with my code, because this send to the market hundreds of buy or send orders depending on whether the trend is up or down. I have this questions: I put the code then to give him a look to see what you think, but I have some doubts:

1. -How can I do that my EA only send one order to buy o sell when the market will be up or down?

2. - How can I send an order to buy or sell without you establish a stoploss or TakeProfit?

- When I bought or sold is better to use OrderClose order (order_id, 1, Ask, 3, Red) as this closes my current operation?

3. -If I have in the market one order by the command ordersend(), I can't to close this order by the command orderclose(), because I dont know the order_id, no?
- I send you the algorithm I want to reflect on MQL, a greeting.

1. Check the open orders, if there is already an open Sell don't open a new one

2. Use OrderSend wil SL = 0 and TP = 0

3. Loop through the open orders, OrderSelect then one by one, filter by Symbol(), Magic Number and Order type to find the Order you want, and then you can use Orderticket()

 
jdcabezas:
  1. -How can I do that my EA only send one order to buy o sell when the market will be up or down?
    count your open orders and don't send more
    int nOrders=0;
        for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
            OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
        &&  OrderMagicNumber()  == Magic.Number                 // my magic number
        &&  OrderSymbol()       == chart.symbol                 // and my pair.
        ){ nOrders++; }
    if (nOrders > 0) return;
    

  2. - How can I send an order to buy or sell without you establish a stoploss or TakeProfit?

        int ticket = OrderSend(..., 0,0,...)
        if (ticket < 0)
           Alert("OrderSend failed: ", GetLastError());
        else if (!OrderSelect(ticket, SELECT_BY_TICKET))
           Alert("OrderSelect failed: ", GetLastError());
        else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
           Alert("OrderModify failed: ", GetLastError());
    

  3. - When I bought or sold is better to use OrderClose order (order_id, 1, Ask, 3, Red) as this closes my current operation?

    Use OrderClose. Opening a new order in the opposite direction will not work on non-US brokers (hedging allowed) and will not work on some (all) US brokers (OrderSend fails with ERR_TRADE_HEDGE_PROHIBITED)


  4. -If I have in the market one order by the command ordersend(), I can't to close this order by the command orderclose(), because I dont know the order_id, no?

    The orderSend gave you the ticket number (see #2) You did check it for errors didn't you? You can also get it in a select loop (see #1)


  5. - I send you the algorithm I want to reflect on MQL
    No Slaves here, learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.
Reason: