iCustom values - page 2

 

Hi,

How to get ticket number of specific position? The plan is to make multi currency ea, so there could be more than one position.

I wrote something like this:

int start()
{

      double x=iCustom(NULL,0,"ADXEURMom",Red,0,0);
      
      double f=iADX("EURUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_PLUSDI,0);
      double g=iADX("EURUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_MAIN,0);
      double h=iADX("EURUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_MINUSDI,0);
      
       if ((OrdersTotal() < MaxOpen)&& (EURShort==0)&&
    (h>f) &&
    (30>x>0))
   
       {
      OrderSend("EURUSD",OP_SELL,Lots,Bid,Slippage,0,0,0,MagicNumber,0,Red);
      EURShortUSD++;
      EURShort++;
      }
      
      if ((OrdersTotal() == MaxOpen) && ((iADX("EURUSD",PERIOD_M15,8,PRICE_CLOSE,MODE_PLUSDI,0)>(iADX("EURUSD",PERIOD_M15,8,PRICE_CLOSE,MODE_MINUSDI,0)))))

      {
      OrderSelect(???????, SELECT_BY_POS);
      OrderClose(????????,OrderLots(),NormalizeDouble(Ask,Digits),3,Blue);
      EURShortUSD--;
      EURShort--;
      
      }
      
return(0);
}

 
WHRoeder:
  1. Franko: 30>x>10, everything seems to work.
    I hope you didn't write that literally because it isn't what you think. Int and bool are interchangeable (true=1, false=0) so if X==20 you get

  2. Franko: When I close it, it opens it again :)
    You don't have any OrderClose calls in you code. Opening an order in the opposite direction is NOT a close, that is hedging.
About 30>x>10

At the moment everything seems to work. You are right about OrderClose. I implemented it. As plan is to make multi currency ea, the problem is ticket number of specific position.

 

i hope you know what you doing

 (30>x>0)) ??? do you know what this line means ?


      OrderSelect(???????, SELECT_BY_POS);
      OrderClose(????????,OrderLots(),NormalizeDouble(Ask,Digits),3,Blue);

you have to loop all the orders and the order you looking

 

as I understand looping=counting

added different conditions, could someone check the loop. Many thanks.

int   v=2;              ///ADX 1min DI starpība
int   z=5;              ///ADX 5min DI starpība 
int   b=4;              ///ADX 15min  DI starpība
int   n=10;             ///ADX Main moments

extern int     MaxOpen      =1;
extern double  Lots         =1;
extern double  StopLoss     =1000;
extern double  TakeProfit   =1000;
extern int     TrailingStop =0;
extern int     Slippage     =10;
extern int     MagicNumber  =10001;

int  EURLong=0;
int  EURLongUSD=0;
int  EURLongGBP=0;
int  EURLongAUD=0;
int  EURLongCAD=0;

int  EURShort=0;
int  EURShortUSD=0;
int  EURShortGBP=0;
int  EURShortAUD=0;
int  EURShortCAD=0;

int USDLong=0;
int USDLongEUR=0;
int USDLongGBP=0;
int USDLongAUD=0;
int USDLongCAD=0;

int USDShort=0;
int USDShortEUR=0;
int USDShortGBP=0;
int USDShortAUD=0;
int USDShortCAD=0;

int GBPLong=0;
int GBPLongEUR=0;
int GBPLongUSD=0;
int GBPLongAUD=0;
int GBPLongCAD=0;

int GBPShort=0;
int GBPShortEUR=0;
int GBPShortUSD=0;
int GBPShortAUD=0;
int GBPShortCAD=0;

int start()
{

      double x=iCustom(NULL,0,"ADXEURMom",Red,0,0);
      double x5=iCustom(NULL,0,"ADXUSDMom",Green,1,0);
      double x10=iCustom(NULL,0,"ADXGBPMom",Blue,2,0);
      
      double f=iADX("EURUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_PLUSDI,0);
      double g=iADX("EURUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_MAIN,0);
      double h=iADX("EURUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_MINUSDI,0);
      
      double f5=iADX("GBPUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_PLUSDI,0);
      double g5=iADX("GBPUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_MAIN,0);
      double h5=iADX("GBPUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_MINUSDI,0);
      
      double f10=iADX("GBPAUD",PERIOD_M1,28,PRICE_CLOSE,MODE_PLUSDI,0);
      double g10=iADX("GBPAUD",PERIOD_M1,28,PRICE_CLOSE,MODE_MAIN,0);
      double h10=iADX("GBPAUD",PERIOD_M1,28,PRICE_CLOSE,MODE_MINUSDI,0);
      
////conditions for open      
      
      
       if ((OrdersTotal() < MaxOpen)&& (EURShort==0)&& (h>f) && (30>x>0))
   
       {
      OrderSend("EURUSD",OP_SELL,Lots,Bid,Slippage,0,0,0,MagicNumber,0,Red);
      EURShortUSD++;
      EURShort++;
      }
      
      
      if ((OrdersTotal() < MaxOpen)&& (USDLong==0)&& (h5>f5) && (30>x>0))
   
       {
      OrderSend("EURUSD",OP_SELL,Lots,Bid,Slippage,0,0,0,MagicNumber,0,Red);
      USDLongGBP++;
      USDLong++;
      }
      
       if ((OrdersTotal() < MaxOpen)&& (GBPShort==0)&& (h10>f10) && (30>x>0))
   
       {
      OrderSend("GBPAUD",OP_SELL,Lots,Bid,Slippage,0,0,0,MagicNumber,0,Red);
      GBPShortAUD++;
      GBPShort++;
      }
      
      
/////Now comes the closing part      
///for EURUSD
      
      if (((iADX("EURUSD",PERIOD_M15,8,PRICE_CLOSE,MODE_PLUSDI,0)>(iADX("EURUSD",PERIOD_M15,8,PRICE_CLOSE,MODE_MINUSDI,0)))))
      
      int OpenOrders(int nOrderType, string strSymbol, int nMagic)
{
   int nOrderCount=0;
   for (int i=OrdersTotal()-1 ; i>=0 ; i--)
   {
      if (!OrderSelect(i,SELECT_BY_POS)) continue;
      if (OrderType() == nOrderType)
         if (OrderMagicNumber() == nMagic)
            if (OrderSymbol() == strSymbol)
               nOrderCount++;
   }
   return(nOrderCount);
}
      
      OrderSelect(???????, SELECT_BY_POS);
      OrderClose(????????,OrderLots(),NormalizeDouble(Ask,Digits),3,Blue);
      EURShortUSD--;
      EURShort--;
      
      }
      
      
////for GBPUSD      
      
      
            if (((iADX("GBPUSD",PERIOD_M15,8,PRICE_CLOSE,MODE_PLUSDI,0)>(iADX("GBPUSD",PERIOD_M15,8,PRICE_CLOSE,MODE_MINUSDI,0)))))
      
      int OpenOrders(int nOrderType, string strSymbol, int nMagic)
{
   int nOrderCount=0;
   for (int i=OrdersTotal()-1 ; i>=0 ; i--)
   {
      if (!OrderSelect(i,SELECT_BY_POS)) continue;
      if (OrderType() == nOrderType)
         if (OrderMagicNumber() == nMagic)
            if (OrderSymbol() == strSymbol)
               nOrderCount++;
   }
   return(nOrderCount);
}
      
      OrderSelect(???????, SELECT_BY_POS);
      OrderClose(????????,OrderLots(),NormalizeDouble(Ask,Digits),3,Blue);
      USDLongGBP--;
      USDLong--;
      
      }
      
      
////for GBPAUD      
      
            if (((iADX("GBPAUD",PERIOD_M15,8,PRICE_CLOSE,MODE_PLUSDI,0)>(iADX("GBPAUD",PERIOD_M15,8,PRICE_CLOSE,MODE_MINUSDI,0)))))
      
      int OpenOrders(int nOrderType, string strSymbol, int nMagic)
{
   int nOrderCount=0;
   for (int i=OrdersTotal()-1 ; i>=0 ; i--)
   {
      if (!OrderSelect(i,SELECT_BY_POS)) continue;
      if (OrderType() == nOrderType)
         if (OrderMagicNumber() == nMagic)
            if (OrderSymbol() == strSymbol)
               nOrderCount++;
   }
   return(nOrderCount);
}
      
      OrderSelect(???????, SELECT_BY_POS);
      OrderClose(????????,OrderLots(),NormalizeDouble(Ask,Digits),3,Blue);
      GBPShortAUD--;
      GBPShort--;
      }
      
      
      
      
      
return(0);
}
 

excuse me sir your code as 64 errors and not compiling

I'll ask again do you know what you doing or just cutting pieces of code from other places ?

 

Hi gjol

Just learning how to code.

Have simple idea of creating multi currency ea which opens position when the currency is rising or falling against all other major pairs.

Made the loop, everything works.


#property copyright "Roberts Maurins"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int   v=2;              ///ADX 1min DI starpība
int   z=5;              ///ADX 5min DI starpība 
int   b=4;              ///ADX 15min  DI starpība
int   n=10;             ///ADX Main moments

extern int     MaxOpen      =3;
extern double  Lots         =1;
extern double  StopLoss     =1000;
extern double  TakeProfit   =1000;
extern int     TrailingStop =0;
extern int     Slippage     =10;
extern int     MagicNumber  =10001;

int  EURLong=0;
int  EURLongUSD=0;
int  EURLongGBP=0;
int  EURLongAUD=0;
int  EURLongCAD=0;

int  EURShort=0;
int  EURShortUSD=0;
int  EURShortGBP=0;
int  EURShortAUD=0;
int  EURShortCAD=0;

int USDLong=0;
int USDLongEUR=0;
int USDLongGBP=0;
int USDLongAUD=0;
int USDLongCAD=0;

int USDShort=0;
int USDShortEUR=0;
int USDShortGBP=0;
int USDShortAUD=0;
int USDShortCAD=0;

int GBPLong=0;
int GBPLongEUR=0;
int GBPLongUSD=0;
int GBPLongAUD=0;
int GBPLongCAD=0;

int GBPShort=0;
int GBPShortEUR=0;
int GBPShortUSD=0;
int GBPShortAUD=0;
int GBPShortCAD=0;

int PositionIndex;
int TotalNumberOfOrders;



int start()
{


      double x=iCustom(NULL,0,"ADXEURMom",Red,0,0);
      double x5=iCustom(NULL,0,"ADXUSDMom",Green,1,0);
      double x10=iCustom(NULL,0,"ADXGBPMom",Blue,2,0);
      
      double f=iADX("EURUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_PLUSDI,0);
      double g=iADX("EURUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_MAIN,0);
      double h=iADX("EURUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_MINUSDI,0);
      
      double f5=iADX("GBPUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_PLUSDI,0);
      double g5=iADX("GBPUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_MAIN,0);
      double h5=iADX("GBPUSD",PERIOD_M1,28,PRICE_CLOSE,MODE_MINUSDI,0);
      
      double f10=iADX("GBPAUD",PERIOD_M1,28,PRICE_CLOSE,MODE_PLUSDI,0);
      double g10=iADX("GBPAUD",PERIOD_M1,28,PRICE_CLOSE,MODE_MAIN,0);
      double h10=iADX("GBPAUD",PERIOD_M1,28,PRICE_CLOSE,MODE_MINUSDI,0);
      
////conditions for open      
      
      
       if ((OrdersTotal() < MaxOpen)&& (EURShort==0) && (h>f) && (30>x>0))
   
       {
      OrderSend("EURUSD",OP_SELL,Lots,Bid,Slippage,0,0,0,MagicNumber,0,Red);
      EURShortUSD++;
      EURShort++;
      }
      
      
      if ((OrdersTotal() < MaxOpen)&& (USDLong==0) && (h5>f5) && (30>x5>0))
   
       {
      OrderSend("EURUSD",OP_SELL,Lots,Bid,Slippage,0,0,0,MagicNumber,0,Red);
      USDLongGBP++;
      USDLong++;
      }
      
       if ((OrdersTotal() < MaxOpen)&& (GBPShort==0)&& (h10>f10) && (30>x10>0))
   
       {
      OrderSend("GBPAUD",OP_SELL,Lots,Bid,Slippage,0,0,0,MagicNumber,0,Red);
      GBPShortAUD++;
      GBPShort++;
      }
      
      
/////Now comes the closing part      
///for EURUSD
      
      if (((iADX("EURUSD",PERIOD_M15,8,PRICE_CLOSE,MODE_PLUSDI,0)>(iADX("EURUSD",PERIOD_M15,8,PRICE_CLOSE,MODE_MINUSDI,0)))))
   
   {TotalNumberOfOrders = OrdersTotal();
   for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
   if( OrderMagicNumber() == MagicNumber      // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == "EURUSD"         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_SELL   ) )         // <-- is the Order a Buy Order ? 
      // <-- or is it a Sell Order ?
   
      if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
   }
   } //  end of For loop
      
////for GBPUSD      
      
      
            if (((iADX("GBPUSD",PERIOD_M15,8,PRICE_CLOSE,MODE_PLUSDI,0)>(iADX("GBPUSD",PERIOD_M15,8,PRICE_CLOSE,MODE_MINUSDI,0)))))
      
       {TotalNumberOfOrders = OrdersTotal();
   for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
   if( OrderMagicNumber() == MagicNumber      // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == "GBPUSD"         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_SELL   ) )         // <-- is the Order a Buy Order ? 
      // <-- or is it a Sell Order ?
   
      if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
   }
   } //  end of For loop
      
      
////for GBPAUD      
      
            if (((iADX("GBPAUD",PERIOD_M15,8,PRICE_CLOSE,MODE_PLUSDI,0)>(iADX("GBPAUD",PERIOD_M15,8,PRICE_CLOSE,MODE_MINUSDI,0)))))
      
       {TotalNumberOfOrders = OrdersTotal();
   for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
   if( OrderMagicNumber() == MagicNumber      // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == "GBPAUD"         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_SELL   ) )         // <-- is the Order a Buy Order ? 
      // <-- or is it a Sell Order ?
   
      if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage ) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
   }
   } //  end of For loop
      
      
      
      
return(0);
}

 

The next problem is limiting open positions for one currency pair. With ++ and -- it doesn't work, max number positions allowed are opened if one pair. Is another way to do that?


Best regards,

Roberts

 
Franko:

The next problem is limiting open positions for one currency pair. With ++ and -- it doesn't work, max number positions allowed are opened if one pair. Is another way to do that?


Best regards,

Roberts


Loop through open orders and count the orders for each pair.
Reason: