Sfoglia il codice sorgente

Ticket: 5360 Partial Closing Logic Updated

Huzaifa-MQLDev 1 mese fa
parent
commit
2094935e8f

BIN
client/localHedgingClientCopierMt5.ex5


+ 116 - 30
client/localHedgingClientCopierMt5.mq5

@@ -43,9 +43,11 @@ struct order
43 43
    int               contractSize;
44 44
    string            comments;
45 45
    int               magicNo;
46
+   double            closedPercentageOfTrade;
46 47
                      order()
47 48
      {
48 49
       ticket =-1;
50
+      closedPercentageOfTrade = 0;
49 51
      }
50 52
   };
51 53
 
@@ -214,7 +216,7 @@ void OnTimer()
214 216
    read_history_file();
215 217
 
216 218
    readData();
217
-   writeDataClientCof();
219
+//writeDataClientCof();
218 220
   }
219 221
 //+------------------------------------------------------------------+
220 222
 //|                                                                  |
@@ -672,7 +674,7 @@ void readData()
672 674
                string Order_specs [];
673 675
                StringSplit(OrderToRead,StringGetCharacter(",",0),Order_specs);
674 676
                Print("Array Size: ", ArraySize(Order_specs));
675
-               if(ArraySize(Order_specs) == 11 || ArraySize(Order_specs) == 12)
677
+               if(ArraySize(Order_specs) == 12 || ArraySize(Order_specs) == 13)
676 678
                  {
677 679
                   string symbol = getSymbol(Order_specs[1]);//prefix+Order_specs[1]+suffix;
678 680
                   int orderType=StringToInteger(Order_specs[2]);
@@ -692,23 +694,24 @@ void readData()
692 694
                      //Saving master order ticket to order magic number
693 695
                      order_array[ArraySize(order_array)-1].magicNo = StringToInteger(Order_specs[7]);
694 696
                      order_array[ArraySize(order_array)-1].accountBalance=StringToDouble(Order_specs[9]);
695
-                     order_array[ArraySize(order_array)-1].contractSize=StringToInteger(Order_specs[10]);
697
+                     order_array[ArraySize(order_array)-1].closedPercentageOfTrade  = StringToDouble(Order_specs[10]);
698
+                     order_array[ArraySize(order_array)-1].contractSize=StringToInteger(Order_specs[11]);
696 699
 
697
-                     if(ArraySize(Order_specs) == 12)
698
-                        order_array[ArraySize(order_array)-1].comments=Order_specs[11];
700
+                     if(ArraySize(Order_specs) == 13)
701
+                        order_array[ArraySize(order_array)-1].comments=Order_specs[12];
699 702
                      else
700 703
                         order_array[ArraySize(order_array)-1].comments="";
701 704
 
702
-                     if(ArraySize(Order_specs) == 12)
705
+                     if(ArraySize(Order_specs) == 13)
703 706
                        {
704
-                        StringReplace(Order_specs[11],"from #","");
705
-                        StringReplace(Order_specs[11]," ","");
707
+                        StringReplace(Order_specs[12],"from #","");
708
+                        StringReplace(Order_specs[12]," ","");
706 709
 
707
-                        Print(" Order_specs[11] ",Order_specs[11]," order_array[ArraySize(order_array)-1].ticket ",order_array[ArraySize(order_array)-1].ticket);
710
+                        Print(" Order_specs[12] ",Order_specs[12]," order_array[ArraySize(order_array)-1].ticket ",order_array[ArraySize(order_array)-1].ticket);
708 711
 
709 712
                         for(int l=0; l<orderCount; l++)
710 713
                           {
711
-                           if(cod[l].magic == int(Order_specs[11]))
714
+                           if(cod[l].magic == int(Order_specs[12]))
712 715
                              {
713 716
                               cod[l].magic = order_array[ArraySize(order_array)-1].ticket;
714 717
                              }
@@ -716,7 +719,12 @@ void readData()
716 719
                        }
717 720
 
718 721
 
719
-                     Print(" Symbol: ",order_array[ArraySize(order_array)-1].symbol,"Open Price: ",order_array[ArraySize(order_array)-1].price," Lot: ",  order_array[ArraySize(order_array)-1].lots,"OPen time: ",order_array[ArraySize(order_array)-1].opentime);
722
+                     Print(" Symbol: ", order_array[ArraySize(order_array)-1].symbol,
723
+                           " Open Price: ", order_array[ArraySize(order_array)-1].price,
724
+                           " Lot: ", order_array[ArraySize(order_array)-1].lots,
725
+                           " Open time: ", order_array[ArraySize(order_array)-1].opentime,
726
+                           " Closed %: ", order_array[ArraySize(order_array)-1].closedPercentageOfTrade);
727
+
720 728
                      ArrayResize(order_array,ArraySize(order_array)+1);
721 729
                      addToMarketWatch(symbol);
722 730
                     }
@@ -773,6 +781,10 @@ void readData()
773 781
                //Print(" ------------------------------------------  Ticket: ", order_array[ArraySize(order_array)-1].ticket, " Magic Number: ", order_array[ArraySize(order_array)-1].magicNo, " Array Size: ", ArraySize(order_array), " Index: ", i);
774 782
                int trade_taken = 2;
775 783
                int partialCloseOrderPosition=-1;
784
+               double percentageToClose = 0;
785
+               double closedPercentage = 0.0;
786
+               double currentLot  = 0.0;
787
+               double totalLot = 0;
776 788
                for(int j=0; j<orderCount; j++)
777 789
                  {
778 790
                   //if(OrderSelect(cod[j].ticket,SELECT_BY_POS))
@@ -782,12 +794,25 @@ void readData()
782 794
                        {
783 795
                         trade_taken = 0;
784 796
                        }
797
+                     if(PositionSelectByTicket(cod[j].ticket))
798
+                       {
799
+                        double sumOfClosed = dealOutLotsCheck(0, PositionGetInteger(POSITION_TICKET));
800
+                        currentLot  = PositionGetDouble(POSITION_VOLUME);
801
+
802
+                        totalLot = sumOfClosed + currentLot;
803
+
804
+                        if(totalLot > 0.0)
805
+                           closedPercentage = (sumOfClosed / totalLot) * 100.0;
806
+                       }
807
+                     //Print(" Magic: ", cod[j].magic, " Ticket: ", order_array[i].ticket, " Master Lot: ", cod[j].masterLot, " Client Lot: ", order_array[i].lots);
785 808
                      if(cod[j].magic==order_array[i].ticket)
786 809
                        {
787
-                        if(cod[j].masterLot != order_array[i].lots)
810
+                        if(closedPercentage < order_array[i].closedPercentageOfTrade)
788 811
                           {
789
-                           Print(" Cod Lot Size: ", cod[j].masterLot, " Order Array Lot: ", order_array[i].lots);
790
-                           Print("Order is partial close:",cod[j].magic);
812
+                           percentageToClose = MathAbs(order_array[i].closedPercentageOfTrade - closedPercentage);
813
+                           //Print(" Cod Lot Size: ", cod[j].masterLot, " Order Array Lot: ", order_array[i].lots);
814
+                           Print("Closed % = ", closedPercentage, " Master Ticket Percentage: ", order_array[i].closedPercentageOfTrade);
815
+                           Print("Order is partial close:",cod[j].magic, " Percentage To Be Closed: ", percentageToClose);
791 816
                            trade_taken=1;
792 817
                            partialCloseOrderPosition=j;
793 818
                           }
@@ -802,29 +827,48 @@ void readData()
802 827
                      if(partialCloseOrderPosition!=-1)
803 828
                        {
804 829
                         bool isOrderClose=false;
805
-                        if(cod[partialCloseOrderPosition].masterLot <= 0)
830
+                        //if(cod[partialCloseOrderPosition].masterLot <= 0)
831
+                        //  {
832
+                        //   Print("Error: Invalid masterLot for partial close");
833
+                        //   continue;
834
+                        //  }
835
+                        //if(OrderSelect(partialCloseOrderPosition,SELECT_BY_TICKET))
806 836
                           {
807
-                           Print("Error: Invalid masterLot for partial close");
808
-                           continue;
837
+                           //Print("Master lot: ",order_array[i].lots," last save lot: ",cod[partialCloseOrderPosition].masterLot);
838
+                           //double lotClosePer = NormalizeDouble(100 - (order_array[i].lots/cod[partialCloseOrderPosition].masterLot*100),2);
839
+                           //Print("Order in master is closed in percentage: ",lotClosePer," last client lot: ",cod[partialCloseOrderPosition].lots);
840
+                           //double closeLot=NormalizeDouble(lotClosePer/100 *cod[partialCloseOrderPosition].lots,2);
841
+                           //Print("Close lot in client: ",closeLot);
842
+                           //if(!trade.PositionClosePartial(cod[partialCloseOrderPosition].ticket,closeLot))
843
+                           //  {
844
+                           //   Print("Partial order did not close:",cod[partialCloseOrderPosition].ticket);
845
+                           //  }
846
+                           //else
847
+                           //  {
848
+                           // isOrderClose=true;
849
+                           //   cod[partialCloseOrderPosition].lots=cod[partialCloseOrderPosition].lots-closeLot; //PositionGetDouble(POSITION_VOLUME);
850
+                           //   cod[partialCloseOrderPosition].masterLot = order_array[i].lots;
851
+                           //  }
809 852
                           }
810
-                        //if(OrderSelect(partialCloseOrderPosition,SELECT_BY_TICKET))
853
+                        double lotClosePer = NormalizeDouble((totalLot * percentageToClose) / 100,2);
854
+                        if(percentageToClose > 0)
811 855
                           {
812
-                           Print("Master lot: ",order_array[i].lots," last save lot: ",cod[partialCloseOrderPosition].masterLot);
813
-                           double lotClosePer = NormalizeDouble(100 - (order_array[i].lots/cod[partialCloseOrderPosition].masterLot*100),2);
814
-                           Print("Order in master is closed in percentage: ",lotClosePer," last client lot: ",cod[partialCloseOrderPosition].lots);
815
-                           double closeLot=NormalizeDouble(lotClosePer/100 *cod[partialCloseOrderPosition].lots,2);
816
-                           Print("Close lot in client: ",closeLot);
817
-                           if(!trade.PositionClosePartial(cod[partialCloseOrderPosition].ticket,closeLot))
856
+                           if(!trade.PositionClosePartial(cod[partialCloseOrderPosition].ticket, lotClosePer))
818 857
                              {
819
-                              Print("Partial order did not close:",cod[partialCloseOrderPosition].ticket);
858
+                              // ExpertRemove();
859
+                              Print("Partial order did not close:",cod[partialCloseOrderPosition].ticket, " Lot To Close is: ", lotClosePer);
820 860
                              }
821 861
                            else
822 862
                              {
823 863
                               isOrderClose=true;
824
-                              cod[partialCloseOrderPosition].lots=cod[partialCloseOrderPosition].lots-closeLot; //PositionGetDouble(POSITION_VOLUME);
825
-                              cod[partialCloseOrderPosition].masterLot = order_array[i].lots;
864
+                              //cod[partialCloseOrderPosition].lots=cod[partialCloseOrderPosition].lots-closeLot; //PositionGetDouble(POSITION_VOLUME);
865
+                              //cod[partialCloseOrderPosition].masterLot = order_array[i].lots;
866
+                              Print("Order Partial Close. currentLot Was =", currentLot,
867
+                                    " Percentage to close = ", percentageToClose,
868
+                                    " Lot I am Going To close = ", lotClosePer);
826 869
                              }
827 870
                           }
871
+
828 872
                        }
829 873
                     }
830 874
                   else
@@ -973,13 +1017,13 @@ void readData()
973 1017
                         int digitss = getDoubleSymbolInfo(PositionGetString(POSITION_SYMBOL), "digits");
974 1018
                         trade_close = false;
975 1019
                         // if(PositionGetDouble(POSITION_TP)!=order_array[j].takeprofit || PositionGetDouble(POSITION_SL)!=order_array[j].stoploss)
976
-                        if(NormalizeDouble(cod[i].takeprofit, digitss) != order_array[j].takeprofit || NormalizeDouble(cod[i].stoploss, digitss) != order_array[j].stoploss)
1020
+                        if(NormalizeDouble(cod[i].takeprofit, digitss) != NormalizeDouble(order_array[j].takeprofit, digitss) || NormalizeDouble(cod[i].stoploss, digitss) != NormalizeDouble(order_array[j].stoploss, digitss))
977 1021
                           {
978 1022
                            if(trade.PositionModify(PositionGetInteger(POSITION_TICKET),order_array[j].stoploss,order_array[j].takeprofit))
979 1023
                              {
980 1024
                               Print("Order modified ticket:",PositionGetInteger(POSITION_TICKET));
981
-                              cod[i].takeprofit=order_array[j].takeprofit;
982
-                              cod[i].stoploss=order_array[j].stoploss;
1025
+                              cod[i].takeprofit=NormalizeDouble(order_array[j].takeprofit, digitss);
1026
+                              cod[i].stoploss=NormalizeDouble(order_array[j].stoploss, digitss);
983 1027
                              }
984 1028
                            else
985 1029
                              {
@@ -1130,6 +1174,32 @@ void deleteAllArray()
1130 1174
 //+------------------------------------------------------------------+
1131 1175
 //|                                                                  |
1132 1176
 //+------------------------------------------------------------------+
1177
+double dealOutLotsCheck(datetime cutOffTimeIs, ulong positionId)
1178
+  {
1179
+   double sumOfLots = 0.0;
1180
+
1181
+   if(HistorySelect(cutOffTimeIs,LONG_MAX))
1182
+     {
1183
+      int totalDeals = HistoryDealsTotal();
1184
+
1185
+      for(int i = 0; i < totalDeals; i++)
1186
+        {
1187
+         ulong dealTicket      = HistoryDealGetTicket(i);
1188
+         ulong dealPositionID  = (ulong)HistoryDealGetInteger(dealTicket, DEAL_POSITION_ID);
1189
+
1190
+         if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT
1191
+            && dealPositionID == positionId)
1192
+           {
1193
+            sumOfLots += HistoryDealGetDouble(dealTicket, DEAL_VOLUME);
1194
+           }
1195
+        }
1196
+     }
1197
+
1198
+   return sumOfLots;
1199
+  }
1200
+//+------------------------------------------------------------------+
1201
+//|                                                                  |
1202
+//+------------------------------------------------------------------+
1133 1203
 bool addOrderMaster(ulong ticket,ulong magic,double lot, string calledBy)
1134 1204
   {
1135 1205
    Print("in addOrderMaster: ", ticket);
@@ -1173,6 +1243,21 @@ bool addOrderMaster(ulong ticket,ulong magic,double lot, string calledBy)
1173 1243
                cod[i].comments=PositionGetString(POSITION_COMMENT);
1174 1244
                cod[i].masterLot = lot;
1175 1245
                Print("Ticket stored in master:",PositionGetInteger(POSITION_TICKET)," stored in index:",i);
1246
+
1247
+               Print("========== CLIENT ORDER STORED ==========");
1248
+               Print(" index=", i,
1249
+                     " | master_magic=", cod[i].magic,
1250
+                     " | client_ticket=", cod[i].ticket,
1251
+                     " | symbol=", cod[i].symbol,
1252
+                     " | order_type=", cod[i].ordertype,
1253
+                     " | open_price=", DoubleToString(cod[i].price, _Digits),
1254
+                     " | lots=", DoubleToString(cod[i].lots, 2),
1255
+                     " | master_lot=", DoubleToString(cod[i].masterLot, 2),
1256
+                     " | sl=", DoubleToString(cod[i].stoploss, _Digits),
1257
+                     " | tp=", DoubleToString(cod[i].takeprofit, _Digits),
1258
+                     " | comment=", cod[i].comments);
1259
+               Print("========================================");
1260
+
1176 1261
                return true;
1177 1262
               }
1178 1263
             else
@@ -1492,6 +1577,7 @@ void clearOrderArray()
1492 1577
    order_array[0].accountBalance=0;
1493 1578
    order_array[0].contractSize=0;
1494 1579
    order_array[0].comments="";
1580
+   order_array[0].closedPercentageOfTrade = 0;
1495 1581
 
1496 1582
   }
1497 1583
 //+------------------------------------------------------------------+

BIN
master/localHedgingMasterCopierMt5.ex5


BIN
master/localHedgingMasterCopierMt5.mq5