Questions from Beginners MQL4 MT4 MetaTrader 4 - page 70

 
Hello, Could you please help me with writing a function in MT4. I know my account balance, I know how many trades will open (e.g. 9), I know the risk (e.g. 3% of AccountBalance), I need to calculate a lot for the first trade, if each next lot will be doubled and all trades will overlap each other.
//Функция расчета торгового лота
double GetLots()
{
 double clots = 0.0;
 double cl = NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT),2); 
 double L2 = NormalizeDouble(cl * Multipler,2);
 double L3 = NormalizeDouble(L2 * Multipler,2);
 double L4 = NormalizeDouble(L3 * Multipler,2);
 double L5 = NormalizeDouble(L4 * Multipler,2);
 double L6 = NormalizeDouble(L5 * Multipler,2);
 double L7 = NormalizeDouble(L6 * Multipler,2);
 double L8 = NormalizeDouble(L7 * Multipler,2);
 double L9 = NormalizeDouble(L8 * Multipler,2);
 
 for(cl = NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT),2);AccountFreeMarginCheck(Symbol(),OP_BUY,L9) <= (AccountBalance()/100)*Risk;cl--)
 {
  if(!IsStopped()) 
  {
   clots = NormalizeDouble(cl,2);
  }
 } 
 return(clots);
}

This is what I got. Please tell me what's wrong. Thank you in advance!

 

Hello!

I'm writing here as there doesn't seem to be anyone on mq4.

Can you tell me what I'm doing wrong? Option 2 is not working.

double A[][10];
int renge_A_one=0;                                                    // =ArrayRange(A, 0); // Размер первого измерения (число строк)
int renge_A_two=0;                                                    // =ArrayRange(A, 1); // Размер второго измерения (число столбцов)

double B[][10];
int renge_B_one=0;                                                    // =ArrayRange(A, 0); // Размер первого измерения (число строк)
int renge_B_two=0;                                                    // =ArrayRange(A, 1); // Размер второго измерения (число столбцов)
int count_B=0;

//+------------------------------------------------------------------+
//| Вариант 1. Копируем строки "А" в "В" без изменений               |
//+------------------------------------------------------------------+
void Fun_1()
   {
    renge_A_one==ArrayRange(A, 0);                                    // Выясняем Размер первого измерения (число строк)
    renge_A_two==ArrayRange(A, 1);                                    // Выясняем Размер второго измерения (число столбцов). "На всякий случай"
    for(int i=0; i<range_A_one; i++)                                  // Перебираем массив A[][10] по ПЕРВОМУ измерению.
        {
         count_B++;
         ArrayResize(B, count_B);
         for(int j=0; j<renge_A_two; j++)                             // Перебираем массив A[][10] по ВТОРОМУ измерению.
              B[i][j]=A[i][j];
        }
    renge_B_one==ArrayRange(B, 0);                                    // Выясняем Размер первого измерения (число строк)
    renge_B_two==ArrayRange(B, 1);                                    // Выясняем Размер второго измерения (число столбцов). "На всякий случай"
   }
// Результат: "А" скопирован в "В" без изменений 
//            renge_A_one = renge_В_one
//            renge_A_two = renge_В_two

//+------------------------------------------------------------------+
//| Вариант 2. Копируем строки "А" в "В" при условии                 |
//+------------------------------------------------------------------+
void Fun_2()
   {
    renge_A_one==ArrayRange(A, 0);                                    // Выясняем Размер первого измерения (число строк)
    renge_A_two==ArrayRange(A, 1);                                    // Выясняем Размер второго измерения (число столбцов). "На всякий случай"
    for(int i=0; i<range_A_one; i++)                                  // Перебираем массив A[][10] по ПЕРВОМУ измерению.
        {
         if(A[i][3]>0)
             {
              count_B++;
              ArrayResize(B, count_B);
              for(int j=0; j<renge_A_two; j++)                        // Перебираем массив A[][10] по ВТОРОМУ измерению.
                   B[i][j]=A[i][j];
             }
        }
    renge_B_one==ArrayRange(B, 0);                                    // Выясняем Размер первого измерения (число строк)
    renge_B_two==ArrayRange(B, 1);                                    // Выясняем Размер второго измерения (число столбцов). "На всякий случай"
   }
// Результат: часть строк "В" с соответствующими значениями
//            часть строк "В" заполнена "0" полностью, при том, что в "А" заполнены ЗНАЧЕНИЯМИ
//            renge_A_one > renge_В_one
//            renge_A_two = renge_В_two
 
Arseniy Barudkin:
Hello, Please help me with writing a function in MT4. I know my account balance, I know how many trades will open (e.g. 9), I know the risk (e.g. 3% of AccountBalance), I need to calculate a lot for the first trade, if each next lot will be doubled and all trades will overlap each other.

This is what I got. Please tell me what's wrong. Thank you in advance!

Elementary. The error is theGetLots() function.The whole function.
 
Leo59:

Hello!

I'm writing here, because there seems to be nobody on mq4.

Can you tell me what I'm doing wrong? Option 2 doesn't work.

double A[][10];
int renge_A_one=0;                                                    // =ArrayRange(A, 0); // Размер первого измерения (число строк)
int renge_A_two=0;                                                    // =ArrayRange(A, 1); // Размер второго измерения (число столбцов)

double B[][10];
int renge_B_one=0;                                                    // =ArrayRange(A, 0); // Размер первого измерения (число строк)
int renge_B_two=0;                                                    // =ArrayRange(A, 1); // Размер второго измерения (число столбцов)
int count_B=0;

//+------------------------------------------------------------------+
//| Вариант 1. Копируем строки "А" в "В" без изменений               |
//+------------------------------------------------------------------+
void Fun_1()
   {
    renge_A_one==ArrayRange(A, 0);                                    // Выясняем Размер первого измерения (число строк)
    renge_A_two==ArrayRange(A, 1);                                    // Выясняем Размер второго измерения (число столбцов). "На всякий случай"
    for(int i=0; i<range_A_one; i++)                                  // Перебираем массив A[][10] по ПЕРВОМУ измерению.
        {
         count_B++;
         ArrayResize(B, count_B);
         for(int j=0; j<renge_A_two; j++)                             // Перебираем массив A[][10] по ВТОРОМУ измерению.
              B[i][j]=A[i][j];
        }
    renge_B_one==ArrayRange(B, 0);                                    // Выясняем Размер первого измерения (число строк)
    renge_B_two==ArrayRange(B, 1);                                    // Выясняем Размер второго измерения (число столбцов). "На всякий случай"
   }
// Результат: "А" скопирован в "В" без изменений 
//            renge_A_one = renge_В_one
//            renge_A_two = renge_В_two

//+------------------------------------------------------------------+
//| Вариант 2. Копируем строки "А" в "В" при условии                 |
//+------------------------------------------------------------------+
void Fun_2()
   {
    renge_A_one==ArrayRange(A, 0);                                    // Выясняем Размер первого измерения (число строк)
    renge_A_two==ArrayRange(A, 1);                                    // Выясняем Размер второго измерения (число столбцов). "На всякий случай"
    for(int i=0; i<range_A_one; i++)                                  // Перебираем массив A[][10] по ПЕРВОМУ измерению.
        {
         if(A[i][3]>0)
             {
              count_B++;
              ArrayResize(B, count_B);
              for(int j=0; j<renge_A_two; j++)                        // Перебираем массив A[][10] по ВТОРОМУ измерению.
                   B[i][j]=A[i][j];
             }
        }
    renge_B_one==ArrayRange(B, 0);                                    // Выясняем Размер первого измерения (число строк)
    renge_B_two==ArrayRange(B, 1);                                    // Выясняем Размер второго измерения (число столбцов). "На всякий случай"
   }
// Результат: часть строк "В" с соответствующими значениями
//            часть строк "В" заполнена "0" полностью, при том, что в "А" заполнены ЗНАЧЕНИЯМИ
//            renge_A_one > renge_В_one
//            renge_A_two = renge_В_two

Highlighted: there should be an assignment - "=" and you have a comparison - "==".

 

Hi all !
I am learning to write robots in mt4, i decided to start with binary options . I am writing simple EA, but compiler generates a lot of warnings, i can't understand it (
Help: "Oh MQL4 Guru" =))))))
Thanks in advance!)

Here is the code for the EA.

//+------------------------------------------------------------------+
//|Bolinger_Bands_traider.mq4 |
//|Copyright 2017, Penrov Nikolay |
//|vk.com/buzamonolit |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Petrov Nikolay"
#property link "vk.com/buzamonolit"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| expert initialization function|
//+------------------------------------------------------------------+
extern int Bolinger_Bands = 20; // Bollinger Band period
extern double Delta = 0.0003; // At what distance to open the order

string Symb; // Name of the financial instrument.
int Total; // Number of orders

//+------------------------------------------------------------------+
//| expert initialization function|
//+------------------------------------------------------------------+
int init()
{
//----
Initialize_Objects(0);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function|
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function|
//+------------------------------------------------------------------+
int start()
{
//----
double price = Bid;
double CurrBol_High = iBands(Symb,0, Bolinger_Bands, 0,0, PRICE_CLOSE, MODE_UPPER, 0);
double CurrBol_Low = iBands(Symb,0, Bolinger_Bands, 0,0, PRICE_CLOSE, MODE_LOWER, 0);
double PriceBol_High = price - CurrBol_High;
double PriceBol_Low = price - CurrBol_Low;
datetime NextClose = Time[0] + Period()*60*2;
int MinutesToNextClose = (NextClose - TimeCurrent())/60 + 1;
int err;

// Order counting
Symb=Symbol();// Name of the fin.symbol(); // symbol(); // symbol(); // order icon(); // symbol().
Total=0;// Number of orders
for(int i=1; i<=OrdersTotal(); i++) // Order loop
{
if (OrderSelect(i-1,SELECT_BY_POS)==true) // If there is a next
{// Order analysis:
if (OrderSymbol()!=Symb)continue; // Not our financial instrument
if (OrderType()>1)// We have a pending order
{
Alert("Pending order detected. Expert Advisor is not working;)
return(0);// Exit()
}
Total++;// Counter market. order
if (Total>1)// Not more than one order
{
Alert("Several market orders. Expert Advisor is not working.");
return(0);// Exit()
}
}
}

ObjectSetText("Obj_Label", "TF" + Period() + ", " + DoubleToStr(price, 5) + ", tick " + TimeToStr(TimeCurrent(), TIME_SECONDS) + ", O@" + TimeToStr(Time[0], TIME_MINUTES) + ", NC@" + TimeToStr(NextClose, TIME_MINUTES) + " (" + MinutesToNextClose + " min)", 10, "Arial", DarkGreen );
ObjectSetText("Obj_Label2", "Orders:" + Total + ", delta " + DoubleToStr(Delta,5) + ", distance to MA:" + DoubleToStr(PriceBol_High, 5), 10, "Arial", DarkGreen );
ObjectSetText("Obj_Label3", "Orders: " + Total + ", Delta " + DoubleToStr(Delta,5) + ", distance to MA:" + DoubleToStr(PriceBol_Low, 5), 10, "Arial", DarkGreen );


// price analysis and order opening

if ((MathAbs(PriceBol_Low) < Delta) && (price > CurrBol_Low) && (Open[0] > CurrBol_Low+Delta) && (Total<1))
{
if (OrderSend(Symbol(), OP_BUY, 1.00,Bid , 3, 0, 0, "BO exp:900", 0,DoubleToStr(MinutesToNextClose,0), Green) >0) Print ("Opened OK");
else
{
err=GetLastError();
Print("error(",err,"));
return(0);

}
Print ("CALL @ " + price + " + TimeToStr(TimeCurrent(), TIME_SECONDS) + " exp in " + MinutesToNextClose);

}

if ((MathAbs(PriceBol_High) < Delta) && (price < CurrBol_High) && (Open[0] < CurrBol_High-Delta) && (Total<1))
{
if (OrderSend(Symbol(), OP_SELL, 1.00,Bid , 3, 0, 0, "BO exp:900", 0,DoubleToStr(MinutesToNextClose,0), Red) > 0) Print ("Opened OK");
else
{
err=GetLastError();
Print("error(",err,"));
return(0);

}
Print ("PUT @ " + price + " + TimeToStr(TimeCurrent(), TIME_SECONDS) + " exp in " + MinutesToNextClose);

}

//----
return(0);
}
//+------------------------------------------------------------------+

int Initialize_Objects(int Win) // User fie
{// .object creation
ObjectCreate("Obj_Label",OBJ_LABEL, Win, 0,0); // object creation
ObjectSet("Obj_Label", OBJPROP_CORNER, 0); // Bind to a corner
ObjectSet("Obj_Label", OBJPROP_XDISTANCE, 13); // X coordinate
if (Win==0)
ObjectSet("Obj_Label",OBJPROP_YDISTANCE, 15);//Y coordinate
else
ObjectSet("Obj_Label",OBJPROP_YDISTANCE,15);//Y Coordinate

ObjectCreate("Obj_Label2",OBJ_LABEL,Win, 0,0); // Object creation
ObjectSet("Obj_Label2",OBJPROP_CORNER, 0); // Binding to a corner
ObjectSet("Obj_Label2", OBJPROP_XDISTANCE, 13); // X coordinate
if (Win==0)
ObjectSet("Obj_Label2",OBJPROP_YDISTANCE, 35);//Y coordinate
else
ObjectSet("Obj_Label2",OBJPROP_YDISTANCE, 35);//Y Coordinate

ObjectCreate("Obj_Label3",OBJ_LABEL, Win, 0,0); // Create object
ObjectSet("Obj_Label3",OBJPROP_CORNER, 0); // Binding to a corner
ObjectSet("Obj_Label3", OBJPROP_XDISTANCE, 13); // X coordinate
if (Win==0)
ObjectSet("Obj_Label3",OBJPROP_YDISTANCE, 55);//Y coordinate
else
ObjectSet("Obj_Label3",OBJPROP_YDISTANCE, 55);//Y Coordinate

return(0);// Exit from user.function
}

 

Here are the warnings. I can't figure out what he doesn't like(


possible loss of data due to type conversion Bolinger_Bands_traider.mq4 51 24
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 76 35
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 76 227
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 77 43
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 78 43
implicit conversion from 'string' to 'number' Bolinger_Bands_traider.mq4 85 71
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 93 23
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 93 91
implicit conversion from 'string' to 'number' Bolinger_Bands_traider.mq4 99 72
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 107 23
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 107 91
0 error(s), 11 warning(s), compile time: 204 msec 1 12

 
Vitalie Postolache:
Elementary. The error is theGetLots() function.The whole function.

Maybe you can tell me how to write it correctly. I wanted to calculate using loop by brute forcing from max lot.
 
Arseniy Barudkin:

Maybe you can tell me how to write it correctly. I wanted to calculate using a loop by trying from the maximum lot.


Where is the logic? You set the maximum allowed lot for the first order and then increase it for each successive order. Doesn't that seem to you, to put it mildly, not very sensible?

Further, you decrease the lot of the first order in the loop using some totally incomprehensible method, while the lots of the other orders that were "calculated" before remain unchanged, and these values do not go beyond the limits of this function. So, what do they do then?

Not to mention that the increment loop can't be a real number, it has to be a counter, an integer. But you set the lot value as a counter and subtract one from it each iteration. This is a major mistake, a very serious one.

Clarify the logic in your mind first and then try to implement it into your code.

 
bog_v_nas:

But here are the warnings. I can not understand what he does not like(


possible loss of data due to type conversion Bolinger_Bands_traider.mq4 51 24
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 76 35
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 76 227
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 77 43
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 78 43
implicit conversion from 'string' to 'number' Bolinger_Bands_traider.mq4 85 71
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 93 23
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 93 91
implicit conversion from 'string' to 'number' Bolinger_Bands_traider.mq4 99 72
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 107 23
implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 107 91
0 error(s), 11 warning(s), compile time: 204 msec 1 12

Output one line with number 51, as there is no count here to understand the error"possible loss of data due to type conversion Bolinger_Bands_traider.mq4 51 24"

The rest: "implicit conversion from 'number' to 'string' Bolinger_Bands_traider.mq4 107 91

translate numeric values into a text string where we create the object - a text string, for example

period()

it should be

DoubleToString(Period(),0)

 
Renat Akhtyamov:

translate the numeric values to a text string where we create a text string object, e.g.

period()

it should be

DoubleToString(Period(),0)


Since when is period a Double type?