Questions from Beginners MQL4 MT4 MetaTrader 4 - page 115

 

Guys, here's a question - how do I put variable values into a one-dimensional array?

Don't send it to help - there is NO example there for such a problem.


Let's say there is a variable a, which changes under certain conditions.

I want an array to store the last 3 values of this variable. How this is implemented in mql4?


I'm writing int mas[3];

mas[]=a; inside the loop, after a condition that changes a,

The compiler gives out ']', but nothing works, although logically it should. - expression expected, it wants me to put some unknown expression inside of brackets in mas[]=a.

Does MQL4 have any function that fills the array with a variable value at each loop iteration?


 
Igor Makanu:

I apologize for not finding the beginning of the correspondence, but I would like to immediately advise you to avoid complex conditions and calculations in them - this makes the code unreadable and as a result complicates the search for logical errors, I would write your code like this:

If you understand what I mean, you can modify the code I gave you a little more, I mean to put it in a separate conditionif(OrderType()==OP_BUY....

Then it will be much easier to find logical errors, imho

Insert after each OrderSend => return

or make a selection via switch :

switch ( OrderType() )
{ OP_BUY : { if (1) { .... break; }
             if (2) { .... break; }
           }
  OP_SELL : { if (3) { .... break; }
              if (4) { .... break; }
            }
}
 
Vladimir Tkach:

Trying this and it doesn't work.

you have few input parameters in your custom indicator call - count how manyexternal variables in theMorning Flat indicator? (extern) - that's how many parameters you should pass when using iCustom

 
John Smith:
.....

I write int mas[3]=a; inside the loop after the condition when a changes,

but nothing works, although logically it should.

Learn to do all actions step by step rather than all at once.

Example :

int mas[3]={0}; // инициализация масива 

.....           // основной код
if ( найдена новая величина а )
{ mas[2]=mas[1];  // самая старая величина а
  mas[1]=mas[0];
  mas[0]=a;       // новая величина а
}
.....           // основной код
 
Igor Makanu:

you have few input parameters in your custom indicator call - count how manyexternal variables in theMorning Flat indicator? (extern) - that's how many parameters you should pass when using iCustom

I have already tried them all.

I have solved it in another way. I have inserted price calculation code from the indicator in my Expert Advisor.

 
Igor Makanu:

The logic of the code is the same, but if you split the conditions, it is faster to find where the logical error is, and in your case, if you put it in a separate condition, you get optimization - the condition will be checked once and not 4 times:

into a separate condition, we get code optimization - the condition will be checked once and not 4 times as in your example.

Check the output in the Expert Advisor's journal using the

for logging so that you can see which code fragment was executed and with what parameters

Thank you for your reply. Split the conditions to the limit. Checked the logic with comments. It turned out that the position closing is based on the conditions of the first open position. It means that if first we opened a Buy or Sell position with Condition 1 and then a second position opened with the same condition but with Condition 2, this closing condition is ignored and the second position is also closed with Condition 1 regardless of the other magic number. What should I do?

 
Vladimir Tkach:

I've already gone through them all.

I have solved the problem in a different way. I have pasted the code of price calculation from indicator to Expert Advisor.

I noticed that iCustom() was not called correctly, it should be that way:

min=iCustom(Symbol(),0,"Morning Flat",StartHour,EndHour,TargetLevel,UpColor,DnColor,TargetUpColor,TargetDnColor,1,0);

I pass 7 parameters when calling the indicator, you called it with 3 parameters

I would leave the indicator call, but I would modify the indicator itself - remove external variables and graphical objects from the indicator code


novichok2018:

I have checked the logic with comments. It turned out that positions are closed according to the conditions of the first open position.

Congratulations! You are on the right track, but it's better at the debugging stage to print it in the journal (Print()) instead of in the comment ( Commetn())

Show me the code.

 
Igor Makanu:

So, I noticed that you didn't call iCustom() correctly, it should be like this:

I pass 7 parameters when calling the indicator, and you called it with 3 parameters

I would leave the indicator call, but I would modify the indicator itself - remove external variables and graphical objects from the indicator code


congratulations! You are on the right way, but it's better at the debugging stage to output in the journal (Print()) instead of in the comment ( Commetn())

show the code.

 int total=OrdersTotal();
      for(int i=OrdersTotal()-1;i>=0;i--)
      {
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {
         if(TimeCurrent()>OrderOpenTime()+100)
           { 
            if(OrderType()==OP_BUY)
              {
               if(OrderMagicNumber==101)
                 {
                 if(MathAbs(WPR1)<5)
                 rez =  OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,Yellow); 
                 }
               if(OrderMagicNumber==111)
                 {
                  if(MathAbs(WPR1)<20) 
                  rez =  OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,Yellow);
                 }
              }
                 
            if(OrderType()==OP_SELL)
              {
               if(OrderMagicNumber==222)
                 {
                  if(MathAbs(WPR1)>97.55)
                  rez =  OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,Yellow); 
                 }
               if(OrderMagicNumber==201)
                 {
                  if(MathAbs(WPR1)>96)
                  rez =  OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,Yellow);
                 } 
              }
            }               
         }
       continue;  
      }   

I have removed the prints.

 
novichok2018:

I have removed the prints.

I don't know all the logic of your code, but after each order closure you definitely need to exit the loop via break; and on the next tick you need to check orders on your conditions again - I don't know howOrderSelect() behavesafter the order is closed - if the order is closed, then what is selected, or is there an error?

Basically, your problem is that you want to check and close everything at once in one loop.

switch()

or make a function to close the order (there are ready-made functions for orders https://www.mql5.com/ru/forum/131859/page2#434206 )

or use break after the order is closed in your code

Полезные функции от KimIV
Полезные функции от KimIV
  • 2008.03.11
  • www.mql5.com
В этой теме я буду выкладывать коды своих функций на языке программирования MQL4, приводить примеры их использования и отвечать на вопросы, связанн...
 
Igor Makanu:

I do not know all the logic of your code, but after each close of an order, you definitely need to exit the loop via break; and on the next tick you need to check your conditions again - I do not know how doesOrderSelect() behaveafter the order is closed - if the order is closed, then what is selected, or is there an error?

Basically, your problem is that you want to check and close everything at once in one loop.

switch()

or make a function to close the order (there are ready-made functions for orders https://www.mql5.com/ru/forum/107476 )

or use break after the order is closed in your code

Thank you. Breaking seems to have helped. We will see what happens next.