How to focus EA on only the first opened position

To add comments, please log in or register
Franzel Botha
902
Franzel Botha  
I got this EA developed.
I am struggling to edit the EA to only focus on the first opened position with a certain magic number.
Currently the EA focuses on every opened order with the magic number related to the EA.

The EA opens a position for you of a different currency when the currency on the chart with the magic number and EA reaches a certain amount of -pips.

Does anyone have a solution to how to let the EA focus just on the first open position with magic number related to the EA?

extern int     MagicNumber = 1 ;
extern double  PipCount    = 20;
extern string  CurrencyToOpen = "GBPUSD";
extern bool    AutoClose   = false;
extern string EaComment = "Opp";

string Pair1,Pair2;
double Lot;
int ticket;
string iEaComment;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  if(Digits==3 || Digits==5){
   PipCount *=10;
  }
//--- create timer
   //EventSetTimer(60);//
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---    

      Pair1 = Symbol();
      Pair2 = CurrencyToOpen;

       for (int cnt = 0; cnt < OrdersTotal(); cnt++) {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)){
         
           if(OrderSymbol()==Pair1 && OrderMagicNumber()==MagicNumber) {
           
           double ask = MarketInfo(Pair2,MODE_ASK);
           double bid = MarketInfo(Pair2,MODE_BID);
           
           iEaComment = (string)OrderTicket()+" "+EaComment;
           Lot       = OrderLots();
           
           if(OrderType()==OP_BUY && OrderOpenPrice()-Close[0]>=PipCount*Point){
               if(cekOpen(Pair2,iEaComment)<1) ticket = OrderSend(Pair2,OP_SELL,Lot,bid,5,0,0,iEaComment,MagicNumber,0,clrRed);
               }
           
          if(OrderType()==OP_SELL && Close[0]-OrderOpenPrice()>=PipCount*Point){
               if(cekOpen(Pair2,iEaComment)<1) ticket = OrderSend(Pair2,OP_BUY,Lot,ask,5,0,0,iEaComment,MagicNumber,0,clrBlue);
               }
           
           }
         }
       }
       
       
        string tiketnya="";
        datetime Tclose = iTime(Symbol(),0,0)-60;
        
        for (int cnt = 0; cnt < OrdersHistoryTotal(); cnt++) {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY)){
         if(OrderSymbol()==Pair1 && OrderMagicNumber()==MagicNumber && OrderCloseTime()>=Tclose){
            tiketnya=(string)OrderTicket()+" "+EaComment;
            if(AutoClose && tiketnya!="") CloseOp(tiketnya);
         }
        }
       }
        
  
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   OnTick();
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+
int cekOpen(string pair, string komen)
{
   int tot=0;
   for (int cnt = 0; cnt < OrdersTotal(); cnt++) {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)){
           if(OrderMagicNumber()==MagicNumber) {
            if(OrderSymbol()==pair && OrderComment()==komen) tot++;
           }
         }
       }
       
   return(tot);
}  

void CloseOp(string komen)
{ 
int total = OrdersTotal();  
        for(int i=total-1;i>=0;i--) {  
          if(OrderSelect(i, SELECT_BY_POS)){
            if(OrderSymbol()==Pair2 && OrderMagicNumber()==MagicNumber){
            if(OrderComment()==komen) ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlack);
            }
         }
     }    
 }  
Franzel Botha
902
Franzel Botha  
Code2219 or probably 2319:
just re-read your post, didn't notice you're asking for mql4 lang . (the posts have been removed as they could be confusing to some)

no problem. thanx for the reply. greatly appreciated. after a long struggle I figured it out.

To add comments, please log in or register