Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // hyrStatus.mqh for MT4
- // by Copyright 2025 hyrolean
- //---------------------------------------------------------------------------
- // Local:: <DataFolder>\MQL4\Include\User\hyrStatus.mqh
- // URL:: https://pastebin.com/D9JP5Ujq
- //---------------------------------------------------------------------------
- #include <Arrays/ArrayObj.mqh>
- //---------------------------------------------------------------------------
- //状態チェック
- //-----
- enum ENUM_ORDERSORT {
- ORDERSORT_NONE=0,
- ORDERSORT_PRICE_ASCEND,
- ORDERSORT_PRICE_DESCEND,
- ORDERSORT_OPENTIME_ASCEND,
- ORDERSORT_OPENTIME_DESCEND,
- ORDERSORT_STOPLOSS_ASCEND,
- ORDERSORT_STOPLOSS_DESCEND
- };
- class COrderStatus : public CObject
- {
- private:
- static int inline DblComp(double v1, double v2) {
- if(v1==v2) return 0 ;
- return v1<v2 ? -1 : +1 ;
- }
- public:
- int Ticket;
- double Price;
- double Stoploss;
- double Takeprofit;
- double Lots;
- double Profit;
- datetime OpenTime;
- string Comment;
- int Tag;
- COrderStatus() : CObject() {
- Ticket = -1 ;
- Price = Stoploss = Takeprofit = Lots = Profit = 0.0 ;
- OpenTime = 0 ;
- Comment = "" ;
- Tag = 0 ;
- }
- COrderStatus(
- int Ticket_,
- double Price_,
- double Stoploss_,
- double Takeprofit_,
- double Lots_,
- double Profit_,
- datetime OpenTime_,
- string Comment_,
- int Tag_=0 ) : CObject() {
- Ticket = Ticket_;
- Price = Price_;
- Stoploss = Stoploss_;
- Takeprofit = Takeprofit_;
- Lots = Lots_;
- Profit = Profit_;
- OpenTime = OpenTime_;
- Comment = Comment_;
- Tag = Tag_ ;
- }
- virtual int Compare(const CObject *node,const int mode=0) const {
- COrderStatus *status = (COrderStatus*) node ;
- switch(mode) {
- case ORDERSORT_PRICE_ASCEND:
- return DblComp(Price,status.Price);
- case ORDERSORT_PRICE_DESCEND:
- return DblComp(status.Price,Price);
- case ORDERSORT_OPENTIME_ASCEND:
- return DblComp(OpenTime,status.OpenTime);
- case ORDERSORT_OPENTIME_DESCEND:
- return DblComp(status.OpenTime,OpenTime);
- case ORDERSORT_STOPLOSS_ASCEND:
- return DblComp(Stoploss,status.Stoploss);
- case ORDERSORT_STOPLOSS_DESCEND:
- return DblComp(status.Stoploss,Stoploss);
- }
- return 0;
- }
- };
- //-----
- class STATUS {
- private:
- void Init() {
- Positions = 0 ;
- PyramidPositions = 0 ;
- AveragePositions = 0 ;
- MinPrice = DBL_MAX ;
- MaxPrice = 0.0 ;
- MinPriceLots = 0.0 ;
- MaxPriceLots = 0.0 ;
- MinPriceProfit = 0.0 ;
- MaxPriceProfit = 0.0 ;
- MinPriceStoploss = 0.0 ;
- MaxPriceStoploss = 0.0 ;
- MinPriceTicket = 0 ;
- MaxPriceTicket = 0 ;
- MinStoploss = DBL_MAX ;
- MaxStoploss = 0.0 ;
- MinTakeprofit = DBL_MAX ;
- MaxTakeprofit = 0.0 ;
- MinLots = DBL_MAX ;
- MaxLots = 0.0 ;
- MinOpenTime = TimeCurrent() ;
- MaxOpenTime = 0 ;
- PyramidMinLots = DBL_MAX ;
- PyramidMaxLots = 0.0 ;
- AverageMinLots = DBL_MAX ;
- AverageMaxLots = 0.0 ;
- BorderPrice = 0.0 ;
- TotalPrices = 0.0 ;
- TotalLots = 0.0 ;
- GrossProfit = 0.0 ;
- PositiveProfit = 0.0 ;
- CurrentPrices = 0.0 ;
- MedianPrices = 0.0 ;
- Tag = 0 ;
- Orders.Clear();
- }
- void Finish() {
- if(OrdersLastSort!=ORDERSORT_NONE)
- Orders.Sort(OrdersLastSort);
- if(TotalLots>0) {
- BorderPrice = TotalPrices / TotalLots ;
- }
- }
- protected:
- CArrayObj *Orders;
- ENUM_ORDERSORT OrdersLastSort;
- virtual void CheckInit() { Init() ; }
- virtual void CheckOrder(COrderStatus *OrderStatus) {}
- virtual void CheckFinish() { Finish() ; }
- public:
- int Type, Magic;
- int Positions, PyramidPositions, AveragePositions;
- double MinPrice, MaxPrice, BorderPrice;
- double MinPriceLots, MaxPriceLots;
- double MinPriceProfit, MaxPriceProfit;
- double MinPriceStoploss, MaxPriceStoploss;
- int MinPriceTicket, MaxPriceTicket;
- double MinStoploss, MaxStoploss;
- double MinTakeprofit, MaxTakeprofit;
- double MinLots, MaxLots;
- datetime MinOpenTime, MaxOpenTime;
- double PyramidMinLots, PyramidMaxLots;
- double AverageMinLots, AverageMaxLots;
- double TotalPrices, TotalLots;
- double CurrentPrices, MedianPrices;
- double GrossProfit;
- double PositiveProfit;
- int Tag;
- STATUS(int Type_=OP_BUY, int Magic_=0) {
- Orders = new CArrayObj();
- Init();
- Type=Type_;
- Magic=Magic_;
- OrdersLastSort=ORDERSORT_NONE;
- }
- ~STATUS() {
- delete Orders;
- }
- virtual void Check(int type, int magic) {
- Type=type;
- Magic=magic;
- CheckInit();
- double halfSpread = (Ask-Bid)/2.0;
- for(int i=OrdersTotal()-1 ; i>=0 ; i--) {
- if( OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() ) {
- if(magic==OrderMagicNumber()) {
- if(type==OrderType()) {
- int orderTicket = OrderTicket() ;
- double orderPrice = OrderOpenPrice();
- double orderStoploss = OrderStopLoss();
- double orderTakeprofit = OrderTakeProfit();
- double orderLots = OrderLots();
- double orderProfit = OrderProfit() ;
- datetime orderOpenTime = OrderOpenTime() ;
- string orderComment = OrderComment();
- COrderStatus *orderStatus = new COrderStatus(
- orderTicket,
- orderPrice,
- orderStoploss,
- orderTakeprofit,
- orderLots,
- orderProfit,
- orderOpenTime,
- orderComment ) ;
- Orders.Add(orderStatus);
- GrossProfit += orderProfit;
- if(orderProfit>0.0)
- PositiveProfit += orderProfit;
- if(orderPrice<MinPrice) {
- MinPrice = orderPrice ;
- MinPriceLots = orderLots ;
- MinPriceProfit = orderProfit ;
- MinPriceStoploss = orderStoploss ;
- MinPriceTicket = orderTicket ;
- }
- if(orderPrice>MaxPrice) {
- MaxPrice = orderPrice ;
- MaxPriceLots = orderLots ;
- MaxPriceProfit = orderProfit ;
- MaxPriceStoploss = orderStoploss ;
- MaxPriceTicket = orderTicket ;
- }
- if(orderStoploss<MinStoploss) MinStoploss = orderStoploss;
- if(orderStoploss>MaxStoploss) MaxStoploss = orderStoploss;
- if(orderTakeprofit<MinTakeprofit) MinTakeprofit = orderTakeprofit;
- if(orderTakeprofit>MaxTakeprofit) MaxTakeprofit = orderTakeprofit;
- if(orderLots<MinLots) MinLots = orderLots;
- if(orderLots>MaxLots) MaxLots = orderLots;
- TotalPrices += orderPrice*orderLots ;
- TotalLots += orderLots;
- if(type==OP_BUY) {
- CurrentPrices += (Bid - orderPrice) * orderLots ;
- MedianPrices += (orderPrice + halfSpread) * orderLots ;
- }
- else if(type==OP_SELL) {
- CurrentPrices += (orderPrice - Ask) * orderLots ;
- MedianPrices += (orderPrice - halfSpread) * orderLots ;
- }
- if(MaxOpenTime<orderOpenTime) MaxOpenTime=orderOpenTime;
- if(MinOpenTime>orderOpenTime) MinOpenTime=orderOpenTime;
- if(StringFind(orderComment,"PRMD")>=0) {
- if(orderLots>PyramidMaxLots)
- PyramidMaxLots = orderLots ;
- if(orderLots<PyramidMinLots)
- PyramidMinLots = orderLots ;
- PyramidPositions++;
- }
- if(StringFind(orderComment,"AVRG")>=0) {
- if(orderLots>AverageMaxLots)
- AverageMaxLots = orderLots ;
- if(orderLots<AverageMinLots)
- AverageMinLots = orderLots ;
- AveragePositions++;
- }
- Positions++;
- CheckOrder(orderStatus);
- }
- }
- }
- }
- CheckFinish();
- }
- void OrdersSort(ENUM_ORDERSORT mode) {
- if(mode!=ORDERSORT_NONE&&mode!=OrdersLastSort)
- Orders.Sort(mode);
- OrdersLastSort = mode ;
- }
- int OrdersCount() const { return Orders.Total(); }
- COrderStatus *OrdersAt(int index) const
- { return (COrderStatus*) Orders.At(index); }
- int OrdersRange(const COrderStatus *Order, int &First, int &Last) const {
- if(OrdersLastSort==ORDERSORT_NONE) return -1 ;
- int first = Orders.SearchFirst(Order);
- if(first<0) return 0 ;
- int last = Orders.SearchLast(Order);
- if(last<0) return 0 ;
- First = first ;
- Last = last ;
- return Last-First+1 ;
- }
- /*
- double ProfitPrice(double borderPrice=0.0) const {
- if(borderPrice==0.0) borderPrice = BorderPrice;
- if(borderPrice>0.0) {
- //double stride = PyramidingStride * Point ;
- switch(Type) {
- case OP_BUY: {
- double bar = MathMax(MaxPrice,BuyMaxPrice)-borderPrice ;
- if(bar>0.0) {
- return bar * (ProfitRatePer / 100.0) + borderPrice ;
- }
- break;
- }
- case OP_SELL: {
- double bar = MathMin(MinPrice,SellMinPrice)-borderPrice ;
- if(bar<0.0) {
- return bar * (ProfitRatePer / 100.0) + borderPrice ;
- }
- break;
- }
- }
- }
- return borderPrice ;
- }
- */
- double MedianBorderPrice(const STATUS &revStatus, double lossPrices=0.0) const
- {
- double medianLots = Type==OP_BUY ? TotalLots : -TotalLots ;
- medianLots += revStatus.Type==OP_BUY ? revStatus.TotalLots: -revStatus.TotalLots ;
- if(Type==OP_BUY?medianLots>0.0:medianLots<0.0) {
- double medianPrices = Type==OP_BUY ? MedianPrices-lossPrices : -MedianPrices+lossPrices ;
- medianPrices += revStatus.Type==OP_BUY ? revStatus.MedianPrices: -revStatus.MedianPrices ;
- double medianBorder = medianPrices/medianLots ;
- switch(Type) {
- case OP_BUY:
- medianBorder -= (Ask-Bid)/2.0;
- break;
- case OP_SELL:
- medianBorder += (Ask-Bid)/2.0;
- break;
- }
- return medianBorder ;
- }
- return 0.0;
- }
- };
- //-----
- int inline CheckStatus(int type, int magic, STATUS *status)
- {
- status.Check(type,magic);
- return status.Positions;
- }
- //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment