Hi There ...
Because you are working with any symbol you can not use things such as bid , ask, point but rather MarketInfo( OrderSymbol(), MODE_BID ), MarketInfo( OrderSymbol(), MODE_ASK ) etc ...
Try updating to this:
//+------------------------------------------------------------------+
//| check profitable positions with magic 111 |
//+------------------------------------------------------------------+
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderMagicNumber()==111 )
{
if(OrderType()==OP_BUY)
{
CurrentProfitB+= MarketInfo( OrderSymbol(), MODE_BID)-OrderOpenPrice() ;
}//if(OrderType()==OP_BUY)
if(OrderType()==OP_SELL)
{
CurrentProfitS+=OrderOpenPrice()- MarketInfo( OrderSymbol(), MODE_ASK );
}//if(OrderType()==OP_SELL)
} //if( OrderSymbol()==Symbol1 && OrderMagicNumber()==111 )
CurrentProfit=CurrentProfitB+CurrentProfitS;
}// for(cnt=OrdersTotal();cnt>=0;cnt--)
//======================
[/PHP]
[PHP]
//+------------------------------------------------------------------+
//| Close all |
//+------------------------------------------------------------------+
void CloseAllOrd(int magic)
{
RefreshRates();
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == magic)
if(IsTradeContextBusy()) Sleep(1000);
if(IsTradeContextBusy()) Sleep(2000);
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),MarketInfo( OrderSymbol(), MODE_BID),slippage,White);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),MarketInfo( OrderSymbol(), MODE_ASK),slippage,White);
}
}
You may want to check your log for more errors but that should do it.
Patrick
Thanks a lot Patrick, I think you are right. I will change the code and try it
I need help please,
I am trying to close orders from different currency pairs ( long and short ) but with the same magic number when the profit reach define target " lets say 15 pips" without touching the other orders with different magic number
Here is some code that should work
void closeProfitable(int tpPips, int magic) {
for(i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
break;
if(OrderMagicNumber() != magic)
continue;
double oAsk = MarketInfo(OrderSymbol(), MODE_ASK);
double oBid = MarketInfo(OrderSymbol(), MODE_BID);
if(OrderType() == OP_BUY &&
oBid > OrderOpenPrice() + tpPips * Point) {
OrderClose(OrderTicket(),OrderLots(),oBid,0);
i--;
}
if(OrderType() == OP_SELL &&
oAsk < OrderOpenPrice() - tpPips * Point) {
OrderClose(OrderTicket(),OrderLots(),oAsk,0);
i--;
}
}
}
ps. that code is not tested, I wrote it to this post directly but it should work
Yes but that would close trades on profit , I am looking at the combined trades menaing that EURUSD could be in 30pips+ and USDCHF could be in -15 pips so I look at the overall profit of trades with magic number 111 if the total profit from all those trades reach 15 pips I close all orders of magic number 111 only at combined profit of 15 pips
Here is some code that should work
void closeProfitable(int tpPips, int magic) {
for(i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
break;
if(OrderMagicNumber() != magic)
continue;
double oAsk = MarketInfo(OrderSymbol(), MODE_ASK);
double oBid = MarketInfo(OrderSymbol(), MODE_BID);
if(OrderType() == OP_BUY &&
oBid > OrderOpenPrice() + tpPips * Point) {
OrderClose(OrderTicket(),OrderLots(),oBid,0);
i--;
}
if(OrderType() == OP_SELL &&
oAsk < OrderOpenPrice() - tpPips * Point) {
OrderClose(OrderTicket(),OrderLots(),oAsk,0);
i--;
}
}
}
Yes but that would close trades on profit , I am looking at the combined trades menaing that EURUSD could be in 30pips+ and USDCHF could be in -15 pips so I look at the overall profit of trades with magic number 111 if the total profit from all those trades reach 15 pips I close all orders of magic number 111 only at combined profit of 15 pips
Okay now I get your point.. If you don't mind using a big array..
#define MAX_MAGIC_NUMBER 100 // or whatever
void closeProfitable(int tpPips, int magic) {
double profit[MAX_MAGIC_NUMBER];
for(i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
break;
if(OrderMagicNumber() != magic)
continue;
double oAsk = MarketInfo(OrderSymbol(), MODE_ASK);
double oBid = MarketInfo(OrderSymbol(), MODE_BID);
if(OrderType() == OP_BUY)
profit[OrderMagicNumber()] += oBid - OrderOpenPrice();
if(OrderType() == OP_SELL)
profit[OrderMagicNumber()] += OrderOpenPrice() - oAsk;
}
for(i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
break;
if(OrderMagicNumber() != magic)
continue;
double oAsk = MarketInfo(OrderSymbol(), MODE_ASK);
double oBid = MarketInfo(OrderSymbol(), MODE_BID);
if(profit[OrderMagicNumber()] >= tpPips*Point) {
if(OrderType() == OP_BUY) {
OrderClose(OrderTicket(),OrderLots(),oBid,0);
i--;
}
if(OrderType() == OP_SELL) {
OrderClose(OrderTicket(),OrderLots(),oAsk,0);
i--;
}
}
}
same disclaimers as above, I didn't test this at all
what does
#define MAX_MAGIC_NUMBER 100 // or whatever
do ?
also this line
double profit[MAX_MAGIC_NUMBER];
do I just put 111 there ? I am a bit confused as I am new in programming
Thanks a lot for your help
Have a look at this ea, which appears to do what you want.
yes I think I can use part of the code it seems to do what I want, now I had made 3 experts , each one based on a reply I got , lucky me.
I will test them this week , I thank you Mistigri , mikkom and waltini your help is really appricated
Minime,
I created the following piece of code to get the total profit of all orders with the same magic number:
{
double dblProfit=0;
int intPOS=0;
bool boolTerm=false;
while(boolTerm==false)
{
if(OrderSelect(intPOS,SELECT_BY_POS))
{
if(OrderMagicNumber()==intMagic) dblProfit=dblProfit+OrderProfit();
intPOS++;
}
else
boolTerm=true;
}
return(dblProfit);
}[/PHP]
All you have to do is copy the above code and paste it in your EA, I usually put it at the bottom, after the
}[/PHP]
You will also want to use a Close By Magic function:
{
int intOffset=0;
int Count = OTBM(intMagic);
while(OTBM(intMagic)>0 && Count > 0)
{
OrderSelect(intOffset,SELECT_BY_POS);
if(OrderMagicNumber()==intMagic)
{
if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);
else if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);
Count--;
}
else {
intOffset++;
}
}
return(0);
}Also include this Orders Total By Magic Function:
[PHP]int OTBM(int intMagic)//OrdersTotalByMagic
{
int intCount=0;
int intPOS=0;
bool boolTerm=false;
while(boolTerm==false)
{
if(OrderSelect(intPOS,SELECT_BY_POS))
{
if(OrderMagicNumber()==intMagic) intCount++;
intPOS++;
}
else
boolTerm=true;
}
return(intCount);
}Then all you need to code is:
[PHP]if (OPBM(Your_Magic_Number) >= Your_Profit_Target)
{
CBM(Your_Magic_Number);
}Hope this helps.
I have been overwhelmed by the generosity of the people in TSD thanks a lot wolfe
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I need help please,
I am trying to close orders from different currency pairs ( long and short ) but with the same magic number when the profit reach define target " lets say 15 pips" without touching the other orders with different magic number
so I wrote this code to check open trades and count the profit on trades with magic number 111
//+------------------------------------------------------------------+
//| check profitable positions with magic 111 |
//+------------------------------------------------------------------+
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderMagicNumber()==111 )
{
if(OrderType()==OP_BUY)
{
CurrentProfitB+=Bid-OrderOpenPrice() ;
}//if(OrderType()==OP_BUY)
if(OrderType()==OP_SELL)
{
CurrentProfitS+=OrderOpenPrice()-Ask;
}//if(OrderType()==OP_SELL)
} //if( OrderSymbol()==Symbol1 && OrderMagicNumber()==111 )
CurrentProfit=CurrentProfitB+CurrentProfitS;
}// for(cnt=OrdersTotal();cnt>=0;cnt--)
//======================
[/PHP]
then I check if we make profit
// Did we make a profit
//======================
if(profit>0 && CurrentProfit>=(profit*Point))
{
while(true)
{
CloseAllOrd(111);
}//while
}//if(profit>0 && CurrentProfit>=(profit*Point))
if we make a profit I call the function CloseAllOrd
[PHP]
//+------------------------------------------------------------------+
//| Close all |
//+------------------------------------------------------------------+
void CloseAllOrd(int magic)
{
RefreshRates();
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == magic)
if(IsTradeContextBusy()) Sleep(1000);
if(IsTradeContextBusy()) Sleep(2000);
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,slippage,White);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,slippage,White);
}
}
however this will only close EURUSD after it reach 1 pip of profit sometimes 2
I need to close all orders( different currency pairs ) with magic number 111 at lets say 15 pips of profit , without touching other orders
can someone please help me