Author: space_cowboy
Price Data Components
0 Views
0 Downloads
0 Favorites
Pipso
ÿþ//+------------------------------------------------------------------+

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

//|                                                     space_cowboy |

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

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

#property copyright "space_cowboy"

#property link      "http://www.metaquotes.net"

#property version   "1.001"

#define MODE_LOW 1

#define MODE_HIGH 2

//---

#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   InpLots           = 0.1;      // Lots

input uchar    InpStartHour      = 21;       // Start hour (from 0 to 23)

input uchar    InpEndHour        = 9;        // End hour (plus to the "Start hour") (from 0 to 23)

input int      InpPeriod         = 36;       //?5@8>4 4;O ?>AB>@>5=8O :0=0;0 2 10@0E

input double   SLpp              = 300;      //AB>? 2 ?@>F5=B0E >B @07<0E0 :0=0;0

//---

ulong          m_magic=29135970;             // magic number

ulong          m_slippage=30;                // slippage

double         m_adjusted_point;             // point value adjusted for 3 or 5 points

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

//| Expert initialization function                                   |

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

int OnInit()

  {

   if(InpStartHour>23 || InpEndHour>23)

     {

      Print("Wrong time! The hour value can be from 0 to 23");

      return(INIT_PARAMETERS_INCORRECT);

     }

   if(InpStartHour==InpEndHour)

     {

      Print("Wrong time! The values of \"Start hour\" and \"End hour\" should be different");

      return(INIT_PARAMETERS_INCORRECT);

     }

//---

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

   RefreshRates();

   m_symbol.Refresh();



   string err_text="";

   if(!CheckVolumeValue(InpLots,err_text))

     {

      Print(err_text);

      return(INIT_PARAMETERS_INCORRECT);

     }

//---

   m_trade.SetExpertMagicNumber(m_magic);

//---

   if(IsFillingTypeAllowed(Symbol(),SYMBOL_FILLING_FOK))

      m_trade.SetTypeFilling(ORDER_FILLING_FOK);

   else if(IsFillingTypeAllowed(Symbol(),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.Digits()==3 || m_symbol.Digits()==5)

      digits_adjust=10;

   m_adjusted_point=m_symbol.Point()*digits_adjust;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   datetime time_current=TimeCurrent(),start_time=0,end_time=0;

   int objs=0;

   double high=0.0,low=0.0;

   int width=0;

   bool op=false,work=false;



   MqlDateTime str_start_time,str_end_time;

   TimeToStruct(TimeCurrent(),str_start_time);

   TimeToStruct(TimeCurrent(),str_end_time);

//--- create start time (hh:mm:ss) Start hour:00:00

   str_start_time.hour=InpStartHour;

   str_start_time.min=0;

   str_start_time.sec=0;

   if(InpStartHour+InpEndHour>24)

      str_start_time.day--;

   start_time=StructToTime(str_start_time); // struct "str_start_day" to DateTime

//--- create InpEndHour time (hh:mm:ss) End hour:00:00

   str_end_time.hour=InpEndHour;

   str_end_time.min=0;

   str_end_time.sec=0;

   end_time=StructToTime(str_end_time); // struct "str_end_time" to DateTime



   if(time_current>=start_time && time_current<=end_time)

      work=true;

   else

      work=false;

//---

   high=iHighest(m_symbol.Name(),Period(),MODE_HIGH,InpPeriod,1);

   if(high==-1 || high==0.0)

      return;

   low=iLowest(m_symbol.Name(),Period(),MODE_LOW,InpPeriod,1);

   if(low==-1 || low==0.0)

      return;



   width=(int)((high-low)*(SLpp/100+1)/m_adjusted_point);

   if(width<m_symbol.StopsLevel())

      width=m_symbol.StopsLevel();



   if(!RefreshRates())

      return;



   if(m_symbol.Bid()>=high)

     {

      ClosePositions(POSITION_TYPE_BUY);

      if(work && CalculatePositions()==0)

         OpenSell(m_symbol.Bid()+width*m_adjusted_point,0.0);

     }



   if(m_symbol.Bid()<=low)

     {

      ClosePositions(POSITION_TYPE_SELL);

      if(work && CalculatePositions()==0)

         OpenBuy(m_symbol.Ask()-width*m_adjusted_point,0.0);

     }

//---

   return;

  }

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

//| Check the correctness of the order volume                        |

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

bool CheckVolumeValue(double volume,string &error_description)

  {

//--- minimal allowed volume for trade operations

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

   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=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   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=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);



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

  }

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

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

  }

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

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

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

bool IsFillingTypeAllowed(string symbol,int fill_type)

  {

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

   int filling=(int)SymbolInfoInteger(symbol,SYMBOL_FILLING_MODE);

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

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

  }

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

//|                                                                  |

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

double iHighest(string symbol,

                ENUM_TIMEFRAMES timeframe,

                int type,

                int count=WHOLE_ARRAY,

                int start=0)

  {

   if(start<0)

      return(-1.0);

   if(count<=0)

      count=Bars(symbol,timeframe);

   if(type==MODE_HIGH)

     {

      double High[];

      ArraySetAsSeries(High,true);

      CopyHigh(symbol,timeframe,start,count,High);

      return(High[ArrayMaximum(High,0,count)]);

     }

//---

   return(0.0);

  }

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

//|                                                                  |

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

double iLowest(string symbol,

               ENUM_TIMEFRAMES timeframe,

               int type,

               int count=WHOLE_ARRAY,

               int start=0)

  {

   if(start<0)

      return(-1.0);

   if(count<=0)

      count=Bars(symbol,timeframe);

   if(type==MODE_LOW)

     {

      double Low[];

      ArraySetAsSeries(Low,true);

      CopyLow(symbol,timeframe,start,count,Low);

      return(Low[ArrayMinimum(Low,0,count)]);

     }

//---

   return(0.0);

  }

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

//| 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))     // selects the position by index for further access to its properties

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

            if(m_position.PositionType()==pos_type) // gets the position type

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

  }

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

//| Calculate positions Buy and Sell                                 |

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

int CalculatePositions()

  {

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

            total++;

//---

   return(total);

  }

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

//| Open Buy position                                                |

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

void OpenBuy(double sl,double tp)

  {

   sl=m_symbol.NormalizePrice(sl);

   tp=m_symbol.NormalizePrice(tp);



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

   double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),InpLots,m_symbol.Ask(),ORDER_TYPE_BUY);



   if(check_volume_lot!=0.0)

      if(check_volume_lot>=InpLots)

        {

         if(m_trade.Buy(InpLots,NULL,m_symbol.Ask(),sl,tp))

           {

            if(m_trade.ResultDeal()==0)

              {

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

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

              }

            else

              {

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

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

              }

           }

         else

           {

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

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

           }

        }

//---

  }

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

//| Open Sell position                                               |

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

void OpenSell(double sl,double tp)

  {

   sl=m_symbol.NormalizePrice(sl);

   tp=m_symbol.NormalizePrice(tp);



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

   double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),InpLots,m_symbol.Bid(),ORDER_TYPE_SELL);



   if(check_volume_lot!=0.0)

      if(check_volume_lot>=InpLots)

        {

         if(m_trade.Sell(InpLots,NULL,m_symbol.Bid(),sl,tp))

           {

            if(m_trade.ResultDeal()==0)

              {

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

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

              }

            else

              {

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

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

              }

           }

         else

           {

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

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

           }

        }

//---

  }

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

Comments