Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 91

 
Vitaly Muzichenko:
You have to sort by price, because you can't search by size, the grid may be different, and you will find either the biggest or the smallest, but not the first and not the last

I mean the function that Marina posted.

The last order is determined by time.

 
Alekseu Fedotov:

I mean the function that Marina posted.

The last order is determined by time.

They were all placed at the same time, so with 14 orders there might be a difference of 1 second, and you can't sort it by time, only by price.
 
Marina Korotkih:

Because I summon it this way

GetOrderOpenPrice(Symbol(),OP_SELLSTOP,_Magic)
Got it.
 

Dear programmers! Help me solve this question. The Expert Advisor enters a trade on M15, but takes conditions from H1 as well. When conditions on H1 are fulfilled on M15 there may be several entry points. Actually the problem is that I want to use only the first entry point, until the conditions on H1 are reversed.

k=0;
for (i=OrdersTotal()-1;i>=0;i--)
{
RefreshRates();
if(OrderSelect (i, SELECT_BY_POS, MODE_TRADES) == true) a = 0;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) k++;
}


if( stM1 <= 20 && stM0 > 20 && stH0 > 70 && k==0 && opp != Time[0]) // purchase

 
customer03:

Dear programmers! Help me solve this question. The Expert Advisor enters a trade on M15, but takes conditions from H1 as well. When conditions on H1 are fulfilled on M15 there may be several entry points. Actually the problem is that I want to limit the first entry point, until the conditions on H1 are not changed to the opposite.

k=0;
for (i=OrdersTotal()-1;i>=0;i--)
{
RefreshRates();
if(OrderSelect (i, SELECT_BY_POS, MODE_TRADES) == true) a = 0;
if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) k++;
}


if( stM1 <= 20 && stM0 > 20 && stH0 > 70 && k==0 && opp != Time[0]) // purchase

But what about without the code? Especially, the code is not clear.

What exactly do you want? Not in the abstract, but exactly what you want to get.

 
customer03:

Dear programmers! Help me solve this question. The Expert Advisor enters a trade on M15, but takes conditions from H1 as well. When conditions on H1 are fulfilled on M15 there may be several entry points. Actually the problem is that I want to limit myself to the first entry point, while on H1 the conditions do not change to the opposite.

k=0;
for (i=OrdersTotal()-1;i>=0;i--)
{
RefreshRates();
if(OrderSelect (i, SELECT_BY_POS, MODE_TRADES) == true) a = 0;
if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) k++;
}


if( stM1 <= 20 && stM0 > 20 && stH0 > 70 && k==0 && opp != Time[0]) // purchase

An approximate algorithm:

bool flag = false;
bool flagH1 = true;
bool flagM15 = true;

if(!flag && flagH1 && flagM15)
{
  flag = true;
}

if(!flagH1 && flag)
flag = false;
 
Artyom Trishkin:

And without the code? All the more reason not to understand the code.

What exactly do you want? Not in the abstract, but exactly what you want to get.

Artyom hello! The goal is to limit the entry on M15 to one (first signal) as long as there is a signal on H1.
#property strict
extern double    Lot= 0.1;

extern int    stoploss=20;
extern int takeprofit=50;
// если stoploss и / или takeprofit = 0, то отключены.
//if stoploss and/or takeprofit = 0, are disconnected.



extern int  Magic = 99999999;
// уникальный номер, кот. советник метит свои ордера и впоследствии работает только с ними. При постановке на разные графики или т/ф, межики должны быть разными
extern int Slipp = 1;        
// величина максимально возможного проскальзывания.
//  slippage
extern int sec = 2000;
// количество миллисекунд сна советника между повторами отправки торгового поручения в случае неудачи
// number of milliseconds sleep of the EA between repetitions of sending a trade assignment in case of failure


//============================= Stochastic
input int InpKPeriod=13; // K Period
input int InpDPeriod=3; // D Period
input int InpSlowing=3; // Slowing

//============================= Stochastic
input int InpKPeriod1=13; // K Period
input int InpDPeriod1=3; // D Period
input int InpSlowing1=3; // Slowing





int A, a, b, i, ii, o;


    double SL, TP, op, stH0, stH1, stM0, stM1;
    bool c;
    datetime OldTime, Time0, opp;

int init()
  {    
  
   OldTime = iTime(NULL,Period(),0);
  
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  

                      
//====================================================================

stH0 = iStochastic(NULL,60,InpKPeriod,InpDPeriod,InpSlowing,MODE_EMA,1,MODE_MAIN,0);
stH1 = iStochastic(NULL,60,InpKPeriod,InpDPeriod,InpSlowing,MODE_EMA,1,MODE_MAIN,1);

stM0 = iStochastic(NULL,15,InpKPeriod1,InpDPeriod1,InpSlowing1,MODE_EMA,1,MODE_MAIN,0);
stM1 = iStochastic(NULL,15,InpKPeriod1,InpDPeriod1,InpSlowing1,MODE_EMA,1,MODE_MAIN,1);
  
//====================================================================  

          
b=0;
    for (i=OrdersTotal()-1;i>=0;i--)  
       {
        RefreshRates();
        if(OrderSelect (i, SELECT_BY_POS, MODE_TRADES) == true) a = 0;
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) b++;
       }

      
if(  stM1 <= 20 && stM0 > 20  && stH0 > 70  && b==0   && opp != Time[0]) // покупка
      {
       opp = Time[0];

    RefreshRates();
    op = Ask;
   A = OrderSend (Symbol(), OP_BUY, Lot, Ask, Slipp, 0, 0, NULL, Magic, 0, Blue);
    o = GetLastError();
         while (A < 0 && o != 4109 && o != 132 && o != 133)
         {
          Print("Error =", o);
         Sleep(sec);
         RefreshRates();
         op = Ask;
         A = OrderSend (Symbol(), OP_BUY, Lot, Ask, Slipp, 0, 0, NULL, Magic, 0, Blue);
         o = GetLastError();
        }      
        
SL = op-stoploss*Point;
if (stoploss == 0) SL = 0;
TP = op+takeprofit*Point;
if (takeprofit == 0 ) TP = 0;        
        
    if (SL != 0 || TP != 0)
      {
    RefreshRates();
    if(OrderSelect (A, SELECT_BY_TICKET) == true) a = 0;
    c = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Green);
    o = GetLastError();
      while (c == false && o != 4109 && o != 132 && o != 133)
                  {
                   Print("Error =", o);
                  Sleep(sec);
                  RefreshRates();
                  if(OrderSelect (A, SELECT_BY_TICKET) == true) a = 0;
                  c = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Green);
                  o = GetLastError();
                  }  }          
     }

//---------------------------------------------------------------------------------------------------------+                          
b=0;
    for (i=OrdersTotal()-1;i>=0;i--)  
       {
        RefreshRates();
        if(OrderSelect (i, SELECT_BY_POS, MODE_TRADES) == true) a = 0;
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) b++;
       }


if( stM1 >= 80 && stM0 < 80  &&  stH0 < 30   && b==0   && opp != Time[0]) // продажа
     {
     opp = Time[0];

      RefreshRates();
      op = Bid;
         A = OrderSend (Symbol(), OP_SELL, Lot, Bid, Slipp, 0, 0, NULL, Magic, 0, Red);
         o = GetLastError();
            while (A < 0 && o != 4109 && o != 132 && o != 133)
            {
             Print("Error =", o);
            Sleep(sec);
            RefreshRates();
            op = Bid;
            A = OrderSend (Symbol(), OP_SELL, Lot, Bid, Slipp, 0, 0, NULL, Magic, 0, Red);
            o = GetLastError();
            }    

SL = op+stoploss*Point;
if (stoploss == 0) SL = 0;
TP = op-takeprofit*Point;
if (takeprofit == 0 ) TP = 0;
        
        if (SL != 0 || TP != 0)
      {
        RefreshRates();
         if(OrderSelect (A, SELECT_BY_TICKET) == true) a = 0;
         c = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Green);
         o = GetLastError();
         while (c == false && o != 4109 && o != 132 && o != 133)
                  {
                   Print("Error =", o);
                  Sleep(sec);
                  RefreshRates();
                  if(OrderSelect (A, SELECT_BY_TICKET) == true) a = 0;
                  c = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Green);                  
                  o = GetLastError();
                  } }
      }
      
    

   return(0);
   }      

  
//+------------------------------------------------------------------+
 
customer03:
Artyom hello! The goal is to limit the entry on M15 to one (first signal) as long as there is a signal on H1.

So check the number of open positions on M15 null candle. If you already have a position and it's open on an M15 null candle, then don't open any more.
 

Hello, dear forum members. Is there any way to set the value of the trailing stop built into Metatrader4 from within the EA? I am interested in this and not in adding to the Expert Advisor the code for the execution of a trailing stop which is independent of the built-in trailing stop.

Thanks in advance for the answers.

 
Dr_G:

Hello, dear forum members. Is there any way to set the value of the trailing stop built into Metatrader4 from within the EA? I am interested in this and not in adding to the EA a code for the execution of a trailing stop which is independent of the built-in trailing stop.

Thanks in advance for the answers.

No.
Reason: