Author: Maxim Kuznetsov
Profit factor:
0.00
Orders Execution
Checks for the total of open ordersChecks for the total of closed ordersIt automatically opens orders when conditions are reached
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Repeator
ÿþ//+------------------------------------------------------------------+

//|                                                     Repeator.mq4 |

//|                                                  Maxim Kuznetsov |

//|                                          https://www.luxtrade.tk |

//+------------------------------------------------------------------+

#property copyright "Maxim Kuznetsov"

#property link      "https://www.luxtrade.tk"

#property version   "1.03"

#property description "Repeats closed position by OCO-pair"

#property strict



/** A>25B=8: ?>2B>@O5B A45;:8 >B;>6:0<8

   ?@8 70:@KB88 @K=>G=>3> >@45@0 ?>;L7>20B5;5<,

   >B:@K205B 425 >B;>6:8 (;8<8B:8) ?> F5=0< GCBL ;CGH5 >B:@KB8O 8 70:@KB8O

   ?@8 A@01>B:5 ;N1>9 87 =8E C40;O5B ?0@=CN

*/

input int OSPREADS=3;   // OpenPrice spread-shift

input int CSPREADS=5;   // ClosePrice spread-shift





double margin,ordersTotal,historyTotal;   // 4;O :>=B@>;O 87<=5=89 8AB>@88

datetime lastCloseTime;

int OnInit()

{

   margin=AccountMargin();

   ordersTotal=OrdersTotal();

   historyTotal=OrdersHistoryTotal();

   lastCloseTime=10;

   EventSetMillisecondTimer(300);

   return(INIT_SUCCEEDED);

}

void OnDeinit(const int reason)

{

}

int reopen[];  // B8:5BK : ?5@5>B:@KB8N



void OnTick()

{

   if (AccountMargin()!=margin || OrdersTotal()!=ordersTotal || OrdersHistoryTotal()!=historyTotal) {

      // GB>-B> ?><5=O;>AL

      

      // 8I8< 2A5 70:@KBK5 >@45@0 ?> ?>A;54=89 8725AB=K9

      datetime ltime=0;

      ArrayResize(reopen,0);

      for(int pos=OrdersHistoryTotal()-1;pos>=0;pos--) {

         if (!OrderSelect(pos,SELECT_BY_POS,MODE_HISTORY)) continue;

         if (ltime<OrderCloseTime()) ltime=OrderCloseTime();

         

         if (OrderType()!=OP_BUY && OrderType()!=OP_SELL) continue;

         

         if (OrderCloseTime()<lastCloseTime-2) continue;

         if (OrderSymbol()!=_Symbol) continue;

         // 4>102;O5< 2 A?8A>:

         ArrayPush(reopen,OrderTicket());

      }

      for(int pos=ArraySize(reopen)-1;pos>=0;pos--) {

         int ticket=reopen[pos];

         if (ticket==-1) continue;

         // C40;O5< >AB02HCNAO >B;>6:C A magic=ticket

         // 5A;8 >B;>65: 425, 8;8 5ABL @K=>G=K9 >@45@, B> ?@>?CA:05<

         if (!DeleteOnePendingByMagic(ticket)) continue;

         if (!OrderSelect(ticket,SELECT_BY_TICKET)) {

            PrintFormat("Unable select %d",ticket);

            continue;

         }

         PrintFormat("REPEATE %d with %f %f",OrderTicket(),OrderOpenPrice(),OrderClosePrice());

         // >B:@K205< 425 >B;>6:8 ?> F50< >B:@KB8O 8 70:@KB8O, => =0 3 A?@540 ;CGH5

         if (OSPREADS>0) {

            double p1=OrderOpenPrice(); 

            if (OrderType()==OP_BUY) p1=p1-(Ask-Bid)*OSPREADS;

            else p1=p1+(Ask-Bid)*OSPREADS;

            if (p1>=Bid) SellLimit(p1,OrderLots(),ticket);

            else BuyLimit(p1,OrderLots(),ticket);

         }

         if (CSPREADS>0) {

            double p2=OrderClosePrice(); 

            if (OrderType()==OP_BUY) p2=p2+(Ask-Bid)*OSPREADS;

            else p2=p2-(Ask-Bid)*OSPREADS;

            if (p2>Bid) SellLimit(p2+(Ask-Bid)*CSPREADS,OrderLots(),ticket);

            else BuyLimit(p2,OrderLots(),ticket);

         }

      }

      // C40;O5< ?0@=K5 OCO >@45@0

      // A>18@05< magic @K=>G=KE >@45@>2

      int collect[]; // :>;;5:F89 magic @K=>G=KE >@45@>2

      for(int pos=OrdersTotal()-1;pos>=0;pos--) {

         if (!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) continue;

         if (OrderType()!=OP_BUY && OrderType()!=OP_SELL) continue;

         if (OrderCloseTime()!=0) continue;

         if (OrderSymbol()!=_Symbol) continue;

         if (OrderMagicNumber()==0) continue;

         ArrayPush(collect,OrderMagicNumber());

      }

      // C40;O5< ?0@=K5 8< >B;>6:8

      for(int pos=OrdersTotal()-1;pos>=0;pos--) {

         if (!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) continue;

         if (OrderType()!=OP_BUYLIMIT && OrderType()!=OP_SELLLIMIT && OrderType()!=OP_BUYSTOP && OrderType()!=OP_SELLSTOP) continue;

         if (OrderCloseTime()!=0) continue;

         if (OrderSymbol()!=_Symbol) continue;

         if (OrderMagicNumber()==0) continue;

         if (ArrayContains(collect,OrderMagicNumber()) && !OrderDelete(OrderTicket())) {

            Alert("Unable to delete %d : %d",OrderTicket(),GetLastError());

         }

      }      

      lastCloseTime=ltime;

      margin=AccountMargin();

      ordersTotal=OrdersTotal();

      historyTotal=OrdersHistoryTotal();

   } else {

      lastCloseTime=TimeCurrent();

   }

}

void OnTimer()

{

   if (IsTradeAllowed()) OnTick();

}

bool DeleteOnePendingByMagic(int magic)

{

   int ticket=-1;

   for(int pos=OrdersTotal()-1;pos>=0;pos--) {

      if (!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)) continue;

      if ((OrderType()==OP_BUY || OrderType()==OP_SELL) && OrderMagicNumber()==magic) {

         return false;

      }

      if (OrderType()!=OP_BUYLIMIT && OrderType()!=OP_SELLLIMIT && OrderType()!=OP_BUYSTOP && OrderType()!=OP_SELLSTOP) continue;

      if (OrderSymbol()!=_Symbol) continue;

      if (OrderMagicNumber()!=magic) continue;

      if (ticket!=-1) {

         return false;

      }

      ticket=OrderTicket();

   }

   if (ticket!=-1 && !OrderDelete(ticket)) {

      Alert("Unable to delete %d : %d",OrderTicket(),GetLastError());

   }

   return true;

}

void BuyLimit(double price,double lots,int magic)

{

   int ticket=OrderSend(_Symbol,OP_BUYLIMIT,lots,NormalizeDouble(price,_Digits),5,0,0,"",magic);

   if (ticket<0) {

         Alert("Unable BuyLimit at %f : %d",price,GetLastError());

   }

}

void SellLimit(double price,double lots,int magic)

{

   int ticket=OrderSend(_Symbol,OP_SELLLIMIT,lots,NormalizeDouble(price,_Digits),5,0,0,"",magic);

   if (ticket<0) {

         Alert("Unable BuyLimit at %f : %d",price,GetLastError());

   }

}

bool ArrayPush(int &arr[],int item)

{

   int pos=ArraySize(arr);

   if (ArrayResize(arr,pos+1)!=pos+1) return false;

   arr[pos]=item;

   return true;

}

bool ArrayContains(int &arr[],int item)

{

   for(int pos=ArraySize(arr)-1;pos>=0;pos--)

      if (arr[pos]==item) return true;

   return false;

}

Comments