Price Data Components
Series array that contains open time of each bar
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
MA Cross
ÿþ//+------------------------------------------------------------------+

//|                            MA Cross(barabashkakvn's edition).mq5 |

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

#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 int            MA_1_Period    = 3;              // 5@8>4 CA@54=5=8O 1-9 

input int            MA_2_Period    = 13;             // 5@8>4 CA@54=5=8O 2-9 

input ENUM_MA_METHOD MA_1_Method    = MODE_SMA;       // 5B>4 2KG8A;5=8O 1

input ENUM_MA_METHOD MA_2_Method    = MODE_LWMA;      // 5B>4 2KG8A;5=8O 2

input ENUM_APPLIED_PRICE MA_1_Price = PRICE_CLOSE;    // 5B>4 2KG8A;5=8O F5=K 1 

input ENUM_APPLIED_PRICE MA_2_Price = PRICE_MEDIAN;   // 5B>4 2KG8A;5=8O F5=K 2

input int            MA_1_Shift     = 0;              // !<5I5=85 8=48:0B>@0 1

input int            MA_2_Shift     = 0;              // !<5I5=85 8=48:0B>@0 2

input double         Lot            = 0.1;            // $8:A8@>20==K9 ;>B

//---

int                  New_Bar;                         // 0/1 $0:B >1@07>20=8O =>2>3> 10@0

datetime             Time_0;                          // @5<O =0G0;0 =>2>3> 10@0

int                  PosOpen;                         // 0?@02;5=85 ?5@5A5G5=8O

int                  PosClose;                        // 0?@02;5=85 ?5@5A5G5=8O

int                  all_positions;                   // >;8G5AB2> >B:@KBKE ?>78F89

double               MA1_0;                           // "5:CI55 7=0G5=85 1-9 

double               MA1_1;                           // @54K4CI55 7=0G5=85 1-9 

double               MA2_0;                           // "5:CI55 7=0G5=85 2-9 

double               MA2_1;                           // @54K4CI55 7=0G5=85 2-9 

int                  PosBuy;                          // 1 = D0:B =0;8G8O ?>78F88 Buy

int                  PosSell;                         // 1 = D0:B =0;8G8O ?>78F88 Sell 

//---

int    handle_iMA_1;                     // variable for storing the handle of the iMA indicator 

int    handle_iMA_2;                    // variable for storing the handle of the iMA indicator 

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

//| Expert initialization function                                   |

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

int OnInit()

  {

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

//--- create handle of the indicator iMA

   handle_iMA_1=iMA(m_symbol.Name(),Period(),MA_1_Period,MA_1_Shift,MA_1_Method,MA_1_Price);

//--- if the handle is not created 

   if(handle_iMA_1==INVALID_HANDLE)

     {

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

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

                  m_symbol.Name(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//--- create handle of the indicator iMA

   handle_iMA_2=iMA(m_symbol.Name(),Period(),MA_2_Period,MA_2_Shift,MA_2_Method,MA_2_Price);

//--- if the handle is not created 

   if(handle_iMA_2==INVALID_HANDLE)

     {

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

      PrintFormat("Failed to create handle of the iMA 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 tick function                                             |

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

void OnTick()

  {

   if(!RefreshRates())

      return;



   PosBuy=0;

   PosSell=0;

   int openOrders=0;

   all_positions=PositionsTotal();                                  // 1I55 :>;8G5AB2> ?>78F89

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

     {

      if(m_position.SelectByIndex(i)) // K18@05< ?>78F8N

        {

         if(m_position.PositionType()==POSITION_TYPE_BUY)// A;8 ?>78F8O BUY

           {

            PosBuy=1;

            if(CrossPositionClose()==1) // 0:@K25< >@45@, 5A;8 C4>2;5B2>@O5B CA;>28N CrossPositionClose()=1

              {

               m_trade.PositionClose(m_position.Ticket());

              }

           }

         if(m_position.PositionType()==POSITION_TYPE_SELL) // A;8 ?>78F8O SELL

           {

            PosSell=1;

            if(CrossPositionClose()==2) // 0:@K25< >@45@, 5A;8 C4>2;5B2>@O5B CA;>28N CrossPositionClose()=2

              {

               m_trade.PositionClose(m_position.Ticket());

              }

           }

        }

     }



   New_Bar=0;                          // ;O =0G0;0 >1=C;8<AO

   if(Time_0!=iTime(m_symbol.Name(),Period(),0)) // A;8 C65 4@C3>5 2@5<O =0G0;0 10@0

     {

      New_Bar=1;                      //  2>B 8 =>2K9 10@

      Time_0=iTime(m_symbol.Name(),Period(),0);               // 0?><=8< 2@5<O =0G0;0 =>2>3> 10@0

     }



   MA1_0=iMAGet(handle_iMA_1,0);       // "5:CI55    7=0G5=85 1-9 

   MA1_1=iMAGet(handle_iMA_1, 1);      // @54K4CI55 7=0G5=85 1-9 

   MA2_0=iMAGet(handle_iMA_2, 0);      // "5:CI55    7=0G5=85 2-9 

   MA2_1=iMAGet(handle_iMA_2, 1);      // @54K4CI55 7=0G5=85 2-9 



   if(CrossPositionOpen()==1 && New_Bar==1) // 2865=85 A=87C 225@E = >B:@. Buy

     {

      OpenBuy();

     }

   if(CrossPositionOpen()==2 && New_Bar==1) // 2865=85 A25@EC 2=87 = >B:@. Sell

     {

      OpenSell();

     }

   return;

  }

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

//|                                                                  |

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

int CrossPositionOpen()

  {

   PosOpen=0;                                                 // >B 345 A>10:0 70@KB0!!:)

   if((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0)) // 5@5A5G5=85 A=87C 225@E  

     {

      PosOpen=1;

     }

   if((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0)) // 5@5A5G5=85 A25@EC 2=87

     {

      PosOpen=2;

     }

   return(PosOpen);                                          // >72@0I05< =0?@02;5=85 ?5@5A5G5=.

  }

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

//|                                                                  |

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

int CrossPositionClose()

  {

   PosClose=0;                                                // >B 345 A>10:0 70@KB0!!:)

   if((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0)) // 5@5A5G5=85 A25@EC 2=87

     {

      PosClose=1;

     }

   if((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0)) // 5@5A5G5=85 A=87C 225@E

     {

      PosClose=2;

     }

   return(PosClose);                                          // >72@0I05< =0?@02;5=85 ?5@5A5G5=.

  }

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

//|                                                                  |

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

void OpenBuy()

  {

   if(all_positions==1)

     {

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

         if(m_position.SelectByIndex(i))

            if(m_position.PositionType()==POSITION_TYPE_BUY)

               return;                          // A;8 buy, B> =5 >B:@K205<AO

     }

   if(!RefreshRates())

      return;



   m_trade.Buy(Lot,m_symbol.Name(),m_symbol.Ask(),0.0,0.0,"Buy: MA_cross_Method_PriceMode");// B:@K205<AO

   return;

  }

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

//|                                                                  |

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

void OpenSell()

  {

   if(all_positions==1)

     {

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

         if(m_position.SelectByIndex(i))

            if(m_position.PositionType()==POSITION_TYPE_SELL)

               return;                             // A;8 sell, B> =5 >B:@K205<AO

     }

   m_trade.Sell(Lot,m_symbol.Name(),m_symbol.Bid(),0.0,0.0,"Sell: MA_cross_Method_PriceMode");

   return;

  }

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

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

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

double iMAGet(const int handle,const int index)

  {

   double MA[];

   ArraySetAsSeries(MA,true);

//--- reset error code 

   ResetLastError();

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

   if(CopyBuffer(handle,0,0,index+1,MA)<0)

     {

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

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

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

      return(0.0);

     }

   return(MA[index]);

  }

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

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