Trouble selecting by ticket

 
Hello,

I wrote a very simple EA based on a custom indicator. Everything was perfect until i tried to change it from selecting by position to selecting by ticket, so that it can be used on multiple pairs. Now, in it's current state, it opens a new order each tick, and does not keep track of the ticket number properly. I hope my code is self explanatory, I'm posting from my phone...
//+------------------------------------------------------------------+
//|                                                    pipfinite.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

   int ticket = 0;

extern double MMlots = 0.01;
extern int ts = 40;
extern int tp = 40;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_HISTORY) == true)
         {    
               //if (!OrderProfit() == 0)
               ticket = 0;
               Print("ticket set to zero");
         }

   Comment(ticket);
   
   
   
   
   
   if (ticket == 0)
   {
      if ((iCustom(NULL,0,"Market\\PipFinite Trend Laser","",2,1000,"",true,7,true,2,8,8,true,"","Dark",true,"",true,false,false,false,"alert.wav",true,14,1) > 0) && (iCustom(NULL,0,"Market\\PipFinite Trend Laser","",2,1000,"",true,7,true,2,8,8,true,"","Dark",true,"",true,false,false,false,"alert.wav",true,18,1) >= 90))
         {
            Print(iCustom(NULL,0,"Market\\PipFinite Trend Laser","",2,1000,"",true,7,true,2,8,8,true,"","Dark",true,"",true,false,false,false,"alert.wav",true,14,1));
            ticket=OrderSend(Symbol(),OP_BUY,MMlots,Ask,3,Bid-Point*ts,Bid+tp*Point,"macd sample",16384,0,Green);
         }
         
      else if ((iCustom(NULL,0,"Market\\PipFinite Trend Laser","",2,1000,"",true,7,true,2,8,8,true,"","Dark",true,"",true,false,false,false,"alert.wav",true,15,1) > 0) && (iCustom(NULL,0,"Market\\PipFinite Trend Laser","",2,1000,"",true,7,true,2,8,8,true,"","Dark",true,"",true,false,false,false,"alert.wav",true,18,1) >= 90))
         {
            ticket=OrderSend(Symbol(),OP_SELL,MMlots,Bid,3,Ask+Point*ts,Ask-Point*tp,"macd sample",16384,0,Green);
         }
   }
   else if (ticket > 0)
   {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)==true)
      {
         if(OrderType()==OP_BUY)
            {
               if (iCustom(NULL,0,"Market\\PipFinite Trend Laser","",2,1000,"",true,7,true,2,8,8,true,"","Dark",true,"",true,false,false,false,"alert.wav",true,15,1) > 0)
                  {
                     if(!OrderClose(ticket,OrderLots(),Ask,3,Red))
                     {
                        Print("OrderClose error ",GetLastError());
                     }
                     else
                     {
                        ticket = 0;
                     }
                  }
               else if(ts>0)
              {
               
                  if(OrderStopLoss()<Bid-Point*ts)
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*ts,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 
              }
            }
        else if(OrderType()==OP_SELL)
         {
            if (iCustom(NULL,0,"Market\\PipFinite Trend Laser","",2,1000,"",true,7,true,2,8,8,true,"","Dark",true,"",true,false,false,false,"alert.wav",true,14,1) > 0)
               if(!OrderClose(ticket,OrderLots(),Bid,3,Red))
                  {
                     Print("OrderClose error ",GetLastError());
                  }
               else
                  {
                     ticket = 0;
                  }
            else if(ts>0)
            {
               if(ts>0)
              {
               
                  if((OrderStopLoss()>(Ask+Point*ts)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*ts,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 
              }
            }
   
          }
      
     }
     
  }
  }
//+------------------------------------------------------------------+

.

 
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_HISTORY) == true)
         {    
               //if (!OrderProfit() == 0)
               ticket = 0;
               Print("ticket set to zero");
         }
  1. Mode is irrelevant when you select by ticket.
  2. What do when you select it? You zero it! What do you do when it is zero? You open another.
  3. 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. Mode is irrelevant when you select by ticket.
  2. What do when you select it? You zero it! What do you do when it is zero? You open another.
The select by history there is intended to check if the last order has been closed by take profit, trail stop, etc. Otherwise I don't know it's closed. The intent is to only have one open order at a time per pair. I'm sure whatever I'm doing wrong is something silly, i just cant find it.
 
LancerSykera: The select by history there is intended to check if the last order has been closed by take profit, trail stop, etc. Otherwise I don't know it's closed.

Selecting by ticket doesn't mean any of those things. The ticket could be pending, open, closed, or deleted. You must test to see if it is closed.

 
whroeder1:

Selecting by ticket doesn't mean any of those things. You must test to see if it is closed.

My understanding was that its only in the history if its been closed, thats how the documentation makes it sound. Will add a check for closure.
 
LancerSykera: My understanding was that its only in the history if its been closed,

That is true (closed, deleted, balance/adjustment entries,) but you are not selecting a position out of history.

You are selecting a ticket. What part of "Mode is irrelevant when you select by ticket" was unclear?

The pool parameter is ignored if the order is selected by the ticket number. The ticket number is a unique order identifier.
          OrderSelect - Trade Functions - MQL4 Reference
Reason: