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

 
Mikhail Tkachev:

I'm asking for help from the community...
How to declare and create MQL5 global objects correctly?

The global ones are declared here

//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//--- 
int      Global_1;
double   Global_2;
string   Global_3;
long     Global_4;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
 
Alexey Viktorov:

I don't know how to tell you to read the documentation. If you have read it and the standard function is not suitable for some reason, you should have said so straight away...

Well, about "send", let the moderators decide who to send and where to send it to...! Ban, for example... Guess who. As for the regular functions, I haven't seen json parsing and saving/writing json files in the documentation. But you, as usual, know some secret section of the documentation where it's all there... And I didn't know... Holy shit. Shame on me. I'm about to be shushed by Alexei... :)

 

help people, going to opena maximum of 2 orders at the same time, I put so does not open any one, without pos_total opens


void OPENORDER(string ord)

  {
    int pos_total=PositionsTotal();
    double priceL=m_symbol.Ask();
   if (pos_total>=2)
   {
   if(ord=="Sell")      
   
        //--- check for free money
            if(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_BUY,my_lot,priceL)<0.0)
               printf("We have no money. Free Margin = %f",m_account.FreeMargin());
            else
      if(!m_trade.Sell(my_lot,Symbol(),m_symbol.Bid(),my_SL,my_TP,""))
         Print("BUY_STOP -> false. Result Retcode: ",m_trade.ResultRetcode(),
               ", description of Retcode: ",m_trade.ResultRetcodeDescription(),
               ", ticket of order: ",m_trade.ResultOrder());                     // Если sell, то не открываемся
     double priceS=m_symbol.Bid();
   if(ord=="Buy")  
         //--- check for free money
            if(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_SELL,my_lot,priceS)<0.0)
               printf("We have no money. Free Margin = %f",m_account.FreeMargin());
            else
      if(!m_trade.Buy(my_lot,Symbol(),m_symbol.Ask(),my_SL,my_TP,""))
 
         Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
               ", description of result: ",m_trade.ResultRetcodeDescription(),
               ", ticket of deal: ",m_trade.ResultDeal());
   return;
 }
}


What am I doing wrong?

 
Eugen8519:

help people, going to opena maximum of 2 orders at the same time, I put so does not open any one, without pos_total opens



What am I doing wrong?

You wrote to open if you already have 2 or more positions
if (pos_total<=2)
 
Alexey Viktorov:

Makar, you used to do the right thing and now you want to do the stupid thing...

Think back to your 5th or so grade maths. How much is (-3)-(-7) and how much is (-3)+(-7)?

It's hard to check on a calculator, Excell will help you.


Alexey knows mathematics from the 5th grade. Bravo, Alexey. Just Captain Hindsight. :)

 
Mihail Matkovskij:

Alexei knows maths as far back as the 5th grade. Bravo, Alexei.

In this case, you're wrong.
 
MakarFX:

Yes, thank you. I jumped to conclusions. I got it right)

The question was not that the function may have negative values, but why add swap and commission to the profit. You and Alexei probably did not understand.

Tsaryoff5614 wanted to calculate the order profit in the history and the function was called lastloss (if you speak English).

https://www.mql5.com/ru/forum/160683/page1559#comment_23786144

You have confused yourself and me. And then you and Alexey... Fool...

Любые вопросы новичков по MQL4 и MQL5, помощь и обсуждение по алгоритмам и кодам
Любые вопросы новичков по MQL4 и MQL5, помощь и обсуждение по алгоритмам и кодам
  • 2021.08.02
  • www.mql5.com
В этой ветке я хочу начать свою помощь тем, кто действительно хочет разобраться и научиться программированию на новом MQL4 и желает легко перейти н...
 
MakarFX:
You are wrong in this case.

Thanks for the tip, but it doesn't filter at all, it opens up to five positions

   if(InpTrailingOrderLimit==0)
        return;
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_position.SelectByIndex(i))
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               if(m_position.PriceCurrent()-m_position.PriceOpen()>ExtTrailingOrderLimit+ExtTrailingOrderStep)
                  if(m_position.StopLoss()<m_position.PriceCurrent()-(ExtTrailingOrderLimit+ExtTrailingOrderStep))        
                       OPENORDER("Buy");
              
                  
              }
            else
              {
               if(m_position.PriceOpen()-m_position.PriceCurrent()>ExtTrailingOrderLimit+ExtTrailingOrderStep)
                  if((m_position.StopLoss()>(m_position.PriceCurrent()+(ExtTrailingOrderLimit+ExtTrailingOrderStep))))
                        OPENORDER("Sell");


it tells it to open positions, no matter where I put pos_total, it doesn't work

 
Eugen8519:

Thanks for the tip, but it doesn't filter at all, it opens up to five positions


It's telling him to open a position, no matter where I put pos_total, it doesn't work.

No need to remove
if (pos_total<=2)

I need to change the sign like I wrote.

 
Mihail Matkovskij:

The question was not that the function can have negative values, but why add swap and commission to the profit. You and Alexey probably did not understand.

Tsaryoff5614 wanted to calculate profit, but his function was called lastloss. He confused himself and me. And now you and Alexey... Fool...

That's why we add it, so that we can calculate all the profit for the period.

Reason: