#property copyright "Copyright(C) 2014,T.F"

#define INDIVAL_CNT 2
extern double Lots = 0.03;
extern int Slippage = 4;
extern int MaxSpread = 5;
extern bool MM = false;
extern double Risk = 2.0;
extern int Magic= 59872465;
extern int I01_RSIOMA = 14;
extern int I01_RSIOMA_MODE = 1;
extern int I01_RSIOMA_PRICE = 0;
extern int I01_Ma_RSIOMA = 21;
extern int I01_Ma_RSIOMA_MODE = 1;
extern int I01_BuyTrigger = 80;
extern int I01_SellTrigger = 20;
extern int I01_BuyTriggerColor = 16748574;
extern int I01_SellTriggerColor = 16711935;
extern int I01_MainTrendLong = 50;
extern int I01_MainTrendShort = 50;
extern int I01_MainTrendLongColor = 255;
extern int I01_MainTrendShortColor = 32768;
extern int If_Num_0 = 13;
extern int If_Num_1 = 80;
extern int If_Num_2 = 80;
extern int If_Num_3 = 13;

double Lotsbk;

int init(){
   Lotsbk = Lots;
   return(0);
}

int start(){
   int limit;
   int i;
   int iTicket;
   int iErrChk;
   int iLots;
   double dMinLots = MarketInfo(Symbol(),MODE_MINLOT);
   double dMaxLots = MarketInfo(Symbol(),MODE_MAXLOT);
   double dMMargin;
   int iAllPosCnt;
   int iBuyCnt;
   int iSellCnt;
   int iBuyLimitCnt;
   int iSellLimitCnt;
   double dIndiVal[INDIVAL_CNT];
   ArrayInitialize(dIndiVal,0);

   if( MM == true ){
      dMMargin = AccountFreeMargin() * Risk * 0.01;
      Lots = MathFloor((dMMargin / MarketInfo(Symbol(),MODE_MARGINREQUIRED)) / dMinLots) * dMinLots;
      if( Lots == 0 ) Lots = dMinLots;      iLots = dMinLots * 100;
      if( iLots % 10 != 0 ){
         Lots = NormalizeDouble(Lots,2);
      }else if( iLots % 100 != 0 ){
         Lots = NormalizeDouble(Lots,1);
      }else{
         Lots = NormalizeDouble(Lots,0);
      }
      if( Lots > dMaxLots ) Lots = dMaxLots;
   }

   double dPoint = Point;
   if( Digits==3 || Digits==5 ) dPoint = Point * 10;

   if( OrdersTotal() > 0){
      for( i=0; i<OrdersTotal(); i++ ){
         if( OrderSelect(i, SELECT_BY_POS) == true && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() ){
            if( OrderType() == OP_BUY ){
               iAllPosCnt++;
               iBuyCnt++;
            }else if( OrderType() == OP_SELL ){
               iAllPosCnt++;
               iSellCnt++;
            }else if( OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP ){
               iBuyLimitCnt++;
            }else if( OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP ){
               iSellLimitCnt++;
            }
         }
      }
   }

   dIndiVal[0] = iCustom(NULL,0,"RSIOMA",I01_RSIOMA,I01_RSIOMA_MODE,I01_RSIOMA_PRICE,I01_Ma_RSIOMA,I01_Ma_RSIOMA_MODE,I01_BuyTrigger,I01_SellTrigger,I01_BuyTriggerColor,I01_SellTriggerColor,I01_MainTrendLong,I01_MainTrendShort,I01_MainTrendLongColor,I01_MainTrendShortColor,0,0);
   dIndiVal[1] = iCustom(NULL,0,"RSIOMA",I01_RSIOMA,I01_RSIOMA_MODE,I01_RSIOMA_PRICE,I01_Ma_RSIOMA,I01_Ma_RSIOMA_MODE,I01_BuyTrigger,I01_SellTrigger,I01_BuyTriggerColor,I01_SellTriggerColor,I01_MainTrendLong,I01_MainTrendShort,I01_MainTrendLongColor,I01_MainTrendShortColor,5,0);

   ObjectCreate("EAName",OBJ_LABEL,0,0,0);
   ObjectSetText("EAName","Alien's.advanced", 18, "Arial",  16777215);
   ObjectSet("EAName", OBJPROP_CORNER, 0);
   ObjectSet("EAName", OBJPROP_XDISTANCE, 30);
   ObjectSet("EAName", OBJPROP_YDISTANCE, 20);
   ObjectCreate("EAPrice",OBJ_LABEL,0,0,0);
   ObjectSetText("EAPrice","Price  "+DoubleToStr(MarketInfo(Symbol(),MODE_BID),Digits), 12, "Arial",  White);
   ObjectSet("EAPrice", OBJPROP_CORNER, 0);
   ObjectSet("EAPrice", OBJPROP_XDISTANCE, 40);
   ObjectSet("EAPrice", OBJPROP_YDISTANCE, 55);
   ObjectCreate("EASpread",OBJ_LABEL,0,0,0);
   ObjectSetText("EASpread","Spread  "+DoubleToStr((Ask - Bid)/dPoint,Digits-4)+"  :MAXSpread  "+DoubleToStr(MaxSpread,Digits-4), 12, "Arial",  White);
   ObjectSet("EASpread", OBJPROP_CORNER, 0);
   ObjectSet("EASpread", OBJPROP_XDISTANCE, 40);
   ObjectSet("EASpread", OBJPROP_YDISTANCE, 80);

   if( iBuyCnt < 1 && iSellCnt == 0 && (MarketInfo(Symbol(),MODE_ASK)-MarketInfo(Symbol(),MODE_BID) < MaxSpread * dPoint) ){
      if( iAllPosCnt == 0 ){
         if( dIndiVal[0] < If_Num_0 ){
            if( dIndiVal[0] < dIndiVal[1] ){
               iTicket = OrderSend( Symbol(),OP_BUY,Lots * 1,MarketInfo(Symbol(),MODE_ASK),Slippage,0,0,"",Magic,0,Red);
               if( iTicket < 0 ){
                  Print( "Error opening BuyOrder :",GetLastError() );
               }
            }
         }
      }
   }
   if( iSellCnt < 1 && iBuyCnt == 0 && (MarketInfo(Symbol(),MODE_ASK)-MarketInfo(Symbol(),MODE_BID) < MaxSpread * dPoint) ){
      if( iAllPosCnt == 0 ){
         if( dIndiVal[0] > If_Num_1 ){
            if( dIndiVal[0] > dIndiVal[1] ){
               iTicket = OrderSend( Symbol(),OP_SELL,Lots * 1,MarketInfo(Symbol(),MODE_BID),Slippage,0,0,"",Magic,0,Blue);
               if( iTicket < 0 ){
                  Print( "Error opening SellOrder :",GetLastError() );
               }
            }
         }
      }
   }
   if( iBuyCnt > 0 || iBuyLimitCnt > 0 ){
      if( dIndiVal[0] > If_Num_2 ){
         if( dIndiVal[0] > dIndiVal[1] ){
            if( iBuyCnt > 0 ){
               while( !IsStopped() ){
                  iErrChk = 0;
                  for( i=OrdersTotal()-1; i>=0; i-- ){
                     if( OrderSelect(i, SELECT_BY_POS) == true ){
                        if( OrderType() == OP_BUY && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() ){
                           if( !(OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),Slippage,Green)) ){
                              iErrChk = 1;
                           }
                        }
                     }
                  }
                  if( iErrChk == 0 ) break;
                  Sleep(500);
                  RefreshRates();
               }
            }
         }
      }
   }
   if( iSellCnt > 0 || iSellLimitCnt > 0 ){
      if( dIndiVal[0] < If_Num_3 ){
         if( dIndiVal[0] < dIndiVal[1] ){
            if( iSellCnt > 0 ){
               while( !IsStopped() ){
                  iErrChk = 0;
                  for( i=OrdersTotal()-1; i>=0; i-- ){
                     if( OrderSelect(i, SELECT_BY_POS) == true ){
                        if( OrderType() == OP_SELL && OrderMagicNumber() == Magic && OrderSymbol() == Symbol() ){
                           if( !(OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),Slippage,Green)) ){
                              iErrChk = 1;
                           }
                        }
                     }
                  }
                  if( iErrChk == 0 ) break;
                  Sleep(500);
                  RefreshRates();
               }
            }
         }
      }
   }

   return(0);
}

int deinit(){
   return(0);
}

