Expert_NEWS

Author: Copyright © 2014, cmillion@narod.ru
Price Data Components
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
0 Views
0 Downloads
0 Favorites
Expert_NEWS
ÿþ//+------------------------------------------------------------------+

//|                         Expert NEWS(barabashkakvn's edition).mq5 | 

//|                              Copyright © 2014, Khlystov Vladimir |

//|                                         http://cmillion.narod.ru |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2014, cmillion@narod.ru"

#property link      "http://cmillion.ru"

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>  

#include <Trade\AccountInfo.mqh>

#include<Trade\OrderInfo.mqh>

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

CAccountInfo   m_account;                    // account info wrapper

COrderInfo     m_order;                      // pending orders object

//--- input parameters

input ushort   InpStoploss          = 10;    //AB>?;>AA, 5A;8 0 B> =5 87<5=O5BAO

input ushort   InpTakeprofit        = 50;    //B59:?@>D8B, 5A;8 0 B> =5 87<5=O5BAO

input ushort   InpTrailingStop      = 10;    //4;8==0 B@0;;0, 5A;8 0 B> =5B B@0;;0

input ushort   InpTrailingStart     = 0;     //:>340 2:;NG0BL B@0;;, =0?@8<5@ ?>A;5 4>AB865=8O 40 ? ?@81K;

input ushort   InpStepTrall         = 2;     //H03 B@0;;0 - ?5@5<5I0BL AB>?;>AA =5 1;865 G5<

input ushort   InpNoLoss            = 0;     //?5@52>4 2 157C1KB>: ?@8 7040==>< :>;-25 ?C=:B>2 ?@81K;8, 5A;8 0 B> =5B ?5@52>40 2 157C1KB>:

input ushort   InpMinProfitNoLoss   = 0;     //<8=8<0;L=0O ?@81K;L ?@8 ?5@52>45 2 157C1KB>:

input ulong    Magic                = 77;    //<038:

input ushort   InpStep              = 10;    //@0AAB>O=85 >B F5=K

input double   Lot                  = 0.1;   //;>B

input ushort   TimeModify           = 300;   //:>;-2> A5:C=4 @0=LH5 :>B>@>3> 70?@5I5=> 87<5=OBL >@45@ (max 65 535)

input ushort   InpSlippage          = 30;    //0:A8<0;L=> 4>?CAB8<>5 >B:;>=5=85 F5=K 4;O @K=>G=KE >@45@>2 (>@45@>2 =0 ?>:C?:C 8;8 ?@>406C).

//---

int  STOPLEVEL=0;

datetime TimeBarB=0,TimeBarS=0;

//---

double   ExtStoploss          = 0;

double   ExtTakeprofit        = 0;

double   ExtTrailingStop      = 0;

double   ExtTrailingStart     = 0;

double   ExtStepTrall         = 0;

double   ExtNoLoss            = 0;

double   ExtMinProfitNoLoss   = 0;

double   ExtStep              = 0;

ulong    ExtSlippage          = 0;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

//if(m_account.MarginMode()!=ACCOUNT_MARGIN_MODE_RETAIL_HEDGING)

//  {

//   Print("Hedging only!");

//   return(INIT_FAILED);

//  }

//---

   m_symbol.Name(Symbol());                  // sets symbol name

   m_trade.SetExpertMagicNumber(Magic);      // sets magic number



   if(!RefreshRates())

     {

      Print("Error RefreshRates.",

            " Bid=",DoubleToString(m_symbol.Bid(),Digits()),

            ", Ask=",DoubleToString(m_symbol.Ask(),Digits()));

      return(INIT_FAILED);

     }

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)

      digits_adjust=10;



   ExtStoploss          = InpStoploss        *digits_adjust;

   ExtTakeprofit        = InpTakeprofit      *digits_adjust;

   ExtTrailingStop      = InpTrailingStop    *digits_adjust;

   ExtTrailingStart     = InpTrailingStart   *digits_adjust;

   ExtStepTrall         = InpStepTrall       *digits_adjust;

   ExtNoLoss            = InpNoLoss          *digits_adjust;

   ExtMinProfitNoLoss   = InpMinProfitNoLoss *digits_adjust;

   ExtStep              = InpStep            *digits_adjust;

   ExtSlippage          = InpSlippage        *digits_adjust;



   m_trade.SetDeviationInPoints(ExtSlippage);    // sets deviation

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

   STOPLEVEL=m_symbol.StopsLevel();

   double PSL,StLo,PriceB=0,PriceS=0,PPO=0,SL=0,TP=0;

   int b=0,s=0;

   ENUM_POSITION_TYPE  PosType;

   ulong TicketB=0,TicketS=0;



   if(!RefreshRates())

      return;



   for(int i=PositionsTotal()-1;i>=0;i--)

     {

      if(m_position.SelectByIndex(i))

        {

         if(m_position.Symbol()==Symbol() && Magic==m_position.Magic())

           {

            PosType=m_position.PositionType();

            PSL = NormalizeDouble(m_position.StopLoss(),Digits());

            PPO = NormalizeDouble(m_position.PriceOpen(),Digits());

            SL=PSL;

            if(PosType==POSITION_TYPE_BUY)

              {

               b++;

               if(PSL<PPO && ExtNoLoss!=0)

                 {

                  StLo=NormalizeDouble(PPO+ExtMinProfitNoLoss*Point(),Digits());

                  if(StLo>PSL && StLo<=NormalizeDouble(m_symbol.Bid()-STOPLEVEL*Point(),Digits()))

                     SL=StLo;

                 }



               if(ExtTrailingStop>=STOPLEVEL && ExtTrailingStop!=0 && (m_symbol.Bid()-PPO)/Point()>=ExtTrailingStart)

                 {

                  StLo=NormalizeDouble(m_symbol.Bid()-ExtTrailingStop *Point(),Digits());

                  if(StLo>=PPO && StLo>PSL+ExtStepTrall*Point())

                     SL=StLo;

                 }



               if(SL>PSL)

                 {

                  if(!m_trade.PositionModify(m_position.Ticket(),SL,TP))

                     Print("Error ",m_trade.ResultRetcode(),

                           "(",m_trade.ResultRetcodeDescription(),"), Order Modify Buy SL ",PSL,"-> ",SL);

                  else

                     Print("Order Buy Modify SL ",PSL,"-> ",SL);

                 }

              }

            if(PosType==POSITION_TYPE_SELL)

              {

               s++;

               if((PSL>PPO || PSL==0) && ExtNoLoss!=0)

                 {

                  StLo=NormalizeDouble(PPO-ExtMinProfitNoLoss*Point(),Digits());

                  if((StLo<PSL || PSL==0) && StLo>=NormalizeDouble(m_symbol.Ask()+STOPLEVEL*Point(),Digits()))

                     SL=StLo;

                 }



               if(ExtTrailingStop>=STOPLEVEL && ExtTrailingStop!=0 && (PPO-m_symbol.Ask())/Point()>=ExtTrailingStart)

                 {

                  StLo=NormalizeDouble(m_symbol.Ask()+ExtTrailingStop *Point(),Digits());

                  if(StLo<=PPO && (StLo<PSL-ExtStepTrall*Point() || PSL==0))

                     SL=StLo;

                 }



               if((SL<PSL || PSL==0) && SL!=0)

                 {

                  if(!m_trade.PositionModify(m_position.Ticket(),SL,TP))

                     Print("Error ",m_trade.ResultRetcode(),

                           "(",m_trade.ResultRetcodeDescription(),"), Order Modify Sell SL ",PSL,"-> ",SL);

                  else

                     Print("Order Sell Modify SL ",PSL,"-> ",SL);

                 }

              }

           }

        }

     }



   for(int i=OrdersTotal()-1;i>=0;i--)

     {

      if(m_order.SelectByIndex(i))

        {

         if(m_order.Symbol()==Symbol() && Magic==m_order.Magic())

           {

            if(m_order.OrderType()==ORDER_TYPE_BUY_STOP)

              {

               PriceB=PPO; TicketB=m_order.Ticket();

              }

            if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)

              {

               PriceS=PPO; TicketS=m_order.Ticket();

              }

           }

        }

     }



   if(b+TicketB==0)

     {

      if(ExtStoploss>=STOPLEVEL && ExtStoploss!=0)

         SL=NormalizeDouble(m_symbol.Bid()-ExtStoploss*Point(),Digits());

      else

         SL=0;



      if(ExtTakeprofit>=STOPLEVEL && ExtTakeprofit!=0)

         TP=NormalizeDouble(m_symbol.Ask()+ExtTakeprofit*Point(),Digits());

      else

         TP=0;



      if(m_trade.BuyStop(Lot,NormalizeDouble(m_symbol.Ask()+ExtStep*Point(),Digits()),

         Symbol(),SL,TP,0,0,"news"))

         TimeBarB=TimeCurrent();

     }

   if(s+TicketS==0)

     {

      if(ExtStoploss>=STOPLEVEL && ExtStoploss!=0)

         SL=NormalizeDouble(m_symbol.Ask()+ExtStoploss*Point(),Digits());

      else SL=0;



      if(ExtTakeprofit>=STOPLEVEL && ExtTakeprofit!=0)

         TP=NormalizeDouble(m_symbol.Bid()-ExtTakeprofit*Point(),Digits());

      else TP=0;



      if(m_trade.SellStop(Lot,NormalizeDouble(m_symbol.Bid()-ExtStep*Point(),Digits()),

         Symbol(),SL,TP,0,0,"news"))

         TimeBarS=TimeCurrent();

     }



   if(TicketB!=0)

     {

      if(m_order.Select(TicketB))

        {

         double order_sl=m_order.StopLoss();

         double order_tp=m_order.TakeProfit();

         double order_price=m_order.PriceOpen();

         if(TimeBarB<TimeCurrent()-TimeModify && 

            MathAbs(NormalizeDouble(m_symbol.Ask()+ExtStep*Point(),Digits())-PriceB)/Point()>ExtStepTrall)

           {

            if(ExtStoploss>=STOPLEVEL && ExtStoploss!=0)

               SL=NormalizeDouble(m_symbol.Bid()-ExtStoploss*Point(),Digits());

            else

               SL=0;



            if(ExtTakeprofit>=STOPLEVEL && ExtTakeprofit!=0)

               TP=NormalizeDouble(m_symbol.Ask()+ExtTakeprofit*Point(),Digits());

            else

               TP=0;



            double price=NormalizeDouble(m_symbol.Ask()+ExtStep*Point(),Digits());

            //--- protection against "[no changes]"

            if(!CompareDoubles(price,order_price))

               if(m_trade.OrderModify(TicketB,price,SL,TP,0,0))

                  TimeBarB=TimeCurrent();

           }

        }

     }

   if(TicketS!=0)

     {

      if(m_order.Select(TicketS))

        {

         double order_sl=m_order.StopLoss();

         double order_tp=m_order.TakeProfit();

         double order_price=m_order.PriceOpen();

         if(TimeBarS<TimeCurrent()-TimeModify && 

            MathAbs(NormalizeDouble(m_symbol.Bid()-ExtStep*Point(),Digits())-PriceS)/Point()>ExtStepTrall)

           {

            if(ExtStoploss>=STOPLEVEL && ExtStoploss!=0)

               SL=NormalizeDouble(m_symbol.Ask()+ExtStoploss*Point(),Digits());

            else SL=0;



            if(ExtTakeprofit>=STOPLEVEL && ExtTakeprofit!=0)

               TP=NormalizeDouble(m_symbol.Bid()-ExtTakeprofit*Point(),Digits());

            else TP=0;



            double price=NormalizeDouble(m_symbol.Bid()-ExtStep*Point(),Digits());

            //--- protection against "[no changes]"

            if(!CompareDoubles(price,order_price))

               if(m_trade.OrderModify(TicketS,price,SL,TP,0,0))

                  TimeBarS=TimeCurrent();

           }

        }

     }

   return;

  }

//+------------------------------------------------------------------+

//| Refreshes the symbol quotes data                                 |

//+------------------------------------------------------------------+

bool RefreshRates()

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

      return(false);

//--- protection against the return value of "zero"

   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)

      return(false);

//---

   return(true);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

bool CompareDoubles(double number1,double number2)

  {

   if(NormalizeDouble(number1-number2,Digits()-1)==0)

      return(true);

   else

      return(false);

  }

//--------------------------------------------------------------------

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