A function to compare open prices of open positions and return the largest or smallest.

 

Hello! I'm writing a function that will compare the open prices of all open positions.

In case it is a buy,  it will determine and return the open price with highest value.

In case it is a sell,  it will determine and return the open price with the lowest value.

The purpose is to make sure all previous positions in the selected symbol are in profits before opening/adding another position when the conditions are met.

I tried this code but I get an error when I attach the expert(array out of range on line 846 and 850).

I tried to initialize the array with empty value first but no avail. 

I'm new in mql5 arrays. I will be happy if anyone could point out what I'm not doing right. Or point me in the

double checkAllProfit(string otypes)
{
   double opPrices[];
   //ArrayInitialize(opPrices,EMPTY_VALUE);//Tried this but same results
   int sizes=PositionsTotal();
   double oPrice=0;
   double level=0;
   for(int i=0; i<sizes; i++) {
      if(_Symbol==PositionGetSymbol(i)) {
      if(PositionGetInteger(POSITION_MAGIC)==EA_Magic) {
            oPrice=PositionGetDouble(POSITION_PRICE_OPEN);
            ArrayResize(opPrices,ArraySize(opPrices)+1);
            opPrices[ArraySize(opPrices)-1]=oPrice;
         }
      }
   }
   if (otypes=="Buy") {
      int max=ArrayMaximum(opPrices);//line 845
      level = opPrices[max];//line 846
   }
   if(otypes=="Sell") {
      int min=ArrayMinimum(opPrices);//line 849
      level = opPrices[min];//line 850
   }
   return(level);
}


right direction.

 
     int max=ArrayMaximum(opPrices);//line 845
      level = opPrices[max];//line 846
What happens when you have no positions?
 
William Roeder #:
What happens when you have no positions?

Thanks for the reply.

I only call the function if I have open position(s).

bool inProfits=false;
if(Sell_opened){  //check for open sell position
     double openLevel=checkAllProfit("Sell");
     if(openLevel>bprice) {  //bprice is the current bid price
        inProfits=true;
      }
}
 
Kevin Onsongo:

Hello! I'm writing a function that will compare the open prices of all open positions.

In case it is a buy,  it will determine and return the open price with highest value.

In case it is a sell,  it will determine and return the open price with the lowest value.

The purpose is to make sure all previous positions in the selected symbol are in profits before opening/adding another position when the conditions are met.

I tried this code but I get an error when I attach the expert(array out of range on line 846 and 850).

I tried to initialize the array with empty value first but no avail. 

I'm new in mql5 arrays. I will be happy if anyone could point out what I'm not doing right. Or point me in the


right direction.

Are positions opened? It should work. Most likely no position is opened, or maybe the symbol (_symbol) of the current chart, is not same with that of your opened position. Troubleshoot

opPrices[] array

to ensure the opens were added, then write a conditional statement to ensure the array have values before line 845

 
Thank-god Avwerosuoghene Odukudu #:

Are positions opened? It should work. Most likely no position is opened, or maybe the symbol (_symbol) of the current chart, is not same with that of your opened position. Troubleshoot

to ensure the opens were added, then write a conditional statement to ensure the array have values before line 845

Thanks a lot for the directions. I found out my issue.

I had performed the Sell_Opened in the OnInit() block instead of in the OnTick() block. So the variable Sell_Opened was set to true once and it remained so as long as the expert was still attached. That's why it was always calling the function even when I had no open positions. I also modified my function further to return a value which would make InProfits variable false if there are no open positions(opPrices is empty) just to make sure I don't encounter similar problems in the future. Thanks a lot guys for your help.