TrailingStopLight

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
TrailingStopLight
//+------------------------------------------------------------------+
//|                                            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             = 0,      //ñòîïëîññ, åñëè 0 òî íå èçìåíÿåòñÿ
               Takeprofit           = 0;      //òåéêïðîôèò, åñëè 0 òî íå èçìåíÿåòñÿ
extern int     TrailingStop         = 20;     //äëèííà òðàëëà, åñëè 0 òî íåò òðàëëà
extern int     StepTrall            = 0;      //øàã òðàëëà - ïåðåìåùàòü ñòîïëîññ íå áëèæå ÷åì StepTrall
extern int     NoLoss               = 10,     //ïåðåâîä â áåçóáûòîê ïðè çàäàííîì êîë-âå ïóíêòîâ ïðèáûëè, åñëè 0 òî íåò ïåðåâîäà â áåçóáûòîê
               MinProfitNoLoss      = 0;      //ìèíèìàëüíàÿ ïðèáûëü ïðè ïåðåâîäå âáåçóáûòîê
//--------------------------------------------------------------------
int  STOPLEVEL,TimeBar;
//--------------------------------------------------------------------
int init()
{
}
//--------------------------------------------------------------------
int deinit()
{
   ObjectDelete("SLb");
   ObjectDelete("SLs");
}
//--------------------------------------------------------------------
int start()                                  
{
   //while(!IsStopped())
   {
      //RefreshRates();
      STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);
      double OSL,OTP,OOP,StLo,SL,TP;
      double Profit,ProfitS,ProfitB,LB,LS,NLb,NLs,price_b,price_s,OL,sl;
      int b,s,OT,OMN;
      for (int i=OrdersTotal()-1; i>=0; i--)
      {                                               
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {
            OMN = OrderMagicNumber();
            if (OrderSymbol() == Symbol())
            {
               OOP = NormalizeDouble(OrderOpenPrice(),Digits);
               OT = OrderType();
               OL = OrderLots();
               if (OT==OP_BUY)
               {
                  price_b = price_b+OOP*OL;
                  b++; LB+= OL;
                  ProfitB+=OrderProfit()+OrderSwap()+OrderCommission();
               }
               if (OT==OP_SELL)
               {
                  price_s = price_s+OOP*OL;
                  s++;LS+= OL;
                  ProfitS+=OrderProfit()+OrderSwap()+OrderCommission();
               }
            }
         }
      }
      ObjectDelete("SLb");
      ObjectDelete("SLs");
      if (b>0) 
      {
         NLb = NormalizeDouble(price_b/LB,Digits);
         ObjectCreate("SLb",OBJ_ARROW,0,Time[0],NLb,0,0,0,0);                     
         ObjectSet   ("SLb",OBJPROP_ARROWCODE,6);
         ObjectSet   ("SLb",OBJPROP_COLOR, Blue);
      }
      if (s>0) 
      {
         NLs = NormalizeDouble(price_s/LS,Digits);
         ObjectCreate("SLs",OBJ_ARROW,0,Time[0],NLs,0,0,0,0);                     
         ObjectSet   ("SLs",OBJPROP_ARROWCODE,6);
         ObjectSet   ("SLs",OBJPROP_COLOR, Red);
      }
      int OTicket;
      for (i=0; i<OrdersTotal(); i++)
      {    
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {
            if (OrderSymbol()==Symbol())
            { 
               OT = OrderType(); 
               OSL = NormalizeDouble(OrderStopLoss(),Digits);
               OTP = NormalizeDouble(OrderTakeProfit(),Digits);
               OOP = NormalizeDouble(OrderOpenPrice(),Digits);
               SL=OSL;TP=OTP;
               if (OT==OP_BUY)             
               {  
                  b++;
                  if (OSL==0 && Stoploss>=STOPLEVEL && Stoploss!=0)
                  {
                      SL = NormalizeDouble(Bid - Stoploss   * Point,Digits);
                  } 
                  else SL=OSL;
                  if (OTP==0 && Takeprofit>=STOPLEVEL && Takeprofit!=0)
                  {
                      TP = NormalizeDouble(Ask + Takeprofit * Point,Digits);
                  } 
                  else TP=OTP;
                  if (NoLoss>=STOPLEVEL && OSL<NLb && NoLoss!=0)
                  {
                     if (OOP<=NLb && NLb!=0 && NLb <= NormalizeDouble(Bid-NoLoss*Point,Digits)) 
                        SL = NormalizeDouble(NLb+MinProfitNoLoss*Point,Digits);
                  }
                  if (TrailingStop>=STOPLEVEL && TrailingStop!=0)
                  {
                     StLo = NormalizeDouble(Bid - TrailingStop*Point,Digits); 
                     if (StLo>=NLb && NLb!=0) if (StLo > OSL) SL = StLo;
                  }
                  if (SL != OSL || TP != OTP)
                  {  
                     OTicket=OrderTicket();
                     if (!OrderModify(OTicket,OOP,SL,TP,0,White)) Print("Error OrderModify ",GetLastError());
                  }
               }                                         
               if (OT==OP_SELL)        
               {
                  s++;
                  if (OSL==0 && Stoploss>=STOPLEVEL && Stoploss!=0)
                  {
                     SL = NormalizeDouble(Ask + Stoploss   * Point,Digits);
                  }
                  else SL=OSL;
                  if (OTP==0 && Takeprofit>=STOPLEVEL && Takeprofit!=0)
                  {
                      TP = NormalizeDouble(Bid - Takeprofit * Point,Digits);
                  }
                  else TP=OTP;
                  if (NoLoss>=STOPLEVEL && (OSL>NLs || OSL==0) && NoLoss!=0)
                  {
                     if (OOP>=NLs && NLs!=0 && NLs >= NormalizeDouble(Ask+NoLoss*Point,Digits)) 
                        SL = NormalizeDouble(NLs-MinProfitNoLoss*Point,Digits);
                  }
                  if (TrailingStop>=STOPLEVEL && TrailingStop!=0)
                  {
                     StLo = NormalizeDouble(Ask + TrailingStop*Point,Digits); 
                     if (StLo<=NLs && NLs!=0) if (StLo < OSL || OSL==0) SL = StLo;
                  }
                  if ((SL != OSL || OSL==0) || TP != OTP)
                  {  
                     OTicket=OrderTicket();
                     if (!OrderModify(OTicket,OOP,SL,TP,0,White)) Print("Error OrderModify ",GetLastError());
                  }
               } 
            }
         }
      }
      if (IsTesting())
      {
         if (TimeBar!=Time[0])
         {
            OrderSend(Symbol(),OP_BUY,1,NormalizeDouble(Ask,Digits),3,0,0,"òåñò",0,0,Blue);
            OrderSend(Symbol(),OP_SELL,1,NormalizeDouble(Bid,Digits),3,0,0,"òåñò",0,0,Red);
            TimeBar=Time[0];
         }
      } 
      //Sleep(1000);
   } 
}
//--------------------------------------------------------------------

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