Download MetaTrader 5

OrderSelect function question

To add comments, please log in or register
boopa26
92
boopa26  

boopa26 can write order send code but cannot write order select function to close orders. wants to write order select function to close orders without for cycles or while cycles. boopa26 wants to write order select function to close orders with if conditions but boopa26 does not understand to assemble order select function to close orders


example buy order send

if(OrdersTotal()==0)OrderSend(Symbol(),OP_BUY,.1,Ask,3,0,0,NULL,9,0,clrNONE);


example order select. boopa26 does not understand

OrderSelect(int index,int select,int pool)


what is int index?

what is int select?

what is int pool?

how does order select do?

Keith Watford
Moderator
10359
Keith Watford  

Did you read the documentation?

Parameters

ticket

[in]  Order index or order ticket depending on the second parameter.

select

[in]  Selecting flags. It can be any of the following values:

SELECT_BY_POS - index in the order pool,
SELECT_BY_TICKET - index is order ticket.

pool=MODE_TRADES

[in]  Optional order pool index. Used when the selected parameter is SELECT_BY_POS. It can be any of the following values:

MODE_TRADES (default)- order selected from trading pool(opened and pending orders),
MODE_HISTORY - order selected from history pool (closed and canceled order).


boopa26
92
boopa26  
boopa26
92
boopa26  
gumrai could have included documentation link. just kiddiing. thank you gumrai
boopa26
92
boopa26  
bool  OrderSelect(
   int     index,            // index or order ticket
   int     select,           // flag
   int     pool=MODE_TRADES  // mode
   );

boopa26 wants to close open orders so boopa chooses MODE_TRADES as third parameter


does boopa26 do SELECT_BY_POS or SELECT_BY_TICKET ? boopa26 does not know

SELECT_BY_POS //index inside order pool,
SELECT_BY_TICKET //index is order ticket.
boopa26
92
boopa26  
boopa26
92
boopa26  

boopa26 chooses MODE_TRADES as third parameter

boopa26 chooses SELECT_BY_POSITION as second parameter

boopa26 chooses order index as first parameter


example

static int order_index;

if(OrderSelect(order_index,SELECT_BY_POSITION,MODE_TRADES)==1
boopa26
92
boopa26  

https://docs.mql4.com/trading/orderclose

how does boopa26 cycle without for cycles or while cycles but with if conditions to close open orders with order select function?

documentation order close requires order ticket to be found but boopa26 does order select to find order position not ticket

bool  OrderClose(
   int        ticket,      // ticket
   double     lots,        // volume
   double     price,       // close price
   int        slippage,    // slippage
   color      arrow_color  // color
   );

1. select order

2. close order

does not know how

boopa26
92
boopa26  
//+------------------------------------------------------------------+
//|                                               Moving Average.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Moving Average sample expert advisor"

#define MAGICMA  20131111
//--- Inputs
input double Lots          =0.1;
input double MaximumRisk   =0.02;
input double DecreaseFactor=3;
input int    MovingPeriod  =12;
input int    MovingShift   =6;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//--- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//--- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//--- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
           {
            Print("Error in history!");
            break;
           }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
            continue;
         //---
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1)
         lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//--- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
   int    res;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//--- sell conditions
   if(Open[1]>ma && Close[1]<ma)
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//--- buy conditions
   if(Open[1]<ma && Close[1]>ma)
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
//---
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //--- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Open[1]>ma && Close[1]<ma)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[1]<ma && Close[1]>ma)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
     }
//---
  }
//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false)
      return;
//--- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//---
  }
//+------------------------------------------------------------------+
moving average expert advisor uses for cycle to close open orders
Fernando Carreiro
2202
Fernando Carreiro  

I think "boopa26" needs to do the following:

  1. First, "boopa26" needs to first learn the basics of coding (like in C).
  2. Then "boopa26" needs to read and follow the Book/Tutorial (even though it is old and out of date).
  3. At the same time, "boopa26" needs to the read the entire documentation with special attention to the MQL4 Updates that are not mentioned in the Book/Tutorial.
  4. Then "boopa26" needs to study several examples of code for Indicators, Expert Advisors (EAs) and Scripts in the user Code Base.
  5. Then "boopa26" can start making his own, but very simple Indicators and slowly build on them until fully able to understand and make more complex Indicators.
  6. Then "boopa26" can start making his own, but very simple EAs and slowly build on them until fully able to understand and make more complex EAs.

"boopa26" must also learn a very important lesson, that "boopa26" should not skip all the necessary steps, because otherwise "boopa26" will just be wasting everyone's time, and instead of a "beautiful cake", all "boopa26" will have is "burnt charcoal".

boopa26
92
boopa26  
FMIC:

I think "boopa26" needs to do the following:

  1. First, "boopa26" needs to first learn the basics of coding (like in C).
  2. Then "boopa26" needs to read and follow the Book/Tutorial (even though it is old and out of date).
  3. At the same time, "boopa26" needs to the read the entire documentation with special attention to the MQL4 Updates that are not mentioned in the Book/Tutorial.
  4. Then "boopa26" needs to study several examples of code for Indicators, Expert Advisors (EAs) and Scripts in the user Code Base.
  5. Then "boopa26" can start making his own, but very simple Indicators and slowly build on them until fully able to understand and make more complex Indicators.
  6. Then "boopa26" can start making his own, but very simple EAs and slowly build on them until fully able to understand and make more complex EAs.

"boopa26" must also learn a very important lesson, that "boopa26" should not skip all the necessary steps, because otherwise "boopa26" will just be wasting everyone's time, and instead of a "beautiful cake", all he will have is "burnt charcoal".

1. does not need c knowledge. mql4 is different functions

2. read book already

3. read documentation already

4. written code already

5. ?

6. ?

boopa26 does not waste peoples time

FMIC teacher that teaches boopa26 to bake charcoal cakes

FMIC charcoal cake class

1234
To add comments, please log in or register