//+------------------------------------------------------------------+
//|                                        GJ_daily_125.mqr, Ver 1.0 |
//|                    strategy created by MichaelLee @ forexfactory |
//|                                              coded by Henry Zhao |
//|                                              shinetrip@yahoo.com |
//|                                                    July 25, 2010 |
//|                                                                  |
//| Original posts:                                                  |
//| http://www.forexfactory.com/showthread.php?t=236366              |
//+------------------------------------------------------------------+

#property copyright "MichaelLee"

extern double lots = 0.01;
extern int server_time_offset = 0;
extern double tp = 1.25;
extern double sl = 1.0;

string trading_system_name = "GJ125";          // 本交易系统的名称
int magic_number = 100921;                     // 用来区分本系统和外系统的订单

datetime t1, t2;


//+------------------------------------------------------------------+
//| 初始化代码                                                       |
//+------------------------------------------------------------------+
int init(){

return(0);

}


//+------------------------------------------------------------------+
//| 反初始化代码                                                     |
//+------------------------------------------------------------------+
int deinit(){

return(0);

}


//+------------------------------------------------------------------+
//| 检查系统中是否已经有特定类型的订单。                             |
//+------------------------------------------------------------------+
bool check_orders( bool direction){
int i, order_num, order_type;

order_num = OrdersTotal();

for(i=0; i<order_num; i++){
   OrderSelect(i, SELECT_BY_POS);
   if( OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic_number ) continue; //不是本系统的订单

   order_type = OrderType();
   if( direction && order_type==OP_BUY )   return(true);
   if( !direction && order_type==OP_SELL ) return(true);
   }
   
return(false);
}


//+------------------------------------------------------------------+
//| 关闭所有多单。                                                   |
//+------------------------------------------------------------------+
int close_long(){
int i, order_num;
double price;

order_num = OrdersTotal();

for(i=0; i<order_num; i++){
      OrderSelect(i, SELECT_BY_POS);
      if( OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic_number ) continue;
      
      if(OrderType()==OP_BUY){
            RefreshRates();
            OrderClose( OrderTicket(), OrderLots(), Bid, 3, CLR_NONE);
            }
      }

return(0);
}


//+------------------------------------------------------------------+
//| 关闭所有空单。                                                   |
//+------------------------------------------------------------------+
int close_short(){
int i, order_num;
double price;

order_num = OrdersTotal();

for(i=0; i<order_num; i++){
      OrderSelect(i, SELECT_BY_POS);
      if( OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic_number ) continue;
      
      if(OrderType()==OP_SELL){
            RefreshRates();
            OrderClose( OrderTicket(), OrderLots(), Ask, 3, CLR_NONE);
            }
      }

return(0);
}


//+------------------------------------------------------------------+
//| 主程序                                                           |
//+------------------------------------------------------------------+
int start(){
double adx, adx1, rsi, cus_trend, off_trend1, off_trend2;
int cus_signal, off_signal, signal, hour, min;

if( Period()!=30) return(0);
hour = TimeHour(TimeCurrent()) + server_time_offset;
min = TimeMinute(TimeCurrent());

signal = -1;
if( hour==14 && min>0 && min<30){
      adx = iADX( NULL, 0, 14, PRICE_CLOSE, MODE_MAIN, 1);
      if( adx>25 && adx<99){
            rsi = iRSI( NULL, 0, 20, PRICE_CLOSE, 1);
            cus_trend = iCustom( NULL, 0, "Custom Trend", 80, 3, 0, 0, 1);
            if( cus_trend>50 && cus_trend<500) cus_signal =0; else cus_signal =1;
            off_trend1 = iCustom( NULL, 0, "ForexOffTrend_Alert", 400, 5, 1.6, 50.6, false, 0, 1);
            off_trend2 = iCustom( NULL, 0, "ForexOffTrend_Alert", 400, 5, 1.6, 50.6, false, 1, 1);
            if( off_trend1>off_trend2) off_signal =0; else off_signal =1;
            if( rsi>50 && cus_signal==0 && off_signal==0) signal =0;
            if( rsi<50 && cus_signal==1 && off_signal==1) signal =1;
            }
      }
if( hour==14 && min>30 && min<60){
      adx1 = iADX( NULL, 0, 14, PRICE_CLOSE, MODE_MAIN, 2);
      adx = iADX( NULL, 0, 14, PRICE_CLOSE, MODE_MAIN, 1);
      if( adx1>25 && adx>25 && adx<99){
            rsi = iRSI( NULL, 0, 20, PRICE_CLOSE, 1);
            cus_trend = iCustom( NULL, 0, "Custom Trend", 80, 3, 0, 0, 1);
            if( cus_trend>50 && cus_trend<500) cus_signal =0; else cus_signal =1;
            off_trend1 = iCustom( NULL, 0, "ForexOffTrend_Alert", 400, 5, 1.6, 50.6, false, 0, 1);
            off_trend2 = iCustom( NULL, 0, "ForexOffTrend_Alert", 400, 5, 1.6, 50.6, false, 1, 1);
            if( off_trend1>off_trend2) off_signal =0; else off_signal =1;
            if( adx>25 && rsi>50 && cus_signal==0 && off_signal==0) signal =0;
            if( adx>25 && rsi<50 && cus_signal==1 && off_signal==1) signal =1;
            }
      }
if( hour==15 && min>0 && min<30){
      adx1 = iADX( NULL, 0, 14, PRICE_CLOSE, MODE_MAIN, 3);
      adx = iADX( NULL, 0, 14, PRICE_CLOSE, MODE_MAIN, 1);
      if( adx1>25 && adx>25 && adx<99){
            rsi = iRSI( NULL, 0, 20, PRICE_CLOSE, 1);
            cus_trend = iCustom( NULL, 0, "Custom Trend", 80, 3, 0, 0, 1);
            if( cus_trend>50 && cus_trend<500) cus_signal =0; else cus_signal =1;
            off_trend1 = iCustom( NULL, 0, "ForexOffTrend_Alert", 400, 5, 1.6, 50.6, false, 0, 1);
            off_trend2 = iCustom( NULL, 0, "ForexOffTrend_Alert", 400, 5, 1.6, 50.6, false, 1, 1);
            if( off_trend1>off_trend2) off_signal =0; else off_signal =1;
            if( adx>25 && rsi>50 && cus_signal==0 && off_signal==0) signal =0;
            if( adx>25 && rsi<50 && cus_signal==1 && off_signal==1) signal =1;
            }
      }
if( hour<14 || hour>15) signal = -1;

if( signal==0 && check_orders(true)==false && TimeCurrent()-t2>7200){
      close_short();
      RefreshRates();
      OrderSend( Symbol(), OP_BUY, lots, Ask, 3, Ask-sl,
                 Ask+tp, trading_system_name, magic_number, 0, CLR_NONE);
      t1 = TimeCurrent();
      }

if( signal==1 && check_orders(false)==false && TimeCurrent()-t2>7200){
      close_long();
      RefreshRates();
      OrderSend( Symbol(), OP_SELL, lots, Bid, 3, Bid+sl,
                 Bid-tp, trading_system_name, magic_number, 0, CLR_NONE);
      t2 = TimeCurrent();
      }

return(0);
}
//+------------------------------------------------------------------+


