Two part question regarding use of IsConnected() and OrderModify()

 

Hello everyone,

My first question is based around the code below. I would like variable (NoConnection) to descend while there is a internet failure. Return the value of NoCorrection is seconds and minutes 

void OnInit()
  {

   if(!IsConnected())
     {
   Alert("Connection Not Available!");
      NoConnection--;
      return;
     }


Second part is to do with Modify Order. What is best practice (SELECT_BY_POS) or, (SELECT_BY_TICKET)
void ModifyOrder()
  {
   TrailingStop=MarketInfo(Symbol(),MODE_STOPLEVEL);
   if(TrailingStop>0)
     {
      SellModifyOrder=OrderSelect(1,SELECT_BY_POS);PriceOrderPlaced=OrderOpenPrice();CurrentSymbol=OrderSymbol();Order=OrderTicket();
      if(Ask+PriceOrderPlaced>Point*TrailingStop)
        {
         if(OrderStopLoss()<Ask+Point*TrailingStop)
           {
           OrderModified=OrderModify(Order,PriceOrderPlaced,NormalizeDouble(Ask+Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue);
            if(!OrderModified)
               Alert(BuyError,GetLastError());
            else
               Alert(BuySuccess);
           }
        }
     }
  }
 
  1. OnInit is called once when the terminal comes up. Then the terminal connects to the market. That code is useless. Calling it in OnTick is also useless because you must be connected to have it called. If you really want to monitor the connection, you have to put it in OnTimer. It is only useful after a failed OrderSend. I never use it; I just log the error and return. On the next tick, it's connected and I retest/retry the operation.

  2. SellModifyOrder=OrderSelect(1,SELECT_BY_POS)
    This assumes you have at least two orders opened. Not two orders on the current chart. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect 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 and MetaTrader 4 - MQL4 programming forum

  3. NormalizeDouble(Ask+Point*TrailingStop,Digits)
    Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  4. if(OrderStopLoss()<Ask+Point*TrailingStop)
    You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 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.)

  5. OrderModify(Order,PriceOrderPlaced,NormalizeDouble(Ask+Point*TrailingStop,Digits)
    How an you place your buy order's SL above the Bid?

  6. Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  7. Your function is named ModifyOrder but only works for one type of order.
  8. As for your question, if you have the ticket number you can just select it but must test if it has already closed. If you use a OrderSelect loop you don't. But remember that on startup you don't have a ticket number. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.
 
whroeder1:
  1. OnInit is called once when the terminal comes up. Then the terminal connects to the market. That code is useless. Calling it in OnTick is also useless because you must be connected to have it called. If you really want to monitor the connection, you have to put it in OnTimer. It is only useful after a failed OrderSend. I never use it; I just log the error and return. On the next tick, it's connected and I retest/retry the operation.

  2. This assumes you have at least two orders opened. Not two orders on the current chart. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect 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 and MetaTrader 4 - MQL4 programming forum

  3. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  4. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 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.)

  5. How an you place your buy order's SL above the Bid?

  6. Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  7. Your function is named ModifyOrder but only works for one type of order.
  8. As for your question, if you have the ticket number you can just select it but must test if it has already closed. If you use a OrderSelect loop you don't. But remember that on startup you don't have a ticket number. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

Hello whroeder1,

I am still going over that very detailed explanation you provided.

This code sort of worked, but still error pron.

void MODIFY_ORDER()
{
   double SL,TP,TS,A_ASK,B_BID;
   double pips = (double)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD); 
   if(Digits%2==1) pips/=10;

   A_ASK=Ask; B_BID=Bid;
   TicketNumber     = OrdersTotal(); // 10
   TrailingStop     = MarketInfo(Symbol(),MODE_STOPLEVEL)+5; //Add 5 points to min Stop Level
   BuyModifyOrder   = OrderSelect(0,SELECT_BY_POS);
   PriceOrderPlaced = OrderOpenPrice();
   CurrentSymbol    = OrderSymbol();
   Order            = OrderTicket();
   
   TS= Point*TrailingStop;
   TP = PriceOrderPlaced+TS/Digits;

   SL =PriceOrderPlaced-TS;
   SL = NormalizeDouble(fabs(SL),5);
      OrderModified=OrderModify(Order,PriceOrderPlaced,SL,TP,0,clrNONE);
      if(!OrderModified)
       Alert(BuyError,GetLastError());
      else
       Alert(BuySuccess);
       }
 

Hello again,

Are pending orders treated differently to current positions?

 
whroeder1:
  1. OnInit is called once when the terminal comes up. Then the terminal connects to the market. That code is useless. Calling it in OnTick is also useless because you must be connected to have it called. If you really want to monitor the connection, you have to put it in OnTimer. It is only useful after a failed OrderSend. I never use it; I just log the error and return. On the next tick, it's connected and I retest/retry the operation.

  2. This assumes you have at least two orders opened. Not two orders on the current chart. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect 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 and MetaTrader 4 - MQL4 programming forum

  3. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  4. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 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.)

  5. How an you place your buy order's SL above the Bid?

  6. Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  7. Your function is named ModifyOrder but only works for one type of order.
  8. As for your question, if you have the ticket number you can just select it but must test if it has already closed. If you use a OrderSelect loop you don't. But remember that on startup you don't have a ticket number. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

Hello all,

I would like to know if,

manually placed positions are placed as reference or, as pointer, and not pointer to location?

Is "SELECT_BY_POS" the reference?

Is "SELECT_BY_TICKET" the pointer?

 
GrumpyDuckMan:

manually placed positions are placed as reference or, as pointer, and not pointer to location?

Is "SELECT_BY_POS" the reference?

Is "SELECT_BY_TICKET" the pointer?

There are no pointers (and a reference is a hidden pointer)

POS is a position in the terminal's array of orders (excluding historical or excluding open/pending.) Ticket is a value of an order. The call is likely a linear search of the array

Reason: