Alligator_v1

Author: Copyright © 2008, Демёхин Виталий Евгеньевич.
Price Data Components
Series array that contains the highest prices of each bar
Orders Execution
Checks for the total of open orders
Indicators Used
Bill Williams AlligatorFractals
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Alligator_v1
ÿþ//+------------------------------------------------------------------+

//|                           Alligator(barabashkakvn's edition).mq5 |

//|                      Copyright © 2008, MetaQuotes Software Corp. |

//|                                        http://www.metaquotes.net |

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

#property copyright "Copyright © 2008, 5<QE8= 8B0;89 235=L528G."

#property link      "Vitalya_1983@list.ru"

#property version   "1.001"

#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 double MaxLot           = 0.5;         //>3@0=8G8205< @07<5@ AB0@B>2>3> ;>B0

input double koeff            = 1.3;         // :>MDD8F85=B C25;8G5=8O ;>B>2 ?@8 <0@B8=359;5

input double risk             = 0.04;        // @8A:, 2;8O5B =0 @07<5@ AB0@B>2>3> ;>B0

input double shirina1         = 0.0005;      // H8@8=0 "7520" Alligator'0 =0 >B:@KB85 >@45@0

input double shirina2         = 0.0001;      // H8@8=0 "7520" Alligator'0 =0 70:@KB85 >@45@0



input bool   Ruchnik          = false;       // ";02=>" 7025@H05B A5AA8N. A;8 =04> >B:;NO8BL A>25B=8:

input bool   Vhod_Alligator   = true;        //  07@5H05B >B:@K20BL >@45@0 ;;830B>@C

input bool   Vhod_Fractals    = false;       // @>25@:0 ;;830B>@0 $@0:B0;><

input bool   Vyhod_Alligator  = false;       //  07@5H05B 70:@K20BL >@45@0 ;;830B>@C

input bool   OnlyOneOrder     = true;        // A;8 False, 2:;NG05BAO 4>:C?:0 ?@8 ?>2B>@5 A83=0;0

input bool   EnableMartingail = true;        // 0@B8=359;

input bool   Trailing         = true;        // "@59;8=3!B>?

//---

input ushort InpTP            = 80;          // TP

input ushort InpSL            = 80;          // SL 

input ushort InpTrailingStep  = 10;          // TrailingStep

input ushort InpProfitPips    = 20;          // @81K;L, pips

input ushort blue             = 0;

input ushort red              = 5;

input ushort green            = 16;

input int    Fractal_bars     = 10;          // >;8G5AB2> 10@>2...

input ushort InpFractalHeight = 30;          // A@548 :>B>@KE 8I5BAO A>>B25BAB2CNI89 ?> 2KA>B5 8 =0?@02;5=8N D@0:B0;

input int    Koleno           = 10;          // >;8G5AB2> ">;5=" 0@B8=359;0

input ushort InpStepKoleno    = 50;          // (03 :>;5=0

//---

bool Proverka_buy,Proverka_sell,Trailing_buy,Trailing_sell,Vihod_Alligator_sell,Vihod_Alligator_buy,Fractal;

int  current_buy=1,current_sell=1,prev_buy,prev_sell;

//string text;

double up,down;

ulong m_magic;

//---

double ExtTP            = 0.0;

double ExtSL            = 0.0;

double ExtTrailingStep  = 0.0;

double ExtProfitPips    = 0.0;

double ExtFractalHeight = 0.0;

double ExtStepKoleno    = 0.0;

//---

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

int    handle_iFractals;                     // variable for storing the handle of the iFractals indicator 

ENUM_ACCOUNT_MARGIN_MODE m_margin_mode;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

   SetMarginMode();

   if(!IsHedging())

     {

      Print("Hedging only!");

      return(INIT_FAILED);

     }

//--- create handle of the indicator iAlligator

   handle_iAlligator=iAlligator(Symbol(),Period(),13,8,8,5,5,3,MODE_SMMA,PRICE_WEIGHTED);

//--- 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",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//--- create handle of the indicator iFractals

   handle_iFractals=iFractals(Symbol(),Period());

//--- if the handle is not created 

   if(handle_iFractals==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//---

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

   if(!RefreshRates())

     {

      Print("Error RefreshRates. Bid=",DoubleToString(m_symbol.Bid(),Digits()),

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

      return(INIT_FAILED);

     }

   m_symbol.Refresh();

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

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

      digits_adjust=10;



   ExtTP            = InpTP            * digits_adjust * m_symbol.Point();

   ExtSL            = InpSL            * digits_adjust * m_symbol.Point();

   ExtTrailingStep  = InpTrailingStep  * digits_adjust * m_symbol.Point();

   ExtProfitPips    = InpProfitPips    * digits_adjust * m_symbol.Point();

   ExtFractalHeight = InpFractalHeight * digits_adjust * m_symbol.Point();

   ExtStepKoleno    = InpStepKoleno    * digits_adjust * m_symbol.Point();



   m_magic=Period();     //05< 038G5A:>5 G8A;> 4;O B>@3>2;8 =0 @07=KE "$

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



   string text="0 Alligator5:   ";

   text=text+Symbol()+"  "+EnumToString(Period())+" ";

   if(Vhod_Alligator)

     {

      text=text+";;830B>@ 2:;  ";

     }

   if(Vhod_Fractals)

     {

      text=text+"Fraktals 2:;  ";

     }

   if(!OnlyOneOrder)

     {

      text=text+"4>:C?:0 2:;  ";

     }

   if(EnableMartingail)

     {

      text=text+"0@B8=359; 2:;  ";

     }

   if(Trailing)

     {

      text=text+"Trailing 2:;  ";

     }

   Alert(text);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//text="";

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//RefreshRates();

// bool order_est_buy = false;      //A1@0AK205< AG5BG8: >B:@KBKE >@45@>2

//  int m_magic = Period();     //05< 038G5A:>5 G8A;> 4;O B>@3>2;8 =0 @07=KE "$

//int Orders=OrdersTotal();      //AG5BG8: ?@>25@:8 >@45@>2

   if(Trailing==true)

     {

      Trailing_start();             // 0?CA:05< B@59;8=3 AB>?

     }

   if(Vhod_Alligator==true) // A?>;L7CO =0AB@>9:8 ;;830B>@0 >?@545;O5< <><5=B 2E>40 2 @K=>:....

     {

      prev_buy=current_buy;         // "!53>4=O B0: 65 :0: 8 2G5@0" :)

      prev_sell=current_sell;



      double blue_line=iAlligatorGet(GATORJAW_LINE,blue);

      double red_line=iAlligatorGet(GATORTEETH_LINE,red);

      double green_line=iAlligatorGet(GATORLIPS_LINE,green);



      double  PriceHigh=iHigh(m_symbol.Name(),Period(),0);



      if(green_line>blue_line+shirina1) // A83=0;K 2E>40

         current_buy=1;



      if(blue_line>green_line+shirina1)

         current_sell=1;



      if(green_line+shirina2<red_line) // A83=0;K 2KE>40

         current_buy=0;



      if(blue_line+shirina2<red_line)

         current_sell=0;

     }



   if(Vhod_Fractals) //@>25@O5< D@0:B0;><

     {

      Fractal();

     }

   if(!Proverka_buy() || OnlyOneOrder==false) // A;8 >B:@KBKE Buy =5B...

     {

      //--- 70:@K205< >B;>65==K5 ORDER_TYPE_BUY_LIMIT

      DeleteOrders(ORDER_TYPE_BUY_LIMIT);



      if(Ruchnik==false && (current_buy>prev_buy || Vhod_Alligator==false)) // 5A;8 ;;830B>@ 40; :><0=4C 8;8 =5 <5H05B...

         if(up>0 || Vhod_Fractals==false) // 8 D@0:B0; 405B 4>1@>

            LongOpen();                             // >B:@K205< 4;8==CN

     }

   if(!Proverka_sell() || OnlyOneOrder==false) // B>65 A0<>5, B>;L:> 4;O Sell...

     {

      //--- 70:@K205< >B;>65==K5 ORDER_TYPE_SELL_LIMIT

      DeleteOrders(ORDER_TYPE_SELL_LIMIT);



      if(Ruchnik==false && (current_sell>prev_sell || Vhod_Alligator==false))

         if(down>0 || Vhod_Fractals==false)

            ShortOpen();

     }

   if(Vyhod_Alligator==true) // 5A;8 ;;830B>@ 40; :><0=4C =0 70:@KB85

     {

      if(current_buy<prev_buy && Vihod_Alligator_buy==true)

        {

         // 70:@K205< ?>78F88 Buy

         ClosePositions(POSITION_TYPE_BUY);

        }

      if(current_sell<prev_sell && Vihod_Alligator_sell==true)

        {

         // 70:@K205< ?>78F88 Sell

         ClosePositions(POSITION_TYPE_BUY);

        }

     }

   return;

  }

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

//| @>25@:0 =0;8G8O >B:@KBKE BUY ?>78F89                            |

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

bool Proverka_buy()

  {

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

      if(m_position.SelectByIndex(i))

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

            if(m_position.PositionType()==POSITION_TYPE_BUY)

               return(true);



   Vihod_Alligator_buy=true;

   return (false);

  }

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

//| @>25@:0 =0;8G8O >B:@KBKE SEll ?>78F89                           |

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

bool Proverka_sell()

  {

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

      if(m_position.SelectByIndex(i))

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

            if(m_position.PositionType()==POSITION_TYPE_SELL)

               return(true);



   Vihod_Alligator_sell=true;

   return (false);

  }

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

//|                                                                  |

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

void ShortOpen()

  {

   double Lot=m_account.FreeMargin()/1000*risk;

   Lot=LotCheck(Lot);

   if(Lot==0)

      return;



   if(EnableMartingail==true)

      ExtTP=ExtSL/koeff;



   if(!RefreshRates())

      return;



   double new_tp=0.0;

   if(ExtTP>0)

      new_tp=m_symbol.Bid()-ExtTP;



   m_trade.Sell(Lot,Symbol(),m_symbol.Bid(),m_symbol.Bid()+ExtSL,new_tp);

   if(EnableMartingail==true) //A;8 <K 8A?>;L7C5< <0@B8=359;

     {

      for(int i=1; i<=Koleno; i++)

        {

         Lot=Lot*koeff;

         Lot=LotCheck(Lot);

         if(Lot==0)

            return;



         if(!RefreshRates())

            return;



         double price_new=NormalizeDouble(m_symbol.Bid()+ExtStepKoleno*(i),Digits());

         double sl_new=NormalizeDouble(price_new+ExtSL,Digits());

         double tp_new=NormalizeDouble(price_new-ExtTP,Digits());



         m_trade.SellLimit(Lot,price_new,Symbol(),sl_new,tp_new);

        }

     }

  }

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

//|                                                                  |

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

void LongOpen()

  {

   double Lot=m_account.FreeMargin()/1000*risk;

   Lot=LotCheck(Lot);

   if(Lot==0)

      return;



   if(EnableMartingail==true)

      ExtTP=ExtSL/koeff;



   if(!RefreshRates())

      return;



   double new_tp=0.0;

   if(ExtTP>0)

      new_tp=m_symbol.Ask()+ExtTP;



   m_trade.Buy(Lot,Symbol(),m_symbol.Ask(),m_symbol.Ask()-ExtSL,new_tp);



   if(EnableMartingail==true)

     {

      for(int i=1; i<=Koleno; i++)

        {

         Lot=Lot*koeff;

         Lot=LotCheck(Lot);

         if(Lot==0)

            return;



         if(!RefreshRates())

            return;



         double price_new=NormalizeDouble(m_symbol.Ask()-ExtStepKoleno*(i),Digits());

         double sl_new=NormalizeDouble(price_new-ExtSL,Digits());

         double tp_new=NormalizeDouble(price_new+ExtTP,Digits());

         m_trade.BuyLimit(Lot,price_new,Symbol(),sl_new,tp_new);

        }

     }

  }

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

//|                                                                  |

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

void Trailing_start()

  {

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

      if(m_position.SelectByIndex(i))

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

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               //--- <>48D8:0F8O SL

               if(ExtSL>0.0)

                 {

                  if(!RefreshRates())

                     return;



                  if(m_symbol.Bid()-ExtSL-ExtTrailingStep>m_position.StopLoss())

                    {

                     m_trade.PositionModify(m_position.Ticket(),m_symbol.Bid()-ExtSL,m_position.TakeProfit());

                     Vihod_Alligator_buy=false;

                    }

                 }

              }

            if(m_position.PositionType()==POSITION_TYPE_SELL)

              {

               //--- <>48D8:0F8O SL

               if(ExtSL>0.0)

                 {

                  if(!RefreshRates())

                     return;



                  if(m_symbol.Ask()+ExtSL+ExtTrailingStep<m_position.StopLoss())

                    {

                     m_trade.PositionModify(m_position.Ticket(),m_symbol.Bid()+ExtSL,m_position.TakeProfit());

                     Vihod_Alligator_buy=false;

                    }

                 }

              }

           }

   return;

  }

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

//|                                                                  |

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

void Fractal()

  {

//--- !1@>A A83=0;>2

   up=0;

   down=0;

   for(int i=Fractal_bars;i>=3;i--)

     {

      //--- 2 =0H8E 10@0E 4>;65= 1KBL D@0:B0;

      double up_prov=iFractalsGet(UPPER_LINE,i);

      if(up_prov>up)

         up=up_prov;

      double down_prov=iFractalsGet(LOWER_LINE,i);

      if(down_prov>down)

         down=down_prov;

     }

   if(!RefreshRates())

      return;



//--- 8 MB>B D@0:B0; 4>;65= 1KBL 70<5B5=

   if(up<m_symbol.Ask()+ExtFractalHeight)

      up=0;

   if(down<m_symbol.Bid()-ExtFractalHeight)

      down=0;

  }

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

//| Get value of buffers for the iAlligator                          |

//|  the buffer numbers are the following:                           |

//|   0 - GATORJAW_LINE, 1 - GATORTEETH_LINE, 2 - GATORLIPS_LINE     |

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

double iAlligatorGet(const int buffer,const int index)

  {

   double Alligator[1];

//--- reset error code 

   ResetLastError();

//--- fill a part of the iStochasticBuffer array with values from the indicator buffer that has 0 index 

   if(CopyBuffer(handle_iAlligator,buffer,index,1,Alligator)<0)

     {

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

      PrintFormat("Failed to copy data from the iAlligator indicator, error code %d",GetLastError());

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

      return(0.0);

     }

   return(Alligator[0]);

  }

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

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

  }

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

//| Get value of buffers for the iFractals                           |

//|  the buffer numbers are the following:                           |

//|   0 - UPPER_LINE, 1 - LOWER_LINE                                 |

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

double iFractalsGet(const int buffer,const int index)

  {

   double Fractals[];

   ArraySetAsSeries(Fractals,true);

//--- reset error code 

   ResetLastError();

//--- fill a part of the iFractalsBuffer array with values from the indicator buffer that has 0 index 

   if(CopyBuffer(handle_iFractals,buffer,0,index+1,Fractals)<0)

     {

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

      PrintFormat("Failed to copy data from the iFractals indicator, error code %d",GetLastError());

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

      return(0.0);

     }

   return(Fractals[index]);

  }

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

//| Delete Orders                                                    |

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

void DeleteOrders(ENUM_ORDER_TYPE order_type)

  {

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

      if(m_order.SelectByIndex(i))

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

            if(m_order.OrderType()==order_type)

               m_trade.OrderDelete(m_order.Ticket());

  }

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

//| Lot Check                                                        |

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

double LotCheck(double lots)

  {

//--- calculate maximum volume

   double volume=NormalizeDouble(lots,2);

   double stepvol=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

   if(stepvol>0.0)

      volume=stepvol*MathFloor(volume/stepvol);

//---

   double minvol=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(volume<minvol)

      volume=0.0;

//---

   double maxvol=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(volume>maxvol)

      volume=maxvol;

   return(volume);

  }

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

//| Close Positions                                                  |

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

void ClosePositions(ENUM_POSITION_TYPE pos_type)

  {

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

      if(m_position.SelectByIndex(i))

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

            if(m_position.PositionType()==pos_type)

               m_trade.PositionClose(m_position.Ticket());

  }

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

//|                                                                  |

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

void SetMarginMode(void)

  {

   m_margin_mode=(ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE);

  }

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

//|                                                                  |

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

bool IsHedging(void)

  {

   return(m_margin_mode==ACCOUNT_MARGIN_MODE_RETAIL_HEDGING);

  }

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

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