EA Modification - Close At Profit

Hi guys i need a little help please, i have the below EA which i use to close trades when profit reaches a certain amount. This EA allows me to close trades in profit at 1 pip where my brokers minimum is 3 pips.

Can anyone make the below modifications ? It would be much appreciated :slight_smile: Thanks

The EA currently only will close trades on all symbols or only on the symbol which the EA is attached.

If the “allsymbols” option is true the EA will close all open trades when their total value reaches the amount set for “profittoclose” or “losstoclose”.

If the “allsymbols” option is false the EA will currently only close trades on the symbol it is attached.

New option “allopentrades” (true/false)

If “allsymbols” is true then the below applies" otherwise it only applies to the currency pair the EA is attached to.

If “allopentrades” is true then the EA will look at all of the open trades. If a trade reaches the set variable “profittoclose” or “losstoclose”, then that trade and only that trade is closed. This should apply to all open trades, regardless of what currency pair the trade has been openned in i.e. EUR/USD. GBP/USD.

-CloseAtProfit.zip (1.99 KB)

//±-----------------------------------------------------------------+
#property copyright “Copyright © 2011 Matus German www.MTexperts.net”

extern bool useProfitToClose = true;
extern double profitToClose = 0.1;
extern bool useLossToClose = false;
extern double lossToClose = 100;
extern bool AllSymbols = false;
extern bool PendingOrders = true;
extern double MaxSlippage = 0;
extern bool showMenu = true;
extern color menuColor = White;
extern color variablesColor = Red;
extern int font = 14;

double pips2dbl, pips2point, pipValue, maxSlippage,
profit;

bool clear;

int medzera = 8,
trades;

double menulots;
//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
//----

Comment(“Copyright © 2011, Matus German”);

if (Digits == 5 || Digits == 3) // Adjust for five (5) digit brokers.
{
pips2dbl = Point*10; pips2point = 10;pipValue = (MarketInfo(Symbol(),MODE_TICKVALUE))*10;
}
else
{
pips2dbl = Point; pips2point = 1;pipValue = (MarketInfo(Symbol(),MODE_TICKVALUE))*1;
}

clear = true;

if(showMenu)
{
DrawMenu();
}
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----
if(showMenu)
{
ObjectDelete(“name”);
ObjectDelete(“Openl”);
ObjectDelete(“Open”);
ObjectDelete(“Lotsl”);
ObjectDelete(“Lots”);
ObjectDelete(“Profitl”);
ObjectDelete(“Profit”);
}
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
//----
if(showMenu)
{
ReDrawMenu();
}

if(!clear)
{
if(AllSymbols)
{
if(PendingOrders)
if(CloseDeleteAll())
clear=true;
else
return;
if(!PendingOrders)
if(CloseDeleteAllNonPending())
clear=true;
else
return;
}
if(!AllSymbols)
{
if(PendingOrders)
if(CloseDeleteAllCurrent())
clear=true;
else
return;
if(!PendingOrders)
if(CloseDeleteAllCurrentNonPending())
clear=true;
else
return;
}
}

profit = ProfitCheck();
if(useProfitToClose)
{
if(profit>profitToClose)
{
if(AllSymbols)
{
if(PendingOrders)
if(!CloseDeleteAll())
clear=false;
if(!PendingOrders)
if(!CloseDeleteAllNonPending())
clear=false;
}
if(!AllSymbols)
{
if(PendingOrders)
if(!CloseDeleteAllCurrent())
clear=false;
if(!PendingOrders)
if(!CloseDeleteAllCurrentNonPending())
clear=false;
}
}
}

if(useLossToClose)
{
if(profit<-lossToClose)
{
if(AllSymbols)
{
if(PendingOrders)
if(!CloseDeleteAll())
clear=false;
if(!PendingOrders)
if(!CloseDeleteAllNonPending())
clear=false;
}
if(!AllSymbols)
{
if(PendingOrders)
if(!CloseDeleteAllCurrent())
clear=false;
if(!PendingOrders)
if(!CloseDeleteAllCurrentNonPending())
clear=false;
}
}
}
//----
return(0);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
bool CloseDeleteAll()
{
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
switch(OrderType())
{
case OP_BUY :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),maxSlippage,Violet))
return(false);
}break;
case OP_SELL :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),maxSlippage,Violet))
return(false);
}break;
}

if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)
if(!OrderDelete(OrderTicket()))
{
Print("Error deleting " + OrderType() + " order : ",GetLastError());
return (false);
}
}
}
return (true);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// delete all on current chart
bool CloseDeleteAllCurrent()
{
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
switch(OrderType())
{
case OP_BUY :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),maxSlippage,Violet))
return(false);
}break;

case OP_SELL :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),maxSlippage,Violet))
return(false);
}break;
}

if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)
if(!OrderDelete(OrderTicket()))
{
return (false);
}
}
}
}
return (true);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// left pending orders
bool CloseDeleteAllNonPending()
{
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
switch(OrderType())
{
case OP_BUY :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),maxSlippage,Violet))
return(false);
}break;
case OP_SELL :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),maxSlippage,Violet))
return(false);
}break;
}
}
}
return (true);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// delete all on current chart left pending
bool CloseDeleteAllCurrentNonPending()
{
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
switch(OrderType())
{
case OP_BUY :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),maxSlippage,Violet))
return(false);
}break;

case OP_SELL :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),maxSlippage,Violet))
return(false);
}break;
}
}
}
}
return (true);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double ProfitCheck()
{
double profit=0;
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(AllSymbols)
profit+=OrderProfit();
else if(OrderSymbol()==Symbol())
profit+=OrderProfit();
}
return(profit);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
bool DrawMenu()
{
ObjectCreate(“name”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Openl”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Open”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Lotsl”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Lots”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Profitl”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Profit”,OBJ_LABEL,0,0,0,0,0);

medzera = 8;

trades = Opened();
menulots = Lots();

ObjectSetText( “name”, “CloseAtProfit”, font+1, “Arial”,menuColor);
ObjectSet(“name”,OBJPROP_XDISTANCE,medzera*font);
ObjectSet(“name”,OBJPROP_YDISTANCE,10+font);
ObjectSet(“name”,OBJPROP_CORNER,1);

ObjectSetText(“Openl”, "Opened trades: ", font, “Arial”,menuColor);
ObjectSet(“Openl”,OBJPROP_XDISTANCE,medzerafont);
ObjectSet(“Openl”,OBJPROP_YDISTANCE,10+2
(font+2));
ObjectSet(“Openl”,OBJPROP_CORNER,1);

ObjectSetText(“Open”, “”+trades, font, “Arial”,variablesColor);
ObjectSet(“Open”,OBJPROP_XDISTANCE,3font);
ObjectSet(“Open”,OBJPROP_YDISTANCE,10+2
(font+2));
ObjectSet(“Open”,OBJPROP_CORNER,1);

ObjectSetText(“Lotsl”, "Lots of opened positions: ", font, “Arial”,menuColor);
ObjectSet(“Lotsl”,OBJPROP_XDISTANCE,medzerafont);
ObjectSet(“Lotsl”,OBJPROP_YDISTANCE,10+3
(font+2));
ObjectSet(“Lotsl”,OBJPROP_CORNER,1);

ObjectSetText(“Lots”, DoubleToStr(menulots,2), font, “Arial”,variablesColor);
ObjectSet(“Lots”,OBJPROP_XDISTANCE,3font);
ObjectSet(“Lots”,OBJPROP_YDISTANCE,10+3
(font+2));
ObjectSet(“Lots”,OBJPROP_CORNER,1);

ObjectSetText(“Profitl”, "Profit of opened positions: ", font, “Arial”,menuColor);
ObjectSet(“Profitl”,OBJPROP_XDISTANCE,medzerafont);
ObjectSet(“Profitl”,OBJPROP_YDISTANCE,10+4
(font+2));
ObjectSet(“Profitl”,OBJPROP_CORNER,1);

ObjectSetText(“Profit”, DoubleToStr(profit,2), font, “Arial”,variablesColor);
ObjectSet(“Profit”,OBJPROP_XDISTANCE,3font);
ObjectSet(“Profit”,OBJPROP_YDISTANCE,10+4
(font+2));
ObjectSet(“Profit”,OBJPROP_CORNER,1);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ReDrawMenu()
{
medzera = 8;

trades = Opened();
menulots = Lots();

ObjectSetText(“Open”, “”+trades, font, “Arial”,variablesColor);
ObjectSetText(“Lots”, DoubleToStr(menulots,2), font, “Arial”,variablesColor);
ObjectSetText(“Profit”, DoubleToStr(profit,2), font, “Arial”,variablesColor);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int Opened()
{
int total = OrdersTotal();
int count = 0;
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(AllSymbols)
{
if(PendingOrders)
count++;
if(!PendingOrders)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
count++;
}
if(!AllSymbols)
{
if(OrderSymbol()==Symbol())
{
if(PendingOrders)
count++;
if(!PendingOrders)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
count++;
}
}
}
return (count);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double Lots()
{
int total = OrdersTotal();
double lots = 0;
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(AllSymbols)
{
if(PendingOrders)
lots+=OrderLots();
if(!PendingOrders)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
lots+=OrderLots();
}
if(!AllSymbols)
{
if(OrderSymbol()==Symbol())
{
if(PendingOrders)
lots+=OrderLots();
if(!PendingOrders)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
lots+=OrderLots();
}
}
}
return (lots);
}

//±-----------------------------------------------------------------+
#property copyright “Copyright © 2011 Matus German www.MTexperts.net”

extern bool useProfitToClose = true;
extern double profitToClose = 0.1;
extern bool useLossToClose = false;
extern double lossToClose = 100;
extern bool AllSymbols = false;
extern bool CloseEachOrder=true; // Use OrderProfitToClose & OrderLossToClose to close each order
extern double OrderProfitToClose=20; // Close when order reaches this in dollars
extern double OrderLossToClose=50; // Close when order falls below this in dollars
extern bool PendingOrders = true;
extern double MaxSlippage = 0;
extern bool showMenu = true;
extern color menuColor = White;
extern color variablesColor = Red;
extern int font = 14;

double pips2dbl, pips2point, pipValue, maxSlippage,
profit;

bool clear;

int medzera = 8,
trades;

double menulots;
//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
//----

Comment(“Copyright © 2011, Matus German”);

if (Digits == 5 || Digits == 3) // Adjust for five (5) digit brokers.
{
pips2dbl = Point*10; pips2point = 10;pipValue = (MarketInfo(Symbol(),MODE_TICKVALUE))*10;
}
else
{
pips2dbl = Point; pips2point = 1;pipValue = (MarketInfo(Symbol(),MODE_TICKVALUE))*1;
}

clear = true;

if(showMenu)
{
DrawMenu();
}
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----
if(showMenu)
{
ObjectDelete(“name”);
ObjectDelete(“Openl”);
ObjectDelete(“Open”);
ObjectDelete(“Lotsl”);
ObjectDelete(“Lots”);
ObjectDelete(“Profitl”);
ObjectDelete(“Profit”);
}
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
//----
if(showMenu)
{
ReDrawMenu();
}

if(CloseEachOrder){
checkprofitloss();
return(0);
}

if(!clear)
{
if(AllSymbols)
{
if(PendingOrders)
if(CloseDeleteAll())
clear=true;
else
return;
if(!PendingOrders)
if(CloseDeleteAllNonPending())
clear=true;
else
return;
}
if(!AllSymbols)
{
if(PendingOrders)
if(CloseDeleteAllCurrent())
clear=true;
else
return;
if(!PendingOrders)
if(CloseDeleteAllCurrentNonPending())
clear=true;
else
return;
}
}

profit = ProfitCheck();
if(useProfitToClose)
{
if(profit>profitToClose)
{
if(AllSymbols)
{
if(PendingOrders)
if(!CloseDeleteAll())
clear=false;
if(!PendingOrders)
if(!CloseDeleteAllNonPending())
clear=false;
}
if(!AllSymbols)
{
if(PendingOrders)
if(!CloseDeleteAllCurrent())
clear=false;
if(!PendingOrders)
if(!CloseDeleteAllCurrentNonPending())
clear=false;
}
}
}

if(useLossToClose)
{
if(profit<-lossToClose)
{
if(AllSymbols)
{
if(PendingOrders)
if(!CloseDeleteAll())
clear=false;
if(!PendingOrders)
if(!CloseDeleteAllNonPending())
clear=false;
}
if(!AllSymbols)
{
if(PendingOrders)
if(!CloseDeleteAllCurrent())
clear=false;
if(!PendingOrders)
if(!CloseDeleteAllCurrentNonPending())
clear=false;
}
}
}
//----
return(0);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
bool CloseDeleteAll()
{
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
switch(OrderType())
{
case OP_BUY :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),maxSlippage,Violet))
return(false);
}break;
case OP_SELL :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),maxSlippage,Violet))
return(false);
}break;
}

if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)
if(!OrderDelete(OrderTicket()))
{
Print("Error deleting " + OrderType() + " order : ",GetLastError());
return (false);
}
}
}
return (true);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// delete all on current chart
bool CloseDeleteAllCurrent()
{
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
switch(OrderType())
{
case OP_BUY :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),maxSlippage,Violet))
return(false);
}break;

case OP_SELL :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),maxSlippage,Violet))
return(false);
}break;
}

if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)
if(!OrderDelete(OrderTicket()))
{
return (false);
}
}
}
}
return (true);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// left pending orders
bool CloseDeleteAllNonPending()
{
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
switch(OrderType())
{
case OP_BUY :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),maxSlippage,Violet))
return(false);
}break;
case OP_SELL :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),maxSlippage,Violet))
return(false);
}break;
}
}
}
return (true);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// delete all on current chart left pending
bool CloseDeleteAllCurrentNonPending()
{
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol())
{
switch(OrderType())
{
case OP_BUY :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),maxSlippage,Violet))
return(false);
}break;

case OP_SELL :
{
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),maxSlippage,Violet))
return(false);
}break;
}
}
}
}
return (true);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double ProfitCheck()
{
double profit=0;
int total = OrdersTotal();
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(AllSymbols)
profit+=OrderProfit();
else if(OrderSymbol()==Symbol())
profit+=OrderProfit();
}
return(profit);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
bool DrawMenu()
{
ObjectCreate(“name”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Openl”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Open”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Lotsl”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Lots”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Profitl”,OBJ_LABEL,0,0,0,0,0);
ObjectCreate(“Profit”,OBJ_LABEL,0,0,0,0,0);

medzera = 8;

trades = Opened();
menulots = Lots();

ObjectSetText( “name”, “CloseAtProfit”, font+1, “Arial”,menuColor);
ObjectSet(“name”,OBJPROP_XDISTANCE,medzera*font);
ObjectSet(“name”,OBJPROP_YDISTANCE,10+font);
ObjectSet(“name”,OBJPROP_CORNER,1);

ObjectSetText(“Openl”, "Opened trades: ", font, “Arial”,menuColor);
ObjectSet(“Openl”,OBJPROP_XDISTANCE,medzerafont);
ObjectSet(“Openl”,OBJPROP_YDISTANCE,10+2
(font+2)) ;
ObjectSet(“Openl”,OBJPROP_CORNER,1);

ObjectSetText(“Open”, “”+trades, font, “Arial”,variablesColor);
ObjectSet(“Open”,OBJPROP_XDISTANCE,3font);
ObjectSet(“Open”,OBJPROP_YDISTANCE,10+2
(font+2));
ObjectSet(“Open”,OBJPROP_CORNER,1);

ObjectSetText(“Lotsl”, "Lots of opened positions: ", font, “Arial”,menuColor);
ObjectSet(“Lotsl”,OBJPROP_XDISTANCE,medzerafont);
ObjectSet(“Lotsl”,OBJPROP_YDISTANCE,10+3
(font+2)) ;
ObjectSet(“Lotsl”,OBJPROP_CORNER,1);

ObjectSetText(“Lots”, DoubleToStr(menulots,2), font, “Arial”,variablesColor);
ObjectSet(“Lots”,OBJPROP_XDISTANCE,3font);
ObjectSet(“Lots”,OBJPROP_YDISTANCE,10+3
(font+2));
ObjectSet(“Lots”,OBJPROP_CORNER,1);

ObjectSetText(“Profitl”, "Profit of opened positions: ", font, “Arial”,menuColor);
ObjectSet(“Profitl”,OBJPROP_XDISTANCE,medzerafont );
ObjectSet(“Profitl”,OBJPROP_YDISTANCE,10+4
(font+2 ));
ObjectSet(“Profitl”,OBJPROP_CORNER,1);

ObjectSetText(“Profit”, DoubleToStr(profit,2), font, “Arial”,variablesColor);
ObjectSet(“Profit”,OBJPROP_XDISTANCE,3font);
ObjectSet(“Profit”,OBJPROP_YDISTANCE,10+4
(font+2) );
ObjectSet(“Profit”,OBJPROP_CORNER,1);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ReDrawMenu()
{
medzera = 8;

trades = Opened();
menulots = Lots();

ObjectSetText(“Open”, “”+trades, font, “Arial”,variablesColor);
ObjectSetText(“Lots”, DoubleToStr(menulots,2), font, “Arial”,variablesColor);
ObjectSetText(“Profit”, DoubleToStr(profit,2), font, “Arial”,variablesColor);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int Opened()
{
int total = OrdersTotal();
int count = 0;
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(AllSymbols)
{
if(PendingOrders)
count++;
if(!PendingOrders)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
count++;
}
if(!AllSymbols)
{
if(OrderSymbol()==Symbol())
{
if(PendingOrders)
count++;
if(!PendingOrders)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
count++;
}
}
}
return (count);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double Lots()
{
int total = OrdersTotal();
double lots = 0;
for (int cnt = total-1 ; cnt >=0 ; cnt–)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(AllSymbols)
{
if(PendingOrders)
lots+=OrderLots();
if(!PendingOrders)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
lots+=OrderLots();
}
if(!AllSymbols)
{
if(OrderSymbol()==Symbol())
{
if(PendingOrders)
lots+=OrderLots();
if(!PendingOrders)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
lots+=OrderLots();
}
}
}
return (lots);
}

void checkprofitloss(){
int i;
if(OrdersTotal()>0){
for(i=OrdersTotal()-1;i>=0;i–){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY){
if(OrderProfit()>OrderProfitToClose || OrderProfit()<OrderLossToClose)
OrderClose(OrderTicket(),OrderLots(),Bid,maxSlippage);
}
if(OrderType()==OP_SELL){
if(OrderProfit()>OrderProfitToClose || OrderProfit()<OrderLossToClose)
OrderClose(OrderTicket(),OrderLots(),Ask,maxSlippage);
}
}
}
}

I havent tested it as it is the week end, but i think this is what you wanted.

Set CloseEachOrder to true,
Use OrderProfitToClose & OrderLossToClose to close each order. (they are a dollar value)

It should close orders when tp or sl is reached whichever pair is open.

as i said, it is untested, so might not work at all :slight_smile:

Also i noticed when it is pasted into a post like this, gaps are added in places.
eg. maxSlippa ge should be maxSlippage
and MarketInf o should be MarketInfo

look at the errors when you compile it and just delete the spaces.

Awesome thanks Goldylox

Also the EA does compile once i removed the spaces as you mentioned thanks :slight_smile:

The only thing i wanted the EA to do was to close trades based on pips rather than profit in dollars, is this an easy change, i can’t work it out?

Thanks

ok, the issue is that you have to check jpy pairs differently. & pips on different currencies have slightly different values. (think of gold for a more extreme example) I’ill give it a go.

Hi Sandy, the ea you requested for modification is something i;ve been looking for as well. Thanks for sharing it.

However, it can’t seem to work when i attached to my chart, i put my profit to close at 0.0005 and the ea doesn’t assign auto TP when it hit the target.

Am i missing something?

Thanks Goldylox its much appreciated :slight_smile:

It should work, were you placing it on JPY pairs like Goldylox mentioned above might be an issue at present?

Well its actually on GU pair, testing on my demo account with it. I have no idea whats wrong with it. Oh yea, does it automatically sets the TP when you place an order or the TP only appears when the profit value is close to it?

Btw, i am using a 5 digit broker. Does this make any difference?

Attach the EA to to any chart and when an open trade hits the take profit or stop loss (in pips), the EA closes that trade.

ok i figured out how to make it work now but it is closing every trades i made. i.e, i set the Uselosstoclose to true and set it to 0.01 (which is 100 pips) but the ea keep on closing every trades that occur a loss.

Set uselosdtoclose to false the EA is
Meant to close every trade when that trade reaches the stop loss.

That is how i wanted the EA to operare which at the moment Goldylox has it it close when an amount in dollars is reached.

But i do hope Goldylox can get the EA to close trades based on pips rather than dollars.

//±-----------------------------------------------------------------+
//| Goldylox.mq4 |
//| Goldylox |
//| |
//±-----------------------------------------------------------------+
#property copyright “Goldylox”
#property link “”

extern double takeprofit=10;
extern double stoploss=20;
double tp,sl,dg;

//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
//----
dg=Digits;
//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
//----

selectorder();
//----
return(0);
}
//±-----------------------------------------------------------------+

void selectorder(){
int i;
double pt;
if(OrdersTotal()>0){
for(i=OrdersTotal()-1;i>=0;i–){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY){
if(dg==3 || dg==5)pt=Point10;
else pt=Point;
if(StringFind(OrderSymbol(),“JPY”,0) >= 0 && (dg==3 || dg==5)) pt=0.01;
if(StringFind(OrderSymbol(),“JPY”,0) >= 0 && (dg!=3 || dg!=5)) pt=0.1;
if(OrderTakeProfit()==0){
OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+takeprofit
pt,0,Blue);
}
if(OrderStopLoss()==0){
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-stoploss*pt,OrderTakeProfit(),0,Blue);
}
}

        if( OrderType()==OP_SELL){
           if(dg==3 || dg==5)pt=Point*10;
              else pt=Point;  
           if(StringFind(OrderSymbol(),"JPY",0) &gt;= 0 && (dg==3 || dg==5)) pt=0.01;
           if(StringFind(OrderSymbol(),"JPY",0) &gt;= 0 && (dg!=3 || dg!=5)) pt=0.1;
           
           if(OrderTakeProfit()==0){
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-takeprofit*pt,0,Red);
               }
            if(OrderStopLoss()==0){
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+stoploss*pt,OrderTakeProfit(),0,Red);
               } 
            }
          }
       }
    }

I think this is more what you wanted?

It is pretty simple and probably should have been a script not ea.

It just sets TP and SL to all open orders with 0 tp and 0 sl

Should work on JPY pairs too.

I checked it on a 5 digit broker. It should work on 4 digit but i didnt check.

Thanks Goldylox will give it a try later when im home.

The main reason i wanted it as an EA rather than a script was so when i open trades the EA will automatically close open trades when they hit 1 pip whether i open them manually or by an EA.

It is just to take some of the emotion out of trading and so i cant get greedy.

Your EA is so short in code compared to the original lol.

Thanks will let you know how i get on with it :slight_smile:

Yep, understand, it has to be an ea.

It is a pretty basic. just sets sl and tp.

ALso dont forget to take the spaces out of the code.

Hi Goldylox, I have tried out your EA but its not closing trades when they reach “1.0” pips or even “0.1” pips.

What i was after this EA to do was to just close open trades when their profit reaches a certain level rather than modify their TP and SL if thats possible?

Cheers

Sorry i dont understand.

When profit reaches a certain level = takeprofit?

Or do you mean overall profit as in a basket take profit?