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
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.
added different conditions, could someone check the loop. Many thanks.
int v=2; ///ADX 1min DI starpībaint z=5; ///ADX 5min DI starpība int b=4; ///ADX 15min DI starpībaint n=10; ///ADX Main momentsexternint MaxOpen =1;
externdouble Lots =1;
externdouble StopLoss =1000;
externdouble TakeProfit =1000;
externint TrailingStop =0;
externint Slippage =10;
externint 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 EURUSDif (((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);
}
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 strictint v=2; ///ADX 1min DI starpībaint z=5; ///ADX 5min DI starpība int b=4; ///ADX 15min DI starpībaint n=10; ///ADX Main momentsexternint MaxOpen =3;
externdouble Lots =1;
externdouble StopLoss =1000;
externdouble TakeProfit =1000;
externint TrailingStop =0;
externint Slippage =10;
externint 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 EURUSDif (((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 PositionIndexif( 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 orderPrint("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 PositionIndexif( 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 orderPrint("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 PositionIndexif( 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 orderPrint("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() ); // <-- if the Order Close failed print some helpful information
}
} // end of For loopreturn(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?
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.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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:
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
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.
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.
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
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.