Want to get max and min price from a loop

 

Hello dears!

I want to find/get max and min price from a certain loop.

Example: there are 3 orders are running in EURUSD. now I want to get max price and min price among the running orders.

    double max_running_price = 0;
    double min_running_price = 0;
    if(OrdersTotal() > 0)
    {
    for(int i = OrdersTotal()-1; i >= 0 && IsStopped() != true; i--)
    {
    bool select =  OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if(select = true && OrderSymbol() == _Symbol && (OrderType() == OP_BUY || OrderType() == OP_SELL) && OrderMagicNumber() == my_magic)
    {
    max_running_price = // want to get max price among running orders
    min_running_price = // want to get min price among running orders

    }
    }
    }

Can anyone help me?

Thanx in advance.

 

Hi Tanvir Ahmed.

Hope you are doing great.

Why are you having 2 variables to compare/get the max and min prices. Instead make array for it.

#define n 100 //n could be any number from 0 - any number

double running_prices[n];

    if(OrdersTotal() > 0)
    {
        for(int i = OrdersTotal()-1; i >= 0 && IsStopped() != true; i--)
        {
                if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
                        if(OrderSymbol() == Symbol() && (OrderType() == OP_BUY || OrderType() == OP_SELL) && OrderMagicNumber() == my_magic)
                        {
                                running_price[i] = OrderGetDouble(ORDER_PRICE_CURRENT); // want to get max price among running orders
                        }
        }
    }

double max_running_price = ArrayMaximum(running_price);
double min_running_price = ArrayMinimum(running_price);
It will return the index of max and minimum price so can you check that or use that afterwards.
 
Tamur Taj:

Hi Tanvir Ahmed.

Hope you are doing great.

Why are you having 2 variables to compare/get the max and min prices. Instead make array for it.

It will return the index of max and minimum price so can you check that or use that afterwards.

Hi Tamur Taj, thank you for your nice example.

Applying your example, I am getting max price nicely, but min price result is not correct/accurate. Please see my coding.

    double mxn_price[1000];
    int max = -1;
    int min = -1;

    if(OrdersTotal() > 0)
    {
    for(int i = OrdersTotal()-1; i >= 0 && IsStopped() != true; i--)
    {
    bool select = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if(select == true && OrderSymbol() == _Symbol && OrderType() == OP_BUY)
    {
    mxn_price[i] = NormalizeDouble(OrderOpenPrice(),_Digits);
    max = ArrayMaximum(mxn_price,WHOLE_ARRAY,0);
    min = ArrayMinimum(mxn_price,WHOLE_ARRAY,0);
    }
    }
    }

    double max_running_price = 0; if(OrderSelect(max,SELECT_BY_POS,MODE_TRADES) == true) {max_running_price = NormalizeDouble(OrderOpenPrice(),_Digits);}
    double min_running_price = 0; if(OrderSelect(min,SELECT_BY_POS,MODE_TRADES) == true) {min_running_price = NormalizeDouble(OrderOpenPrice(),_Digits);}

    Alert(sym," :: Max: ",max_running_price," (",max,"), Min: ",min_running_price," (",min,")");

Can I get another help to fix this (min price) issue?

 
Finally I found my solution by this way---
    int size = OrdersTotal();
    double mxn_price[];
    ArrayResize(mxn_price,size);

//--array filling with value:
    if(size > 0)
    {
    for(int i = 0; i <= size-1 && IsStopped() != true; i++)
    {
    bool select = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if(select == true && OrderSymbol() == _Symbol && OrderType() == OP_BUY)
    {
    mxn_price[i] = NormalizeDouble(OrderOpenPrice(),_Digits);
    }
    }
    }
//--finding max price:
    int max = ArrayMaximum(mxn_price,WHOLE_ARRAY,0);
    double max_running_price = 0; if(max >= 0 && OrderSelect(max,SELECT_BY_POS,MODE_TRADES) == true) {max_running_price = NormalizeDouble(OrderOpenPrice(),_Digits);}
//--zero value are resetting with max price:
    for(int i = 0; i <= size-1 && IsStopped() != true; i++)
    {
    if(mxn_price[i] <= 0) {mxn_price[i] = max_running_price;} else{mxn_price[i] = mxn_price[i];}
    }
//--finding min price:
    int min = ArrayMinimum(mxn_price,WHOLE_ARRAY,0);
    double min_running_price = 0; if(min >= 0 && OrderSelect(min,SELECT_BY_POS,MODE_TRADES) == true) {min_running_price = NormalizeDouble(OrderOpenPrice(),_Digits);}
//--result show:
    Alert(_Symbol," :: Max: ",max_running_price," (",max,"), Min: ",min_running_price," (",min,")");

If there is any shorter and easy way, please tell us. Thanx in advance. 

 


Tanvir Ahmed:

Hi Tamur Taj, thank you for your nice example.

Applying your example, I am getting max price nicely, but min price result is not correct/accurate. Please see my coding.

Can I get another help to fix this (min price) issue?

Hi Tanvir. 

Just place the min and max outside the loop it will be good to go.

 
Tanvir Ahmed:
Finally I found my solution by this way---

If there is any shorter and easy way, please tell us. Thanx in advance. 

Use this method of array initialization you have used in this code.

 
Tamur Taj:

Use this method of array initialization you have used in this code.

Got the point. Thank you very much for your kind help.

Reason: