iCustom values - page 2

53

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()
{

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++;
}

{
OrderSelect(???????, SELECT_BY_POS);
EURShortUSD--;
EURShort--;

}

return(0);
}

```
53

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.

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.

3243

i hope you know what you doing

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

```      OrderSelect(???????, SELECT_BY_POS);

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

53

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

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  EURShort=0;
int  EURShortUSD=0;
int  EURShortGBP=0;
int  EURShortAUD=0;

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

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

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

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

int start()
{

////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

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);
EURShortUSD--;
EURShort--;

}

////for GBPUSD

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);
USDLongGBP--;
USDLong--;

}

////for GBPAUD

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);
GBPShortAUD--;
GBPShort--;
}

return(0);
}
```
3243

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 ?

53

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.

```#property copyright "Roberts Maurins"
#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

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  EURShort=0;
int  EURShortUSD=0;
int  EURShortGBP=0;
int  EURShortAUD=0;

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

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

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

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

int PositionIndex;
int TotalNumberOfOrders;

int start()
{

////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

{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

{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

{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);
}

```
53

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

Moderator
10561

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.