Alligator Save Positions

Author: Copyright © 2021, Vladimir Karputov
Price Data Components
Series array that contains tick volumes of each bar
Indicators Used
Bill Williams Alligator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Alligator Save Positions
ÿþ//+------------------------------------------------------------------+

//|                                     Alligator Save Positions.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 "The EA performs two functions:"

#property description "---> 1: Sets a breakeven"

#property description "---> 2: Trails along the 'Teeth' line"

/*

   barabashkakvn Trading engine 4.004

*/

#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 ENUM_BAR_CURRENT     InpTrailingBarCurrent   = bar_1;          // Trailing on ...

input uint                 InpTrailingStop         = 250;            // Trailing Stop (min distance from price to Stop Loss)

input uint                 InpTrailingStep         = 50;             // Trailing Step

input group             "Alligator"

input int                  Inp_Alligator_jaw_period      = 13;             // Alligator: period for the calculation of jaws

input int                  Inp_Alligator_jaw_shift       = 8;              // Alligator: horizontal shift of jaws

input int                  Inp_Alligator_teeth_period    = 8;              // Alligator: period for the calculation of teeth

input int                  Inp_Alligator_teeth_shift     = 5;              // Alligator: horizontal shift of teeth

input int                  Inp_Alligator_lips_period     = 5;              // Alligator: period for the calculation of lips

input int                  Inp_Alligator_lips_shift      = 3;              // Alligator: horizontal shift of lips

input ENUM_MA_METHOD       Inp_Alligator_ma_method       = MODE_SMMA;      // Alligator: type of smoothing

input ENUM_APPLIED_PRICE   Inp_Alligator_applied_price   = PRICE_MEDIAN;   // Alligator: type of price

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)

//---

double   m_trailing_stop            = 0.0;      // Trailing Stop              -> double

double   m_trailing_step            = 0.0;      // Trailing Step              -> double



int      handle_iAlligator;                     // variable for storing the handle of the iAlligator indicator



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

int      m_bar_current              = 0;

bool     m_init_error               = false;    // error on InInit

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- forced initialization of variables

   m_trailing_stop            = 0.0;      // Trailing Stop              -> double

   m_trailing_step            = 0.0;      // Trailing Step              -> double

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

   m_bar_current              = 0;

   m_init_error               = false;    // error on InInit

//---

   if(InpTrailingStop!=0 && InpTrailingStep==0)

     {

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

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

                      "Trailing is not possible: parameter \"Trailing Step\" 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(0);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(m_symbol.Name());

   m_trade.SetDeviationInPoints(InpDeviation);

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

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

      digits_adjust=10;

//---

   m_trailing_stop            = InpTrailingStop             * m_symbol.Point();

   m_trailing_step            = InpTrailingStep             * m_symbol.Point();

//--- create handle of the indicator iAlligator

   handle_iAlligator=iAlligator(m_symbol.Name(),InpWorkingPeriod,

                                Inp_Alligator_jaw_period,Inp_Alligator_jaw_shift,

                                Inp_Alligator_teeth_period,Inp_Alligator_teeth_shift,

                                Inp_Alligator_lips_period,Inp_Alligator_lips_shift,

                                Inp_Alligator_ma_method,Inp_Alligator_applied_price);

//--- if the handle is not created

   if(handle_iAlligator==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iAlligator indicator for the symbol %s/%s, error code %d",

                  m_symbol.Name(),

                  EnumToString(InpWorkingPeriod),

                  GetLastError());

      //--- the indicator is stopped early

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   m_bar_current=(InpTrailingBarCurrent==bar_1)?1:0;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   if(m_init_error)

      return;

//---

   if(InpTrailingBarCurrent==bar_0) // trailing at every tick

      TrailingIndicator();

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

   if(InpTrailingBarCurrent==bar_1)

     {

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

      if(time_0==m_prev_bars)

         return;

      m_prev_bars=time_0;

      if(InpTrailingBarCurrent==bar_1) // trailing only at the time of the birth of new bar

         TrailingIndicator();

     }

//---

  }

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

//| TradeTransaction function                                        |

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

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

//---

  }

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

//| Compare doubles                                                  |

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

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

  {

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

      return(true);

   else

      return(false);

  }

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

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

  }

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

//| Get value of buffers                                             |

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

bool iGetArray(const int handle,const int buffer,const int start_pos,

               const int count,double &arr_buffer[])

  {

   bool result=true;

   if(!ArrayIsDynamic(arr_buffer))

     {

      if(InpPrintLog)

         PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);

      return(false);

     }

   ArrayFree(arr_buffer);

//--- reset error code

   ResetLastError();

//--- fill a part of the iBands array with values from the indicator buffer

   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);

   if(copied!=count)

     {

      //--- if the copying fails, tell the error code

      if(InpPrintLog)

         PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",

                     __FILE__,__FUNCTION__,count,copied,GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

   return(result);

  }

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

//| Indicator trailing                                               |

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

void TrailingIndicator(void)

  {

   double freeze=0.0,stops=0.0;

   FreezeStopsLevels(freeze,stops);

   /*

   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

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

   */

   double lips[],jaws[],teeth[];

   ArraySetAsSeries(lips,true);

   ArraySetAsSeries(teeth,true);

   ArraySetAsSeries(jaws,true);

   int start_pos=0,count=3;

   if(!iGetArray(handle_iAlligator,GATORLIPS_LINE,start_pos,count,lips) || !iGetArray(handle_iAlligator,GATORJAW_LINE,start_pos,count,jaws) ||

      !iGetArray(handle_iAlligator,GATORTEETH_LINE,start_pos,count,teeth))

      return;

//--- indicator trailing: no 'Trailing Stop' and no 'Trailing Step'

   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();

            double ask           = m_symbol.Ask();

            double bid           = m_symbol.Bid();

            //---

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               if(teeth[m_bar_current]>price_open)

                  if(stop_loss<teeth[m_bar_current] && !CompareDoubles(stop_loss,teeth[m_bar_current],m_symbol.Digits(),m_symbol.Point()))

                     if(price_current-teeth[m_bar_current]>=freeze && (take_profit-bid>=freeze || take_profit==0.0))

                        if(price_current-m_trailing_stop>=teeth[m_bar_current]) // (min distance from price to Stop Loss)

                          {

                           m_trade.SetExpertMagicNumber(m_position.Magic());

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

                                                      m_symbol.NormalizePrice(teeth[m_bar_current]),

                                                      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(teeth[m_bar_current]<price_open)

                  if((stop_loss>teeth[m_bar_current] && !CompareDoubles(stop_loss,teeth[m_bar_current],m_symbol.Digits(),m_symbol.Point())) || stop_loss==0.0)

                     if(teeth[m_bar_current]-price_current>=freeze && ask-take_profit>=freeze)

                        if(price_current+m_trailing_stop<=teeth[m_bar_current]) // (min distance from price to Stop Loss)

                          {

                           m_trade.SetExpertMagicNumber(m_position.Magic());

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

                                                      m_symbol.NormalizePrice(teeth[m_bar_current]),

                                                      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()));

  }

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

//| Gets the information about permission to trade                   |

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

bool IsTradeAllowed()

  {

   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))

     {

      /*if(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")

         Alert("@>25@LB5 2 =0AB@>9:0E B5@<8=0;0 @07@5H5=85 =0 02B><0B8G5A:CN B>@3>2;N!");

      else

         Alert("Check if automated trading is allowed in the terminal settings!");*/

      return(false);

     }

   else

     {

      if(!MQLInfoInteger(MQL_TRADE_ALLOWED))

        {

         /*if(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")

            Alert("2B><0B8G5A:0O B>@3>2;O 70?@5I5=0 2 A2>9AB20E ?@>3@0<<K 4;O ",__FILE__);

         else

            Alert("Automated trading is forbidden in the program settings for ",__FILE__);*/

         return(false);

        }

     }

   if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT))

     {

      /*if(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")

         Alert("2B><0B8G5A:0O B>@3>2;O 70?@5I5=0 4;O AG5B0 ",AccountInfoInteger(ACCOUNT_LOGIN),

               " =0 AB>@>=5 B>@3>2>3> A5@25@0");

      else

         Alert("Automated trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),

               " at the trade server side");*/

      return(false);

     }

   if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED))

     {

      /*if(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")

         Comment("">@3>2;O 70?@5I5=0 4;O AG5B0 ",AccountInfoInteger(ACCOUNT_LOGIN),

                 ".\n >7<>6=>, ?>4:;NG5=85 : B>@3>2><C AG5BC ?@>872545=> ?> 8=25AB ?0@>;N.",

                 "\n @>25@LB5 6C@=0; B5@<8=0;0, 5ABL ;8 B0< B0:0O 70?8AL:",

                 "\n\'",AccountInfoInteger(ACCOUNT_LOGIN),"\': trading has been disabled - investor mode.");

      else

         Comment("Trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),

                 ".\n Perhaps an investor password has been used to connect to the trading account.",

                 "\n Check the terminal journal for the following entry:",

                 "\n\'",AccountInfoInteger(ACCOUNT_LOGIN),"\': trading has been disabled - investor mode.");*/

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