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

 
Vitaly Muzichenko:
Do you put the prices in all at once, or do you get them and then put them in the calculation?
Vitaly Muzichenko:
Do you set the prices at once, or get them and then paste them into calculation?
int k=period;
for(int i=1; i<=period; i++)
{
H1_Close[i]=Close[k];
k--;
}
I take a chunk of timesession equal to the period starting from the last closed bar and flip it over in the array, because 1 is the penultimate bar in timesession and calculation must start from i-th
i.e. my function should return SMMA value of the last closed bar.
 
Good afternoon, everyone.
The code below encodes a simple idea.
If the last closed order was on SEL
If the last closed order was on the NEL
Open a NEL order
if (OrderSelect (Ht-1,SELECT_BY_POS, MODE_HISTORY))                                            
if ( OrderType ()==OP_SELL)                                            
X = OrderProfit( );                                            

if (OrderSelect (Ht-1,SELECT_BY_POS, MODE_HISTORY))                                            
if ( OrderType ()==OP_BUY)                                              
if(X < 0)                                              

OrderSend(Symbol(),OP_SELL,0.1,Bid ,3,Ask+400*Point,Ask-200*Point,"17-10",123 );



QUESTION
What language construct can be used to add two more conditions to this code (circled in red)

If the last closed order is NEL
If the last CEL order closed
If the last closed order has closed on the NEL
If the last closed order has been closed on aSL

Thank you for your help.


I would be very grateful if you could write the code and not just explain what has to be done.
 
Hello. I am writing a script to delete all pending orders. However, it only deletes one order. I always have two pending orders or one. In either case it only deletes one Buy Stop. To delete Sell Stop you have to run the script again, provided there are no other Buy Stop orders. Please advise where the error is.

void DeleteOrders()
{
int Total=OrdersTotal();
for(int i=0;i<Total;i++)
if(OrderSelect(i,SELECT_BY_POS))
{
int type=OrderType(), ticket=OrderTicket();
bool c;
switch(type)
{
case 4:
c=OrderDelete(ticket);
if(!c)
Print(GetLastError());
break;
case 5:
c=OrderDelete(ticket);
if(!c)
Print(GetLastError());
break;
}
}
}
 
0B53RV3R:
Hello. I am writing a script to delete all pending orders. However, it only deletes one order. I always have two pending orders or one. In either case it only deletes one Buy Stop. To delete Sell Stop you have to run the script again, provided there are no other Buy Stop orders. Please advise where the error is located.

void DeleteOrders()
{
int Total=OrdersTotal();
for(int i=0;i<Total;i++)
if(OrderSelect(i,SELECT_BY_POS))
{
int type=OrderType(), ticket=OrderTicket();
bool c;
switch(type)
{
case 4:
c=OrderDelete(ticket);
if(!c)
Print(GetLastError());
break;
case 5:
c=OrderDelete(ticket);
if(!c)
Print(GetLastError());
break;
}
}
}
Change the direction of the search for(i=total-1;i>=0;i--)
 
Maxim Kuznetsov:
change the direction of the search for(i=total-1;i>=0;i--)
Thank you, now it works. Am I right to understand that when an order with index 0 is deleted, an order with index 1 is assigned index 0, and at the next iteration i == 1, and such an order does not exist. Can you tell me if this is the case?
 
0B53RV3R:
Thank you, now it works. Do I understand correctly that when an order with index 0 is deleted, an order with index 1 is assigned index 0, and at the next iteration i == 1, and no such order appears. Can you tell if this is the case?
Right.
 

There's a customisable fractal feature, well geez, that's awfully heavy. I've left the drawing of objects only to visually show if the fractals are forming correctly - it's not going to happen.

Question: how can we make it easier, because visual testing is stupid:

extern int FrLeft=15; // Баров слева
extern int FrRight=5; // Баров справа

//-----------------------------------------------------------------------------------------------
void OnTick()
{
int nFrUp= GetBarFractal(Symbol(),PERIOD_CURRENT,FrLeft,FrRight,0,MODE_UPPER); // Возвращает номер бара
int nFrDn= GetBarFractal(Symbol(),PERIOD_CURRENT,FrLeft,FrRight,0,MODE_LOWER); // Возвращает номер бара
double FrHigh = High[nFrUp]; // Цена верхнего фрактала
double FrLow  = Low [nFrDn]; // Цена нижнего фрактала

SetArrow("FrUp"+"_"+(string)Time[nFrUp], Time[nFrUp], High[nFrUp], clrDeepSkyBlue, 217, 2, ANCHOR_BOTTOM);
SetArrow("FrDn"+"_"+(string)Time[nFrDn], Time[nFrDn], Low[nFrDn], clrDeepPink, 218, 2, ANCHOR_TOP);

// Comment("Price: ",FrHigh,", Num: ",nFrUp,"\nPrice: ",FrLow,", Num: ",nFrDn);
}

//----------------------------------------------------------------------------------------------+
//---------------------- Возвращает номер бара фрактала (настраиваемый) ------------------------+
//----------------------------------------------------------------------------------------------+
int GetBarFractal(string symb,ENUM_TIMEFRAMES tf=0,int nLeft=2,int nRight=2,int numFr=0,int mode=MODE_UPPER) {
int i=0,cn=0,pos=0,r=0,l=0,e=0,equals,bars;
double _high[], _low[];
nLeft=nLeft<=2?2:nLeft;
nRight=nRight<=2?2:nRight;
equals=nLeft+nRight;
bars=Bars(symb,tf)-equals;
ArraySetAsSeries(_high,true);
ArraySetAsSeries(_low,true);

  for(pos=nRight+1; pos<bars; pos++) {
   r=nRight;
   if(mode==MODE_UPPER) {
    CopyHigh(symb,tf,0,pos+equals+1,_high);
    for(i=1; i<=r; i++) {
     if(_high[pos]<=_high[pos-i]) break;
   }}
   if(mode==MODE_LOWER) {
    CopyLow(symb,tf,0,pos+equals+1,_low);
    for(i=1; i<=r; i++) {
     if(_low[pos]>=_low[pos-i]) break;
   }}
   //--
   if(i==r+1) {
    l=nLeft;
    e=equals;
     for(int j=1; j<=l+equals; j++) {
      if(mode==MODE_UPPER) {
       if(_high[pos]<_high[pos+j])  break;
       if(_high[pos]>_high[pos+j])  l--;
       if(_high[pos]==_high[pos+j]) e--;
      }
      if(mode==MODE_LOWER) {
       if(_low[pos]>_low[pos+j])  break;
       if(_low[pos]<_low[pos+j])  l--;
       if(_low[pos]==_low[pos+j]) e--;
      }
      if(l==0) {
       cn++;
       if(cn>numFr) return(pos);
      }
      //--
      if(e<0) break;
   }}
  }
   Print(__FUNCTION__": Фрактал не найден");
  return(0);
}

//----------------------------------------------------------------------------------------------+
//------------------- Функция рисования значка на графике, объект OBJ_ARROW --------------------+
//----------------------------------------------------------------------------------------------+
void SetArrow(string nm="", datetime t1=0, double p1=0, color col=clrRed,
                                           int code=252, int width=1, int anchor=0) {
if(ObjectFind(0,nm)==-1) {
    ObjectCreate(0,nm,OBJ_ARROW,0,0,0);
    ObjectSetInteger(0,nm,OBJPROP_COLOR,col);
    ObjectSetInteger(0,nm,OBJPROP_ARROWCODE,code);
    ObjectSetInteger(0,nm,OBJPROP_ANCHOR,anchor);
    ObjectSetInteger(0,nm,OBJPROP_WIDTH,width);
    ObjectSetInteger(0,nm,OBJPROP_SELECTED,false);
    ObjectSetInteger(0,nm,OBJPROP_SELECTABLE,true);
    ObjectSetInteger(0,nm,OBJPROP_HIDDEN,false);
    ObjectSetDouble(0,nm,OBJPROP_PRICE,p1);
    ObjectSetInteger(0,nm,OBJPROP_TIME,t1);
   }
}

Thanks!

 
Vitaly Muzichenko:

There's a customisable fractal feature, well geez, that's awfully heavy. I've left the drawing of objects only to visually show if the fractals are forming correctly - it's not going to happen.

Question: how can I make it easier, because visual testing is stupid:

Thanks!

You can only make it easier by turning it all into an indicator. Even the comment in the upper left corner of the chart and especially the position open and close marks, which the tester itself puts, are slowing down the testing.
 

Hello. Could you advise a newcomer?

Here's an example:

if(Condition1)

if(Condition2)

{

}

else

{

}

According to the code, Else should refer to if(condition1) and will be executed if condition1 is not met

But in fact it will also be executed if Condition2 is not met.

 
Andy-D:

Hello. Could you advise a newcomer?

Here's an example:

if(Condition1)

if(Condition2)

{

}

else

{

}

According to the code, Else should refer to if(condition1) and will be executed if condition1 is not met

But in fact it will be executed if Condition2 is not satisfied.


Get in the habit of putting curly braces immediately with the condition

if(Condition)

{

}

And according to your question

if(Условие1)
   {
       if(Условие2)

        {

        }
    }
   else

    {

    }