[ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5. - page 190

 

Sergey Dubakin kindly offered the CloseBy function, but unfortunately it does not meet the important condition for me to close a position with a maximum loss with the opposite position with a maximum profit:


avatar
19
Diubakin 25.02.2013 23:26
borilunad:
Uv. pro! Where can I find the СloseBy() function that works in manual mode? I want to insert it in my owl to be able not manually, but automatically according to the conditions to close the position with a maximum minus the opposite with a maximum plus. I could not find it in codebase. I have not found it in codebase. Thank you!

Try this variant:

bool LockOFF(int posit, int oppos, color arrow_color) {
double Result, PrevLoss, PrevProfit;
    int pos, orders_total, order_type, MaxProfitTicket, MaxLossTicket;
   bool Ans;

 MaxProfitTicket=-1; MaxLossTicket=-1;

 orders_total=OrdersTotal();
 for(pos=orders_total-1; pos>=0; pos--) {
  if(!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=EA_Magic) continue; // не наш ордер
  if(OrderType()>1) continue;
  Result=OrderProfit()+OrderSwap()+OrderCommission();
  if(Result<0.0 && (PrevLoss==0.0 || Result<PrevLoss)) {
   PrevLoss=Result; MaxLossTicket=OrderTicket(); order_type=OrderType(); } } // end of for

 if(MaxLossTicket==-1) return(false); // нет убыточной позиции
 if(order_type==OP_BUY) order_type=OP_SELL; else order_type=OP_BUY; 

 orders_total=OrdersTotal();
 for(pos=orders_total-1; pos>=0; pos--) {
  if(!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=EA_Magic) continue; // не наш ордер
  if(order_type!=OrderType()) continue;
  Result=OrderProfit()+OrderSwap()+OrderCommission();
  if(Result>0.0 && (PrevProfit==0.0 || Result>PrevProfit)) {
   PrevProfit=Result; MaxProfitTicket=OrderTicket(); } } // end of for

 if(MaxProfitTicket==-1) return(false); // нет противоположной прибыльной позиции

 Ans=OrderCloseBy(MaxLossTicket, MaxProfitTicket);
 if(!Ans) { Print("Ошибка при встречном закрытие"); return(false); }
 
 return(true); }

I've made functions defining maxloss GetMaxLoss() and maxprofit GetMaxProfit(), but I cannot make LockOFF() select a pair I've specified instead of any of them. Please show me in what place to put my maxes, and maybe I need more functions to determine the ticket or index of those positions, then it's not a problem for me, but where to put it all in this LockOFF()? Thanks!

 
borilunad:

Sergey Dubakin kindly offered the CloseBy function, but unfortunately it does not meet an important condition for me, i.e. closing a position with a maximal loss by an opposite position with a maximal profit.

Hello, this function closes a position with a maximal loss with the opposite position with a maximal profit. Here is a test EA for the tester:

extern int EA_Magic=135; // внешняя переменная

int TimeNow, TimePrev, PrevType; // глобальные переменные

int start()
 {
  double Price,SL,TP;
     int Ticket;

  TimeNow=iTime(NULL,240,0);
  if(TimePrev==TimeNow) return(0);

  if(PrevType!=1) {
   Price=NormalizeDouble(Ask,Digits);    
   SL=NormalizeDouble(Price-300*Point,Digits);    
   TP=NormalizeDouble(Price+300*Point,Digits);
   Ticket=OrderSend(Symbol(),OP_BUY,0.1,Price,3,SL,TP,"",EA_Magic);
   if(Ticket!=-1) { TimePrev=TimeNow; PrevType=1; } }

  else if(PrevType!=-1) {
   Price=NormalizeDouble(Bid,Digits);    
   SL=NormalizeDouble(Price+300*Point,Digits);    
   TP=NormalizeDouble(Price-300*Point,Digits);
   Ticket=OrderSend(Symbol(),OP_SELL,0.1,Price,3,SL,TP,"",EA_Magic);
   if(Ticket!=-1) { TimePrev=TimeNow; PrevType=-1; } }

  if(Hour()==0 && TimePrev==TimeNow) LockOFF(EA_Magic);

  return(0);
 }

bool LockOFF(int EA_Magic) {
 double Result, PrevLoss, PrevProfit;
    int pos, orders_total, order_type, MaxProfitTicket, MaxLossTicket;
   bool Ans;

 MaxProfitTicket=-1; MaxLossTicket=-1;

 orders_total=OrdersTotal();
 for(pos=orders_total-1; pos>=0; pos--) {
  if(!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=EA_Magic) continue; // не наш ордер
  if(OrderType()>1) continue;
  Result=OrderProfit()+OrderSwap()+OrderCommission();
  if(Result<0.0 && (PrevLoss==0.0 || Result<PrevLoss)) {
   PrevLoss=Result; MaxLossTicket=OrderTicket(); order_type=OrderType(); } } // end of for

 if(MaxLossTicket==-1) return(false); // нет убыточной позиции
 if(order_type==OP_BUY) order_type=OP_SELL; else order_type=OP_BUY; 

 orders_total=OrdersTotal();
 for(pos=orders_total-1; pos>=0; pos--) {
  if(!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=EA_Magic) continue; // не наш ордер
  if(order_type!=OrderType()) continue;
  Result=OrderProfit()+OrderSwap()+OrderCommission();
  if(Result>0.0 && (PrevProfit==0.0 || Result>PrevProfit)) {
   PrevProfit=Result; MaxProfitTicket=OrderTicket(); } } // end of for

 if(MaxProfitTicket==-1) return(false); // нет противоположной прибыльной позиции

 Ans=OrderCloseBy(MaxLossTicket, MaxProfitTicket);
 if(!Ans) { Print("Ошибка при встречном закрытие!"); return(false); }
 
 return(true); }

It opens Buy and Sell positions alternately when a new 4 hour bar appears and makes one opposite close when a new day opens.

 
Diubakin:

Hello, the function closes the position with the maximum loss with the opposite position with the maximum profit. Here is a test EA for the tester:

It opens Buy and Sell positions alternately when a new 4 hour bar appears and makes one opposite close when a new day opens.

Hello Sergey, I have a different algorithm! The thing is, this function closes what it has, and I need it to close the maximum, not any! Well, nothing, I'll finish it myself if no one helps me! Your version is only good for the tester, so added to the header according to the rules. Works fine without errors, but does not select the maximum! All the same, thank you for your help!
 
alsu:

That's it, I've got it now. If price on the current bar reaches Open[0] + 30, then we open. If the bar has ended, Open[0] has changed and the open level is shifted accordingly. There should be only one trade in the market, right?

Then it is like this:

There is no need to track the new bar, because when it is formed, the system will automatically add a new value to Open[0] and this means that the new level will be calculated correctly. Please note that the BUY deal is opened at the current Ask price, while it is closed (TP and SL) at Bid price. In addition, the price values should be normalized.


Thank you very much for your help.

But opens trades in the tester on every minute candle. The info is in this link http://clip2net.com/s/2T98Y

I want it to open one order on a five-minute candlestick, but not on every candlestick.

But only if the current five-minute candlestick is > or = 30 pips (from the open price to its maximum).

I.e., open one market order on the thirtieth point from the opening price of the zero=current five-minute candle.

And if the current five-minute period from the open price to its maximum is less than 30 points, we should not open a market order.

I.e. the orders will be a little, I have counted in Excel, since 1999 - a little more than 3000 on euro-dollar in both directions.

We switch to the next five-minute period and if it, i.e. the current five-minute period > or = 30 pips, then we open at the thirtieth pips, if less, we switch to the next five-minute period ... and so on to the end of the chart.

But the thing is, by a rough estimate, most of the five-minute plans that > or = 30 points far more often go past 15 points than are closed at 30 points stop.

You have a condition - if there are no open orders

But in my system, two five-minute periods with the parameters described above may be formed in a row and if the order from the previous Five-Minute period is not closed, then no order will be opened at the second Five-Minute period.

I need one order to be opened at every five-minute period with the parameters described above, regardless of whether there are orders in the trade or not.

 
if (OrdesTotal()==0 && Close[0]>=Price)  
 
alsu:

That's it, I've got it now. If price on the current bar reaches Open[0] + 30, then we open. If the bar has ended, Open[0] has changed and the open level is shifted accordingly. There should be only one trade in the market, right?

Then it is like this:

There is no need to track the new bar, because when it is formed, the system will automatically add a new value to Open[0] and this means that the new level will be calculated correctly. Please note that the BUY deal is opened at the current Ask price, while it is closed (TP and SL) at Bid price. In addition, the price values should be normalized.


And you also have a second condition - if the close price of the current five minutes >= open price of the order.

But in my idea the closing price of the current 5-minute period can be any price, as long as its High is > or = 30 pips.


Close[0]>=Price

 
borilunad:
Hello Sergei, I have a different algorithm! That's the thing, this function closes what it has and I want it to close the maximal ones, not any! Well, nothing, I'll finish it myself if no one helps me! Your version is only good for the tester, so added to the header according to the rules. Works fine without errors, but does not select the maximum! All the same, thank you for your help!

How do you want it? The one I posted determines the most unprofitable position in the deposit currency(in money), then determines the most profitable opposite position also in the deposit currency and makes a counter-close.

borilunad:

I've implemented functions determining the maxloss GetMaxLoss() and maxprofit GetMaxProfit() but I cannot make LockOFF() function select a pair specified by me and not any of them. Please show me in what place to put my maxes, and maybe I need more functions to determine the ticket or index of those positions, then it's not a problem for me, but where to put it all in this LockOFF()? Thanks!

The function has it all - defining max loss, max profit, the ticket is also defined.

 
Diubakin:

How do you want it? The one that you posted, determines the most unprofitable position in the deposit currency (in money), then determines the most profitable opposite position also in the deposit currency and makes a counter-close.

The function has it all - defines max loss, max profit, ticket is also defined.

You are wrong! Probably you have the only pair of opposite positions, while I may have more. That's why I put all data in comments and see that it closes not maxiLoss maxiProfit, but any of the first ones I come across. And my functions clearly show position indexes with maximal Loss and Profit. It only remains to add these checks to LockOFF()!
 
borilunad:
You are wrong! You probably have the only pair of opposite positions, while I may have more, because I have displayed all the data in the comments and see that it closes not maxiLoss maxiProfit, but any of the first ones that come up in the search. And my functions clearly show position indexes with maximal Loss and Profit. I only need to add these checks to LockOFF()!
Have you seen the test EA for this function in the tester? It closes exactly the max loss with max profit, no matter how many orders there are - two or twenty.
 
Vinin:

https://www.mql5.com/ru/code/7835 Only this is an indicator. You have to use objects in an EA.
It is the Expert Advisor I am interested in, objects - should I draw a candle myself?
 
Diubakin:
Have you looked at the test EA for the function in the tester? It closes exactly the max. loss with max. profit, no matter how many orders there are - two or twenty.
Of course, I did! True, I have not more than three. I looked everywhere, in the tester in results and in the journal, on the demo and on the chart too! When I do, I'll show you!
Reason: