Author: Copyright © 2008, Mark Johnson.
Price Data Components
Indicators Used
Moving average indicatorRelative strength index
0 Views
0 Downloads
0 Favorites
Improve
ÿþ//+------------------------------------------------------------------+

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

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

#property copyright "Copyright © 2008, Mark Johnson."

#property link      "mark.johnson.uk@hotmail.com"

#property version   "1.000"

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>  

#include <Trade\AccountInfo.mqh>

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol_Base;                // symbol info object

CSymbolInfo    m_symbol_Hedge;               // symbol info object

CAccountInfo   m_account;                    // account info wrapper

//--- input parameters

input double               InpLots                 = 0.1;            // Lots

input double               InpVirtualProfit        = 50;             // Virtual Profit (in money)

sinput string              InpHedge                = "USDCHF";       // Hedge symbol

input int                  MA_Fast_ma_period       = 8;              // MA Fast: averaging period 

input int                  MA_Fast_ma_shift        = 0;              // MA Fast: horizontal shift 

input ENUM_MA_METHOD       MA_Fast_ma_method       = MODE_SMMA;      // MA Fast: smoothing type 

input ENUM_APPLIED_PRICE   MA_Fast_applied_price   = PRICE_MEDIAN;   // MA Fast: type of price  

input int                  MA_Slow_ma_period       = 21;             // MA Slow: averaging period 

input int                  MA_Slow_ma_shift        = 0;              // MA Slow: horizontal shift 

input ENUM_MA_METHOD       MA_Slow_ma_method       = MODE_SMMA;      // MA Slow: smoothing type 

input ENUM_APPLIED_PRICE   MA_Slow_applied_price   = PRICE_MEDIAN;   // MA Slow: type of price 

input int                  RSI_ma_period           = 21;             // RSI: averaging period 

input ENUM_APPLIED_PRICE   RSI_applied_price       = PRICE_CLOSE;    // RSI: type of price 

sinput ulong               m_magic                 = 13433244;       // magic number

//---

ulong    m_slippage=10;                                              // slippage



long     m_start_time_in_sec=0;



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

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

int      handle_iRSI;                                                // variable for storing the handle of the iRSI indicator 

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   if(MA_Fast_ma_period>=MA_Slow_ma_period)

     {

      Print(__FUNCTION__,", ERROR: \"MA Fast: averaging period\" (",IntegerToString(MA_Fast_ma_period),") can not ",

            "be greater than or equal ","to \"MA Slow: averaging period\" (",IntegerToString(MA_Slow_ma_period),") !");

      return(INIT_PARAMETERS_INCORRECT);

     }

//---  

   if(!m_symbol_Hedge.Name(InpHedge)) // sets symbol name

      return(INIT_FAILED);

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

      return(INIT_PARAMETERS_INCORRECT);

//---

   string err_text="";

   if(!CheckVolumeValue(InpLots,err_text))

     {

      Print(__FUNCTION__,", ERROR: ",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);

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

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

      digits_adjust=10;

//--- create handle of the indicator iMA

   handle_iMA_Fast=iMA(m_symbol_Base.Name(),Period(),MA_Fast_ma_period,MA_Fast_ma_shift,

                       MA_Fast_ma_method,MA_Fast_applied_price);

//--- if the handle is not created 

   if(handle_iMA_Fast==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_Base.Name(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//--- create handle of the indicator iMA

   handle_iMA_Slow=iMA(m_symbol_Base.Name(),Period(),MA_Slow_ma_period,MA_Slow_ma_shift,

                       MA_Slow_ma_method,MA_Slow_applied_price);

//--- if the handle is not created 

   if(handle_iMA_Slow==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_Base.Name(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early 

      return(INIT_FAILED);

     }

//--- create handle of the indicator iRSI

   handle_iRSI=iRSI(m_symbol_Base.Name(),Period(),RSI_ma_period,RSI_applied_price);

//--- if the handle is not created 

   if(handle_iRSI==INVALID_HANDLE)

     {

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

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

                  m_symbol_Base.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()

  {

//--- check the total profit

   if(IsProfit())

      return;

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

   static datetime PrevBars=0;

   datetime time_0=iTime(0);

   if(time_0==PrevBars)

      return;

   PrevBars=time_0;

//---

   double FastMA=iMAGet(handle_iMA_Fast,1);

   double SlowMA=iMAGet(handle_iMA_Slow,1);

   if(FastMA==0.0 || SlowMA==0.0)

     {

      PrevBars=iTime(1);

      return;

     }

   double RSI=iRSIGet(0);

   if(SlowMA-FastMA>0 && RSI<=30)

     {

      if(!RefreshRates(m_symbol_Base))

         return;

      if(!RefreshRates(m_symbol_Hedge))

         return;

      OpenBuy(m_symbol_Base);

      OpenBuy(m_symbol_Hedge);

      return;

     }

   if(SlowMA-FastMA<0 && RSI>=70/* && CorDiff<0*/)

     {

      if(!RefreshRates(m_symbol_Base))

         return;

      if(!RefreshRates(m_symbol_Hedge))

         return;

      OpenSell(m_symbol_Base);

      OpenSell(m_symbol_Hedge);

      return;

     }

//---

   return;

  }

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

//| TradeTransaction function                                        |

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

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

//---



  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates(CSymbolInfo &m_symbol)

  {

//--- 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_Base.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_Base.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_Base.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_Base.TradeFillFlags();

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

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

  }

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

//| Get Time for specified bar index                                 | 

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

datetime iTime(const int index,string symbol=NULL,ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT)

  {

   if(symbol==NULL)

      symbol=m_symbol_Base.Name();

   if(timeframe==0)

      timeframe=Period();

   datetime Time[1];

   datetime time=0; // D'1970.01.01 00:00:00'

   int copied=CopyTime(symbol,timeframe,index,1,Time);

   if(copied>0)

      time=Time[0];

   return(time);

  }

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

//| Check profit                                                     |

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

bool IsProfit()

  {

   double total_profit=0.0;

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

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

         if((m_position.Symbol()==m_symbol_Base.Name() || m_position.Symbol()==InpHedge) && m_position.Magic()==m_magic)

           {

            total_profit=m_position.Commission()+m_position.Swap()+m_position.Profit();

            if(total_profit>InpVirtualProfit)

              {

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

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

                     if((m_position.Symbol()==m_symbol_Base.Name() || m_position.Symbol()==InpHedge) && m_position.Magic()==m_magic)

                        m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol

               return(true);

              }

           }

   return(false);

  }

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

//| Get value of buffers for the iMA                                 |

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

double iMAGet(const int handle_iMA,const int index)

  {

   double MA[1];

//--- reset error code 

   ResetLastError();

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

   if(CopyBuffer(handle_iMA,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[0]);

  }

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

//| Get value of buffers for the iRSI                                |

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

double iRSIGet(const int index)

  {

   double RSI[1];

//--- reset error code 

   ResetLastError();

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

   if(CopyBuffer(handle_iRSI,0,index,1,RSI)<0)

     {

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

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

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

      return(0.0);

     }

   return(RSI[0]);

  }

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

//| Calculate all positions                                          |

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

int CalculateAllPositions()

  {

   int total=0;



   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_Base.Name() || m_position.Symbol()==InpHedge) && m_position.Magic()==m_magic)

            total++;

//---

   return(total);

  }

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

//| Open Buy position                                                |

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

void OpenBuy(CSymbolInfo &symbol)

  {

   double check_open_long_lot=InpLots;

//--- check volume before OrderSend to avoid "not enough money" error (CTrade)

   double check_volume_lot=m_trade.CheckVolume(symbol.Name(),check_open_long_lot,symbol.Ask(),ORDER_TYPE_BUY);



   if(check_volume_lot!=0.0)

     {

      if(check_volume_lot>=check_open_long_lot)

        {

         if(m_trade.Buy(check_open_long_lot,NULL,symbol.Ask(),0.0,0.0))

           {

            if(m_trade.ResultDeal()==0)

              {

               Print("#1 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),

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

               PrintResult(m_trade,symbol);

              }

            else

              {

               Print("#2 Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),

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

               PrintResult(m_trade,symbol);

              }

           }

         else

           {

            Print("#3 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),

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

            PrintResult(m_trade,symbol);

           }

        }

      else

        {

         Print(__FUNCTION__,", ERROR: method CheckVolume (",DoubleToString(check_volume_lot,2),") ",

               "< \"Lots\" (",DoubleToString(check_open_long_lot,2),")");

         return;

        }

     }

   else

     {

      Print(__FUNCTION__,", ERROR: method CheckVolume returned the value of \"0.0\"");

      return;

     }

//---

  }

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

//| Open Sell position                                               |

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

void OpenSell(CSymbolInfo &symbol)

  {

   double check_open_short_lot=InpLots;

//--- check volume before OrderSend to avoid "not enough money" error (CTrade)

   double check_volume_lot=m_trade.CheckVolume(symbol.Name(),check_open_short_lot,symbol.Bid(),ORDER_TYPE_SELL);



   if(check_volume_lot!=0.0)

     {

      if(check_volume_lot>=check_open_short_lot)

        {

         if(m_trade.Sell(check_open_short_lot,NULL,symbol.Bid(),0.0,0.0))

           {

            if(m_trade.ResultDeal()==0)

              {

               Print("#1 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),

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

               PrintResult(m_trade,symbol);

              }

            else

              {

               Print("#2 Sell -> true. Result Retcode: ",m_trade.ResultRetcode(),

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

               PrintResult(m_trade,symbol);

              }

           }

         else

           {

            Print("#3 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),

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

            PrintResult(m_trade,symbol);

           }

        }

      else

        {

         Print(__FUNCTION__,", ERROR: method CheckVolume (",DoubleToString(check_volume_lot,2),") ",

               "< \"Lots\" (",DoubleToString(check_open_short_lot,2),")");

         return;

        }

     }

   else

     {

      Print(__FUNCTION__,", ERROR: method CheckVolume returned the value of \"0.0\"");

      return;

     }

//---

  }

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

//| Print CTrade result                                              |

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

void PrintResult(CTrade &trade,CSymbolInfo &symbol)

  {

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

   Print("code of request result: "+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(trade.ResultBid(),symbol.Digits()));

   Print("current ask price: "+DoubleToString(trade.ResultAsk(),symbol.Digits()));

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

   DebugBreak();

  }

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

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