Author: Copyright � 2011, cmillion@narod.ru
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_EA_News
//+------------------------------------------------------------------+
//|                                            TrailingStopLight.mq4 |
//|                              Copyright © 2011, Khlystov Vladimir |
//|                                         http://cmillion.narod.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, cmillion@narod.ru"
#property link      "http://cmillion.narod.ru"
#property show_inputs
//--------------------------------------------------------------------
extern int     Stoploss             = 10,     //ñòîïëîññ, åñëè 0 òî íå èçìåíÿåòñÿ
               Takeprofit           = 50;     //òåéêïðîôèò, åñëè 0 òî íå èçìåíÿåòñÿ
extern int     TrailingStop         = 10;     //äëèííà òðàëëà, åñëè 0 òî íåò òðàëëà
extern int     TrailingStart        = 0;      //êîãäà âêëþ÷àòü òðàëë, íàïðèìåð ïîñëå äîñòèæåíèÿ 40 ï ïðèáûë
extern int     StepTrall            = 2;      //øàã òðàëëà - ïåðåìåùàòü ñòîïëîññ íå áëèæå ÷åì StepTrall
extern int     NoLoss               = 0,      //ïåðåâîä â áåçóáûòîê ïðè çàäàííîì êîë-âå ïóíêòîâ ïðèáûëè, åñëè 0 òî íåò ïåðåâîäà â áåçóáûòîê
               MinProfitNoLoss      = 0;      //ìèíèìàëüíàÿ ïðèáûëü ïðè ïåðåâîäå âáåçóáûòîê
extern int     Magic                = 77;     //ìàãèê
extern int     Step                 = 10;     //ðàññòîÿíèå îò öåíû
extern double  Lot                  = 0.1;
extern int     TimeModify           = 30;     //êîë-âî ñåêóíä ðàíüøå êîòîðîãî çàïðåùåíî èçìåíÿòü îðäåð
extern int     slippage             = 30;     //Ìàêñèìàëüíî äîïóñòèìîå îòêëîíåíèå öåíû äëÿ ðûíî÷íûõ îðäåðîâ (îðäåðîâ íà ïîêóïêó èëè ïðîäàæó).
//--------------------------------------------------------------------
int  STOPLEVEL,TimeBarB,TimeBarS;
//--------------------------------------------------------------------
int init()
{
}
//--------------------------------------------------------------------
int deinit()
{
}
//--------------------------------------------------------------------
int start()                                  
{
   STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);
   double OSL,StLo,PriceB,PriceS,OOP,SL,TP;
   int b,s,TicketB,TicketS,OT;
   for (int i=0; i<OrdersTotal(); i++)
   {    
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if (OrderSymbol()==Symbol() && Magic==OrderMagicNumber())
         { 
            OT = OrderType(); 
            OSL = NormalizeDouble(OrderStopLoss(),Digits);
            OOP = NormalizeDouble(OrderOpenPrice(),Digits);
            SL=OSL;
            if (OT==OP_BUY)             
            {  
               b++;
               if (OSL<OOP && NoLoss!=0)
               {
                  StLo = NormalizeDouble(OOP+MinProfitNoLoss*Point,Digits); 
                  if (StLo > OSL && StLo <= NormalizeDouble(Bid - STOPLEVEL * Point,Digits)) SL = StLo;
               }
               
               if (TrailingStop>=STOPLEVEL && TrailingStop!=0 && (Bid - OOP)/Point >= TrailingStart)
               {
                  StLo = NormalizeDouble(Bid - TrailingStop*Point,Digits); 
                  if (StLo>=OOP && StLo > OSL+StepTrall*Point) SL = StLo;
               }
               
               if (SL > OSL)
               {  
                  if (!OrderModify(OrderTicket(),OOP,SL,TP,0,White)) Print("Error ",GetLastError(),"   Order Modify Buy   SL ",OSL,"->",SL);
                  else Print("Order Buy Modify   SL ",OSL,"->",SL);
               }
            }                                         
            if (OT==OP_SELL)        
            {
               s++;
               if ((OSL>OOP || OSL==0) && NoLoss!=0)
               {
                  StLo = NormalizeDouble(OOP-MinProfitNoLoss*Point,Digits); 
                  if (StLo < OSL || OSL==0 && StLo >= NormalizeDouble(Ask + STOPLEVEL * Point,Digits)) SL = StLo;
               }
               
               if (TrailingStop>=STOPLEVEL && TrailingStop!=0 && (OOP - Ask)/Point >= TrailingStart)
               {
                  StLo = NormalizeDouble(Ask + TrailingStop*Point,Digits); 
                  if (StLo<=OOP && (StLo < OSL-StepTrall*Point || OSL==0)) SL = StLo;
               }
               
               if ((SL < OSL || OSL==0) && SL!=0)
               {  
                  if (!OrderModify(OrderTicket(),OOP,SL,TP,0,White)) Print("Error ",GetLastError(),"   Order Modify Sell   SL ",OSL,"->",SL);
                  else Print("Order Sell Modify   SL ",OSL,"->",SL);
               }
            } 
            if (OT==OP_BUYSTOP)  {PriceB=OOP; TicketB=OrderTicket();}     
            if (OT==OP_SELLSTOP) {PriceS=OOP; TicketS=OrderTicket();}  
         }
      }
   }
   if (b+TicketB==0)
   {
      if (Stoploss>=STOPLEVEL && Stoploss!=0) SL = NormalizeDouble(Bid - Stoploss * Point,Digits); else SL=0;
      if (Takeprofit>=STOPLEVEL && Takeprofit!=0) TP = NormalizeDouble(Ask + Takeprofit * Point,Digits); else TP=0;
      if (OrderSend(Symbol(),OP_BUYSTOP,Lot,NormalizeDouble(Ask+Step * Point,Digits),slippage,SL,TP,"news",Magic,0,CLR_NONE)!=-1) TimeBarB=TimeCurrent();
   } 
   if (s+TicketS==0)
   {
      if (Stoploss>=STOPLEVEL && Stoploss!=0) SL = NormalizeDouble(Ask + Stoploss * Point,Digits); else SL=0;
      if (Takeprofit>=STOPLEVEL && Takeprofit!=0) TP = NormalizeDouble(Bid - Takeprofit * Point,Digits); else TP=0;
      if (OrderSend(Symbol(),OP_SELLSTOP,Lot,NormalizeDouble(Bid - Step * Point,Digits),slippage,SL,TP,"news",Magic,0,CLR_NONE)!=-1) TimeBarS=TimeCurrent();
   } 
   if (TicketB!=0)
   {
      if (TimeBarB<TimeCurrent()-TimeModify && MathAbs(NormalizeDouble(Ask + Step * Point,Digits)-PriceB)/Point>StepTrall)
      {
         if (Stoploss>=STOPLEVEL && Stoploss!=0) SL = NormalizeDouble(Bid - Stoploss * Point,Digits); else SL=0;
         if (Takeprofit>=STOPLEVEL && Takeprofit!=0) TP = NormalizeDouble(Ask + Takeprofit * Point,Digits); else TP=0;
         if (OrderModify(TicketB,NormalizeDouble(Ask + Step * Point,Digits),SL,TP,0,CLR_NONE)) TimeBarB=TimeCurrent();
      }
   } 
   if (TicketS!=0)
   {
      if (TimeBarS<TimeCurrent()-TimeModify && MathAbs(NormalizeDouble(Bid - Step * Point,Digits)-PriceS)/Point>StepTrall)
      {
         if (Stoploss>=STOPLEVEL && Stoploss!=0) SL = NormalizeDouble(Ask + Stoploss * Point,Digits); else SL=0;
         if (Takeprofit>=STOPLEVEL && Takeprofit!=0) TP = NormalizeDouble(Bid - Takeprofit * Point,Digits); else TP=0;
         if (OrderModify(TicketS,NormalizeDouble(Bid - Step * Point,Digits),SL,TP,0,CLR_NONE)) TimeBarS=TimeCurrent();
      }
   } 
}
//--------------------------------------------------------------------

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 ---