MACDSimpleReshetov

Author: Copyright © 2006, Yury V. Reshetov ICQ: 282715499
Price Data Components
Indicators Used
MACD Histogram
0 Views
0 Downloads
0 Favorites
MACDSimpleReshetov
ÿþ//+------------------------------------------------------------------+

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

//|                               Copyright © 2006, Yury V. Reshetov |

//|                                         http://reshetov.xnet.uz/ |

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

#property copyright "Copyright © 2006, Yury V. Reshetov ICQ: 282715499"

#property link      "http://reshetov.xnet.uz/"

#property version   "1.001"

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>  

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

//--- input parameters

input double   Lots        = 2;  // 1J5< >B:@K205<KE ?>78F89

input uchar    DF          = 1;  // @8@0I5=85 ?5@8>40 1KAB@>9 A@54=59 : ?5@8>4C A83=0;L=>9 ;8=88

input uchar    DS          = 2;  // @8@0I5=85 ?5@8>40 <54;5==>9 A@54=59 : ?5@8>4C 1KAB@>9 A@54=59

input uchar    SignalPeriod= 10; // 5@8>4 A83=0;L=>9 ;8=88

input ulong    m_magic=42828093; // magic number

ulong          m_slippage=10;    // slippage

//---

int         handle_iMACD;        // variable for storing the handle of the iMACD indicator 

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

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

      return(INIT_FAILED);

   RefreshRates();



   string err_text="";

   if(!CheckVolumeValue(Lots,err_text))

     {

      Print(err_text);

      return(INIT_PARAMETERS_INCORRECT);

     }

//---

   m_trade.SetExpertMagicNumber(m_magic);

//---

   if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK))

      m_trade.SetTypeFilling(ORDER_FILLING_FOK);

   else if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC))

      m_trade.SetTypeFilling(ORDER_FILLING_IOC);

   else

      m_trade.SetTypeFilling(ORDER_FILLING_RETURN);

//---

   m_trade.SetDeviationInPoints(m_slippage);

//--- create handle of the indicator iMACD

   handle_iMACD=iMACD(m_symbol.Name(),Period(),SignalPeriod+DF,SignalPeriod+DS+DF,SignalPeriod,PRICE_CLOSE);

//--- if the handle is not created 

   if(handle_iMACD==INVALID_HANDLE)

     {

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

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

                  m_symbol.Name(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//--- <K @01>B05< B>;L:> 2> 2@5<O @>645=8O =>2>3> 10@0

   static datetime PrevBars=0;

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

   if(time_0==PrevBars)

      return;

   PrevBars=time_0;

//--- 7=0G5=85 >A=>2=>9 ;8=88 MACD

   double main=iMACDGet(MAIN_LINE,0);

//--- 7=0G5=85 A83=0;L=>9 ;8=88 MACD

   double signal=iMACDGet(SIGNAL_LINE,0);

//--- >B:@KBK5 ?>78F88

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

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

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

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY && main<0) // 745AL <K ?>:C?0;8, 0 A83=0; =0 ?@>406C

              {

               //--- B0: 45;> =5 ?>945B, 2KE>48< 87 @K=:0

               m_trade.PositionClose(m_position.Ticket());

               return;

              }

            else if(m_position.PositionType()==POSITION_TYPE_SELL && main>0) // 745AL <K ?@>4020;8, 0 A83=0; =0 ?>:C?:C

              {

               //--- 25@=8B5 =0H8 45=56:8 8;8 B>, GB> >B =8E >AB0;>AL

               m_trade.PositionClose(m_position.Ticket());

               return;

              }

            //--- B:@KB0O ?>78F8O =0 MB>< 8=AB@C<5=B5 C65 5ABL, 2B>@CN >B:@K20BL =5 1C45<, 

            //--- 0 ;CGH5 7025@H8< @01>BC M:A?5@B0

            return;

           }

//--- 5A;8 >15 ;8=88 ?> >4=C AB>@>=C D@>=B0, B> 2E>48< 2 @K=>:

   if((main*signal)>0)

     {

      //--- 5A;8 A83=0; =0 ?>:C?:C

      if(main>0)

        {

         //--- 7=0G8B :C?8<

         if(main>signal)

            m_trade.Buy(Lots,m_symbol.Name());

         return;

        }

      else

        {

         //--- 745AL <K ?@>405<

         if(main<signal)

            m_trade.Sell(Lots,m_symbol.Name());

         return;

        }

     }

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates(void)

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

     {

      Print("RefreshRates error");

      return(false);

     }

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

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

      return(false);

//---

   return(true);

  }

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

//| Check the correctness of the order volume                        |

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

bool CheckVolumeValue(double volume,string &error_description)

  {

//--- minimal allowed volume for trade operations

   double min_volume=m_symbol.LotsMin();

   if(volume<min_volume)

     {

      error_description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);

      return(false);

     }

//--- maximal allowed volume of trade operations

   double max_volume=m_symbol.LotsMax();

   if(volume>max_volume)

     {

      error_description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);

      return(false);

     }

//--- get minimal step of volume changing

   double volume_step=m_symbol.LotsStep();

   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001)

     {

      error_description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",

                                     volume_step,ratio*volume_step);

      return(false);

     }

   error_description="Correct volume value";

   return(true);

  }

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

//| Checks if the specified filling mode is allowed                  | 

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

bool IsFillingTypeAllowed(int fill_type)

  {

//--- Obtain the value of the property that describes allowed filling modes 

   int filling=m_symbol.TradeFillFlags();

//--- Return true, if mode fill_type is allowed 

   return((filling & fill_type)==fill_type);

  }

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

//| Get value of buffers for the iMACD                               |

//|  the buffer numbers are the following:                           |

//|   0 - MAIN_LINE, 1 - SIGNAL_LINE                                 |

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

double iMACDGet(const int buffer,const int index)

  {

   double MACD[1];

//--- reset error code 

   ResetLastError();

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

   if(CopyBuffer(handle_iMACD,buffer,index,1,MACD)<0)

     {

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

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

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

      return(0.0);

     }

   return(MACD[0]);

  }

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

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