Hi,
Need support on coding function to close all orders for all pairs with same magic number if net profit equal spasified money amount
I searched around and tried to code it myself but unfortunately not succeed :)
Appreciate your support
Thanks
- Close all trades, Based on Magic Number Modification
- 'Close all'/'Open' tools
- Magic number
Alain Verleyen:
Show your code if you need coding help.
Show your code if you need coding help.
Hi Alain thank you for your reply, I have tried it two ways as below and on both not succeeded, what am looking for is to close all trades with same magic number on all pairs if profit is reached without the need to close other manual trades or trades with different magic numbers from other EAs
I think am missing how to collect the orders with same magic number or am not sure what is wrong :)
1st this code
extern int MagicNumber = 12345; extern double My_Money_Profit_Target=100; int Slippage=5; ------------------------------------------------------------------------------------ int i; { if (AccountProfit()>= My_Money_Profit_Target&& MagicNumber == 12345) { for(i=OrdersTotal()-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; switch(type) { //Close opened long positions case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Pink); break; //Close opened short positions case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Pink); } if(result == false) { Sleep(0); } } Print ("Account Profit Reached. All Open Trades Have Been Closed"); //return(0); }
2nd code I use is this
int i; { if (AccountProfit()>= My_Money_Profit_Target&& MagicNumber == 12345) { for(int i=OrdersTotal()-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); bool result = false; if ( OrderType() == OP_BUY && OrderProfit()+OrderSwap()>My_Money_Profit_Target) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); if ( OrderType() == OP_SELL && OrderProfit()+OrderSwap()>My_Money_Profit_Target) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); } return; } Comment("Balance: ",AccountBalance(),", Account Equity: ",AccountEquity(),", Account Profit: ",AccountProfit(), "\nMy Account Profit Target: ",My_Money_Profit_Target); //return(0); }
thanks in advance for your help
Ali Hasan:
Hi,
Hi,
Need support on coding function to close all orders for all pairs with same magic number if net profit equal spasified money amount
I searched around and tried to code it myself but unfortunately not succeed :)
Appreciate your support
Thanks
Hi Ali,
Here I created a function for you.
You can easily use it by call closealltrade()
string error_table; int OnInit() { error_table="129;130;135;136;137;138"; //--- return(INIT_SUCCEEDED); }
if(AccountProfit() >= My_Money_Profit_Target) closealltrade();
void closealltrade() { int total_pos=OrdersTotal(); for(int i=total_pos-1; i>=0; i--) { ticket=OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(OrderType()>=2 || OrderMagicNumber()!=MagicNumber) continue; Print("* Closing ... ",OrderSymbol(),". Ticket #"+IntegerToString(OrderTicket()) ); while (true) { ResetLastError(); if(OrderType()==OP_BUY) ticket=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,clrBlue); else if(OrderType()==OP_SELL) ticket=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),3,clrRed); error=GetLastError(); if(ticket<=0) { if(StringFind(error_table,IntegerToString(error),0)<0) //-- if error not found in error table then exit the loop (while) { Print("*! Can't close position on broker's side for ",OrderSymbol()," ticket: ",OrderTicket(),". Try again later. ",IntegerToString(error)); break; } RefreshRates(); //-- if error found in error table then retry } else { Print("* ",OrderSymbol(),", ticket: ",OrderTicket()," closed."); break; } //---- 2 seconds wait Sleep(2000); } // --> while true }//--for }//--end closealltrade
Kind regards,
Yohana (^̮^)
old code
int OnInit() { //--- EventSetMillisecondTimer(200); //--- return(INIT_SUCCEEDED); }
replaced with new code
string error_table="129;130;131;135;136;137;138"; int ticket, error; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--EventSetMillisecondTimer(200); delete this command //--- return(INIT_SUCCEEDED); }
Ali Hasan:
old code
replaced with new code
yes, you got it (^̮^)
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