You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Another very important note with regards to use of you EA on multiple currency pairs or time-frames.
Your code does not use the "Magic Number" selection nor does it check the Symbol (nor "Magic number") in use when checking for the open orders in History. So in essence your EA will not work correctly neither with itself nor other EA's.
As it is, you can only use this EA by itself with no other EA running, not even itself on another chart!
bool ans;
//You do not select an order
if(OrderType()==OP_BUY){
while(fastma < slowma) //Use if not while
{
ans = OrderClose(OrderTicket(),Lots,Bid,10,clrNONE);
if(ticket < 1) //ticket is a local variable and has not been assigned a value
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("Buy order closed : ", OrderClosePrice());
}
else
Print("Error closing order : ", GetLastError());
return(0);
}
}
Thank you very much for helping me with my code so far and your time, I will try to make the necessary adjustments.
Because you have too many logic problems in your EA, I suggest that you have a look at the sample code by MetaQuotes, namely "Moving Average.mq4" and/or "MACD Sample.mq4" and built on that, until you understand the basic principles of how an EA works.
I have attached the files, but you will find them in your "MQL4\Experts" folder.
EDIT: With regards to the code, you are using the OrderSelect, but you are always assuming that it works every time. And when it does not work you just continue to use the Order details functions like OrderTicket() in your close, which could fail if the initial OrderSelect() failed too.
After noticing your edit.....
I had always assumed that an OrderSelect() would be lost when there is a new tick.
So I checked and found that once an order is selected it remains selected even in subsequent new ticks.
Learn something new every day :)
I had always assumed that an OrderSelect() would be lost when there is a new tick.
So I checked and found that once an order is selected it remains selected even in subsequent new ticks.
Learn something new every day :)
Another very important note with regards to use of you EA on multiple currency pairs or time-frames.
Your code does not use the "Magic Number" selection nor does it check the Symbol (nor "Magic number") in use when checking for the open orders in History. So in essence your EA will not work correctly neither with itself nor other EA's.
As it is, you can only use this EA by itself with no other EA running, not even itself on another chart!
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol()) continue;
//---Check Order Type
if(OrderType()==OP_BUY)
{
if(fastma < slowma)
{
if(OrderClose(OrderTicket(),Lots,Bid,10,clrNONE))
Print("Order Closed : ", OrderClosePrice());
return(0);
}
else
Print("Error : ", GetLastError());
return(0);
break;
}
if(OrderType()==OP_SELL)
{
if(fastma > slowma)
{
if(OrderClose(OrderTicket(),Lots,Ask,10,clrNONE))
Print("Order Closed : ", OrderClosePrice());
return(0);
}
else
Print("Error : ", GetLastError());
return(0);
break;
}
}
Here is the OrderClose section, I am no longer getting the invalid ticket error, but i am now getting invalid price
Here it is directly from the log
2016.12.09 10:47:10.243 Simple Moving Average Crossover Strategy EURUSDi,H1: invalid price 1.05454000 for OrderClose function
{
if(OrderClose(OrderTicket(),Lots,Bid,10,clrNONE))
Print("Order Closed : ", OrderClosePrice());
return(0);
}
...
{
if(OrderClose(OrderTicket(),Lots,Ask,10,clrNONE))
Print("Order Closed : ", OrderClosePrice());
return(0);
Here is the OrderClose section, I am no longer getting the invalid ticket error, but i am now getting invalid price
Here it is directly from the log
2016.12.09 10:47:10.243 Simple Moving Average Crossover Strategy EURUSDi,H1: invalid price 1.05454000 for OrderClose function
Don't use Bid/Ask when closing, use OrderClosePrice(). It will be automatically the good price.
Should I just replace it in the OrderClose function? and if so should I remove the OrderClosePrice in the print function underneath it?
Edit:
I just did that, but now it will not hold any positions open. Just opens and closes positions, although it is now not having any issue working on multiple pairs.
if(OrderType()==OP_BUY)
{
if(fastma < slowma)
{
if(OrderClose(OrderTicket(),Lots,Bid,10,clrNONE))
Print("Order Closed : ", OrderClosePrice());
return(0);
}
else
Print("Error : ", GetLastError());
return(0);
break;
}
There is no beed for the returns or the break as you need to complete the loop
Also, your error print is dependant on
if(fastma < slowma)
being false, not related to whether the OrderClose() fails
if(OrderType()==OP_BUY)
{
if(fastma<slowma)
{
if(OrderClose(OrderTicket(),Lots, OrderClosePrice(),10,clrNONE))
Print("Order Closed : ",OrderClosePrice());
else
Print("Error : ",GetLastError())
}
}