Author: Copyright © 2008, MetaQuotes Software Corp.
Price Data Components
Orders Execution
Checks for the total of open orders
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
21hour
ÿþ//+------------------------------------------------------------------+

//|                              21hour(barabashkakvn's edition).mq5 |

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

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

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

#property copyright "Copyright © 2008, MetaQuotes Software Corp."

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

#property description "040Q< 2@5<5==K5 8=B5@20;K #"  % !#""

#property version "1.001"

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

//| 1.00:                                                            |

//|  >48= 2@5<5==>9 ?@><56CB>:                                       |

//| 1.01:                                                            |

//|  420 2@5<5==KE ?@><56CB:0, ?@8 MB>< >=8 =5 ?5@5A5:0NBAO          |

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

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>  

#include <Trade\OrderInfo.mqh>

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

COrderInfo     m_order;                      // pending orders object

//--- input parameters

input double   Lots              = 0.1;      // Lots

input uchar    HourStartFirst    = 8;        // HourStartFirst

input uchar    HourStopFirst     = 21;       // HourStopFirst

input uchar    HourStartSecond   = 22;       // HourStartSecond

input uchar    HourStopSecond    = 23;       // HourStopSecond

input ushort   InpStep           = 5;        // Step

input ushort   InpTakeProfit     = 40;       // TakeProfit

//--- parameters

double         ExtStep=0.0;

double         ExtTakeProfit=0.0;

ulong          m_magic=12321;    // magic number

ENUM_ACCOUNT_MARGIN_MODE m_margin_mode;

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

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//SetMarginMode();

//if(!IsHedging())

//  {

//   Print("Hedging only!");

//   return(INIT_FAILED);

//  }

//---checking timeslots

   if(!CheckingTimeslots())

      return(INIT_PARAMETERS_INCORRECT);

//---

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

//---

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

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

   ExtStep        = InpStep       * m_adjusted_point;

   ExtTakeProfit  = InpTakeProfit * m_adjusted_point;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   static datetime prevtime=0;            // AB0B8G5A:0O ?5@5<5==0O 4;O E@0=5=8O 2@5<5=8

//--- ?>AG8B05<, 5ABL ;8 >B:@KB0O ?>78F8O

   int total=0;

   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)

            total++;

//--- GB>-B> ?>H;> =5 B0:, ?>78F89 1>;LH5 G5< >4=0

   if(total>1)

     {

      ClosePositions();

      return;

     }

//--- 5ABL >4=0 ?>78F8O, 7=0G8B C40;O5< >B;>65==K9/>B;>65==K5 >@45@

   if(total==1)

      DeleteOrders();

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

   if(iTime(0)==prevtime)

      return;

   prevtime=iTime(0);



   if(!IsTradeAllowed())

     {

      prevtime=iTime(0);

      return;

     }



   MqlDateTime str1;

   TimeToStruct(TimeCurrent(),str1);



//--- 5A;8 B5:CI55 2@5<O ?>?0405B 2 =0G0;> ?5@2>3> 8;8 2B>@>3> ?@><56CB:0

   if((str1.hour==HourStartFirst && str1.min==0) || (str1.hour==HourStartSecond && str1.min==0))

     {

      //--- B0: :0: @01>B05B B>;L:> =0 =>2>< 10@5, B> 2 A;CG05

      //--- >H81:8 >1=>2;5=8O B5:CI8E F5= A1@0AK205< B09<5@

      if(!RefreshRates())

        {

         prevtime=iTime(1);

         return;

        }

      double price=NormalizeDouble(m_symbol.Ask()+ExtStep,m_symbol.Digits());

      double tp   =NormalizeDouble(m_symbol.Ask()+ExtStep+ExtTakeProfit,m_symbol.Digits());

      m_trade.BuyStop(Lots,price,NULL,0.0,tp);



      price       =NormalizeDouble(m_symbol.Bid()-ExtStep,m_symbol.Digits());

      tp          =NormalizeDouble(m_symbol.Bid()-ExtStep-ExtTakeProfit,m_symbol.Digits());

      m_trade.SellStop(Lots,price,NULL,0.0,tp);



      return;

     }



//--- 5A;8 B5:CI55 2@5<O ?>?0405B 2 :>=5F ?5@2>3> 8;8 2B>@>3> ?@><56CB:0, B>

//--- 70:@K205< ?>78F8N/?>78F88 8 C40;O5< >B;>65==K9 >@45@/>@45@0

   if((str1.hour==HourStopFirst && str1.min==0) || (str1.hour==HourStopSecond && str1.min==0))

     {

      ClosePositions();

      DeleteOrders();

     }



//---

   return;

  }

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

//|                                                                  |

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

bool CheckingTimeslots()

  {

   bool res=true;



//--- ?@>25@:0 ?5@2>3> ?@><56CB:0 =0 @025=AB2>: =5;L7O, GB>1K G0A AB0@B0 1K; @025= G0AC D8=8H0

   if(HourStartFirst==HourStopFirst)

     {

      Print("Error: HourStartFirst (",HourStartFirst,") == HourStopFirst (",HourStopFirst,")");

      return(false);

     }

//--- ?@>25@:0 2B>@>3> ?@><56CB:0 =0 @025=AB2>: =5;L7O, GB>1K G0A AB0@B0 1K; @025= G0AC D8=8H0

   if(HourStartSecond==HourStopSecond)

     {

      Print("Error: HourStartSecond (",HourStartSecond,") == HourStopSecond (",HourStopSecond,")");

      return(false);

     }



//--- ?@>25@:0 ?5@2>3> 8 2B>@>3> ?@><56CB:0 =0 ?@028;L=>ABL 2@5<5=8: =5;L7O, GB>1K G0AK AB0@B0 1K; @02=K 

//--- 8;8 GB>1K "  ?@><56CB>: 1K; @0=LH5  

   if(HourStartFirst>=HourStartSecond)

     {

      Print("Error: HourStartFirst (",HourStartFirst,") >= HourStartSecond (",HourStartSecond,")");

      return(false);

     }

//--- ?@>25@:0 ?5@2>3> 8 2B>@>3> ?@><56CB:0 =0 ?@028;L=>ABL 2@5<5=8: =5;L7O, GB>1K G0AK D8=8H0 1K; @02=K 

//--- 8;8 GB>1K "  ?@><56CB>: 1K; @0=LH5  

   if(HourStopFirst>=HourStopSecond)

     {

      Print("Error: HourStopFirst (",HourStopFirst,") >= HourStopSecond (",HourStopSecond,")");

      return(false);

     }



//--- ?@>25@:0 ?5@2>3> ?@><56CB:0 =0 ?@028;L=>ABL 2@5<5=8: =5;L7O, GB>1K 2@5<O D8=8H0 1K;> <5=LH5 2@5<5=8 AB0@B0

   if(HourStopFirst<HourStartFirst)

     {

      Print("Error: HourStopFirst (",HourStopFirst,") < HourStartFirst (",HourStartFirst,")");

      return(false);

     }

//--- ?@>25@:0 2B>@>3> ?@><56CB:0 =0 ?@028;L=>ABL 2@5<5=8: =5;L7O, GB>1K 2@5<O D8=8H0 1K;> <5=LH5 2@5<5=8 AB0@B0

   if(HourStopSecond<HourStartSecond)

     {

      Print("Error: HourStopSecond (",HourStopSecond,") < HourStartSecond (",HourStartSecond,")");

      return(false);

     }



//--- ?@>25@:0 =0 2;>65==>ABL:

   if(HourStartFirst<HourStartSecond && HourStartSecond<HourStopFirst)

     {

      Print("Error: HourStartFirst (",HourStartFirst,") < HourStartSecond (",HourStartSecond,") && ",

            "HourStartSecond (",HourStartSecond,") < HourStopFirst (",HourStopFirst,")");

      return(false);

     }

//---

   return(res);

  }

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

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

  }

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

//| Delete All Orders                                                |

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

void DeleteOrders()

  {

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

      if(m_order.SelectByIndex(i))

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

            m_trade.OrderDelete(m_order.Ticket());

  }

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

//| Close All Positions                                              |

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

void ClosePositions()

  {

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

      if(m_position.SelectByIndex(i))

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

            m_trade.PositionClose(m_position.Ticket());

  }

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

//| Get Time for specified bar index                                 | 

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

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

  {

   if(symbol==NULL)

      symbol=Symbol();

   if(timeframe==0)

      timeframe=Period();

   datetime Time[1];

   datetime time=0;

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

   if(copied>0) time=Time[0];

   return(time);

  }

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

//| Gets the information about permission to trade                   |

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

bool IsTradeAllowed()

  {

   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))

     {

      Alert("Check if automated trading is allowed in the terminal settings!");

      return(false);

     }

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

//|                                                                  |

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

   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))

     {

      Alert("Check if automated trading is allowed in the terminal settings!");

      return(false);

     }

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

//|                                                                  |

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

   else

     {

      if(!MQLInfoInteger(MQL_TRADE_ALLOWED))

        {

         Alert("Automated trading is forbidden in the program settings for ",__FILE__);

         return(false);

        }

     }

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

//|                                                                  |

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

   if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT))

     {

      Alert("Automated trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),

            " at the trade server side");

      return(false);

     }

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

//|                                                                  |

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

   if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED))

     {

      Comment("Trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),

              ".\n Perhaps an investor password has been used to connect to the trading account.",

              "\n Check the terminal journal for the following entry:",

              "\n\'",AccountInfoInteger(ACCOUNT_LOGIN),"\': trading has been disabled - investor mode.");

      return(false);

     }

//---

   return(true);

  }

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

Comments