A simple matrix/array operation

 

Hi,

Can someone help me with a simple matrix operation?

Here, I have built a matrix of all open orders. I have sorted them from the most loss-making to the most profitable.

Now, I take the most losing order and check if the profitable orders are able to compensate it with a small profit. It seems simple but it does not work for me.

Please give me a hint.

orders array:

for(int i=0; i<=OrdersTotal()-1; i++)
     {
      OrderSelect(i,SELECT_BY_POS);
      if(OrderSymbol()!=Symbol() || OrderType()>1 || OrderMagicNumber()!=magic)

        {
         continue;
        }
      count++;
      ArrayResize(mypool,count);
      mypool[count-1][0]=NormalizeDouble(OrderProfit()+OrderCommission()+OrderSwap(),2);  // to sort
      mypool[count-1][1]=OrderTicket();
      mypool[count-1][2]=NormalizeDouble(OrderLots(),2);
      mypool[count-1][3]=OrderType();
      mypool[count-1][4]=OrderOpenTime();

     }
   if(count>1 )
     {
      ArraySort(mypool,count,0,MODE_ASCEND);

Now, I want to check if element mypool[0,0] can be compensated by: mypool[count-1,0] or maybe SUM= mypool[count-1,0] + mypool[count-2,0]     or maybe bigger SUM (three elements)

I tried something like this:

for (int i=count-1;i==1 ; i--)

(
        SUM += mypool[i,0];

        if(SUM+mypool[0,0]>small_profit)  break;

        index= i;

);

but it doesn't work. What is wrong? I can see/print the 'count', I can see every element of this array but I can't see or print 'SUM'

Then I wanted to close the orders pointed by 'index' and of course the first one.

Like this:

 for( int a=count-1; a==index; a--)
  {  
   if (mypool[a,3]==0)   //type of order 
   
      OrderClose(mypool[a,1],mypool[a,2],Bid,3,White);
      else
      OrderClose(mypool[a,1],mypool[a,2],Ask,3,Green);

  };
         if(mypool[0,3]==0)
           OrderClose(ticketlow,orderlotlow,Bid,3,Green); 
         else/           {
            OrderClose(ticketlow,orderlotlow,Ask,3,Green);  
           }

I would be very grateful for any help. We have a weekend coming up so I'm glad to be able to spend time on the problem but without help I'm not going to move forward.

Regards

Kris
 

 
kankrz:

Hi,

Can someone help me with a simple matrix operation?

Here, I have built a matrix of all open orders. I have sorted them from the most loss-making to the most profitable.

Now, I take the most losing order and check if the profitable orders are able to compensate it with a small profit. It seems simple but it does not work for me.

Please give me a hint.

orders array:

Now, I want to check if element mypool[0,0] can be compensated by: mypool[count-1,0] or maybe SUM= mypool[count-1,0] + mypool[count-2,0]     or maybe bigger SUM (three elements)

I tried something like this:

but it doesn't work. What is wrong? I can see/print the 'count', I can see every element of this array but I can't see or print 'SUM'

Then I wanted to close the orders pointed by 'index' and of course the first one.

Like this:

I would be very grateful for any help. We have a weekend coming up so I'm glad to be able to spend time on the problem but without help I'm not going to move forward.

Regards

Kris
 

I haven't tried this piece of code. But I'll try once the system boots up.


//+------------------------------------------------------------------+
//|                                            KarZararKarsilaEA.mq4 |
//|                                 Copyright 2019, Haskaya Software |
//|                                   https://www.haskayayazilim.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Haskaya Software"
#property link      "https://www.haskayayazilim.net"
#property version   "1.00"
#property strict
input double SmartProfit=40;
input int magic=99243;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CloseCompareOrders();
  }
//+------------------------------------------------------------------+

void CloseCompareOrders()
{
int ticketOld=0,ticketNew=0;
double ProfitOld=0,ProfitNew=0;
   for( int pos = OrdersTotal() - 1; pos >= 0; pos-- )
   {
      if( OrderSelect( pos, SELECT_BY_POS ) )
      {
      if(OrderSymbol()==Symbol() && OrderType()<2  && OrderMagicNumber()==magic)
         {
          ticketOld   = OrderTicket();
          ProfitOld   = OrderProfit()+OrderCommission()+OrderSwap();
         }
  }
///////////////
for( int posnewx = OrdersTotal() - 1; posnewx >= 0; posnewx-- )
   {
      if( OrderSelect( posnewx, SELECT_BY_POS ) )
      {
      if(ticketOld!=OrderTicket() && OrderSymbol()==Symbol() && OrderType()<2  &&  OrderMagicNumber()==magic)
         {
            ticketNew   = OrderTicket();
            ProfitNew   = OrderProfit()+OrderCommission()+OrderSwap();
         if(ProfitOld+ProfitNew>SmartProfit) 
         {
         OrderCloseBySmartProfit(ticketOld,ticketNew);
         break;
         }
         }
  }
  
  
   }
   }
  
}

void OrderCloseBySmartProfit(int ticketOld,int ticketNew)
{
bool OrderCloseResult;
   for( int pos = OrdersTotal() - 1; pos >= 0; pos-- )
   {
      if( OrderSelect( pos, SELECT_BY_POS ) )
      {
       if(OrderType()<2 && (OrderTicket()==ticketOld || OrderTicket()==ticketNew))
         {
         OrderCloseResult=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(), 9999, clrNONE );
         }
      }
      }
      }
      
 

Hi Mehmet,

You present a different idea. Your code will find any two orders whose total profit is higher than the assumed one. Also interesting, but not as effective.

My idea is to compensate the worst order by the sum of the good ones. In this way I get rid of the most losing orders and make room for new ones.

Thank you for your  post.

Regards

 
If I get your idea right, I would solve it following:

Make 2 lists (arrays). One for the losers and one for the winners.

Order the lists based on your criteria.

Now have a main loop going through the list of loosers. Inside this loop have a while loop which goes through the winners and sums up the profits until the looser has been compensated.

Mark the winners as "used".
(You can use an independent index pointer for the winners array to keep track of what you already have consumed from the list)

Then exit the inner loop and let the outer loop take the next looser.... And so on.


You could also "carry over" any non consumed profits to the next round.

To save the results, you could store the independent winners pts within the array of losers (some additional variable or another array) to "assign" the winners to the losers.

Well, there you go. That's giving you the results you asked for.

 

Hi Dominik,

Tank you for join. Your idea is interesting but quite complicated.

Have a look:


all my orders are placed and sorted by profit in one array.

Now I take the biggest looser (-7) and I start to count the SUM of winners, starting from the greatest winner (5).

In this case I need two orders to compensate the looser (-7+5+3>0), so I am closing Orders 0, 5,6 - done.


Now I have something like this:


and now I can't compensate -5 (with small profit), so I am waiting for next orders to be open.


size of the array is dynamic but the bigest looser is always at position 0 - first one.

It should be easy to program but I stuck :)

Regards

Kris

 
kankrz:

Hi Dominik,

Tank you for join. Your idea is interesting but quite complicated.

Have a look:


all my orders are placed and sorted by profit in one array.

Now I take the biggest looser (-7) and I start to count the SUM of winners, starting from the greatest winner (5).

In this case I need two orders to compensate the looser (-7+5+3>0), so I am closing Orders 0, 5,6 - done.


Now I have something like this:


and now I can't compensate -5 (with small profit), so I am waiting for next orders to be open.


size of the array is dynamic but the bigest looser is always at position 0 - first one.

It should be easy to program but I stuck :)

Regards

Kris

The sample data does not match what you wrote. I made an EA similar to this in 2003. If I can find it, I will send it to you. I think the result will not change. it will only create psychological joy.

In market orders, as the profit increases, the loss decreases, while the loss increases, the profit decreases.

 
kankrz:

Hi Dominik,

Tank you for join. Your idea is interesting but quite complicated.

Have a look:


all my orders are placed and sorted by profit in one array.

Now I take the biggest looser (-7) and I start to count the SUM of winners, starting from the greatest winner (5).

In this case I need two orders to compensate the looser (-7+5+3>0), so I am closing Orders 0, 5,6 - done.


Now I have something like this:


and now I can't compensate -5 (with small profit), so I am waiting for next orders to be open.


size of the array is dynamic but the bigest looser is always at position 0 - first one.

It should be easy to program but I stuck :)

Regards

Kris

I understood correctly, the screen shape is attached.

as

 

Hi Mehmet,

I can see that you're interested in the problem:)

Actually, after closing Orders 0, 5,6 I let the EA to play and open new positions till next big looser (here -5) can be compensated.

This is what I got on 5M EURUSD 2021 - Jan-till now,

As an entry signal I use RSI period 6, levels 20/80

The only problem is that I wrote all this procedure 'manually'  and I can check only four winners.

Regards

Kris

Reason: