Stop Loss OHLC N Nars

Author: Copyright © 2021, Vladimir Karputov
Price Data Components
Series array that contains tick volumes of each bar
Miscellaneous
It issuies visual alerts to the screen
1 Views
0 Downloads
0 Favorites
Stop Loss OHLC N Nars
ÿþ//+------------------------------------------------------------------+

//|                                        Stop Loss OHLC N Nars.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.000"

#property description "Stop Loss in Points (1.00055-1.00045=10 points)"

/*

   barabashkakvn Trading engine 4.003

*/

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

CSymbolInfo    m_symbol;                     // object of CSymbolInfo class

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

//| Enum Bar !urrent                                                 |

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

enum ENUM_BAR_CURRENT

  {

   bar_0=0,    // bar #0 (at every tick)

   bar_1=1,    // bar #1 (on a new bar)

  };

//--- input parameters

input group             "Trading settings"

input ENUM_TIMEFRAMES      InpWorkingPeriod        = PERIOD_CURRENT; // Working timeframe

input uint                 InpMinimumStopLoss      = 150;            // Minimum Stop Loss

input uchar                InpNBars                = 12;             // N Bars

input ENUM_BAR_CURRENT     InpSignalsBarCurrent    = bar_1;          // Search signals on ...

input group             "Rectangle"

input color                Inp_Rectangle_Color     = clrMediumPurple;// Color

input ENUM_LINE_STYLE      Inp_Rectangle_Style     = STYLE_DASH;     // Style

input int                  Inp_Rectangle_Width     = 2;              // Width

input bool                 Inp_Rectangle_Fill      = false;          // Filling  color

input bool                 Inp_Rectangle_Back      = false;          // Background

input bool                 Inp_Rectangle_Selection = false;          // Highlight to move

input bool                 Inp_Rectangle_Hidden    = true;           // Hidden in the object list

input long                 Inp_Rectangle_ZOrder    = 0;              // Priority for mouse click

input group             "Additional features"

input bool                 InpPrintLog             = true;           // Print log

input uchar                InpFreezeCoefficient    = 1;              // Coefficient (if Freeze==0 Or StopsLevels==0)

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

input ulong                InpMagic                = 459876208;      // Magic number

//---

double   m_min_stop_loss   = 0.0;                                 // Minimum Stop Loss -> double

datetime m_prev_bars       = 0;                                   // "0" -> D'1970.01.01 00:00';

bool     m_init_error      = false;                               // error on InInit

string   m_name            = "Stop Loss OHLC N Nars Rectangle";   // Rectangle name

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- forced initialization of variables

   m_min_stop_loss            = 0.0;      // Minimum Stop Loss          -> double

   m_prev_bars                = 0;        // "0" -> D'1970.01.01 00:00';

   m_init_error               = false;    // error on InInit

///---

   if(InpNBars==0)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      ""@59;8=3 =52>7<>65=: ?0@0<5B@ \"N Bars\" @025= =C;N!":

                      "Trailing is not possible: parameter \"N Bars\" is zero!";

      if(MQLInfoInteger(MQL_TESTER)) // when testing, we will only output to the log about incorrect input parameters

         Print(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      else // if the Expert Advisor is run on the chart, tell the user about the error

         Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   ResetLastError();

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

     {

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

      return(INIT_FAILED);

     }

   RefreshRates();

//---

   m_trade.SetExpertMagicNumber(InpMagic);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(m_symbol.Name());

   m_trade.SetDeviationInPoints(InpDeviation);

//---

   m_min_stop_loss            = InpMinimumStopLoss          * m_symbol.Point();

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   ObjectDelete(ChartID(),m_name);

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   if(m_init_error)

      return;

//---

   if(InpSignalsBarCurrent==bar_0) // search for trading signals at every tick

     {

      if(!RefreshRates())

         return;

      if(!SearchTradingSignals())

         return;

     }

//--- we work only at the time of the birth of new bar

   if(InpSignalsBarCurrent==bar_1)

     {

      datetime time_0=iTime(m_symbol.Name(),InpWorkingPeriod,0);

      if(time_0==m_prev_bars)

         return;

      m_prev_bars=time_0;

      if(InpSignalsBarCurrent==bar_1) // search for trading signals only at the time of the birth of new bar

        {

         if(!RefreshRates())

           {

            m_prev_bars=0;

            return;

           }

         //--- search for trading signals

         if(!SearchTradingSignals())

           {

            m_prev_bars=0;

            return;

           }

        }

     }

//---

  }

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

//| TradeTransaction function                                        |

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

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

//---

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates()

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

     {

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

      return(false);

     }

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

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

     {

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

      return(false);

     }

//---

   return(true);

  }

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

//| Check Freeze and Stops levels                                    |

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

void FreezeStopsLevels(double &freeze,double &stops)

  {

//--- check Freeze and Stops levels

   /*

   SYMBOL_TRADE_FREEZE_LEVEL shows the distance of freezing the trade operations

      for pending orders and open positions in points

   ------------------------|--------------------|--------------------------------------------

   Type of order/position  |  Activation price  |  Check

   ------------------------|--------------------|--------------------------------------------

   Buy Limit order         |  Ask               |  Ask-OpenPrice  >= SYMBOL_TRADE_FREEZE_LEVEL

   Buy Stop order          |  Ask               |  OpenPrice-Ask  >= SYMBOL_TRADE_FREEZE_LEVEL

   Sell Limit order        |  Bid               |  OpenPrice-Bid  >= SYMBOL_TRADE_FREEZE_LEVEL

   Sell Stop order         |  Bid               |  Bid-OpenPrice  >= SYMBOL_TRADE_FREEZE_LEVEL

   Buy position            |  Bid               |  TakeProfit-Bid >= SYMBOL_TRADE_FREEZE_LEVEL

                           |                    |  Bid-StopLoss   >= SYMBOL_TRADE_FREEZE_LEVEL

   Sell position           |  Ask               |  Ask-TakeProfit >= SYMBOL_TRADE_FREEZE_LEVEL

                           |                    |  StopLoss-Ask   >= SYMBOL_TRADE_FREEZE_LEVEL

   ------------------------------------------------------------------------------------------



   SYMBOL_TRADE_STOPS_LEVEL determines the number of points for minimum indentation of the

      StopLoss and TakeProfit levels from the current closing price of the open position

   ------------------------------------------------|------------------------------------------

   Buying is done at the Ask price                 |  Selling is done at the Bid price

   ------------------------------------------------|------------------------------------------

   TakeProfit        >= Bid                        |  TakeProfit        <= Ask

   StopLoss          <= Bid                        |  StopLoss          >= Ask

   TakeProfit - Bid  >= SYMBOL_TRADE_STOPS_LEVEL   |  Ask - TakeProfit  >= SYMBOL_TRADE_STOPS_LEVEL

   Bid - StopLoss    >= SYMBOL_TRADE_STOPS_LEVEL   |  StopLoss - Ask    >= SYMBOL_TRADE_STOPS_LEVEL

   ------------------------------------------------------------------------------------------

   */

   double coeff=(double)InpFreezeCoefficient;

   if(!RefreshRates() || !m_symbol.Refresh())

      return;

//--- FreezeLevel -> for pending order and modification

   double freeze_level=m_symbol.FreezeLevel()*m_symbol.Point();

   if(freeze_level==0.0)

      if(InpFreezeCoefficient>0)

         freeze_level=(m_symbol.Ask()-m_symbol.Bid())*coeff;

//--- StopsLevel -> for TakeProfit and StopLoss

   double stop_level=m_symbol.StopsLevel()*m_symbol.Point();

   if(stop_level==0.0)

      if(InpFreezeCoefficient>0)

         stop_level=(m_symbol.Ask()-m_symbol.Bid())*coeff;

//---

   freeze=freeze_level;

   stops=stop_level;

//---

   return;

  }

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

//| Trailing                                                         |

//|   InpTrailingStop: min distance from price to Stop Loss          |

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

void Trailing(const double high,const double low)

  {

   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 price_open    = m_position.PriceOpen();

            double stop_loss     = m_position.StopLoss();

            double take_profit   = m_position.TakeProfit();

            //---

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               if(!CompareDoubles(stop_loss,low,m_symbol.Digits(),m_symbol.Point()))

                  if(stop_loss<low)

                    {

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

                                                m_symbol.NormalizePrice(low),

                                                take_profit))

                        if(InpPrintLog)

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

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

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

                     if(InpPrintLog)

                       {

                        RefreshRates();

                        m_position.SelectByIndex(i);

                        PrintResultModify(m_trade,m_symbol,m_position);

                       }

                     continue;

                    }

              }

            else

              {

               if(!CompareDoubles(stop_loss,low,m_symbol.Digits(),m_symbol.Point()))

                  if(stop_loss>high || stop_loss==0.0)

                    {

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

                                                m_symbol.NormalizePrice(high),

                                                take_profit))

                        if(InpPrintLog)

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

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

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

                     if(InpPrintLog)

                       {

                        RefreshRates();

                        m_position.SelectByIndex(i);

                        PrintResultModify(m_trade,m_symbol,m_position);

                       }

                    }

              }

           }

  }

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

//| Print CTrade result                                              |

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

void PrintResultModify(CTrade &trade,CSymbolInfo &symbol,CPositionInfo &position)

  {

   Print("File: ",__FILE__,", symbol: ",symbol.Name());

   Print("Code of request result: "+IntegerToString(trade.ResultRetcode()));

   Print("code of request result as a string: "+trade.ResultRetcodeDescription());

   Print("Deal ticket: "+IntegerToString(trade.ResultDeal()));

   Print("Order ticket: "+IntegerToString(trade.ResultOrder()));

   Print("Volume of deal or order: "+DoubleToString(trade.ResultVolume(),2));

   Print("Price, confirmed by broker: "+DoubleToString(trade.ResultPrice(),symbol.Digits()));

   Print("Current bid price: "+DoubleToString(symbol.Bid(),symbol.Digits())+" (the requote): "+DoubleToString(trade.ResultBid(),symbol.Digits()));

   Print("Current ask price: "+DoubleToString(symbol.Ask(),symbol.Digits())+" (the requote): "+DoubleToString(trade.ResultAsk(),symbol.Digits()));

   Print("Broker comment: "+trade.ResultComment());

   Print("Freeze Level: "+DoubleToString(symbol.FreezeLevel(),0),", Stops Level: "+DoubleToString(symbol.StopsLevel(),0));

   Print("Price of position opening: "+DoubleToString(position.PriceOpen(),symbol.Digits()));

   Print("Price of position's Stop Loss: "+DoubleToString(position.StopLoss(),symbol.Digits()));

   Print("Price of position's Take Profit: "+DoubleToString(position.TakeProfit(),symbol.Digits()));

   Print("Current price by position: "+DoubleToString(position.PriceCurrent(),symbol.Digits()));

  }

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

//| Search trading signals                                           |

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

bool SearchTradingSignals(void)

  {

   MqlRates rates[];

   ArraySetAsSeries(rates,true);

   int start_pos=0,count=InpNBars;

   if(CopyRates(m_symbol.Name(),InpWorkingPeriod,start_pos,count,rates)!=count)

      return(false);

   double max_high=DBL_MIN,min_low=DBL_MAX;

   for(int i=0; i<count; i++)

     {

      if(rates[i].high>max_high)

         max_high=rates[i].high;

      if(rates[i].low<min_low)

         min_low=rates[i].low;

     }

//---

   long cahrt_id=ChartID();

   if(ObjectFind(cahrt_id,m_name)<0)

     {

      RectangleCreate(cahrt_id,m_name,0,rates[count-1].time,max_high,rates[0].time,min_low,Inp_Rectangle_Color,Inp_Rectangle_Style,Inp_Rectangle_Width,

                      Inp_Rectangle_Fill,Inp_Rectangle_Back,Inp_Rectangle_Selection,Inp_Rectangle_Hidden,Inp_Rectangle_ZOrder);

     }

   else

     {

      RectanglePointChange(cahrt_id,m_name,0,rates[count-1].time,max_high);

      RectanglePointChange(cahrt_id,m_name,1,rates[0].time,min_low);

     }

//---

   Trailing(max_high,min_low);

//---

   return(true);

  }

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

//| Compare doubles                                                  |

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

bool CompareDoubles(double number1,double number2,int digits,double points)

  {

   if(MathAbs(NormalizeDouble(number1-number2,digits))<=points)

      return(true);

   else

      return(false);

  }

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

//| Create rectangle by the given coordinates                        |

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

bool RectangleCreate(const long            chart_ID=0,        // chart's ID

                     const string          name="Rectangle",  // rectangle name

                     const int             sub_window=0,      // subwindow index

                     datetime              time1=0,           // first point time

                     double                price1=0,          // first point price

                     datetime              time2=0,           // second point time

                     double                price2=0,          // second point price

                     const color           clr=clrRed,        // rectangle color

                     const ENUM_LINE_STYLE style=STYLE_SOLID, // style of rectangle lines

                     const int             width=1,           // width of rectangle lines

                     const bool            fill=false,        // filling rectangle with color

                     const bool            back=false,        // in the background

                     const bool            selection=true,    // highlight to move

                     const bool            hidden=true,       // hidden in the object list

                     const long            z_order=0)         // priority for mouse click

  {

////--- set anchor points' coordinates if they are not set

//ChangeRectangleEmptyPoints(time1,price1,time2,price2);

//--- reset the error value

   ResetLastError();

//--- create a rectangle by the given coordinates

   if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE,sub_window,time1,price1,time2,price2))

     {

      Print(__FUNCTION__,

            ": failed to create a rectangle! Error code = ",GetLastError());

      return(false);

     }

//--- set rectangle color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set the style of rectangle lines

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set width of the rectangle lines

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- enable (true) or disable (false) the mode of filling the rectangle

   ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of highlighting the rectangle for moving

//--- when creating a graphical object using ObjectCreate function, the object cannot be

//--- highlighted and moved by default. Inside this method, selection parameter

//--- is true by default making it possible to highlight and move the object

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- hide (true) or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- successful execution

   return(true);

  }

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

//| Move the rectangle anchor point                                  |

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

bool RectanglePointChange(const long   chart_ID=0,       // chart's ID

                          const string name="Rectangle", // rectangle name

                          const int    point_index=0,    // anchor point index

                          datetime     time=0,           // anchor point time coordinate

                          double       price=0)          // anchor point price coordinate

  {

//--- if point position is not set, move it to the current bar having Bid price

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- move the anchor point

   if(!ObjectMove(chart_ID,name,point_index,time,price))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

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