HYROLEAN

hyrStatus.mqh for MT4

Mar 5th, 2026 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.14 KB | Money | 0 0
  1. // hyrStatus.mqh for MT4
  2. //   by Copyright 2025 hyrolean
  3. //---------------------------------------------------------------------------
  4. // Local:: <DataFolder>\MQL4\Include\User\hyrStatus.mqh
  5. // URL:: https://pastebin.com/D9JP5Ujq
  6. //---------------------------------------------------------------------------
  7. #include <Arrays/ArrayObj.mqh>
  8. //---------------------------------------------------------------------------
  9. //状態チェック
  10. //-----
  11. enum ENUM_ORDERSORT {
  12.   ORDERSORT_NONE=0,
  13.   ORDERSORT_PRICE_ASCEND,
  14.   ORDERSORT_PRICE_DESCEND,
  15.   ORDERSORT_OPENTIME_ASCEND,
  16.   ORDERSORT_OPENTIME_DESCEND,
  17.   ORDERSORT_STOPLOSS_ASCEND,
  18.   ORDERSORT_STOPLOSS_DESCEND
  19. };
  20. class COrderStatus : public CObject
  21. {
  22. private:
  23.   static int inline DblComp(double v1, double v2) {
  24.     if(v1==v2) return 0 ;
  25.     return v1<v2 ? -1 : +1 ;
  26.   }
  27. public:
  28.   int Ticket;
  29.   double Price;
  30.   double Stoploss;
  31.   double Takeprofit;
  32.   double Lots;
  33.   double Profit;
  34.   datetime OpenTime;
  35.   string Comment;
  36.   int Tag;
  37.   COrderStatus() : CObject() {
  38.     Ticket = -1 ;
  39.     Price = Stoploss = Takeprofit = Lots = Profit = 0.0 ;
  40.     OpenTime = 0 ;
  41.     Comment = "" ;
  42.     Tag = 0 ;
  43.   }
  44.   COrderStatus(
  45.       int Ticket_,
  46.       double Price_,
  47.       double Stoploss_,
  48.       double Takeprofit_,
  49.       double Lots_,
  50.       double Profit_,
  51.       datetime OpenTime_,
  52.       string Comment_,
  53.       int Tag_=0 ) : CObject() {
  54.     Ticket = Ticket_;
  55.     Price = Price_;
  56.     Stoploss = Stoploss_;
  57.     Takeprofit = Takeprofit_;
  58.     Lots = Lots_;
  59.     Profit = Profit_;
  60.     OpenTime = OpenTime_;
  61.     Comment = Comment_;
  62.     Tag = Tag_ ;
  63.   }
  64.   virtual int Compare(const CObject *node,const int mode=0) const {
  65.     COrderStatus *status = (COrderStatus*) node ;
  66.     switch(mode) {
  67.       case ORDERSORT_PRICE_ASCEND:
  68.         return DblComp(Price,status.Price);
  69.       case ORDERSORT_PRICE_DESCEND:
  70.         return DblComp(status.Price,Price);
  71.       case ORDERSORT_OPENTIME_ASCEND:
  72.         return DblComp(OpenTime,status.OpenTime);
  73.       case ORDERSORT_OPENTIME_DESCEND:
  74.         return DblComp(status.OpenTime,OpenTime);
  75.       case ORDERSORT_STOPLOSS_ASCEND:
  76.         return DblComp(Stoploss,status.Stoploss);
  77.       case ORDERSORT_STOPLOSS_DESCEND:
  78.         return DblComp(status.Stoploss,Stoploss);
  79.     }
  80.     return 0;
  81.   }
  82. };
  83. //-----
  84. class STATUS {
  85. private:
  86.   void Init() {
  87.     Positions = 0 ;
  88.     PyramidPositions = 0 ;
  89.     AveragePositions = 0 ;
  90.     MinPrice = DBL_MAX ;
  91.     MaxPrice = 0.0 ;
  92.     MinPriceLots = 0.0 ;
  93.     MaxPriceLots = 0.0 ;
  94.     MinPriceProfit = 0.0 ;
  95.     MaxPriceProfit = 0.0 ;
  96.     MinPriceStoploss = 0.0 ;
  97.     MaxPriceStoploss = 0.0 ;
  98.     MinPriceTicket = 0 ;
  99.     MaxPriceTicket = 0 ;
  100.     MinStoploss = DBL_MAX ;
  101.     MaxStoploss = 0.0 ;
  102.     MinTakeprofit = DBL_MAX ;
  103.     MaxTakeprofit = 0.0 ;
  104.     MinLots = DBL_MAX ;
  105.     MaxLots = 0.0 ;
  106.     MinOpenTime = TimeCurrent() ;
  107.     MaxOpenTime = 0 ;
  108.     PyramidMinLots = DBL_MAX ;
  109.     PyramidMaxLots = 0.0 ;
  110.     AverageMinLots = DBL_MAX ;
  111.     AverageMaxLots = 0.0 ;
  112.     BorderPrice = 0.0 ;
  113.     TotalPrices = 0.0 ;
  114.     TotalLots = 0.0 ;
  115.     GrossProfit = 0.0 ;
  116.     PositiveProfit = 0.0 ;
  117.     CurrentPrices = 0.0 ;
  118.     MedianPrices = 0.0 ;
  119.     Tag = 0 ;
  120.     Orders.Clear();
  121.   }
  122.   void Finish() {
  123.     if(OrdersLastSort!=ORDERSORT_NONE)
  124.       Orders.Sort(OrdersLastSort);
  125.     if(TotalLots>0) {
  126.       BorderPrice = TotalPrices / TotalLots ;
  127.     }
  128.   }
  129. protected:
  130.   CArrayObj *Orders;
  131.   ENUM_ORDERSORT OrdersLastSort;
  132.   virtual void CheckInit() { Init() ; }
  133.   virtual void CheckOrder(COrderStatus *OrderStatus) {}
  134.   virtual void CheckFinish() { Finish() ; }
  135. public:
  136.   int Type, Magic;
  137.   int Positions, PyramidPositions, AveragePositions;
  138.   double MinPrice, MaxPrice, BorderPrice;
  139.   double MinPriceLots, MaxPriceLots;
  140.   double MinPriceProfit, MaxPriceProfit;
  141.   double MinPriceStoploss, MaxPriceStoploss;
  142.   int MinPriceTicket, MaxPriceTicket;
  143.   double MinStoploss, MaxStoploss;
  144.   double MinTakeprofit, MaxTakeprofit;
  145.   double MinLots, MaxLots;
  146.   datetime MinOpenTime, MaxOpenTime;
  147.   double PyramidMinLots, PyramidMaxLots;
  148.   double AverageMinLots, AverageMaxLots;
  149.   double TotalPrices, TotalLots;
  150.   double CurrentPrices, MedianPrices;
  151.   double GrossProfit;
  152.   double PositiveProfit;
  153.   int Tag;
  154.   STATUS(int Type_=OP_BUY, int Magic_=0) {
  155.     Orders = new CArrayObj();
  156.     Init();
  157.     Type=Type_;
  158.     Magic=Magic_;
  159.     OrdersLastSort=ORDERSORT_NONE;
  160.   }
  161.   ~STATUS() {
  162.     delete Orders;
  163.   }
  164.   virtual void Check(int type, int magic) {
  165.     Type=type;
  166.     Magic=magic;
  167.     CheckInit();
  168.     double halfSpread = (Ask-Bid)/2.0;
  169.     for(int i=OrdersTotal()-1 ; i>=0 ; i--) {
  170.       if( OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() ) {
  171.         if(magic==OrderMagicNumber()) {
  172.           if(type==OrderType()) {
  173.             int orderTicket = OrderTicket() ;
  174.             double orderPrice = OrderOpenPrice();
  175.             double orderStoploss = OrderStopLoss();
  176.             double orderTakeprofit = OrderTakeProfit();
  177.             double orderLots = OrderLots();
  178.             double orderProfit = OrderProfit() ;
  179.             datetime orderOpenTime = OrderOpenTime() ;
  180.             string orderComment = OrderComment();
  181.             COrderStatus *orderStatus = new COrderStatus(
  182.               orderTicket,
  183.               orderPrice,
  184.               orderStoploss,
  185.               orderTakeprofit,
  186.               orderLots,
  187.               orderProfit,
  188.               orderOpenTime,
  189.               orderComment ) ;
  190.             Orders.Add(orderStatus);
  191.             GrossProfit += orderProfit;
  192.             if(orderProfit>0.0)
  193.               PositiveProfit += orderProfit;
  194.             if(orderPrice<MinPrice) {
  195.               MinPrice = orderPrice ;
  196.               MinPriceLots = orderLots ;
  197.               MinPriceProfit = orderProfit ;
  198.               MinPriceStoploss = orderStoploss ;
  199.               MinPriceTicket = orderTicket ;
  200.             }
  201.             if(orderPrice>MaxPrice) {
  202.               MaxPrice = orderPrice ;
  203.               MaxPriceLots = orderLots ;
  204.               MaxPriceProfit = orderProfit ;
  205.               MaxPriceStoploss = orderStoploss ;
  206.               MaxPriceTicket = orderTicket ;
  207.             }
  208.             if(orderStoploss<MinStoploss) MinStoploss = orderStoploss;
  209.             if(orderStoploss>MaxStoploss) MaxStoploss = orderStoploss;
  210.             if(orderTakeprofit<MinTakeprofit) MinTakeprofit = orderTakeprofit;
  211.             if(orderTakeprofit>MaxTakeprofit) MaxTakeprofit = orderTakeprofit;
  212.             if(orderLots<MinLots) MinLots = orderLots;
  213.             if(orderLots>MaxLots) MaxLots = orderLots;
  214.             TotalPrices += orderPrice*orderLots ;
  215.             TotalLots += orderLots;
  216.             if(type==OP_BUY) {
  217.               CurrentPrices += (Bid - orderPrice) * orderLots ;
  218.               MedianPrices += (orderPrice + halfSpread) * orderLots ;
  219.             }
  220.             else if(type==OP_SELL) {
  221.               CurrentPrices += (orderPrice - Ask) * orderLots ;
  222.               MedianPrices += (orderPrice - halfSpread) * orderLots ;
  223.             }
  224.             if(MaxOpenTime<orderOpenTime) MaxOpenTime=orderOpenTime;
  225.             if(MinOpenTime>orderOpenTime) MinOpenTime=orderOpenTime;
  226.             if(StringFind(orderComment,"PRMD")>=0) {
  227.               if(orderLots>PyramidMaxLots)
  228.                 PyramidMaxLots = orderLots ;
  229.               if(orderLots<PyramidMinLots)
  230.                 PyramidMinLots = orderLots ;
  231.               PyramidPositions++;
  232.             }
  233.             if(StringFind(orderComment,"AVRG")>=0) {
  234.               if(orderLots>AverageMaxLots)
  235.                 AverageMaxLots = orderLots ;
  236.               if(orderLots<AverageMinLots)
  237.                 AverageMinLots = orderLots ;
  238.               AveragePositions++;
  239.             }
  240.             Positions++;
  241.             CheckOrder(orderStatus);
  242.           }
  243.         }
  244.       }
  245.     }
  246.     CheckFinish();
  247.   }
  248.   void OrdersSort(ENUM_ORDERSORT mode) {
  249.     if(mode!=ORDERSORT_NONE&&mode!=OrdersLastSort)
  250.       Orders.Sort(mode);
  251.     OrdersLastSort = mode ;
  252.   }
  253.   int OrdersCount() const { return Orders.Total(); }
  254.   COrderStatus *OrdersAt(int index) const
  255.   { return (COrderStatus*) Orders.At(index); }
  256.   int OrdersRange(const COrderStatus *Order, int &First, int &Last) const {
  257.     if(OrdersLastSort==ORDERSORT_NONE) return -1 ;
  258.     int first = Orders.SearchFirst(Order);
  259.     if(first<0) return 0 ;
  260.     int last = Orders.SearchLast(Order);
  261.     if(last<0) return 0 ;
  262.     First = first ;
  263.     Last = last ;
  264.     return Last-First+1 ;
  265.   }
  266.   /*
  267.   double ProfitPrice(double borderPrice=0.0) const {
  268.     if(borderPrice==0.0) borderPrice = BorderPrice;
  269.     if(borderPrice>0.0) {
  270.       //double stride = PyramidingStride * Point ;
  271.       switch(Type) {
  272.         case OP_BUY: {
  273.           double bar = MathMax(MaxPrice,BuyMaxPrice)-borderPrice ;
  274.           if(bar>0.0) {
  275.             return bar * (ProfitRatePer / 100.0) + borderPrice ;
  276.           }
  277.           break;
  278.         }
  279.         case OP_SELL: {
  280.           double bar = MathMin(MinPrice,SellMinPrice)-borderPrice ;
  281.           if(bar<0.0) {
  282.             return bar * (ProfitRatePer / 100.0) + borderPrice ;
  283.           }
  284.           break;
  285.         }
  286.       }
  287.     }
  288.     return borderPrice ;
  289.   }
  290.   */
  291.   double MedianBorderPrice(const STATUS &revStatus, double lossPrices=0.0) const
  292.   {
  293.     double medianLots = Type==OP_BUY ? TotalLots : -TotalLots ;
  294.     medianLots += revStatus.Type==OP_BUY ? revStatus.TotalLots: -revStatus.TotalLots ;
  295.     if(Type==OP_BUY?medianLots>0.0:medianLots<0.0) {
  296.       double medianPrices = Type==OP_BUY ? MedianPrices-lossPrices : -MedianPrices+lossPrices ;
  297.       medianPrices += revStatus.Type==OP_BUY ? revStatus.MedianPrices: -revStatus.MedianPrices ;
  298.       double medianBorder = medianPrices/medianLots ;
  299.       switch(Type) {
  300.         case OP_BUY:
  301.             medianBorder -= (Ask-Bid)/2.0;
  302.             break;
  303.         case OP_SELL:
  304.             medianBorder += (Ask-Bid)/2.0;
  305.             break;
  306.       }
  307.       return medianBorder ;
  308.     }
  309.     return 0.0;
  310.   }
  311. };
  312. //-----
  313. int inline CheckStatus(int type, int magic, STATUS *status)
  314. {
  315.   status.Check(type,magic);
  316.   return status.Positions;
  317. }
  318. //---------------------------------------------------------------------------
  319.  
Tags: Mt4
Advertisement
Add Comment
Please, Sign In to add comment