Now I insert ticket and Lots in the OrderClose without using OrderTicket() and OrderLots() but it not closing the order too. Please help.
There are a couple of things you did wrong. I assume you want this EA to work on JPY pairs on M1 time frame. Do you want to place orders on new bar only and close orders on new bar only or do you want entry on new bar and exit if conditions are met? Here is what I think :
First you count orders with OrdersTotal().
Then calculate your variables
If there are orders in the market check if they have to be closed
If no orders in the market, check if entry conditions are met.
Let me know if you have any questions.
//+------------------------------------------------------------------+//| chuale_test1.mq4 |//| Copyright 2014, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2014, MetaQuotes Software Corp."#property link"http://www.abc.com"#property version"1.00"#property strictexterndouble TakeProfit=500;
externdouble Lots=0.1;
externdouble StopLoss=300;
double bar1;
double bar2;
double bar3;
int total,ticket;
datetime previousTime;
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+int init()
{
//---//---return(0);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+int deinit()
{
//---return(0);
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+int start()
{
total=OrdersTotal();
/*
counted_bars=IndicatorCounted();
if(counted_bars>0) counted_bars--;
IndicatorCounted() is actually for indicators not EA
*/
bar1=(iClose(Symbol(),PERIOD_M1,2)-iClose(Symbol(),PERIOD_M1,3))*100;
bar2=(iClose(Symbol(),PERIOD_M1,1)-iClose(Symbol(),PERIOD_M1,2))*100;
bar3=(iClose(Symbol(),PERIOD_M1,0)-iClose(Symbol(),PERIOD_M1,1))*100;
/*
Comment("Bar1 = "+DoubleToString(bar1,Digits)+"\n"+
"Bar2 = "+DoubleToString(bar2,Digits)+"\n"+
"Bar3 = "+DoubleToString(bar3,Digits)+"\n"
);
*///Close any orders if conditions are metif(total>0)
{
if(bar3<-1) OrderClose(ticket,OrderLots(),Bid,3,Green);
if(bar3>1) OrderClose(ticket,OrderLots(),Ask,3,Red);
}
if(Time[0]==previousTime) return(0); //EA will not go past this point unless it is a new bar
previousTime=Time[0]; // If it was a new bar , it's old now but continue to the end// if no order open and new barif(total<1)
{
if(bar2>1)// if condition met
{
//place buy order
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Chua EA",12345,0,Green);
if(ticket<0)Print("Error OP_BUY order failed. error : "+GetLastError()); // if returned ticket is -1 OrderSent failedreturn(0);
}
// or if conditions for sell metif(bar2<-1)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid+-TakeProfit*Point,"Chua EA",12345,0,Red);
if(ticket<0)Print("Error OP_SELL order failed. error : "+GetLastError());
return(0); //return to start
}
}
return(0);
}
//+------------------------------------------------------------------+
yes, it can close now. thank you GumRai. now i know ticket number keep on changing according to the ticks, so how to obtain the ticket number of the open order? I have to.use OrderTicket() right?
chuale: yes, it can close now. thank you GumRai. now i know ticket number keep on changing according to the ticks, so how to obtain the ticket number of the open order? I have to.use OrderTicket() right?
If you use OrderTicket() then just make sure you use OrderSelect() first and also make sure that it is the trade that you want to close.
chuale: yes, it can close now. thank you GumRai. now i know ticket number keep on changing according to the ticks, so how to obtain the ticket number of the open order? I have to.use OrderTicket() right?
Hi Chuale,
Here is an easy way to select your orders and close orders by symbol and by Magic number :
//+------------------------------------------------------------------+//| chuale_test1.mq4 |//| Copyright 2014, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2014, MetaQuotes Software Corp."#property link"http://www.abc.com"#property version"1.00"#property strictexterndouble TakeProfit=500;
externdouble Lots=0.1;
externdouble StopLoss=300;
externint MagicNumber = 12345;
externint Slip = 3;
double bar1;
double bar2;
double bar3;
int myTrades,ticket;
datetime previousTime;
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+int init()
{
//---//---return(0);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+int deinit()
{
//---return(0);
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+int start()
{
myTrades = CountTrades();//Count trades function counts this EA trades only//
bar1=(iClose(Symbol(),PERIOD_M1,2)-iClose(Symbol(),PERIOD_M1,3))*100;
bar2=(iClose(Symbol(),PERIOD_M1,1)-iClose(Symbol(),PERIOD_M1,2))*100;
bar3=(iClose(Symbol(),PERIOD_M1,0)-iClose(Symbol(),PERIOD_M1,1))*100;
//Close any orders if conditions are metif(myTrades>0)
{
if(bar3<-1) CloseThisSymbolAll();
if(bar3>1) CloseThisSymbolAll();
}
if(Time[0]==previousTime) return(0); //EA will not go past this point unless it is a new bar
previousTime=Time[0]; // If it was a new bar , it's old now but continue to the end// if no order open and new barif(myTrades<1)
{
if(bar2>1)// if condition met
{
//place buy order
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Chua EA",MagicNumber,0,Green);
if(ticket<0)Print("Error OP_BUY order failed. error : "+GetLastError()); // if returned ticket is -1 OrderSent failedreturn(0);
}
// or if conditions for sell metif(bar2<-1)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid+-TakeProfit*Point,"Chua EA",MagicNumber,0,Red);
if(ticket<0)Print("Error OP_SELL order failed. error : "+GetLastError());
return(0); //return to start
}
}
return(0);
}
//========================================================================int CountTrades()
{
int count=0;
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber)
continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
if(OrderType()==OP_SELL || OrderType()==OP_BUY)
count++;
}
return(count);
}
//========================================================================void CloseThisSymbolAll()
{
int trade;
for(trade=OrdersTotal();trade>=0;trade--)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol()) continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(OrderType()==OP_BUY)OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
if(OrderType()==OP_SELL)OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
}
//Sleep(1000);
}
}
//========================================================================
chuale: yes, it can close now. thank you GumRai. now i know ticket number keep on changing according to the ticks, so how to obtain the ticket number of the open order? I have to.use OrderTicket() right?
Hello again Chuale,
I was playing around with your idea on USDJPY and I couldn't get positive results without some changes.Here is a sample of my results on USDJPY on M1 chart :
Did you intend to use it on a different pair?
Exit on bar3 < -1 or bar3 > 1 didn't return positive results.
with an orderloop you can then select the right trade to close with
.
with your method
you lose ticket value moment you restart your pc so
fails .....
If you mean that it isn't the best way to close an order I agree but if the guy doesn't want to use OrderSelect (or doesn't know how) and doesn't leave any open orders in the market it works.
We try to help each other here and your answer did not helped a bit, you didn't show him how to use Order Select function and his EA isn't working any better now due to your comment.
Next time you want to help, do it the right way, put the right code in there so others may actually learn something .
There are a couple of things you did wrong. I assume you want this EA to work on JPY pairs on M1 time frame. Do you want to place orders on new bar only and close orders on new bar only or do you want entry on new bar and exit if conditions are met? Here is what I think :
First you count orders with OrdersTotal().
Then calculate your variables
If there are orders in the market check if they have to be closed
If no orders in the market, check if entry conditions are met.
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 All,
Now I insert ticket and Lots in the OrderClose without using OrderTicket() and OrderLots() but it not closing the order too. Please help.
There are a couple of things you did wrong. I assume you want this EA to work on JPY pairs on M1 time frame. Do you want to place orders on new bar only and close orders on new bar only or do you want entry on new bar and exit if conditions are met? Here is what I think :
First you count orders with OrdersTotal().
Then calculate your variables
If there are orders in the market check if they have to be closed
If no orders in the market, check if entry conditions are met.
Let me know if you have any questions.
ticket is a local variable, so next tick, it will not have the same value
Put outside of the functions to make it Globalscope
total=OrdersTotal();?????? ..... useless
check correctly what trades you have
look for right symbol and right magicnumber
with an orderloop you can then select the right trade to close with
.
with your method
you lose ticket value moment you restart your pc so
fails .....
Why did you quote my post and then not add a comment?
yes, it can close now. thank you GumRai. now i know ticket number keep on changing according to the ticks, so how to obtain the ticket number of the open order? I have to.use OrderTicket() right?
If you use OrderTicket() then just make sure you use OrderSelect() first and also make sure that it is the trade that you want to close.
yes, it can close now. thank you GumRai. now i know ticket number keep on changing according to the ticks, so how to obtain the ticket number of the open order? I have to.use OrderTicket() right?
Hi Chuale,
Here is an easy way to select your orders and close orders by symbol and by Magic number :
yes, it can close now. thank you GumRai. now i know ticket number keep on changing according to the ticks, so how to obtain the ticket number of the open order? I have to.use OrderTicket() right?
Hello again Chuale,
I was playing around with your idea on USDJPY and I couldn't get positive results without some changes.Here is a sample of my results on USDJPY on M1 chart :
Did you intend to use it on a different pair?
Exit on bar3 < -1 or bar3 > 1 didn't return positive results.
?????? ..... useless
check correctly what trades you have
look for right symbol and right magicnumber
with an orderloop you can then select the right trade to close with
.
with your method
you lose ticket value moment you restart your pc so
fails .....
If you mean that it isn't the best way to close an order I agree but if the guy doesn't want to use OrderSelect (or doesn't know how) and doesn't leave any open orders in the market it works.
We try to help each other here and your answer did not helped a bit, you didn't show him how to use Order Select function and his EA isn't working any better now due to your comment.
Next time you want to help, do it the right way, put the right code in there so others may actually learn something .
There are a couple of things you did wrong. I assume you want this EA to work on JPY pairs on M1 time frame. Do you want to place orders on new bar only and close orders on new bar only or do you want entry on new bar and exit if conditions are met? Here is what I think :
First you count orders with OrdersTotal().
Then calculate your variables
If there are orders in the market check if they have to be closed
If no orders in the market, check if entry conditions are met.
Let me know if you have any questions.