Author: Copyright � 2010, Vladimir Hlystov
Profit factor:
0.00
1 Views
0 Downloads
0 Favorites
StopOrders
//+------------------------------------------------------------------+
//|                                                StopOrders_v2.mq4 |
//|                               Copyright © 2010, Vladimir Hlystov |
//| v1 Óñòàíàâëèâàåò äî 5 îðäåðîâ Stop èëè Limit â îáå ñòîðîíû       |
//|    îò òåêóùåé öåíû. Îáîçíà÷àåò èõ ãîðèçîíòàëüíûìè ëèíèÿìè.       |
//|    Ïåðåìåùàÿ ëèíèè ìîæíî ïåðåìåùàòü ñòîïû îðäåðà                 |
//|    Stop = true  - Stop                                           |
//|    Stop = False - Limit                                          |
//|    Óðîâíè ñòîïîâ çàäàþòñÿ ïåðåìåííûìè SLoss è TProfit            |
//|    Ëîò, âûñòàâëÿåòñÿ ó êàæäëé ëèíèè ñâîé lot1,2,3,4,5            |
//|    Ïðè óäàëåíèè ëèíèè, óäàëÿåòñÿ è îðäåð ïî íåé                  |
//|    Ïàðàìåòðû prise_Buy è prise_Sell çàäàþò öåíîâûå óðîâíè ëèíèé  |
//|    Åñëè îíè ðàâíû 0, òî ëèíèè âûñòàâëÿþòñÿ ïî ïàðàìåòðó          |
//|    DeltaPrice - ðàññòîÿíèå ìåæäó ëèíèÿìè                         |
//|    Åñëè â ìîìåíò óñòàíîâêè ýêñïåðòà íà ãðàôèêå áûëè îòëîæåííûå   |
//|    îðäåðà, òî îíè ïåðåìåñòÿòñÿ ïî òåêóùåìó ðàñïîëîæåíèþ ëèíèé.   |
//|                                                                  |
//| v2 åñëè ñðàáîòàë ñòîï îðäåð è âûñòàâèëñÿ ðûíî÷íûé, òî íîâûé ñòîï |
//|    îðäåð ýòîãî íîìåðà íå âûñòàâèòüñÿ, ïîêà íå çàêðîåòñÿ ðûíî÷íûé |
//|    îðäåð                                                         |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Vladimir Hlystov"
#property link      "cmillion@narod.ru"
//-------------------------------------------------------------------
extern int    MAX_Lines       = 5;     //ìàêñèìàëüíîå êîëëè÷åñòâî ëèíèé 
                                       //êàæäîãî íàïðàâëåíèÿ (íå áîëåå 5)
extern bool   Stop            = true;  //true - Stop False - Limit
extern double lot1            = 0.10;  //ËÎÒÛ ñîòòâåòñòâóþùèõ ëèíèé
extern double lot2            = 0.20;
extern double lot3            = 0.30;
extern double lot4            = 0.40;
extern double lot5            = 0.50;
extern double prise_Buy_1     = 0.0;
extern double prise_Buy_2     = 0.0;
extern double prise_Buy_3     = 0.0;
extern double prise_Buy_4     = 0.0;
extern double prise_Buy_5     = 0.0;
extern double prise_Sell_1    = 0.0;
extern double prise_Sell_2    = 0.0;
extern double prise_Sell_3    = 0.0;
extern double prise_Sell_4    = 0.0;
extern double prise_Sell_5    = 0.0;
extern int    DeltaPrice      = 10;
extern int    SLoss           = 100;
extern int    TProfit         = 10;
extern color  ColorSellLine   = Brown;     //öâåòà ëèíèé
extern color  ColorBuylLine   = MediumBlue;
extern bool   DeleteLines     = false;     //óäàëÿòü ëèíèè ïðè ñðàáîòêå îðäåðà
extern int    FontSize        = 8;
extern bool   DelOrders       = true;      //Óäàëÿòü ëèìèòíûå îðäåðà ïðè âûõîäå
//-------------------------------------------------------------------
string Obj_N;
int SL[6],TP[6];
double lot[6],prise_Sell[6],prise_Buy[6];
datetime dlinna;
string TipStOr;
//-------------------------------------------------------------------
int init()
{
   if (MAX_Lines>5) MAX_Lines=5;
   for (int i=1; i<=MAX_Lines; i++) {SL[i]=SLoss;TP[i]=TProfit;}
   lot[1]=lot1;lot[2]=lot2;lot[3]=lot3;lot[4]=lot4;lot[5]=lot5;
   
   if (prise_Sell_1!=0) prise_Sell[1] = prise_Sell_1; 
   else {if (Stop) prise_Sell[1] = NormalizeDouble(Bid - 1*DeltaPrice*Point,Digits); 
              else prise_Sell[1] = NormalizeDouble(Ask + 1*DeltaPrice*Point,Digits);}
   if (prise_Sell_2!=0) prise_Sell[2] = prise_Sell_2; 
   else {if (Stop) prise_Sell[2] = NormalizeDouble(Bid - 2*DeltaPrice*Point,Digits); 
              else prise_Sell[2] = NormalizeDouble(Ask + 2*DeltaPrice*Point,Digits);}
   if (prise_Sell_3!=0) prise_Sell[3] = prise_Sell_3; 
   else {if (Stop) prise_Sell[3] = NormalizeDouble(Bid - 3*DeltaPrice*Point,Digits); 
              else prise_Sell[3] = NormalizeDouble(Ask + 3*DeltaPrice*Point,Digits);}
   if (prise_Sell_4!=0) prise_Sell[4] = prise_Sell_4; 
   else {if (Stop) prise_Sell[4] = NormalizeDouble(Bid - 4*DeltaPrice*Point,Digits); 
              else prise_Sell[4] = NormalizeDouble(Ask + 4*DeltaPrice*Point,Digits);}
   if (prise_Sell_5!=0) prise_Sell[5] = prise_Sell_5; 
   else {if (Stop) prise_Sell[5] = NormalizeDouble(Bid - 5*DeltaPrice*Point,Digits); 
              else prise_Sell[5] = NormalizeDouble(Ask + 5*DeltaPrice*Point,Digits);}
   if (prise_Buy_1!=0) prise_Buy[1] = prise_Buy_1; 
   else {if (Stop) prise_Buy[1] = NormalizeDouble(Ask + 1*DeltaPrice*Point,Digits); 
              else prise_Buy[1] = NormalizeDouble(Bid - 1*DeltaPrice*Point,Digits);}
   if (prise_Buy_2!=0) prise_Buy[2] = prise_Buy_2; 
   else {if (Stop) prise_Buy[2] = NormalizeDouble(Ask + 2*DeltaPrice*Point,Digits); 
              else prise_Buy[2] = NormalizeDouble(Bid - 2*DeltaPrice*Point,Digits);}
   if (prise_Buy_3!=0) prise_Buy[3] = prise_Buy_3; 
   else {if (Stop) prise_Buy[3] = NormalizeDouble(Ask + 3*DeltaPrice*Point,Digits); 
              else prise_Buy[3] = NormalizeDouble(Bid - 3*DeltaPrice*Point,Digits);}
   if (prise_Buy_4!=0) prise_Buy[4] = prise_Buy_4; 
   else {if (Stop) prise_Buy[4] = NormalizeDouble(Ask + 4*DeltaPrice*Point,Digits); 
              else prise_Buy[4] = NormalizeDouble(Bid - 4*DeltaPrice*Point,Digits);}
   if (prise_Buy_5!=0) prise_Buy[5] = prise_Buy_5; 
   else {if (Stop) prise_Buy[5] = NormalizeDouble(Ask + 5*DeltaPrice*Point,Digits); 
              else prise_Buy[5] = NormalizeDouble(Bid - 5*DeltaPrice*Point,Digits);}
   ObjectCreate("time", OBJ_LABEL, 0, 0, 0);// Ñîçäàíèå îáú.
   ObjectSet("time", OBJPROP_CORNER, 1);
   ObjectSet("time", OBJPROP_XDISTANCE, 200);
   ObjectSet("time", OBJPROP_YDISTANCE, 0);
   if (Stop) TipStOr="STOP "; else TipStOr="LIMIT";
   for (i=1; i<=MAX_Lines; i++)
   {  
      Obj_N = StringConcatenate("LINES SELL ",TipStOr," ",i);
      if (ObjectFind(Obj_N)==-1)drawline(Obj_N,ColorSellLine,prise_Sell[i]);
      Obj_N = StringConcatenate("LINES BUY  ",TipStOr," ",i);
      if (ObjectFind(Obj_N)==-1)drawline(Obj_N,ColorBuylLine,prise_Buy[i]);
   }
   return(0);
}
//-------------------------------------------------------------------
int deinit()
{
   ObjectsDeleteAll(0,OBJ_HLINE);
   ObjectsDeleteAll(0,OBJ_TEXT);
   ObjectDelete("time");
   if (DelOrders)
   {
      while(true)
      {int i;
         for (int j = 0; j < OrdersTotal(); j++)
         {
            if (OrderSelect(j, SELECT_BY_POS))
            {
               if (OrderSymbol() == Symbol())
               {
                  if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT || 
                      OrderType()==OP_BUYSTOP  || OrderType()==OP_BUYLIMIT ) 
                      OrderDelete(OrderTicket(), CLR_NONE);
                  Sleep(100);i++;
               }
            }
         }
         if (i==0) break;
      }
   }
   return(0);
}
//-------------------------------------------------------------------
int start()
{//WindowRedraw();
   while(true)
   {
      for (int i=1; i<=MAX_Lines; i++)
      {  
         massiv();
         Obj_N = StringConcatenate("LINES SELL ",TipStOr," ",i);
         if (ObjectFind(Obj_N)==-1)
         {
            if (DeleteLines) drawline(Obj_N,ColorSellLine,prise_Sell[i]);
            else {deleteOrder(-1,i);ObjectDelete (Obj_N+"n");}
         }

         Obj_N = StringConcatenate("LINES BUY  ",TipStOr," ",i);
         if (ObjectFind(Obj_N)==-1)
         {
            if (DeleteLines) drawline(Obj_N,ColorBuylLine,prise_Buy[i]);
            else {deleteOrder(1,i);ObjectDelete (Obj_N+"n");}
         }
      }
      checkapp();
      Sleep(1000);
      ObjectSetText("time",TimeToStr(CurTime(),TIME_SECONDS),8,"Arial",ColorSellLine);
   }
return;
}
//------------------------------------------------------------------
int deleteOrder(int ord,int mag)
{
   int tip;
   for (int i=0; i<OrdersTotal(); i++)
   {  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
      {
         if (OrderSymbol()!=Symbol()) continue;
         if (OrderMagicNumber()==mag)
         { 
            tip=OrderType();
            if ((tip==OP_BUYSTOP  ||tip==OP_BUYLIMIT) && ord== 1) 
               OrderDelete(OrderTicket());
            if ((tip==OP_SELLLIMIT||tip==OP_SELLSTOP) && ord==-1) 
               OrderDelete(OrderTicket());
         }
      }
   }
   return(0);
}
//------------------------------------------------------------------
void massiv()
{
   int tip,mag;
   for (int i=0; i<OrdersTotal(); i++)
   {  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
      {
         if (OrderSymbol()!=Symbol()) continue;
         mag=OrderMagicNumber();
         if (mag<6)
         { 
            tip=OrderType();
            if (tip==OP_BUYSTOP  ||tip==OP_BUYLIMIT) 
               prise_Buy[mag] =OrderOpenPrice();
            if (tip==OP_SELLLIMIT||tip==OP_SELLSTOP) 
               prise_Sell[mag]=OrderOpenPrice();
         }
      }
   }
   return;
}
//------------------------------------------------------------------
int TrueOrder(int ord,int mag)
{
   int tip;
   for (int i=0; i<OrdersTotal(); i++)
   {  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
      {
         if (OrderSymbol()!=Symbol()) continue;
         if (OrderMagicNumber()==mag)
         { 
            tip=OrderType();
            if ((tip==OP_BUYSTOP  ||tip==OP_BUYLIMIT||tip==OP_BUY ) && ord== 1) 
               return(OrderTicket());
            if ((tip==OP_SELLLIMIT||tip==OP_SELLSTOP||tip==OP_SELL) && ord==-1) 
               return(OrderTicket());
         }
      }
   }
   return(0);
}
//-------------------------------------------------------------------
int drawline(string NameL, color col, double Y1)
{
   ObjectCreate(NameL, OBJ_HLINE, 0,0,Y1,0,0);
   ObjectSet   (NameL, OBJPROP_COLOR, col);
   ObjectSet   (NameL, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet   (NameL, OBJPROP_WIDTH, 1);
   ObjectSet   (NameL, OBJPROP_BACK,  false);
   ObjectSet   (NameL, OBJPROP_RAY,   false);
   return;
}
//--------------------------------------------------------------------
void checkapp()
{
   datetime X_1,X_2,X_3;
   int ord,mag,tiket;
   double pr,price;
   double shift_Y = (WindowPriceMax()-WindowPriceMin()) / 40;
   color col;
   string NameLine,info,NameMetka;
   for(int n=ObjectsTotal()-1; n>=0; n--) 
   {
      string Obj_N=ObjectName(n);
      if (ObjectType(Obj_N)!=OBJ_HLINE) continue;
      if (StringFind(Obj_N,"LINES ",0)!=-1)//íàéäåí îáüåêò-òðåíä ê êîòîðîìó âû÷èñëÿåòñÿ ïðèáëèæåíèå
      {
         mag = StrToInteger(StringSubstr(Obj_N,17,1));
         if (StringSubstr(Obj_N,6,2)=="BU") ord= 1;
         if (StringSubstr(Obj_N,6,2)=="SE") ord=-1;
         if (ord==0) {Comment("Error ëèøíÿÿ ëèíèÿ");continue;}
         col=ObjectGet(Obj_N, OBJPROP_COLOR);
         tiket = TrueOrder(ord,mag);
         if (ord== 1) price = prise_Buy[mag];
         if (ord==-1) price = prise_Sell[mag];
         pr = NormalizeDouble(ObjectGet(Obj_N, OBJPROP_PRICE1),Digits);
         //continue;
         if (price == pr) //ëèíèÿ íå ïåðåìåøàëàñü
         { 
            if (tiket==0) SendORDER(ord,mag,price);
         }
         else
         {
            price = pr;
            if (ord== 1) prise_Buy[mag]=price;
            if (ord==-1) prise_Sell[mag]=price;
            if (tiket==0) SendORDER(ord,mag,price);
            else
            {
               if (Stop)
               {
                  if (ord==-1) 
                     OrderModify(tiket,price,price+SL[mag]*Point,price-TP[mag]*Point,0,Green);
                  if (ord== 1) 
                     OrderModify(tiket,price,price-SL[mag]*Point,price+TP[mag]*Point,0,Green);
               }
               else
               { 
                  if (ord==-1) 
                     OrderModify(tiket,price,price+SL[mag]*Point,price-TP[mag]*Point,0,Green);
                  if (ord== 1) 
                     OrderModify(tiket,price,price-SL[mag]*Point,price+TP[mag]*Point,0,Green);
               }
            }
         }
         NameLine=StringConcatenate(Obj_N,"n");
         info=StringConcatenate(StringSubstr(Obj_N,6,11),
            "  lot ",DoubleToStr(lot[mag],2),
            "  SL " ,DoubleToStr(SL[mag] ,0),
            "  TP " ,DoubleToStr(TP[mag] ,0));
         ObjectDelete (NameLine);
         ObjectCreate (NameLine, OBJ_TEXT,0,Time[WindowFirstVisibleBar()/2],pr+shift_Y,0,0,0,0);
         ObjectSetText(NameLine, info ,FontSize,"Arial");
         ObjectSet    (NameLine, OBJPROP_COLOR, col);
//         NameMetka = NameMetka+info+"   mag "+mag+" ord "+ord+" tiket "+tiket+"\n";
      }
   }
//Comment(NameMetka);
   return;
}
//------------------------------------------------------------------------------------------- 
void SendORDER(int ord,int mag,double price)
{
   if (Stop)
   {   
      if (ord==-1) 
         OrderSend(Symbol(),OP_SELLSTOP,lot[mag],price,1,price+SL[mag]*Point,
         price-TP[mag]*Point,"LINES SELLSTOP "+mag,mag,0);
      if (ord== 1) 
         OrderSend(Symbol(),OP_BUYSTOP, lot[mag],price,1,price-SL[mag]*Point,
         price+TP[mag]*Point,"LINES BUYSTOP "+mag,mag,0);
   }
   else
   {
      if (ord==-1) 
         OrderSend(Symbol(),OP_SELLLIMIT,lot[mag],price,1,price+SL[mag]*Point,
         price-TP[mag]*Point,"LINES SELLLIMIT "+mag,mag,0);
      if (ord== 1) 
         OrderSend(Symbol(),OP_BUYLIMIT, lot[mag],price,1,price-SL[mag]*Point,
         price+TP[mag]*Point,"LINES BUYLIMIT "+mag,mag,0);
   }
}
//-------------------------------------------------------------------

Comments