Close the highest Specific Open Order

 
hello, I just wondering if there is a way that i can close the highest open position using OrderProfit() function because my strategy is once i maxed out the max trade out of my ea. the ea will close the highest profit order and open new order and it will repeat the process. 
 
Karl Robeck if there is a way that i can

Yes. Code it to do that.
     How To Ask Questions The Smart Way. (2004)
          Only ask questions with yes/no answers if you want “yes” or “no” as the answer.

 
void  CloseProfit() {
         
            double MAXprofit=0;
               for(int i=OrdersTotal()-1; i>=0; i--)
                  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
                     if(OrderSymbol()==Symbol())
                        if(OrderMagicNumber()==InpMagicNumber) 
                         {
                           MAXprofit=MathMax(MAXprofit,OrderProfit()+OrderSwap()+OrderCommission());                                        
                           if(MAXprofit==OrderProfit())
                           int close = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0);
                         }
                        
                 }
 
Karl Robeck #:
void  CloseProfit() {
         
            double MAXprofit=0;
            double OrdProfit=0;
            double OrdTicket=0;
               for(int i=OrdersTotal()-1; i>=0; i--)
                  {
                  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
                     if(OrderSymbol()==Symbol())
                        if(OrderMagicNumber()==InpMagicNumber) 
                         {
                           OrdProfit=OrderProfit()+OrderSwap()+OrderCommission();
                           if(OrdProfit>0 &&  OrdProfit>=MAXprofit)
                            {
                             MAXprofit=OrdProfit; 
                             OrdTicket=OrderTicket();
                            }
                           //int close = OrderClose(, OrderLots(), OrderClosePrice(), 0);
                         }
                        

                     }
                     if (MAXprofit>0 && OrdTicket>0)
                         {
                         if(OrderSelect(OrdTicket,SELECT_BY_POS,MODE_TRADES))
                         bool  close = OrderClose(OrdTicket, OrderLots(), OrderClosePrice(), 0);
                         }
                   
                 }
 
Karl Robeck #:
Two doubles cannot be equal … I would store the profits into an Array and use ArrayMaximum… this will give you the index of the element with the highest value. 
 
Mehmet Bastem #:

hi, i've compared the codes that i've made to yours. to my surprise your code is much better but there is a problem to your code and mine. my problem with my code is that it closes too many trades after that max trade is reach that's why the EA will open many trades and if the EA reach the max trade it will close the most profitable trade again and so on but the strategy is that if the EA reach the MAX trade that is allowed it must remove the most profitable trade in order to open new trades again. the problem that i saw to your code after i backtested it is that after it reach the max trade the EA will wait for the most profitable trade on all of the order that is open that's why it does not open new trades it just wait and it does not use MathMax to filter out the most profitable trade (just correct me if i'm wrong hehe). here's the result on my code and yours(it can have higher profit and lower drawdown if i complete the EA because this strategy is day trading)


StrategyTester is my code

StrategyTester2 is your code

 
Daniel Cioca #:
Two doubles cannot be equal … I would store the profits into an Array and use ArrayMaximum… this will give you the index of the element with the highest value. 

thanks for the idea mate i will try that and btw do you have any example that i can use tnx again :)

 
Karl Robeck #:

thanks for the idea mate i will try that and btw do you have any example that i can use tnx again :)

Still don’t understand the idea… because for loop will run and close the highest profit order… and at the next tick will run again… and close the highest profit order…. Until it is nothing left open… 😁
 
Daniel Cioca #:
Still don’t understand the idea… because for loop will run and close the highest profit order… and at the next tick will run again… and close the highest profit order…. Until it is nothing left open… 😁
Sorry for the confusion hehe. The trading strategy is that if the indicator gave a signal it will open trade every new bar depending on the timeframe until the max trade is reach. Once the max trade is reach the EA will go through a loop and gather all of the open order and get the highest profitable order and close the specific order so that will be one close order and one open order every candle. I hope you understand the strategy ☺️
 
Daniel Cioca #:
Still don’t understand the idea… because for loop will run and close the highest profit order… and at the next tick will run again… and close the highest profit order…. Until it is nothing left open… 😁
So the only problem that i'm struggling is that if i reach the max trade ex(20) how can i close the most profitable order and open new order. The plan is that if i reach 20 trades close the most profitable trade and that would be 19 orders and open new order to make it 20 And the strategy repeats 
 
Daniel Cioca #:
Still don’t understand the idea… because for loop will run and close the highest profit order… and at the next tick will run again… and close the highest profit order…. Until it is nothing left open… 😁
And it only runs every new candle 
Reason: