/  Forum 

If you are like me, you get dozens of newsletters in your email inbox daily and sometimes hourly. I delete most of them, but hardly ever unsubscribe because I know one of these days I am going to need the services of those I have chosen to subscribe to in the first place. One in particular caught my eye recently. It was from the owner of a yoga studio that opened about a year ago in Santa Monica, California. I am interested in following the movement and decisions of this particular owner because her business model seems unusual. She opened her studio and classes are by donation only. That’s rightyou pay whatever you feel is appropriate and you can wait until the class is over to decide what you want to pay. And, they video stream live classes, on occasion, so that people all over the world can join. I am watching this business model to see how it’s working. It’s actually booming! 

You are making decisions about closing orders before you know if . . . "the Sum of profits and losses is positive. " . . . you only know if the " Sum of profits and losses is positive. " only after you have summed all the losing trades and all the winning trades, and that will be after the loop is complete . . . For example, take the first trade seen in the loop, if it is a Buy and a loss then Sellprofit (0) will be greater than Buyprofit (a loss) and if Bid > Close[1] the order will be closed. 

RaptorUK: You are making decisions about closing orders before you know if . . . "the Sum of profits and losses is positive. " . . . you only know if the " Sum of profits and losses is positive. " only after you have summed all the losing trades and all the winning trades, and that will be after the loop is complete . . . For example, take the first trade seen in the loop, if it is a Buy and a loss then Sellprofit (0) will be greater than Buyprofit (a loss) and if Bid > Close[1] the order will be closed.
if(OrderType()==OP_BUY ) { Buyprofit+=OrderProfit()+OrderSwap()+OrderCommission(); } if(OrderType()==OP_SELL) { Sellprofit+=OrderProfit()+OrderSwap()+OrderCommission(); } Buyprofit will sums all profit and losses when the OrderType()==OP_BUY and the Sellprofit will sums all profit and losses when the OrderType()==OP_SELL. so if the Totalprofit=Buyprofit+Sellprofit is greater than 0 then the sum of profits and losses is more than 0. if you take look at the above code you see that i made two closing conditions and by each one Totalprofit>0 is needed. 

hmrt135: Yep, OK, I see what you mean about Totalprofit . . . I just don't get the logic of doing this as you go through the loop, order by order . . . if the first order you see is in profit then you close it ? this doesn't make sense in light of what you said . . . . "
I want to code an Ea that could close all open trades at once "
Buyprofit will sums all profit and losses when the OrderType()==OP_BUY and the Sellprofit will sums all profit and losses when the OrderType()==OP_SELL. so if the Totalprofit=Buyprofit+Sellprofit is greater than 0 then the sum of profits and losses is more than 0. if you take look at the above code you see that i made two closing conditions and by each one Totalprofit>0 is needed. 
3854 
Second.... its confusing what you're looking for. Given what I understand, you'll want to check for total profit first before closing anything. Something like below, I didn't test it. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void start(){ int My_Magic=7; double Buy_Profit=Profit_Total_Magic_Symbol_Type(My_Magic,Symbol(),OP_BUY); double Sel_Profit=Profit_Total_Magic_Symbol_Type(My_Magic,Symbol(),OP_SELL); double Total_Profit=Buy_Profit+Sel_Profit; if(Total_Profit>0){Close_Order_Of_Largest_Size_Recommended();} } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ double Profit_Total_Magic_Symbol_Type( /* Function Returns the Total Profit After Swaps and Commissions for Particular MagicNumber, Symbol, OrderType. Only works on Active Orders and NotUpon Historical Orders Example of Call: Profit_Total_Magic_Symbol_Type(7,"GBPUSD",1); */ int Magic=0, string Symb="", int Type=0 //Example OP_BUY ){ double Res; if(Symb==""){Symb=Symbol();} for(int i=OrdersTotal()1; i>=0; i){ if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ if(OrderMagicNumber()==Magic && OrderSymbol()==Symb && OrderType()==Type ){ Res+=OrderProfit()+OrderSwap()+OrderCommission(); } } }return(Res); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

Trades will be closed one by one you can't change that If there are also pendingtrades that has to be deleted then a little change will be needed inside this //++ // NEW close all trades.mq4  //++ #property copyright "T. de Vries" #property link "" extern int Slippage = 3; extern double MaxLoss = 420.0; extern double MinProfit = 40.0; extern int Magic1 = 1234; // Globals int TradesSlippage; double Poin,profitnow; bool CloseOrdersNow=false; //++ //  //++ //calculating total profit double MyProfitsTotal(int Magic1) //(int Magic1) { double profit=0; for (int cnt = OrdersTotal()1 ; cnt >= 0 ; cnt) { OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if (OrderMagicNumber() == Magic1 && OrderSymbol()==Symbol()) { // if(OrderType() == OP_BUY  OrderType() == OP_SELL) profit+= OrderProfit() + OrderCommission() + OrderSwap(); } //if } //for return(profit); } int init() { if (Digits == 4  Digits == 2) { TradesSlippage = Slippage; Poin = Point; } if (Digits == 5  Digits == 3) { TradesSlippage = Slippage*10; Poin = Point*10; } return(0); } int deinit(){return(0);} void ScreenMsg() { string profitstr = DoubleToStr(profitnow,2); string ls_80 = "\n" + "\n" + "\n"; string ls_64 = "\n"; string ls_88 = " Account Number: " + AccountNumber() + "\n" + " Server Time: " + TimeToStr(TimeCurrent(), TIME_SECONDS) + "\n" + " TradingResult = " + profitstr + "\n"; Comment(ls_80 + ls_64 + ls_88); } int start() { profitnow = MyProfitsTotal(Magic1); if((CloseOrdersNow) && (profitnow == 0)){CloseOrdersNow = false;} if(profitnow > MinProfit profitnow < (1*MaxLoss)){CloseOrdersNow = true;} ScreenMsg(); if (CloseOrdersNow) { for (int cnt = OrdersTotal()1 ; cnt >= 0 ; cnt) { OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if (OrderMagicNumber() == Magic1 && OrderSymbol()==Symbol() && OrderType()<2) { OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),TradesSlippage,Blue); } //if } //for } return(0); } // the end 

RaptorUK: Yep, OK, I see what you mean about Totalprofit . . . I just don't get the logic of doing this as you go through the loop, order by order . . . if the first order you see is in profit then you close it ? this doesn't make sense in light of what you said . . . . " I want to code an Ea that could close all open trades at once "
when i run my ea, it closes only orders, which has met closing condition and is in profit and the ea leave other orders to be stay open. Exactly i need a closing code that close all opened orders simultanusly as soon as totalprofit > 0 and the other custom closing conditions are met. For example: I made a hedge with 3 orders. 2 Buy orders and 1 sell order. if the sum of Buy profits is greater than Sell losses (Totalprofit=Buyprofit+Sellprofit >0) and the closing condition for example ( Close[0] < Low [1]) is meetd then Ea must close all 3 orders simultanusly in order to have profit. I think you got what i am looking for. on the above code i thought that when the Ea closed the profitable orders then Totalprofit is not positive any more so the lossing trades stays open. Therefor i wrote the closing code for losses trades first and after that the profitable one. But it does not work again and the Ea closed only profitable orders and the the non profitable orders stays open. #define NL "\n" // for (int y=OrdersTotal()1;y>=0;y) { double Sellprofit; double Buyprofit ; double Totalprofit ; OrderSelect(y,SELECT_BY_POS,MODE_TRADES); if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { if(OrderType()==OP_BUY ) { Buyprofit+=OrderProfit()+OrderSwap()+OrderCommission(); } if(OrderType()==OP_SELL) { Sellprofit+=OrderProfit()+OrderSwap()+OrderCommission(); } Totalprofit=Buyprofit+Sellprofit; Comment(" Comments Last Update 12122006 10:00pm", NL, " Buyprofit ", Buyprofit, NL, " Sellprofit ", Sellprofit, NL, " Totalprofit ", Totalprofit, NL, " Current Time is ",TimeHour(CurTime()),":",TimeMinute(CurTime()),".",TimeSeconds(CurTime())); } } // return(0); } 

Your hedging is that with one currency ? if (Close[0]>Close[1] && Totalprofit>0 && Sellprofit>Buyprofit) Are those conditions checked every tick ?? But your basic error might be this for (int y=OrdersTotal()1;y>=0;y) { double Sellprofit; double Buyprofit ; double Totalprofit ; With every next order in the loop you change those 3 conditions back to empty Place it above the loop and after finishing the loop you can calculate Totalprofit not inside the loop 

deVries: Your hedging is that with one currency ? if (Close[0]>Close[1] && Totalprofit>0 && Sellprofit>Buyprofit) Are those conditions checked every tick ?? But your basic error might be this With every next order in the loop you change those 3 conditions back to empty Place it above the loop and after finishing the loop you can calculate Totalprofit not inside the loop
so how could it close orders when the profit calculation loop is after closing loop? 

hmrt135:
so how could it close orders when the profit calculation loop is after closing loop?
Take your time to read the topic again and then make it with two loops like I advice you to do.... Then show your new code... 

RaptorUK: Exactly what I was saying above . .
You are making decisions about closing orders before you know if . . . "the Sum of profits and losses is positive. " . . . you only know if the " Sum of profits and losses is positive. " only after you have summed all the losing trades and all the winning trades, and that will be after the loop is complete . . . 
Hi Guys,
I want to code an Ea that could close all open trades at once, with closing conditions as soon as the Sum of profits and losses is positive. There are many articles in the web that offers codes, scripts or EA in order to close trades at profit. But these codes are to close opened trades that are in profit and they leave the losses trades to be open.
I hope that you understand what i am seeking for. For example i want to close all opened orders by Hedging as soon as the closing conditions are meeting and the sum of profit and losses is positive.
I made the below code for my ea but it closes trades that they meet the closing conditions and are in profit and the losses trades will be stay opened.
can anyone help me by this problem?
I thought that by closing profitable trades, the sum of profit and losses is not positive any more then the losses trades will be stay opened. therefor i wrote the closing code by each situation so that the losses trades must be closed first and after that the profitable trades closing code, but it does not work again!
Sorry for my English knowledge.