e-News-Lucky_v1

Download script
Author: Lucky$ & KimIV
Profit factor:
0.00

This script is an automated trading tool designed to manage orders and positions based on specific time triggers and market price actions. It is structured to handle the lifecycle of a trade—from the initial placement of pending orders to the management of active positions and the final closure of trades.

Time-Based Execution Logic

The core behavior of the script is driven by three distinct milestones in time:

  • Order Placement: At a specific pre-defined starting time, the script automatically places two "pending" orders (Buy Stop and Sell Stop) at a set distance from the current market price.
  • Order Removal: At a later designated time, the script is programmed to clear out any remaining pending orders that were not triggered by the market.
  • Position Closure: At a final designated time, the script will automatically close all active trading positions at the current market price.

Order Management and Positioning

Once the initial orders are placed, the script monitors the market with several automated rules:

  • Dynamic Modification: If the market moves, the script can adjust the price levels of pending orders to maintain a consistent distance from the current price.
  • Opposite Order Cleanup: To prevent conflicting trades, the script automatically deletes a pending order if an opposite position has already been opened (for example, removing a "Buy" request if a "Sell" trade is currently active).
  • Distance Buffering: It calculates entry points by taking into account the current market spread to ensure orders are placed at accurate price levels.

Active Trade Protection (Trailing Stop)

For positions that are successfully opened, the script includes a "Trailing Stop" mechanism. This logic aims to protect profits as the market moves in your favor:

  • Profit Protection: It tracks the movement of the price. If the trade moves significantly into profit, the script automatically moves the Stop Loss closer to the current price.
  • Incremental Adjustments: The trailing logic moves in specific "steps," ensuring that the protection level is updated as the price fluctuates, effectively "locking in" gains while allowing for continued price movement.

User Interface and Feedback

The script includes several visual and auditory features to keep the user informed:

  • Color Coding: Different colors are assigned to various actions (Buying, Selling, Modifying, or Closing), allowing for easy visual tracking of the script's activity on the trading platform.
  • Audio Alerts: The tool can provide audible signals whenever an order is placed, modified, deleted, or closed.
  • Trade Identification: Every action performed by the script is tagged with a unique identification number (Magic Number), ensuring that it only manages its own trades and does not interfere with other manual trades or different automated systems running on the same account.
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 reachedIt Closes Orders by itself
Miscellaneous
It plays sound alerts
10 Views
0 Downloads
0 Favorites
e-News-Lucky_v1
//+------------------------------------------------------------------+
//|                                                e-News-Lucky$.mq4 |
//|                                                   Lucky$ & KimIV |
//|                                              http://www.kimiv.ru |
//+------------------------------------------------------------------+
#property copyright "Lucky$ & KimIV"
#property link      "http://www.kimiv.ru"
#define   MAGIC     20051024


//------- Âíåøíèå ïàðàìåòðû ñîâåòíèêà --------------------------------
//--- ñòðîãèé ðåæèì êîìïèëÿöèè
#property strict
//--- ðåæèì ïîêàçà âõîäíûõ ïàðàìåòðîâ
//#property script_show_inputs

extern string     _Parameters_Trade="----- Ïàðàìåòðû òîðãîâëè";
extern double     Lots           = 1;     // Ðàçìåð òîðãóåìîãî ëîòà
extern int        StopLoss       = 40;      // Ðàçìåð ôèêñèðîâàííîãî ñòîïà
extern int        TakeProfit     = 20;       // Ðàçìåð ôèêñèðîâàííîãî òýéêà
extern datetime   TimeSetOrders  = __DATETIME__+5*60; // Âðåìÿ óñòàíîâêè îðäåðîâ //D'22.03.2010 13:00';
extern datetime   TimeDelOrders  = __DATETIME__+14*60*60; // Âðåìÿ óäàëåíèÿ îðäåðîâ //D'22.03.2010 22:30';
extern datetime   TimeClosePos   = __DATETIME__+12*60*60;// Âðåìÿ çàêðûòèÿ ïîçèöèé //D'22.03.2010 22:30'; 
extern int        DistanceSet    = 20;      // Ðàññòîÿíèå îò ðûíêà
extern bool       UseTrailing    = True;    // Èñïîëüçîâàòü òðàë
extern bool       ProfitTrailing = True;    // Òðàëèòü òîëüêî ïðîôèò
extern int        TrailingStop   = 25;      // Ôèêñèðîâàííûé ðàçìåð òðàëà
extern int        TrailingStep   = 5;       // Øàã òðàëà
extern int        Slippage       = 3;       // Ïðîñêàëüçûâàíèå öåíû

extern string _Parameters_Expert="----- Ïàðàìåòðû ñîâåòíèêà";
extern string Name_Expert   = "e-News-Lucky$";
extern bool   UseSound      = True;         // Èñïîëüçîâàòü çâóêîâîé ñèãíàë
extern string NameFileSound = "expert.wav"; // Íàèìåíîâàíèå çâóêîâîãî ôàéëà
extern color  clOpenBuy     = LightBlue;    // Öâåò îòêðûòèÿ ïîêóïêè
extern color  clOpenSell    = LightCoral;   // Öâåò îòêðûòèÿ ïðîäàæè
extern color  clModifyBuy   = Aqua;         // Öâåò ìîäèôèêàöèè ïîêóïêè
extern color  clModifySell  = Tomato;       // Öâåò ìîäèôèêàöèè ïðîäàæè
extern color  clCloseBuy    = Blue;         // Öâåò çàêðûòèÿ ïîêóïêè
extern color  clCloseSell   = Red;          // Öâåò çàêðûòèÿ ïðîäàæè

//---- Ãëîáàëüíûå ïåðåìåííûå ñîâåòíèêà -------------------------------
int prevBar;
bool exP = true;

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
void deinit() 
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void start() 
  {
  //òåêóùåå âðåìÿ ïî ñåðâåðó
  // string tc=TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES);
  //òåêóùåå ëîêàëüíîå âðåìÿ
  if ((TimeLocal()>TimeSetOrders) && exP)   Print("Âðåìÿ óïóùåíî");
    
  string tc=TimeToStr(TimeLocal(),TIME_DATE|TIME_MINUTES);
  //âðåìÿ îòêðûòèÿ îðäåðà 
   string tso=TimeToStr(TimeSetOrders,TIME_DATE|TIME_MINUTES);
   //Âðåìÿ óäàëåíèÿ îðäåðà
   string tdo=TimeToStr(TimeDelOrders,TIME_DATE|TIME_MINUTES);
   //Âðåìÿ çàêðûòèÿ îðäåðà
   string tco=TimeToStr(TimeClosePos,TIME_DATE|TIME_MINUTES);
   if(tc==tso) SetOrders();
   if(prevBar!=Bars && ExistOrder(1) && ExistOrder(2)) ModifyOrders();
   DeleteOppositeOrders();
   TrailingPositions();
   if(tc==tdo) DeleteAllOrders();
   if(tc==tco) CloseAllPositions();
   prevBar=Bars;
   exP=false;
  }
//+------------------------------------------------------------------+
//| Óñòàíîâêà îðäåðîâ                                                |
//+------------------------------------------------------------------+
void SetOrders() 
  {
   double ldStop=0,ldTake=0;
   int    spr=MarketInfo(Symbol(),MODE_SPREAD);
   double pAsk=Ask+(DistanceSet+spr)*Point;
   double pBid=Bid-DistanceSet*Point;

   if(!ExistOrder(1)) 
     {
      if(StopLoss!=0) ldStop=pAsk-StopLoss*Point;
      if(TakeProfit!=0) ldTake=pAsk+TakeProfit*Point;
      SetOrder(OP_BUYSTOP,pAsk,ldStop,ldTake,1);
     }
   if(!ExistOrder(2)) 
     {
      if(StopLoss!=0) ldStop=pBid+StopLoss*Point;
      if(TakeProfit!=0) ldTake=pBid-TakeProfit*Point;
      SetOrder(OP_SELLSTOP,pBid,ldStop,ldTake,2);
     }
  }
//+------------------------------------------------------------------+
//| Ìîäèôèêàöèÿ îðäåðîâ                                              |
//+------------------------------------------------------------------+
void ModifyOrders() 
  {
   bool   fm;
   double ldStop=0,ldTake=0;
   int    spr=MarketInfo(Symbol(),MODE_SPREAD);
   double pAsk=Ask+(DistanceSet+spr)*Point;
   double pBid=Bid-DistanceSet*Point;

   for(int i=0; i<OrdersTotal(); i++) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC+1) 
           {
            if(StopLoss!=0) ldStop=pAsk-StopLoss*Point;
            if(TakeProfit!=0) ldTake=pAsk+TakeProfit*Point;
            OrderModify(OrderTicket(),pAsk,ldStop,ldTake,0,clModifyBuy);
           }
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC+2) 
           {
            if(StopLoss!=0) ldStop=pBid+StopLoss*Point;
            if(TakeProfit!=0) ldTake=pBid-TakeProfit*Point;
            OrderModify(OrderTicket(),pBid,ldStop,ldTake,0,clModifySell);
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Âîçâðàùàåò ôëàã ñóùåñòâîâàíèÿ îðäåðà èëè ïîçèöèè ïî íîìåðó       |
//+------------------------------------------------------------------+
bool ExistOrder(int mn) 
  {
   bool Exist=False;
   for(int i=0; i<OrdersTotal(); i++) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC+mn) 
           {
            Exist=True; break;
           }
        }
     }
   return(Exist);
  }
//+------------------------------------------------------------------+
//| Âîçâðàùàåò ôëàã ñóùåñòâîâàíèÿ ïîçèöèè ïî íîìåðó                  |
//+------------------------------------------------------------------+
bool ExistPosition(int mn) 
  {
   bool Exist=False;
   for(int i=0; i<OrdersTotal(); i++) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC+mn) 
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL) 
              {
               Exist=True; break;
              }
           }
        }
     }
   return(Exist);
  }
//+------------------------------------------------------------------+
//| Óñòàíîâêà îðäåðà                                                 |
//| Ïàðàìåòðû:                                                       |
//|   op     - îïåðàöèÿ                                              |
//|   pp     - öåíà                                                  |
//|   ldStop - óðîâåíü ñòîï                                          |
//|   ldTake - óðîâåíü òåéê                                          |
//|   mn     - äîáàâèòü ê MAGIC                                      |
//+------------------------------------------------------------------+
void SetOrder(int op,double pp,double ldStop,double ldTake,int mn) 
  {
   color  clOpen;
   string lsComm=GetCommentForOrder();

   if(op==OP_BUYSTOP) clOpen=clOpenBuy;
   else clOpen=clOpenSell;
   OrderSend(Symbol(),op,Lots,pp,Slippage,ldStop,ldTake,lsComm,MAGIC+mn,0,clOpen);
   if(UseSound) PlaySound(NameFileSound);
  }
//+------------------------------------------------------------------+
//| Ãåíåðèðóåò è âîçâðàùàåò ñòðîêó êîìåíòàðèÿ äëÿ îðäåðà èëè ïîçèöèè |
//+------------------------------------------------------------------+
string GetCommentForOrder() 
  {
   return(Name_Expert);
  }
//+------------------------------------------------------------------+
//| Óäàëåíèå âñåõ îðäåðîâ                                            |
//+------------------------------------------------------------------+
void DeleteAllOrders() 
  {
   bool fd;
   for(int i=OrdersTotal()-1; i>=0; i--) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if(OrderMagicNumber()>MAGIC && OrderMagicNumber()<=MAGIC+2) 
           {
            if(OrderSymbol()==Symbol()) 
              {
               if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP) 
                 {
                  fd=OrderDelete(OrderTicket());
                  if(fd && UseSound) PlaySound(NameFileSound);
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Çàêðûòèå âñåõ ïîçèöèé ïî ðûíî÷íîé öåíå                           |
//+------------------------------------------------------------------+
void CloseAllPositions() 
  {
   bool fc;
   for(int i=OrdersTotal()-1; i>=0; i--) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if(OrderMagicNumber()>MAGIC && OrderMagicNumber()<=MAGIC+2) 
           {
            if(OrderSymbol()==Symbol()) 
              {
               fc=False;
               if(OrderType()==OP_BUY) 
                 {
                  fc=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clCloseBuy);
                 }
               if(OrderType()==OP_SELL) 
                 {
                  fc=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clCloseSell);
                 }
               if(fc && UseSound) PlaySound(NameFileSound);
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Óäàëåíèå ïðîòèâîïîëîæíûõ îðäåðîâ                                 |
//+------------------------------------------------------------------+
void DeleteOppositeOrders() 
  {
   bool fd,fep1,fep2;

   fep1=ExistPosition(1);
   fep2=ExistPosition(2);

   for(int i=OrdersTotal()-1; i>=0; i--) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if(OrderSymbol()==Symbol()) 
           {
            fd=False;
            if(OrderType()==OP_BUYSTOP && OrderMagicNumber()==MAGIC+1) 
              {
               if(fep2) fd=OrderDelete(OrderTicket());
              }
            if(OrderType()==OP_SELLSTOP && OrderMagicNumber()==MAGIC+2) 
              {
               if(fep1) fd=OrderDelete(OrderTicket());
              }
            if(fd && UseSound) PlaySound(NameFileSound);
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Ñîïðîâîæäåíèå ïîçèöèè ïðîñòûì òðàëîì                             |
//+------------------------------------------------------------------+
void TrailingPositions() 
  {
   for(int i=0; i<OrdersTotal(); i++) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if(OrderMagicNumber()>MAGIC && OrderMagicNumber()<=MAGIC+2) 
           {
            if(OrderSymbol()==Symbol()) 
              {
               if(OrderType()==OP_BUY) 
                 {
                  if(!ProfitTrailing || (Bid-OrderOpenPrice())>TrailingStop*Point) 
                    {
                     if(OrderStopLoss()<Bid-(TrailingStop+TrailingStep-1)*Point) 
                       {
                        ModifyStopLoss(Bid-TrailingStop*Point,clModifyBuy);
                       }
                    }
                 }
               if(OrderType()==OP_SELL) 
                 {
                  if(!ProfitTrailing || OrderOpenPrice()-Ask>TrailingStop*Point) 
                    {
                     if(OrderStopLoss()>Ask+(TrailingStop+TrailingStep-1)*Point || OrderStopLoss()==0) 
                       {
                        ModifyStopLoss(Ask+TrailingStop*Point,clModifySell);
                       }
                    }
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Ïåðåíîñ óðîâíÿ StopLoss                                          |
//| Ïàðàìåòðû:                                                       |
//|   ldStopLoss - óðîâåíü StopLoss                                  |
//|   clModify   - öâåò ìîäèôèêàöèè                                  |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStop,color clModify) 
  {
   bool   fm;
   double ldOpen=OrderOpenPrice();
   double ldTake=OrderTakeProfit();

   fm=OrderModify(OrderTicket(),ldOpen,ldStop,ldTake,0,clModify);
   if(fm && UseSound) PlaySound(NameFileSound);
  }
//+------------------------------------------------------------------+

Comments