Hi guys... I've been killing myself over this piece of script for the last 1 week
(yes, one entrie week...) and I still can't find the problem... Can you please
help me? I'm trying to write a small function to close up the one order that's
currently in the biggest loss (or: the "Least pofitable open order").
The code as follows: [CODE] void KillLeastProfitable() { int totalorders=OrdersTotal();
int num_array[99999]; int ctm,bbb; for (bbb=totalorders-1;bbb>=0;bbb--) { if(OrderSelect(bbb,
SELECT_BY_POS, MODE_TRADES)) { ctm=OrderCloseTime(); // if (ctm==0 && OrderMagicNumber()
== uniqueMagic) if (ctm==0) { num_array[bbb]=OrderProfit(); } } } bbb=num_array[ArrayMinimum(num_array,
0,0)]; if (OrderSelect(bbb, SELECT_BY_POS, MODE_TRADES)) { if ( OrderType() ==
OP_BUY ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID),
5, Red ); if ( OrderType() == OP_SELL ) OrderClose( OrderTicket(), OrderLots(),
MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); if ( OrderType() > 1 ) OrderDelete(
OrderTicket() ); } } [/CODE] As you can see, the idea is to round up all the orders
into one array, then use ArrayMinimum() to select the one with the least profit
(or rather: the biggest loss negative), then take that sucker and kill it. At least
in theory. Reality is: Nothing happens. However, if I reverse the idea and replace
ArrayMinimum with ArrayMaximum, it closes the most profitable order - as you would
expect. So the problem - i figure - should be around ArrayMinimum... Can somebody
please help me fix this code so that it will close the order with the biggest loss?
:( Thank you sincerely... Levente
- Function to effectively close all orders.
- How to code?
- Kurka Trader - GREEDY BS
void KillLeastProfitable() { int totalorders=OrdersTotal(); int num_array[99999]; int ctm,bbb; for (bbb=totalorders-1;bbb>=0;bbb--) { if(OrderSelect(bbb, SELECT_BY_POS, MODE_TRADES)) { ctm=OrderCloseTime(); // if (ctm==0 && OrderMagicNumber() == uniqueMagic) if (ctm==0) { num_array[bbb]=OrderProfit(); } } } bbb=num_array[ArrayMinimum(num_array, 0,0)]; if (OrderSelect(bbb, SELECT_BY_POS, MODE_TRADES)) { if ( OrderType() == OP_BUY ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); if ( OrderType() == OP_SELL ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); if ( OrderType() > 1 ) OrderDelete( OrderTicket() ); }There, now it is readable, at least.
I might approach it this way, to get the one with the lowest profit:
double minProfit = 100000000; int minTicket = 0; for(int i = OrdersTotal()-1; i >= 0; i--){ OrderSelect(i, SEL_BY_POS, MODE_TRADES); if(OrderProfit() < minProfit && OrderType() < 2){ minProfit = OrderProfit(); minTicket = OrderTicket(); } } if(minTicket > 0){ OrderSelect(minTicket, SEL_BY_TICKET, MODE_TRADES); if ( OrderType() == OP_BUY ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); if ( OrderType() == OP_SELL ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); }
Many Hails, this is wonderful, now it works!!! Thank you so much! :))))) Btw just
for future reference, I realize it's a stupid and redundant question but how do
you make the code appear so well formatted in the post? :) Many thanks again, Levente
Select some text in your reply and click the little button in the toolbar as you are editting marked "MQL"
it will put a box like this around the selected text
Thank_You_All();
Ray:
Thank you all.
Select some text in your reply and click the little button in the toolbar as you are editting marked "MQL"
Please edit
your (original) post and use the CODE button
(Alt-S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor
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