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

 

Greetings, the script works as I ask it to.

//+------------------------------------------------------------------+
//|                                                       Око_15.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//#include <OOP_CLibArr_01.mqh>
struct Gass
  {
   int               mag;
   double            prof;
  };
Gass plus[]= {{0},{0}};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class Ohistory
  {
public:
                     Ohistory(
      string        _sy = "",
      int           _op =-1,
      int           _mn =-1,
      datetime      _dt =0
   )
     {
      int k=3;
      GetProfit_His(_sy, _op, _mn, 0);
      Alert("Конструктор"," k =",k);
     }
                    ~Ohistory() { Alert("Деструктор"); }

   double              inits(Gass& us[],int _magik,datetime _zeit)
     {
      double pro=GetProfit_His(Symbol(), -1, _magik, _zeit);
      for(int i=0; i<ArraySize(us); i++)
        {
         if(us[i].mag==3)
            pro+=us[i].prof;
        }
      return(pro);
     }
private:
   
   double            GetProfit_His(string sy="", int op=-1, int mn=-1, datetime dt=0)
     {
      double p=0;
      int    i, k=OrdersHistoryTotal();

      if(sy=="")
         sy=Symbol();
      for(i=0; i<k; i++)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
           {
            if((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op))
              {
               if(OrderType()==OP_BUY || OrderType()==OP_SELL)
                 {
                  if(mn<0 || OrderMagicNumber()==mn)
                    {
                     if(dt<OrderCloseTime())
                       {
                        p+=OrderProfit()+OrderCommission()+OrderSwap();
                       }
                    }
                 }
              }
           }
        }
      Alert(" p =",p);
      return(p);
     }
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int si=5,
       m=3;
   ArrayResize(plus,si);
   for(int i=0; i<ArraySize(plus); i++)
      plus[i].mag=m;

   int size=ArraySize(plus);
   if(si>=3)
     {
      switch(size==0)
        {
         case 0:
            if(plus[0].mag==m)
               plus[0].prof=400;

         case 1:
            if(plus[1].mag==m)
               plus[1].prof=300;
            ;
         case 2:
            if(plus[1].mag==5)
               plus[2].prof=90;
        }
     }
   for(int i=0; i<ArraySize(plus); i++)
     {
      Print(" plus[i].prof =",plus[i].prof);
     }
   Ohistory hi;
   Alert("Мужду");

   Print(" Сколько ???',  ",hi.inits(plus,-1,D'2021.10.28'));

  }
//+------------------------------------------------------------------+

But if I move it

class Ohistory

into the include file, it stops seeing the array of structures

struct Gass
  {
   int               mag;
   double            prof;
  };
Gass plus[]= {{0},{0}};

the line

  double              inits(Gass& us[],int _magik,datetime _zeit)

Doesn't work that way. I can't figure out how to fix this.

 

Can you give me a hint? Here are two functions to calculate and display the price grid of orders and min/max orders. I have written the latter one by myself based on the previous function's analogy. The first one works as expected but the second one, the min/max order price, does not want to show on the chart.

Please help us to find the error. Please help me to find the error.

//+----------------------------------------------------------------------------+
//| Расчет средней цены (0)-buy (1)-sell ()-all                                |
//+----------------------------------------------------------------------------+
double GetAveragePrice(int ot=-1)
  {
   double order_lots = 0, order_price = 0, avg_price = 0;
     {
      for(int i = OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType()==ot||ot<0)
                 {
                  order_lots += OrderLots();
                  order_price += OrderOpenPrice() * OrderLots();
                 }
              }
           }
        }
     }
   avg_price = NormalizeDouble(order_price / order_lots, Digits);

   if(ObjectFind(0,"AveragePriceLine"+IntegerToString(ot))!=0)
      ObjectCreate(0,"AveragePriceLine"+IntegerToString(ot),OBJ_HLINE, 0, 0, avg_price);
   else
      ObjectSetDouble(0,"AveragePriceLine"+IntegerToString(ot),OBJPROP_PRICE,avg_price);
   if(ot==0)
      ObjectSet("AveragePriceLine"+IntegerToString(ot),OBJPROP_COLOR, clrLime);
   if(ot==1)
      ObjectSet("AveragePriceLine"+IntegerToString(ot),OBJPROP_COLOR, clrRed);
   return(avg_price);
  }
//+----------------------------------------------------------------------------+
//| Расчет средней цены мин и макс ордеров(0)-buy (1)-sell ()-all              |
//+----------------------------------------------------------------------------+
double GetAveragePriceManMaxOrders(int ot =-1)
  {
   double avg_price_min_max_orders = 0;
     {
      for(int i = OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType()==ot||ot<0)
                 {
                  avg_price_min_max_orders = NormalizeDouble((PriceMaxOrder()*FindLastLots() + PriceMinOrder()*GetMinLotOrder())
                                             /(FindLastLots() + GetMinLotOrder()),Digits());
                  if(ObjectFind(0,"AveragePriceLineMinMaxOrders"+IntegerToString(ot))!=0)
                     ObjectCreate(0,"AveragePriceLineMinMaxOrders"+IntegerToString(ot),OBJ_HLINE, 0, 0, avg_price_min_max_orders);
                  else
                     ObjectSetDouble(0,"AveragePriceLineMinMaxOrders"+IntegerToString(ot),OBJPROP_PRICE, avg_price_min_max_orders);
                  if(ot==0)
                     ObjectSet("AveragePriceLineMinMaxOrders"+IntegerToString(ot),OBJPROP_COLOR, clrLime);
                  ObjectSet("AveragePriceLineMinMaxOrders", OBJPROP_STYLE, STYLE_DASH);
                  if(ot==1)
                     ObjectSet("AveragePriceLineMinMaxOrders"+IntegerToString(ot),OBJPROP_COLOR, clrRed);
                  ObjectSet("AveragePriceLineMinMaxOrders", OBJPROP_STYLE, STYLE_DASH);
                 }
              }
           }
        }
     }
   return(avg_price_min_max_orders);
  }
 
EVGENII SHELIPOV #:

Can you give me a hint? Here are two functions to calculate and display the price grid of orders and min/max orders. I have written the latter one by myself based on the previous function's analogue. The first one works as expected but the second one, the min/max order price, does not want to show on the chart.

Please help us to find the error. Please help me to find the error.

//+----------------------------------------------------------------------------+
//| Расчет средней цены мин и макс                                             |
//+----------------------------------------------------------------------------+
double GetAveragePriceManMaxOrders()
  {
   double avg_price_min_max_orders = 0;
   double avg=NormalizeDouble((PriceMaxOrder()*FindLastLots() + PriceMinOrder()*GetMinLotOrder())
                                             /(FindLastLots() + GetMinLotOrder()),Digits());
   if(avg>0)
     {
      avg_price_min_max_orders = avg;
      if(ObjectFind(0,"AveragePriceLineMinMaxOrders"+IntegerToString(ot))!=0)
         ObjectCreate(0,"AveragePriceLineMinMaxOrders"+IntegerToString(ot),OBJ_HLINE, 0, 0, avg_price_min_max_orders);
      else
         ObjectSetDouble(0,"AveragePriceLineMinMaxOrders"+IntegerToString(ot),OBJPROP_PRICE, avg_price_min_max_orders);
      if(ot==0)
         ObjectSet("AveragePriceLineMinMaxOrders"+IntegerToString(ot),OBJPROP_COLOR, clrLime);
         ObjectSet("AveragePriceLineMinMaxOrders", OBJPROP_STYLE, STYLE_DASH);
      if(ot==1)
         ObjectSet("AveragePriceLineMinMaxOrders"+IntegerToString(ot),OBJPROP_COLOR, clrRed);
         ObjectSet("AveragePriceLineMinMaxOrders", OBJPROP_STYLE, STYLE_DASH);
     }
   return(avg_price_min_max_orders);
  }
 
MakarFX #:

No, Makar, it's not working.

 
EVGENII SHELIPOV #:

No, Makar, it's not working.

List the functions

PriceMaxOrder()

FindLastLots()

PriceMinOrder()

GetMinLotOrder()

FindLastLots()

GetMinLotOrder()

 
MakarFX #:

List the functions

PriceMaxOrder()

FindLastLots()

PriceMinOrder()

GetMinLotOrder()

FindLastLots()

GetMinLotOrder()

//+----------------------------------------------------------------------------+
//| Определение размера лота последнего ордера                                 |
//+----------------------------------------------------------------------------+
double FindLastLots()
  {
   int oldticket;
   double oldlots = 0;
   ticket = 0;

   for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
     {
      if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
           {
            if(OrderType() == OP_BUY || OrderType() == OP_SELL)
              {
               oldticket = OrderTicket();
               if(oldticket > ticket)
                 {
                  ticket = oldticket;
                  oldlots = OrderLots();
                 }
              }
           }
        }
     }
   return(oldlots);
  }
//+----------------------------------------------------------------------------+
//| Определение цены открытия макс лота                                        |
//+----------------------------------------------------------------------------+
double PriceMaxOrder()
  {
   double max_price = 0 ;
   max_ticket = 0;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() > max_ticket)
                    {
                     max_ticket = OrderTicket();
                     max_price = OrderOpenPrice();
                    }
                 }
              }
           }
        }
     }
   return(max_price);
  }
//+----------------------------------------------------------------------------+
//| Определение размера лота минимального ордера в сетке                       |
//+----------------------------------------------------------------------------+
double GetMinLotOrder()
  {
   double min_lot_order = 0;
   min_ticket=INT_MAX;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() < min_ticket)
                    {
                     min_ticket = OrderTicket();
                     min_lot_order = OrderLots();
                    }
                 }
              }
           }
        }
     }
   return(min_lot_order);
  }
//+----------------------------------------------------------------------------+
//| Определение цены открытия мин лота                                         |
//+----------------------------------------------------------------------------+
double PriceMinOrder()
  {
   double min_price = 0;
   min_ticket=INT_MAX;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() < min_ticket)
                    {
                     min_ticket = OrderTicket();
                     min_price = OrderOpenPrice();
                    }
                 }
              }
           }
        }
     }
   return(min_price);
  }
 
EVGENII SHELIPOV #:
//+----------------------------------------------------------------------------+
//| Расчет средней цены мин и макс                                             |
//+----------------------------------------------------------------------------+
double GetAveragePriceManMaxOrders()
  {
   double avg_price_min_max_orders = 0;
   if(OrdersTotal()>1)
     {
      avg_price_min_max_orders = NormalizeDouble((PriceMaxOrder()*FindLastLots() + PriceMinOrder()
                                 *GetMinLotOrder())/(FindLastLots() + GetMinLotOrder()),Digits());
      if(ObjectFind(0,"AveragePriceLineMinMaxOrders")!=0)
         ObjectCreate(0,"AveragePriceLineMinMaxOrders",OBJ_HLINE, 0, 0, avg_price_min_max_orders);
      else
         ObjectSetDouble(0,"AveragePriceLineMinMaxOrders",OBJPROP_PRICE, avg_price_min_max_orders);
     }
   return(avg_price_min_max_orders);
  }
 
Galim_V #:

Greetings, the script works as I ask it to.

But if I move it

into the include file, it stops seeing the array of structures

the line

Doesn't work that way. I can't figure out how to fix it.

Move the structure to the include file too, above the class.

 
Alexey Viktorov #:

Move the structure to the include file too, above the class.

All right!) Thanks, Alexey!

 
MakarFX #:

Makar something is wrong. Love it thank you. I found a not-so-pretty option myself, but it works.

Reason: