Pending orders by time

Author: Copyright © 2008, Юрий, yuriy@fortrader.ru
Price Data Components
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
Pending orders by time
ÿþ//+------------------------------------------------------------------+

//|              Pending orders by time(barabashkakvn's edition).mq5 |

//|                       Copyright © 2008, .@89, yuriy@fortrader.ru |

//|     http://www.ForTrader.ru, =0;8B8G5A:89 6C@=0; 4;O B@5945@>2. |

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

#property copyright "Copyright © 2008, .@89, yuriy@fortrader.ru"

#property link      "http://www.ForTrader.ru, =0;8B8G5A:89 6C@=0; 4;O B@5945@>2."

#property version   "1.001"

//---

#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

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

//| Enum hours                                                       |

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

enum ENUM_HOURS

  {

   hour_00  =0,   // 00

   hour_01  =1,   // 01

   hour_02  =2,   // 02

   hour_03  =3,   // 03

   hour_04  =4,   // 04

   hour_05  =5,   // 05

   hour_06  =6,   // 06

   hour_07  =7,   // 07

   hour_08  =8,   // 08

   hour_09  =9,   // 09

   hour_10  =10,  // 10

   hour_11  =11,  // 11

   hour_12  =12,  // 12

   hour_13  =13,  // 13

   hour_14  =14,  // 14

   hour_15  =15,  // 15

   hour_16  =16,  // 16

   hour_17  =17,  // 17

   hour_18  =18,  // 18

   hour_19  =19,  // 19

   hour_20  =20,  // 20

   hour_21  =21,  // 21

   hour_22  =22,  // 22

   hour_23  =23,  // 23

  };

//--- input parameters

input ENUM_HOURS  InpOpeningHour = 9;        // Opening hour

input ENUM_HOURS  InpClosingHour = 2;        // Closing hour

input ushort      InpDistance    = 20;       // Distance from the price (in pips)

input double      InpLots        = 0.1;      // Volume transaction

input ushort      InpStopLoss    = 20;       // Stop Loss (in pips)

input ushort      InpTakeProfit  = 500;      // Take Profit (in pips)

input ulong       m_magic        = 244888906;// magic number

//---

ulong             m_slippage=30;             // slippage

double            ExtDistance=0;

double            ExtStopLoss=0;

double            ExtTakeProfit=0;

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

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

//| Expert initialization function                                   |

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

int OnInit()

  {

   if(Period()>PERIOD_H1)

     {

      Print("Period must be < H1");

      return(INIT_FAILED);

     }

//---

   if(InpLots<=0.0)

     {

      Print("The \"volume transaction\" can't be smaller or equal to zero");

      return(INIT_PARAMETERS_INCORRECT);

     }

//---

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

      return(INIT_FAILED);

   RefreshRates();



   string err_text="";

   if(!CheckVolumeValue(InpLots,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);

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



   ExtDistance=InpDistance*m_adjusted_point;

   ExtStopLoss=InpStopLoss*m_adjusted_point;

   ExtTakeProfit=InpTakeProfit*m_adjusted_point;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

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



   if(!RefreshRates())

     {

      PrevBars=iTime(1);

      return;

     }



   MqlDateTime str1;

   TimeToStruct(TimeCurrent(),str1);

   if(str1.hour==InpClosingHour)

      DeleteAllOrders();

//---

   PosManager(str1);

   TimePattern(str1);

  }

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

//| TradeTransaction function                                        |

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

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

//---



  }

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

//|                                                                  |

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

void PosManager(MqlDateTime &time_struct)

  {

   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)

               if(m_position.PriceCurrent()-m_position.PriceOpen()>=ExtTakeProfit || time_struct.hour==InpClosingHour)

                  m_trade.PositionClose(m_position.Ticket());

            if(m_position.PositionType()==POSITION_TYPE_SELL)

               if(m_position.PriceOpen()-m_position.PriceCurrent()>=ExtTakeProfit || time_struct.hour==InpClosingHour)

                  m_trade.PositionClose(m_position.Ticket());

           }

  }

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

//|                                                                  |

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

void TimePattern(MqlDateTime &time_struct)

  {

   if(time_struct.hour==InpOpeningHour)

     {

      m_trade.SellStop(InpLots,m_symbol.NormalizePrice(m_symbol.Bid()-ExtDistance),m_symbol.Name(),

                       m_symbol.NormalizePrice(m_symbol.Bid()-ExtDistance+ExtStopLoss));

      m_trade.BuyStop(InpLots,m_symbol.NormalizePrice(m_symbol.Ask()+ExtDistance),m_symbol.Name(),

                      m_symbol.NormalizePrice(m_symbol.Ask()+ExtDistance-ExtStopLoss));

     }

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates()

  {

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

   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=m_symbol.LotsMax();

   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=m_symbol.LotsStep();

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

  }

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

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

  }

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

//| Delete all pending rders                                         |

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

void DeleteAllOrders()

  {

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

      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties

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

            m_trade.OrderDelete(m_order.Ticket());

  }

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

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