//+------------------------------------------------------------------+
//|                                                 MA-EA-Mark11.mq4 |
//|                                  Copyright(c) 2010 Masaru Sasaki |
//|                                                                  |
//+------------------------------------------------------------------+
//
// < MetaTrader4 FX(O×ÖØàæø)©®VXeEA >
// u\[XR[höJRv
//  *êÊIÈÂlg[_[ÌêlƵÄAvOÉsµêÈûÖÌ
//   QlƵÄöJ·éɵܵ½B
//   ©ªÍK¢ÉàvO~Oðï¡ÆµÄ½Tf[vO}[
//   Å é©çAVXevO~O(EA)ªÅ«Üµ½B
//   vO~OÍK¤æèAµêë¾Æv¢Ü·B
//
// uÖ~v
//  *±ÌvO\[XR[hð»ÌÜܬpµ½\tgÌÌð
//   êØÖ~vµÜ·B
// 
// uÆÓv
//  *±ÌvOÉîÃs×Ìʶµ½áQA¹¸ÈÇÉ¢Ä
//    ìÒÍêØÌÓCð¢Ü¹ñB
//
// uà¾v
//  *^wWÆPivotwWðgí¹½
//   VXeÅ·B
//
// QlÐFFX^g[_[üå (PanRolling)
//           FX^g[_[ÀHvO~O (PanRolling)
//           VXeüå (PanRolling)
//           ÌVXe with TradeStation (PanRolling)
//           ¿áñÆ×¯½¢lÌ¿`[gªÍåS  (©R¯Ð)
//           ±±ÜÅg¦é[\N«`[g (ú{oÏV·Ð)
//
#property copyright "Copyright(c) 2010 Masaru Sasaki"
#property link      "youtarou.blogzine.jp"
#include <stderror.mqh>
// }WbNio[
#define MAGIC 20100913
// XgbvX^Cv
#define STOPLOSPIPS 1
#define STOPLOSRATE 2
// XgbvXðpipsÉ·é©¿iÉηép[Ze[W·é©ðIð
extern int STOPLosType = STOPLOSPIPS;    // 0=pips  1=[g                       
extern int STOPpips = 50;     // XgbvXpips
extern double STOPrate = 0.01; // XgbvX[g 0.01=1
// H¢l (Pips)
extern int TAKEProfit = 200;
// H¢lÌIItÝè WI
extern bool TAKEProfitFlag = true;
// g[hÔÑÝè
extern string STime = "22:00";  // Jn
extern string ETime = "5:00";   // I¹
extern bool TradeTime = false;   // g[hÔÑÌIIt WÍIt
// obWÌIð
extern double Leverage = 1.0; // 1.0 -> 1{
// [A[gÊmÌ on/off
extern bool mail_flag = false;  // WÝèÍIt
// Ŭbg ØïÐÉæèÙÈéB
extern double minlot  = 0.01;   // 1000ÊÝ FOREX.COM Japan
// ^úÔ
extern int Mom_period = 20;
// Xbvx[W
extern int Slippage = 3;      
// fobO on/off tO
bool debug = false;
// |WVÏÖ startÉÄÄoµÄgp
void ClosePositions()
{
   for( int i=0; i < OrdersTotal(); i++ ){
      if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false )
         break;
      if( OrderMagicNumber() != MAGIC || OrderSymbol() != Symbol() )
         continue;
      // I[_[^CvÌ`FbN
      if( OrderType() == OP_BUY )   // ¢
      {
         OrderClose( OrderTicket(), OrderLots(), Bid, Slippage, White);
         break;
      }
      if( OrderType() == OP_SELL )  // è
      {
         OrderClose( OrderTicket(), OrderLots(), Ask, Slippage, White);
         break;
      }
   }
}
// I[_[L³Ì»èÖ startÉÄÄoµÄgp
int OrderPosition()
{
   int r_type = 0;
   
   for( int i=0; i < OrdersTotal(); i++ ){
      if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false )
         break;
      if( OrderMagicNumber() != MAGIC || OrderSymbol() != Symbol() )
         continue;
      // I[_[^CvÌ`FbN
      if( OrderType() == OP_BUY || OrderType() == OP_SELL )
      {
         r_type = OrderType();
         break;
      }
   }
   return(r_type);
}
// g[hÔÑtB^[Ö  startÉÄÄoµÄgp
bool SignalFilter()
{
   string Sdata = TimeToStr(TimeCurrent(), TIME_DATE);
   datetime s_time = StrToTime(Sdata + " " + STime);
   datetime e_time = StrToTime(Sdata + " " + ETime);
   
   bool r = false;
   if( s_time < e_time )
   {
      if( TimeCurrent() >= s_time && TimeCurrent() < e_time ) r = true;
      else r = false;
   }
   else
   {
      if( TimeCurrent() >= e_time && TimeCurrent() < s_time ) r = false;
      else r = true;
   }
   // g[hÔÑItÌêAíÉ^
   if( TradeTime == false ) r = true;
   
   return(r);
}
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   // o[ÌnlÅg[hÂ\©`FbN
   if( Volume[0] > 1 || IsTradeAllowed() == false )
      return(0);
   
   // PivotÌvZÆuCNAEg»è (ÔgÍú«ðgp)
   int s = iBarShift(NULL, PERIOD_D1, Time[1]) + 1;
   double LH = iHigh(NULL, PERIOD_D1, s);
   double LL = iLow(NULL, PERIOD_D1, s);
   double LC = iClose(NULL, PERIOD_D1, s);
   double P = (LH + LL + LC)/3;
   double R1 = P + (P - LL);
   double S1 = P - (LH - P);
   double R2 = P + (LH - LL);
   double S2 = P - (LH - LL);
   double HBOP = LH + 2*(P - LL);
   double LBOP = LL - 2*(LH - P); 
   if( Close[2] <= HBOP && Close[1] > HBOP ) bool Pivot_Up = true;
   if( Close[2] >= LBOP && Close[1] < LBOP ) bool Pivot_Down = true;
   
   // ^ÌvZ
   double Mom1 = iMomentum(NULL, 0, Mom_period, PRICE_CLOSE, 1);   
     
   // XgbvXvZp
   double StopType = 0.0;
   
   // H¢lvZp
   double TakeProfit = 0.0;
   
   // bgÌvZ
   double Lots = NormalizeDouble(AccountFreeMargin()*Leverage/100000,1);
   if( Lots < minlot ) Lots = minlot;
   
   // I[_[^CAEgÔ 12bÔ
   int OrderWaitTime = 12;
   int startTime;
   
   // ¢VOi
   // ðF^lª100æèã©ÂPivotwWÅ㲯
   //
   if( SignalFilter() == true )
   if( Mom1 > 100 )
   if( Pivot_Up == true )
   {
      if( OrderPosition() == OP_BUY  )  // ¢I[_[ª·ÅÉLéêÍLZ·éBisµÈ¢B
         return(0);
      ClosePositions();
      if( STOPLosType == STOPLOSPIPS ) StopType = Ask-STOPpips*Point;
      else
         StopType = Ask*(1-STOPrate);
      if( TAKEProfitFlag == true ) TakeProfit = Ask+TAKEProfit*Point;
      else
         TakeProfit = 0;
      
      // ÊMG[ª Á½êAI[_[ðJèÔ·B
      startTime = GetTickCount();
      while( true )
      {
         if( GetTickCount() - startTime > OrderWaitTime*1000)
         {
            Alert("OrderSend Time Out ERROR!! Check Experts Log!!");
            // [MÂÅ êÎAG[ðMB
            if( mail_flag == true )
               SendMail("MetaTrader 4 MAIL A[g:", "OrderSend ¢^CAEgG[!!"+Symbol());
            return(false);
         }
         if( IsTradeAllowed() == true )
         {
            RefreshRates();
            if(OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopType, TakeProfit, "", MAGIC, 0, Red) != -1)
               return(true);
            int buyerror = GetLastError();
            Print("OrderSend Error : ", buyerror);
         }
         Sleep(100);
      }
      // [A[gÊm
      if( mail_flag == true )
         SendMail("MetaTrader4 MAIL A[g:", "¢" + Symbol() + "at" + DoubleToStr(Ask, Digits));
      return(0);
   }
   
   // èVOi
   // ðF^lª100æèº©ÂPivotwW겝
   //
   if( SignalFilter() == true )
   if( Mom1 < 100 )
   if( Pivot_Down == true )
   {
      if( OrderPosition() == OP_SELL  )  // èI[_[ª·ÅÉLéêÍLZ·éBisµÈ¢B
         return(0);
      ClosePositions();
      if( STOPLosType == STOPLOSPIPS ) StopType = Bid+STOPpips*Point;
      else
         StopType = Bid*(1+STOPrate);
      if( TAKEProfitFlag == true ) TakeProfit = Bid-TAKEProfit*Point;
      else
         TakeProfit = 0;
      // ÊMG[ª Á½êAI[_[ðJèÔ·B
      startTime = GetTickCount();
      while( true )
      {
         if( GetTickCount() - startTime > OrderWaitTime*1000)
         {
            Alert("OrderSend Time Out ERROR!! Check Experts Log!!");
            // [MÂÅ êÎAG[ðMB
            if( mail_flag == true )
               SendMail("MetaTrader 4 MAIL A[g:", "OrderSend è^CAEgG[!!"+Symbol());
            return(false);
         }
         if( IsTradeAllowed() == true )
         {
            RefreshRates();
            if(OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopType, TakeProfit, "", MAGIC, 0, Blue) != -1)
               return(true);
            int sellerror = GetLastError();
            Print("OrderSend Error : ", sellerror);   
         }
         Sleep(100);
      }
      // [A[gÊm
      if( mail_flag == true )
         SendMail("MetaTrader4 MAIL A[g:", "è" + Symbol() + "at" + DoubleToStr(Bid, Digits));
      return(0);
   }
   
   // ÝèÔÑÅ|WVªLéêÌèd¢
   if( OrderPosition() == OP_BUY )
   {
      if( SignalFilter() == false ) ClosePositions();
   }
   else
   if( OrderPosition() == OP_SELL )
   {
      if( SignalFilter() == false ) ClosePositions();
   }
   
   // fobO\¦
   if( debug == true )
   {
      Print("Il :", Close[1]);
      Print("^ :", Mom1);
      Print("ohº²¯:", Pivot_Down);
      Print("oh㲯:", Pivot_Up);
   }
   
   return(0);
  }
//+------------------------------------------------------------------+
             
            
Comments