Author: Copyright © 2008 Grib
Price Data Components
0 Views
0 Downloads
0 Favorites
RndTrade
ÿþ//+------------------------------------------------------------------+

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

//|                                            Copyright © 2008 Grib |

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

#property copyright "Copyright © 2008 Grib"

#property version   "1.000"

//---

#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 uchar    InpInterval = 60;                // Interval of opening and closing positions (in minutes)

input ulong    m_magic     = 74007192;          // magic number

//---

ulong          m_slippage=30;                // slippage

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

//| Expert initialization function                                   |

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

int OnInit()

  {

   if(InpInterval==0)

     {

      Print("The timer can not be zero!");

      return(INIT_PARAMETERS_INCORRECT);

     }

//--- create timer

   ResetLastError();

   if(!EventSetTimer(InpInterval*60))

     {

      Print("Error create timer #",GetLastError());

      return(INIT_FAILED);

     }

//---

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

      return(INIT_FAILED);

   RefreshRates();

//---

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

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//--- destroy timer

   EventKillTimer();



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---



  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

   Print(__FUNCTION__);

   if(!RefreshRates())

      OnTimer();

//--- step 1: close  position

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

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

//--- step 2: open new position

   int  math_rand=MathRand();

   if(math_rand<32767/2)

      m_trade.Buy(m_symbol.LotsMin());

   else

      m_trade.Sell(m_symbol.LotsMin());



  }

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

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

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates(void)

  {

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

  }

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

Comments