cm-Breakdown_Line_3

Author: Copyright � 2012, Vladimir Hlystov
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
cm-Breakdown_Line_3
//+------------------------------------------------------------------+
//|                               Copyright © 2012, Vladimir Hlystov |
//| îòêðûâàåò îðäåðà ïðè ïðîáîå ëèíèè BUY èëè SELL Close[1] >< ëèíèè |
//| äàëåå óäàëÿåì âñå ëèíèè ïðîòèâîïîëîæíîãî íàïðàâëåíèÿ è æäåì ïåðå-|
//| ñå÷åíèÿ ñ óñðåäíÿþùåé ëèíèåé (äëÿ buy îíè íèæå äëÿ sell âûøå)    |
//| ëèíèè óñòåðäåíèÿ ìîãóò èìåòü ëþáîå èìÿ                           |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, Vladimir Hlystov"
#property link      "cmillion@narod.ru"
//-------------------------------------------------------------------
extern double  Lot         = 0.10;  //ëîò
extern double  K           = 2;     //êîýôôèöèåíò óìíîæåíèÿ óñðåäíÿþùåãî ëîòà
extern int     Takeprofit  = 30;    //ïðèáûëü îðäåðà äëÿ çàêðûòèÿ âñåõ
extern int     slippage    = 3;
extern int     Magic       = 0;     //óíèêàëüíûé íîìåð îðäåðà
datetime TimeOpen;
//-------------------------------------------------------------------
int init()
{
   return(0);
}
//-------------------------------------------------------------------
int deinit()
{
   return(0);
}
//-------------------------------------------------------------------
int start()
{
   int OT,b,s;
   double OL,OOP,price_b,price_s,BuyLot,SellLot,MaxLot,PriceSell,PriceBuy;
   for (int i=0; i<OrdersTotal(); i++) 
   {  
      if (OrderSelect(i, SELECT_BY_POS))
      {  
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
         {
            OOP = NormalizeDouble(OrderOpenPrice(),Digits);
            OT = OrderType();
            OL = OrderLots();
            if (MaxLot<OL) MaxLot=OL;
            if (OT==OP_BUY) 
            {
               b++;
               price_b += OOP*OL;
               BuyLot+=OL;
               if (PriceBuy>OOP || PriceBuy==0) PriceBuy=OOP;
            }
            if (OT==OP_SELL) 
            {
               s++;
               price_s += OOP*OL;
               SellLot+=OL;
               if (PriceSell<OOP) PriceSell=OOP;
            }
         }
      }
   }
   double NLs,NLb;
   ObjectDelete("infoÁåçóáûòîê Buy");
   ObjectDelete("infoÁåçóáûòîê Sell");
   if (b>0) 
   {
      NLb = NormalizeDouble(price_b/BuyLot,Digits);
      ObjectCreate("infoÁåçóáûòîê Buy",OBJ_ARROW,0,Time[0],NLb,0,0,0,0);                     
      ObjectSet   ("infoÁåçóáûòîê Buy",OBJPROP_ARROWCODE,6);
      ObjectSet   ("infoÁåçóáûòîê Buy",OBJPROP_COLOR, Blue);
   } 
   else NLb=0;
   if (s>0) 
   {
      NLs = NormalizeDouble(price_s/SellLot,Digits);
      ObjectCreate("infoÁåçóáûòîê Sell",OBJ_ARROW,0,Time[0],NLs,0,0,0,0);                     
      ObjectSet   ("infoÁåçóáûòîê Sell",OBJPROP_ARROWCODE,6);
      ObjectSet   ("infoÁåçóáûòîê Sell",OBJPROP_COLOR, Red);
   } 
   else NLs=0;
   double TP,OTP,OSL;
   int STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);
   if (b+s>0)
   {
      for (i=0; i<OrdersTotal(); i++)
      {    
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {
            if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
            { 
               OT = OrderType(); 
               OTP = NormalizeDouble(OrderTakeProfit(),Digits);
               OSL = NormalizeDouble(OrderStopLoss(),Digits);
               TP=OTP;
               if (OT==OP_BUY)             
               {  
                  TP = NormalizeDouble(NLb + Takeprofit * Point,Digits);
                  if (NormalizeDouble((TP-Ask)/Point,0)<STOPLEVEL) TP=OTP;
                  if (TP != OTP) if (!OrderModify(OrderTicket(),OOP,OSL,TP,0,White)) Print("Error OrderModify ",GetLastError());
               }                                         
               if (OT==OP_SELL)        
               {
                  TP = NormalizeDouble(NLs - Takeprofit * Point,Digits);
                  if (NormalizeDouble((Bid-TP)/Point,0)<STOPLEVEL) TP=OTP;
                  if (TP != OTP) if (!OrderModify(OrderTicket(),OOP,OSL,TP,0,White)) Print("Error OrderModify ",GetLastError());
               } 
            }
         }
      }
   }
   double PRICE;
   string Obj_N,txt;
   for(i=ObjectsTotal()-1; i>=0; i--) 
   {
      Obj_N=ObjectName(i);
      if (ObjectType(Obj_N)!=OBJ_HLINE) continue;
      PRICE = ObjectGet(Obj_N, OBJPROP_PRICE1); 
      if (StringFind(Obj_N,"BUY",0)!=-1)
      {
         if (s>0) {ObjectDelete(Obj_N);continue;}
         ObjectSet(Obj_N,OBJPROP_STYLE,0);
         ObjectSet(Obj_N,OBJPROP_COLOR,Blue);
         txt = StringConcatenate(txt,"\n","ïåðâûé BUY ïî öåíå ",DoubleToStr(PRICE,Digits));
         if (b==0 && (Low[2]<PRICE || Low[1]<PRICE) && Close[1]>=PRICE && TimeOpen!=Time[0]) if (SendOrder(OP_BUY, Lot, Obj_N)) {ObjectDelete(Obj_N);TimeOpen=Time[0];continue;}
      }
      else
      {
         if (StringFind(Obj_N,"SELL",0)!=-1)
         {
            if (b>0) {ObjectDelete(Obj_N);continue;}
            ObjectSet(Obj_N,OBJPROP_STYLE,0);
            ObjectSet(Obj_N,OBJPROP_COLOR,Red);
            txt = StringConcatenate(txt,"\n","ïåðâûé SELL ïî öåíå ",DoubleToStr(PRICE,Digits));
            if (s==0 && (High[2]>PRICE || Open[1]>PRICE) && Close[1]<=PRICE && TimeOpen!=Time[0]) if (SendOrder(OP_SELL, Lot, Obj_N)) {ObjectDelete(Obj_N);TimeOpen=Time[0];continue;}
         }
         else
         {
            ObjectSet(Obj_N,OBJPROP_STYLE,2);
            if (s>0)
            {
               if (PRICE<=PriceSell) {ObjectDelete(Obj_N);continue;}
               ObjectSet(Obj_N,OBJPROP_COLOR,Red);
               if ((High[2]>PRICE || Open[1]>PRICE) && Close[1]<=PRICE && TimeOpen!=Time[0]) if (SendOrder(OP_SELL, MaxLot*K, Obj_N)) {ObjectDelete(Obj_N);TimeOpen=Time[0];continue;}
            }
            if (b>0)
            {
               if (PRICE>=PriceBuy) {ObjectDelete(Obj_N);continue;}
               ObjectSet(Obj_N,OBJPROP_COLOR,Blue);
               if ((Low[2]<PRICE || Open[1]<PRICE) && Close[1]>=PRICE && TimeOpen!=Time[0]) if (SendOrder(OP_BUY, MaxLot*K, Obj_N)) {ObjectDelete(Obj_N);TimeOpen=Time[0];continue;}
            }
            txt = StringConcatenate(txt,"\n","óñðåäíÿþùèé ïî öåíå ",DoubleToStr(PRICE,Digits));
         }
      }
   }
   Comment(txt);
}
//-------------------------------------------------------------------------
bool SendOrder(int tip, double lot, string com)
{
   for (int i=0; i<OrdersTotal(); i++) 
   {  
      if (OrderSelect(i, SELECT_BY_POS))
      {  
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
         {
            if (OrderComment()==com) return;
         }
      }
   }
   int error,nn;
   while(true)
   {
      RefreshRates();
      if (tip==OP_BUY) error = OrderSend(Symbol(),OP_BUY,lot,NormalizeDouble(Ask,Digits),slippage,0,0,com,Magic,0,Blue);
      
      if (tip==OP_SELL) error = OrderSend(Symbol(),OP_SELL,lot,NormalizeDouble(Bid,Digits),slippage,0,0,com,Magic,0,Red);
      
      if (error==-1)
      {
         Print("OrderSend Error ",GetLastError()," Lot ",lot);
         Sleep(1000);
      }
      else return(1);
      nn++;
      if (nn>10) return(0);
   }
   return(1);
}
//-------------------------------------------------------------------

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---