////////////////////////////////////////////////////////////////////////////////
void Close_all_f()
{
int type;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderMagicNumber()!=Magic || OrderSymbol()!=Symbol()) continue;
type=OrderType();
bool ticket_ex=false;
for (int j_ex = 0;j_ex < 64; j_ex++)
{
while(IsTradeContextBusy()) Sleep(200);
RefreshRates();
if(type==OP_BUY ) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrYellow);
else
if(type==OP_SELL) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrYellow);
else
if(type==OP_SELLSTOP || type==OP_BUYSTOP || type==OP_SELLLIMIT || type==OP_BUYLIMIT) ticket_ex=OrderDelete(OrderTicket(),clrBrown);
else
break;
if(ticket_ex==true)break;
}
}
}
void Close_all_f()
{
int type;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderMagicNumber()!=Magic || OrderSymbol()!=Symbol()) continue;
type=OrderType();
bool ticket_ex=false;
for (int j_ex = 0;j_ex < 64; j_ex++)
{
while(IsTradeContextBusy()) Sleep(200);
RefreshRates();
if(type==OP_BUY ) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrYellow);
else
if(type==OP_SELL) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrYellow);
else
if(type==OP_SELLSTOP || type==OP_BUYSTOP || type==OP_SELLLIMIT || type==OP_BUYLIMIT) ticket_ex=OrderDelete(OrderTicket(),clrBrown);
else
break;
if(ticket_ex==true)break;
}
}
}
//Global
int ticketbuy=-1;int ticketsell=-1;
...
if(previousfast<previousslow && currentfast>currentslow)
{
if(ticketsell!=-1) int sellclose = OrderClose(ticketsell,closelots,50,Blue);
ticketbuy = OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss),Ask+(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Green);
}
if (previousfast>previousslow && currentfast<currentslow)
{
if(ticketbuy!=-1) int buyclose = OrderClose(ticketbuy,closelots,50,Blue);
ticketsell = OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss),Bid-(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Red);
}
int sellclose = OrderClose(ticketsell,closelots,50,Blue);
Your first problem is you've missed out price.
The compiler didn't give a warning or error because the final parameter (color) doesn't have to be passed. Blue is an integer (16711680) so it took that as the slippage value, and 50 as the price.
A minor point - OrderClose returns a bool not an integer. You would normally test the result
if(!OrderClose(...)) Print("OrderClose failed....
eevviill14:
int ticketsell=-1;
...
if(previousfast<previousslow && currentfast>currentslow)
{
if(ticketsell!=-1) int sellclose = OrderClose(ticketsell,closelots,50,Blue);
ticketbuy = OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss),Ask+(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Green);
}
if (previousfast>previousslow && currentfast<currentslow)
{
if(ticketbuy!=-1) int buyclose = OrderClose(ticketbuy,closelots,50,Blue);
ticketsell = OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss),Bid-(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Red);
}
//Global
int ticketbuy=-1;int ticketsell=-1;
...
if(previousfast<previousslow && currentfast>currentslow)
{
if(ticketsell!=-1) int sellclose = OrderClose(ticketsell,closelots,50,Blue);
ticketbuy = OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss),Ask+(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Green);
}
if (previousfast>previousslow && currentfast<currentslow)
{
if(ticketbuy!=-1) int buyclose = OrderClose(ticketbuy,closelots,50,Blue);
ticketsell = OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss),Bid-(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Red);
}
thanks heaps ! but im still getting that orderclose error 3 ;/
excuse all the greyed out stuff
//+------------------------------------------------------------------+
//| Two Moving Average Crossover EA.mq4 |
//| Reilly |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Reilly"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//////////////////////////////////////////////////////////////////////
//INPUT//
//////////////////////////////////////////////////////////////////////
extern double takeprofit=0.005;
extern double stoploss=0.0025;
extern double closelots = 0.01;
extern double lotsize=0.01;
extern int fastma=5;
extern int fastmashift=0;
//^ ma shift is offset of linegraph on chart
extern int fastmamethod=0;
//^ma method is simple/exponential moving average etc
extern int fastmaappliedto=0;
//^applied to is basing the moving average on the closing price of the bar or the opening price etc
extern int slowma=21;
extern int slowmashift=0;
extern int slowmamethod=0;
extern int slowmaappliedto=0;
extern int Slippage = 50;
extern int maxopentrades=3;
extern int magicnumber = 1337;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int ticketbuy=-1;
int ticketsell=-1;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
/*
double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
if (ticksize = 0.00001 || 0.001)
double pips = (ticksize*10);
else pips = ticksize;
*/
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Close_all_f()
{
int type;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderMagicNumber()!=magicnumber || OrderSymbol()!=Symbol()) continue;
type=OrderType();
bool ticket_ex=false;
for (int j_ex = 0;j_ex < 64; j_ex++)
{
while(IsTradeContextBusy()) Sleep(200);
RefreshRates();
if(type==OP_BUY ) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrYellow);
else
if(type==OP_SELL) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrYellow);
else
if(type==OP_SELLSTOP || type==OP_BUYSTOP || type==OP_SELLLIMIT || type==OP_BUYLIMIT) ticket_ex=OrderDelete(OrderTicket(),clrBrown);
else
break;
if(ticket_ex==true)break;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(OrdersTotal()>=maxopentrades) return;
double previousfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,2);
double currentfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,1);
//^(NULL=moving average on the current currency pair. 0=the current timeframe. fastma= the moving average period.
double previousslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,2);
double currentslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,1);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
int ticketbuy = OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss),Ask+(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Green);
int ticketsell = OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss),Bid-(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Red);
int sellclose = OrderClose(ticketsell,closelots,50,Blue);
int buyclose = OrderClose(ticketbuy,closelots,50,Blue);
*/
static datetime BarTime=0;
datetime now_bar=Time[0];
if(BarTime!=now_bar)
{
BarTime=now_bar;
/*
if(previousfast<previousslow && currentfast>currentslow)
{
if(sellclose==-1)
Print(GetLastError());
if(ticketbuy==-1)
Print(GetLastError());
}
if (previousfast>previousslow && currentfast<currentslow)
{
if (buyclose==-1)
Print(GetLastError());
if (ticketsell==-1)
Print(GetLastError());
}
}
*/
if(previousfast<previousslow && currentfast>currentslow)
{
if(ticketsell!=-1) int sellclose = OrderClose(ticketsell,closelots,50,Blue);
ticketbuy = OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss),Ask+(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Green);
}
if (previousfast>previousslow && currentfast<currentslow)
{
if(ticketbuy!=-1) int buyclose = OrderClose(ticketbuy,closelots,50,Blue);
ticketsell = OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss),Bid-(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Red);
}
}
}
//+------------------------------------------------------------------+
//| Two Moving Average Crossover EA.mq4 |
//| Reilly |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Reilly"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//////////////////////////////////////////////////////////////////////
//INPUT//
//////////////////////////////////////////////////////////////////////
extern double takeprofit=0.005;
extern double stoploss=0.0025;
extern double closelots = 0.01;
extern double lotsize=0.01;
extern int fastma=5;
extern int fastmashift=0;
//^ ma shift is offset of linegraph on chart
extern int fastmamethod=0;
//^ma method is simple/exponential moving average etc
extern int fastmaappliedto=0;
//^applied to is basing the moving average on the closing price of the bar or the opening price etc
extern int slowma=21;
extern int slowmashift=0;
extern int slowmamethod=0;
extern int slowmaappliedto=0;
extern int Slippage = 50;
extern int maxopentrades=3;
extern int magicnumber = 1337;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int ticketbuy=-1;
int ticketsell=-1;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
/*
double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
if (ticksize = 0.00001 || 0.001)
double pips = (ticksize*10);
else pips = ticksize;
*/
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Close_all_f()
{
int type;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderMagicNumber()!=magicnumber || OrderSymbol()!=Symbol()) continue;
type=OrderType();
bool ticket_ex=false;
for (int j_ex = 0;j_ex < 64; j_ex++)
{
while(IsTradeContextBusy()) Sleep(200);
RefreshRates();
if(type==OP_BUY ) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrYellow);
else
if(type==OP_SELL) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrYellow);
else
if(type==OP_SELLSTOP || type==OP_BUYSTOP || type==OP_SELLLIMIT || type==OP_BUYLIMIT) ticket_ex=OrderDelete(OrderTicket(),clrBrown);
else
break;
if(ticket_ex==true)break;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(OrdersTotal()>=maxopentrades) return;
double previousfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,2);
double currentfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,1);
//^(NULL=moving average on the current currency pair. 0=the current timeframe. fastma= the moving average period.
double previousslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,2);
double currentslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,1);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
int ticketbuy = OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss),Ask+(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Green);
int ticketsell = OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss),Bid-(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Red);
int sellclose = OrderClose(ticketsell,closelots,50,Blue);
int buyclose = OrderClose(ticketbuy,closelots,50,Blue);
*/
static datetime BarTime=0;
datetime now_bar=Time[0];
if(BarTime!=now_bar)
{
BarTime=now_bar;
/*
if(previousfast<previousslow && currentfast>currentslow)
{
if(sellclose==-1)
Print(GetLastError());
if(ticketbuy==-1)
Print(GetLastError());
}
if (previousfast>previousslow && currentfast<currentslow)
{
if (buyclose==-1)
Print(GetLastError());
if (ticketsell==-1)
Print(GetLastError());
}
}
*/
if(previousfast<previousslow && currentfast>currentslow)
{
if(ticketsell!=-1) int sellclose = OrderClose(ticketsell,closelots,50,Blue);
ticketbuy = OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss),Ask+(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Green);
}
if (previousfast>previousslow && currentfast<currentslow)
{
if(ticketbuy!=-1) int buyclose = OrderClose(ticketbuy,closelots,50,Blue);
ticketsell = OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss),Bid-(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Red);
}
}
}
//+------------------------------------------------------------------+

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
Im working on a simple 2 moving average EA
where when the lines cross over it will sell off the last order and make a new buy or sell order depending on which way the lines crossed.
Im getting quite a few errors when I run it, feels like im missing alot.
-orderclose error 4051
-orderclose error 3
Here is my failing EA
//| Two Moving Average Crossover EA.mq4 |
//| Reilly |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Reilly"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//////////////////////////////////////////////////////////////////////
//INPUT//
//////////////////////////////////////////////////////////////////////
extern double takeprofit=0.005;
extern double stoploss=0.0025;
extern double closelots = 0.01;
extern double lotsize=0.01;
extern int fastma=5;
extern int fastmashift=0;
//^ ma shift is offset of linegraph on chart
extern int fastmamethod=0;
//^ma method is simple/exponential moving average etc
extern int fastmaappliedto=0;
//^applied to is basing the moving average on the closing price of the bar or the opening price etc
extern int slowma=21;
extern int slowmashift=0;
extern int slowmamethod=0;
extern int slowmaappliedto=0;
extern int maxopentrades=3;
extern int magicnumber = 1337;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
/*
double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
if (ticksize = 0.00001 || 0.001)
double pips = (ticksize*10);
else pips = ticksize;
*/
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(OrdersTotal()>=maxopentrades) return;
double previousfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,2);
double currentfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,1);
//^(NULL=moving average on the current currency pair. 0=the current timeframe. fastma= the moving average period.
double previousslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,2);
double currentslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,1);
int ticketbuy = OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss),Ask+(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Green);
int ticketsell = OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss),Bid-(takeprofit),"Two Moving Average Crossover EA Trade",magicnumber,0,Red);
int sellclose = OrderClose(ticketsell,closelots,50,Blue);
int buyclose = OrderClose(ticketbuy,closelots,50,Blue);
static datetime BarTime=0;
datetime now_bar=Time[0];
if(BarTime!=now_bar)
{
BarTime=now_bar;
if(previousfast<previousslow && currentfast>currentslow)
{
if(sellclose==-1)
Print(GetLastError());
if(ticketbuy==-1)
Print(GetLastError());
}
if (previousfast>previousslow && currentfast<currentslow)
{
if (buyclose==-1)
Print(GetLastError());
if (ticketsell==-1)
Print(GetLastError());
}
}
}
//+------------------------------------------------------------------+