ChartPriceOnDropped Modification

Author: Copyright © 2019, Vladimir Karputov
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
ChartPriceOnDropped Modification
ÿþ//+------------------------------------------------------------------+

//|                             ChartPriceOnDropped Modification.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

#property script_show_inputs

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

#include <Trade\OrderInfo.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

CSymbolInfo    m_symbol;                     // object of CSymbolInfo class

COrderInfo     m_order;                      // object of COrderInfo class

//---

input ulong    InpDeviation         = 10;    // Deviation, in points (1.00045-1.00055=10 points)

input bool     InpPrintLog          = false; // Print log

input ulong    InpMagic             = 456;   // Magic number

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

//| Script program start function                                    |

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

void OnStart()

  {

//---

   if(!m_symbol.Name(Symbol())) // sets symbol name

     {

      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");

      return;

     }

   RefreshRates();

//---

   m_trade.SetExpertMagicNumber(InpMagic);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(m_symbol.Name());

   m_trade.SetDeviationInPoints(InpDeviation);

//---

   double price_on_dropped=ChartPriceOnDropped();



   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions

      if(m_position.SelectByIndex(i))

         if(m_position.Symbol()==m_symbol.Name())

           {

            double price_current = m_position.PriceCurrent();

            double stop_loss     = m_position.StopLoss();

            //---

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               if(price_on_dropped>price_current)

                 {

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

                                             stop_loss,

                                             m_symbol.NormalizePrice(price_on_dropped)))

                     if(InpPrintLog)

                        Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify BUY ",m_position.Ticket(),

                              " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),

                              ", description of result: ",m_trade.ResultRetcodeDescription());

                  continue;

                 }

              }

            else

              {

               if(price_on_dropped<price_current)

                 {

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

                                             stop_loss,

                                             m_symbol.NormalizePrice(price_on_dropped)))

                     if(InpPrintLog)

                        Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify SELL ",m_position.Ticket(),

                              " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),

                              ", description of result: ",m_trade.ResultRetcodeDescription());

                  continue;

                 }

              }

           }



   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders

      if(m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties

         if(m_order.Symbol()==m_symbol.Name())

           {

            double price_current = m_order.PriceCurrent();

            double price_open    = m_order.PriceOpen();

            double stop_loss     = m_order.StopLoss();

            ENUM_ORDER_TYPE_TIME type_time=m_order.TypeTime();

            datetime expiration  = m_order.TimeExpiration();

            //---

            if(m_order.OrderType()==ORDER_TYPE_BUY_LIMIT)

              {

               if(price_on_dropped>price_open)

                 {

                  if(!m_trade.OrderModify(m_order.Ticket(),price_open,

                                          stop_loss,

                                          m_symbol.NormalizePrice(price_on_dropped),

                                          type_time,expiration))

                     if(InpPrintLog)

                        Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify Buy limit ",m_position.Ticket(),

                              " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),

                              ", description of result: ",m_trade.ResultRetcodeDescription());

                  continue;

                 }

              }

            else

               if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT)

                 {

                  if(price_on_dropped<price_open)

                    {

                     if(!m_trade.OrderModify(m_order.Ticket(),price_open,

                                             stop_loss,

                                             m_symbol.NormalizePrice(price_on_dropped),

                                             type_time,expiration))

                        if(InpPrintLog)

                           Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify Sell limit ",m_position.Ticket(),

                                 " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),

                                 ", description of result: ",m_trade.ResultRetcodeDescription());

                     continue;

                    }

                 }

               else

                  if(m_order.OrderType()==ORDER_TYPE_BUY_STOP)

                    {

                     if(price_on_dropped>price_open)

                       {

                        if(!m_trade.OrderModify(m_order.Ticket(),price_open,

                                                stop_loss,

                                                m_symbol.NormalizePrice(price_on_dropped),

                                                type_time,expiration))

                           if(InpPrintLog)

                              Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify Buy stop ",m_position.Ticket(),

                                    " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),

                                    ", description of result: ",m_trade.ResultRetcodeDescription());

                        continue;

                       }

                    }

                  else

                     if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)

                       {

                        if(price_on_dropped<price_open)

                          {

                           if(!m_trade.OrderModify(m_order.Ticket(),price_open,

                                                   stop_loss,

                                                   m_symbol.NormalizePrice(price_on_dropped),

                                                   type_time,expiration))

                              if(InpPrintLog)

                                 Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify Sell stop ",m_position.Ticket(),

                                       " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),

                                       ", description of result: ",m_trade.ResultRetcodeDescription());

                           continue;

                          }

                       }

           }

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates()

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

     {

      if(InpPrintLog)

         Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");

      return(false);

     }

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

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

     {

      if(InpPrintLog)

         Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");

      return(false);

     }

//---

   return(true);

  }

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

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