Ordersellect the highest and the lowest position

 

Hello my name's Redpich , I just start to create my EA...... And I need some help.........

I want to select the highest and the lowest position and assign them to parameter High_price and Low_price.


Example..... My order that opened are

EUR/USD

1.3050   >> I want to assign this value to High_price

1.3040

1.3030

1.3020

1.3010  >> I want to assign this value to Low_price


Thank you very much for any help.

 

Lets see what you've attempted so far.  Insert Source Instructions.

 
//+------------------------------------------------------------------+
//|                                           EA_redpich_Bullish.mq4 |
//|                      Copyright ฉ 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright ฉ 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
extern double LOT = 0.1;
extern int TP  = 100;
extern int SL  = 100;
double High_price = 0;
extern int Range =100;
datetime closetime;
extern int magic = 200;


int init()
  {
   closetime=TimeCurrent();
//----
   High_price=Ask;
   if(check_price(High_price+Range*Point ))
      {

         OrderSend(Symbol(),OP_BUY,LOT,Ask,3,Ask-SL*Point,Ask+TP*Point," ",magic,0,Green);
         OrderSelect(0,SELECT_BY_POS);
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

   if(Ask-High_price>Range*Point)
      {

         High_price=High_price+Range*Point;
         if(check_price(High_price+Range*Point ))
         {
            OrderSend(Symbol(),OP_BUY,LOT,Ask,3,Ask-SL*Point,Ask+TP*Point," ",magic,0,Green);
         }

      }

//----
   return(0);
  }
//+------------------------------------------------------------------+
bool check_price(double price)
{
  int OrderCount=OrdersTotal();
  int i,type;
  for (i=0;i<=OrderCount;i++){
  OrderSelect(i,SELECT_BY_POS);
  if( OrderType( ) ==OP_BUY )
  if(DoubleToStr( OrderOpenPrice( ),5) == DoubleToStr(price,5))
  {
   return (false);
  }
  }
  return(true);
}

My EA is a very simple concept......

For example EA will buy every 10 pip when ask is above last order and do the same thing every 10 pip.  every order will close by stoploss only . 

buy eu 1.3050   Last_price

buy eu 1.3040   

buy eu 1.3030

Affter that the price is going down to 1.3040 untill the order 1.3050 close by stoploss

closeed 1.3050   Last_price (Still be this value)

buy eu  1.3040   (I want to change Last_price to this value = 1.3040)

buy eu  1.3030

My problem is... When the price is going down untill last order reach to stoploss, Last_price still be last value. So, I want to assign a value of the highest order to Last_Price parameter.

 
Its better to search for the orders on the brokers server. Don't use variables like high_price [they lose their values on restart]. I use allot of functions and recommend it. Create a function which returns the high_price. Also create a function which returns last_price.
 

ubzen ... Thank you very much for your recommend. and could you please give me some example of HighPriceCheck(){   } function . because I don't know how to assign of the highest price to High_price parameter. thank you advance. I hope to hearing from you soon.

double High_check(double price)
{

  int OrderCount=OrdersTotal();
  int i,type;

  for (i=0;i<=OrderCount;i++)
 {
  OrderSelect(i,SELECT_BY_POS);
  
 }

}
 
redpich:

ubzen ... Thank you very much for your recommend. and could you please give me some example of HighPriceCheck(){   } function . because I don't know how to assign of the highest price to High_price parameter. thank you advance. I hope to hearing from you soon.

This one is very basic, and you'll want to practice and learn by making your own mistakes so that you know what every line does. I'll recommend reading the mql-book if making function above your level. To get you started, you'll want to loop through all open-orders and check the order open prices. If its greater-than the last-value checked then highest=new-value. Return highest_price at the end of the function.
 

I wish to echo what ubzen said.  The very first step to learning how to use MQL is to read the Book and study the Documentation.  Use those two excellant resources (along with the discussions here in the forum) to educate yourself about syntax and operations.  But don't simply expect people in this forum to just give you all of the answers.  There are many people here who reward honest learning with honest teaching, so if you make the honest effort to learn to code, this forum will help you with answers and guidance.

With that in mind, here is some code to get you started:

Compiled but untested:

double highest_buy_open_price = 0, lowest_sell_open_price = 0;

for (int i = OrdersTotal() - 1; i >= 0; i--)
   if (OrderSelect(i, SELECT_BY_POS))
      if (OrderSymbol() == Symbol())                                    // should also discriminate by OrderMagicNumber()
         if (OrderType() <= 1 && OrderCloseTime() == 0)                 // for open OP_BUY and OP_SELL orders only
            switch(OrderType()) {
               case OP_BUY:
                  if (OrderOpenPrice() > highest_buy_open_price)        // be careful when comparing doubles (see Can price != price ?)
                     highest_buy_open_price = OrderOpenPrice();         // save highest price
                  break;
               case OP_SELL:
                  if (OrderOpenPrice() < lowest_sell_open_price)        // be careful when comparing doubles (see above)
                     lowest_sell_open_price = OrderOpenPrice();         // save lowest price
               default:
                  break;
            }
Print ("The highest price is ", DoubleToStr(highest_buy_open_price, Digits), " and the lowest price is ", DoubleToStr(lowest_sell_open_price, Digits));
 
Thirteen:

Compiled but untested: 

About the code you gave

1)   make use of an OrderMagicNumber to get the trades of your EA

2)   you have to finish the loop the moment all trades are checked .....    check out what break is doing

3)    if you set   value lowest_sell_open_price to 0   before you start the loop you never can find a lowest_sell_open_price all prices are higher then 0

 
deVries:

About the code you gave

1)   make use of an OrderMagicNumber to get the trades of your EA

2)   you have to finish the loop the moment all trades are checked .....    check out what break is doing

3)    if you set   value lowest_sell_open_price to 0   before you start the loop you never can find a lowest_sell_open_price all prices are higher then 0

  1. Yes, hence my comment in the code stating "should also discriminate by OrderMagicNumber()".  It was my attempt to give the OP some code that he could start with, learn from, and modify to achieve the desired solution.
  2. The loop indeed finishes after checking all trades.  The break operator breaks out of the switch operator, not the for loop.  See Break ("A break operator terminates the execution of the nearest nested outward switch, while, or for operator.").
  3. Yup, you got me that one. :)  You are right...probably ought to set it to 9999 or something like that.

 

Thirteen: 

The loop indeed finishes after checking all trades.  The break operator breaks out of the switch operator, not the for loop.  See Break ("A break operator terminates the execution of the nearest nested outward switch, while, or for operator.").


About 2   You were correct
 
Thank you very much for every suggest, I will try to learn from any source that I can. And I really sorry for my bad question. Thank you very much for all.
Reason: