Errors, bugs, questions - page 857

 
G001: Sorry, I don't know what to answer, I don't know where the problem is, I know it doesn't do what I need.
Let's take the simplest way: after each meaningful line in this part of the code, write Print() and see what is printed.
 
//+------------------------------------------------------------------+
//|                                                   CalcMartin.mq5 |
//|                                                        avoitenko |
//|                        https://www.mql5.com/en/users/avoitenko |
//+------------------------------------------------------------------+
#property copyright "avoitenko"
#property link      "https://www.mql5.com/en/users/avoitenko"
#property version   "1.00"
#property script_show_inputs

input bool Martingale=true;
input uint MaxLossTrade=5;
input double StartLot=0.1;
input double LotMultiply=2;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   double ResultLot=StartLot;
//----- Martingale
   if(Martingale)
     {

      HistorySelect(0,TimeCurrent());
      int Total=HistoryDealsTotal();
      int Losses=0;  // Number of losses orders without a break

      for(int i=Total-1;i>=0;i--)
        {
         ulong Ticket=HistoryDealGetTicket(i);

         if((HistoryDealGetInteger(Ticket,DEAL_TYPE)==DEAL_TYPE_BUY || HistoryDealGetInteger(Ticket,DEAL_TYPE)==DEAL_TYPE_SELL) && 
            (HistoryDealGetInteger(Ticket,DEAL_ENTRY)==DEAL_ENTRY_INOUT || HistoryDealGetInteger(Ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT))
           {
            double profit=HistoryDealGetDouble(Ticket,DEAL_PROFIT)+HistoryDealGetDouble(Ticket,DEAL_SWAP);

            if(profit>=0) break;
            Losses++;
           }
         if(Losses<=MaxLossTrade)ResultLot=(StartLot*MathPow(LotMultiply,Losses));
         else {ResultLot=StartLot;break;}
        }
     }
//-----
   Print("Result Lot = ",ResultLot);
  }
//+------------------------------------------------------------------+
 
avoitenko:

Thank you very much, understood my mistakes.

Sorry for the bluntness.

 

how do i get the number of days in a year and in a month? just by prewriting it? like the month number is if it is January then it is 31, etc.?

what about the year?

 
FiftyStars:

how do i get the number of days in a year and in a month? just by prewriting it? like the month number is if it is January then it is 31, etc.?

what about the year?

If divided by 4 it is high, the rest are 365.
 
Urain:
If you divide it by 4, it's high, the rest is 365.
However, there are exceptions... although you can ignore them :)
 
Urain:
If divisible by 4, high, the rest 365.

2100 is also divisible by 4, but it's 365

1900 too...

 
mql5:
However, there are exceptions... although you can ignore them :)

Here's the code with exceptions, but only for our era. The function returns true if the year is a leap year.

bool IsLeapYear(int year)
  {
   return(MathMod(year,4)==0 && (MathMod(year,100)!=0 || MathMod(year,400)==0));
  } 
 
avoitenko:

Here is the code with exceptions, but only for our era. The function returns true if the year is a leap year.

Exactly, but I would have done it differently... so if the condition is true, there's no need to do the next check.

bool IsLeapYear(int year)
  {
   if(year%4!=0)return(false);
   else
     {
      if(year%100==0)
        {
         if(year%400==0)return(true);
         else return(false);
        }
      else return(true);
     }
  }

Most of the years will come out as false in the first check,

if the year is divisible by 4, then we check if it is divisible by 100, most of the remaining ones are not divisible and it will be true,

if it is divisible, then we check the third condition, whether the year is divisible by 400.

Thus only minimum part of years will be tested for all three conditions (which is statistically insignificant and will not affect performance).

The speed of such a function will tend to the speed of one check, while you have the speed of two checks.

And I think the number of days is better by swiping:

int CountDayforMonth(int month,bool leapyear=false)// количество дней в месяце
  {
   switch(month)
     {
      case  1: return(31);
      case  2: if(leapyear)return(29);else return(28);
      case  3: return(31);
      case  4: return(30);
      case  5: return(31);
      case  6: return(30);
      case  7: return(31);
      case  8: return(31);
      case  9: return(30);
      case  10: return(31);
      case  11: return(30);
      case  12: return(31);      
      default:return(0);
     }
  }
 
Urain:

Exactly, but I would do it differently... So if the condition worked, there is no need to do another check.

Most of the years will come out on the first check as false,

if the year is divisible by 4, then we check if it is divisible by 100, most of the remaining years are not divisible and we exit as true,

if it is divisible, then we check the third condition, whether the year is divisible by 400.

Thus only minimum part of years will be tested for all three conditions (which is statistically insignificant and will not affect performance).

The performance of such a function will tend to the speed of one check, while yours will tend to the speed of two checks.

I have settled on

int GetDaysInYear(int Year) export
  {
   bool Leap=false;
   if(Year%4==0)
     {
      if(Year%100==0)
        {
         if(Year%400==0)
            Leap=true;
        }
      else Leap=true;
     }
   if(Leap)
      return(366);
   else
      return(365);
  }
Reason: