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

 
trader781:
You'll get the last losing one, what to do with it and what parameters to adjust for yourself, I think you'll figure it out

Thanks, I think I've got it.

How do I make the lot return to its original value after take profit?

if (OrderProfit()>0 && OrderType()<2)
            {
            lot=0.1
            }
it doesn't work that way)
 
wishmast:

Thanks, I think I've got it.

How do I get the lot to revert back to its original value after take profit?

if (OrderProfit()>0 && OrderType()<2)
            {
            lot=0.1
            }
it does not work that way)

I have the following scheme

if (OrdersTotal()==0)

{

open with initial lot

}

else

...

i.e. if there are no orders, it will not open with the old lot

Of course it is possible to memorise each order, but they do not tell us how to do it.
 
Vitalie Postolache:
A counter should be started and reset to its initial value when a certain threshold is reached:

Thank you.

Help me solve a problem.
There are 3 buy positions, a sell position appeared, we need to close the sell position and the oldest buy position. Two buy positions should remain.
I have them all closed.

void OnTick()
  {
//---
int b_STOP,s_STOP,b=0,s=0;

  for (int i=0; i<OrdersTotal(); i++)
    {
     if (OrderSelect(i, SELECT_BY_POS)==true)
      {  
        if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
        if (OrderType()==OP_BUY) b++;
        if (OrderType()==OP_SELL) s++;
        if (OrderType()==OP_BUYSTOP) b_STOP++;
        if (OrderType()==OP_SELLSTOP) s_STOP++;      
      }  
     if (OrderType()==OP_BUY)
     {
     if(b>=1&&s==1)//бай позиций больше или 1 и появилась SELL позиция
     {
      OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,CLR_NONE);
      CLOSEORDER(OP_SELL);
     }
     }
    }
  }

 

Explain why the OrderProfit()<0 function does not work?

   for(int q=OrdersHistoryTotal()-1; q>=0; q--)
      {
      if(OrderSelect(q,SELECT_BY_POS,MODE_HISTORY))
         {
         if(OrderType()<2 && OrderMagicNumber()==magic)
            {
            if (OrderProfit()<0)
               {
               if (DayOfWeek()==1)
                  {
                  day1++;
                  Print ("Понедельник SL=",day1);
                  }
               if (DayOfWeek()==2)
                  {
                  day2++;
                  Print ("Вторник SL=",day2);
                  }
               }
            }
         }
      
      }

i want to see how many stoplosses by day of the week

for(int l=OrdersHistoryTotal()-1; l>=0; l--)
        {
        if(OrderSelect(l,SELECT_BY_POS,MODE_HISTORY))
           {
           if(OrderProfit()<0 && OrderType()<2)
              {
              oldticket=OrderTicket();
              }
           if (oldticket>=ticket)
              {
              ticket=oldticket;
              lot=OrderLots()*4;
              }
           if (OrderProfit()>0 && OrderType()<2)
              {
              lot=0.01;
              }
           }
        }

and here i think it's the same reason why it doesn't work

 
wishmast:

Explain why the OrderProfit()<0 function does not work?

   for(int q=OrdersHistoryTotal()-1; q>=0; q--)
      {
      if(OrderSelect(q,SELECT_BY_POS,MODE_HISTORY))
         {
         if(OrderType()<2 && OrderMagicNumber()==magic)
            {
            if (OrderProfit()<0)
               {
               if (DayOfWeek()==1)
                  {
                  day1++;
                  Print ("Понедельник SL=",day1);
                  }
               if (DayOfWeek()==2)
                  {
                  day2++;
                  Print ("Вторник SL=",day2);
                  }
               }
            }
         }
      
      }

i want to see how many stoplosses by day of the week

for(int l=OrdersHistoryTotal()-1; l>=0; l--)
        {
        if(OrderSelect(l,SELECT_BY_POS,MODE_HISTORY))
           {
           if(OrderProfit()<0 && OrderType()<2)
              {
              oldticket=OrderTicket();
              }
           if (oldticket>=ticket)
              {
              ticket=oldticket;
              lot=OrderLots()*4;
              }
           if (OrderProfit()<0 && OrderType()<2)
              {
              lot=0.01;
              }
           }
        }

and here I take it it's the same reason why it doesn't work

Because DayOfWeek() returns the order number of the day of the week of the last known server time.

And you need the day of the week of the order closing time: TimeDayOfWeek(OrderCloseTime());

 
Artyom Trishkin:

Because DayOfWeek() returns the order number of the day of the week of the last known server time.

And you need the day of the week of the order closing time: TimeDayOfWeek(OrderCloseTime());

But the result is the same, as if ignoringif(OrderProfit()<0) and there is a loop, variables get large values.

2017.01.19 00:42:47.883 2014.02.20 23:59:48   EURUSD,H1: Вторник SL=1430208
2017.01.19 00:42:47.883 2014.02.20 23:59:48   EURUSD,H1: Понедельник SL=963666
2017.01.19 00:42:47.883 2014.02.20 23:59:48   EURUSD,H1: Вторник SL=1430207
2017.01.19 00:42:47.883 2014.02.20 23:59:48   EURUSD,H1: Понедельник SL=963665

What about the loop with a lot? I copied it incorrectly here, corrected the signif(OrderProfit()>0&&OrderType()<2), but it doesn't change the essence, the loop works correctly without this condition.

 
wishmast:

But the result is the same, as ifif(OrderProfit()<0) is ignored and there is a loop, variables have large values.

2017.01.19 00:42:47.883 2014.02.20 23:59:48   EURUSD,H1: Вторник SL=1430208
2017.01.19 00:42:47.883 2014.02.20 23:59:48   EURUSD,H1: Понедельник SL=963666
2017.01.19 00:42:47.883 2014.02.20 23:59:48   EURUSD,H1: Вторник SL=1430207
2017.01.19 00:42:47.883 2014.02.20 23:59:48   EURUSD,H1: Понедельник SL=963665

And about the loop with a lot? I copied it incorrectly here, corrected the signif(OrderProfit()>0&&OrderType()<2), but it does not change the essence, the loop works correctly without this condition.

Where are day1 and day2 declared ?
 
Artyom Trishkin:
Where are day1 and day2 declared ?
as global, at the beginning, before OnTick () and before all functions
 
Artyom Trishkin:
Where are day1 and day2 declared ?

I suspect I am selecting the order incorrectly, it does not work(OrderProfit()<0), I need to be more specific about which order I need. I don't know why it skips an order when the last one closed at Take-Point and there are no others. Maybe, if I want to search through the history, the loop should be built differently?

for(int w=OrdersHistoryTotal()-1; w>=0; w--)
      {
      if(OrderSelect(w,SELECT_BY_POS,MODE_HISTORY))

The reason is that Expert Advisors build loops with similar conditions and they work as required, but they work usingOrdersTotal().

for(int n=OrdersTotal()-1;n>=0;n--)            
         {                                          
         if(OrderSelect(n,SELECT_BY_POS))            
            {                                        
            if(OrderMagicNumber()==magic)                  
               {
               if(OrderType()>OP_SELL)
 
sile:
Thank you.

Help me solve a problem.
There are 3 buy positions, a sell position appeared, we need to close the sell position and the oldest buy position. Two buy positions should remain.
All of them are closed.

void OnTick()
  {
//---
int b_STOP,s_STOP,b=0,s=0;

  for (int i=0; i<OrdersTotal(); i++)
    {
     if (OrderSelect(i, SELECT_BY_POS)==true)
      {  
        if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
        if (OrderType()==OP_BUY) b++;
        if (OrderType()==OP_SELL) s++;
        if (OrderType()==OP_BUYSTOP) b_STOP++;
        if (OrderType()==OP_SELLSTOP) s_STOP++;      
      }  
     if (OrderType()==OP_BUY)
     {
     if(b>=1&&s==1)//бай позиций больше или 1 и появилась SELL позиция
     {
      OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,CLR_NONE);
      CLOSEORDER(OP_SELL);
     }
     }
    }
  }

This cannot be solved within a single cycle. In this loop, you should memorize the tickers of orders to be deleted in the array, in the next loop, you should delete them by tickers.

In addition, you have a mess with curly brackets, you are working with orders outside theOrderSelect{} block, this is incorrect.