Holding İndicator results until the value will reach a specific level

 


Hello,

I wonder that how can I do explained below.
For example, if RSI > 50, MACD cross upward if its value below 0 and lastly CCI just cross over -100 for buy signal. Also, for second trade, all of them should turn to their default levels as the initial period of the first order(RSI should go below 50 and then again take buy signal if it cross 50 upwards, CCI should go below -100 and waiting a new cross)

Actually I wrote the conditions but, I couldn't code the condition they should turn to their default positions for buy signal.

Thank you for your interest,

Cheers,
Baris

 
BarisYalcinkaya: I wonder that how can I do explained below.For example, if RSI > 50, MACD cross upward if its value below 0 and lastly CCI just cross over -100 for buy signal. Also, for second trade, all of them should turn to their default levels as the initial period of the first order(RSI should go below 50 and then again take buy signal if it cross 50 upwards, CCI should go below -100 and waiting a new cross) Actually I wrote the conditions but, I couldn't code the condition they should turn to their default positions for buy signal.

Please show your relevant codes.

 
#property copyright "Copyright 2014, BarisYalcinkaya."
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict


extern double StopLoss_Pips = 9;
//extern double TakeProfit_Pips = 36;
extern double RiskPercent = 3;
extern double Reward_Ratio = 1.5;


extern int RSI_Period_W = 14;

extern int SMA_Period_D = 200;
extern int MACD_Fast_D  = 26;
extern int MACD_Slow_D  = 12;
extern int MACD_Shift_D = 9;
extern int ADX_Period_D = 14;
extern int CCI_Period_D = 14;
extern int RSI_Period_D = 14;

extern int MACD_Fast_H4  = 26;
extern int MACD_Slow_H4  = 12;
extern int MACD_Shift_H4 = 9;
extern int ADX_Period_H4 = 14;
extern int CCI_Period_H4 = 14;
extern int RSI_Period_H4 = 14;

double pips;

int init()
  {
//----
   double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);
      if(ticksize == 0.00001 || ticksize == 0.001)
      pips = ticksize*10;
      else pips = ticksize;
//----
   return(0);
  }


int start()
{
 //Rule_0(Weekly) BUY Variables
 double RSI_W;
 
 //Rule_1(Daily) BUY Variables
 double SMA_D;
 
 double DIplus_D,DIminus_D;
 double CCI_Current_D, CCI_Previous_D;
 double RSI_D;
 double MACD_CurMain_D, MACD_CurSignal_D, MACD_PrevMain_D, MACD_PrevSignal_D;
 
 //Rule_2(4Hours) BUY Variables
 double MACD_CurMain_H4, MACD_CurSignal_H4, MACD_PrevMain_H4, MACD_PrevSignal_H4;
 double DIplus_H4,DIminus_H4;
 double CCI_Current_H4, CCI_Previous_H4;
 double RSI_H4;
 
 bool BuyRule_W_RSI;
 bool BuyRule_D_SMA, BuyRule_D_MACD, BuyRule_D_RSI, BuyRule_D_DI, BuyRule_D_CCI;
 bool BuyRule_H4_MACD, BuyRule_H4_RSI, BuyRule_H4_DI, BuyRule_H4_CCI;
 
 bool SellRule_W_RSI;
 bool SellRule_D_SMA, SellRule_D_MACD, SellRule_D_RSI, SellRule_D_DI, SellRule_D_CCI;
 bool SellRule_H4_MACD, SellRule_H4_RSI, SellRule_H4_DI, SellRule_H4_CCI;

 // For loop
 int cnt, ticket, total;
 int MagicNumber=1234;
 

 
   if(Bars<100)
    {
     Print("Bars less than 100");
     return(0);
    } 
//------------------------------------------------------
   //------RSI Weekly
   RSI_W=iRSI(NULL,PERIOD_W1,RSI_Period_W,PRICE_CLOSE,0);
   
   //------SMA Daily
   SMA_D=iMA(NULL,PERIOD_D1,SMA_Period_D,0,MODE_SMA,PRICE_CLOSE,0);
   //MACD Daily
   MACD_CurMain_D=iMACD(NULL,PERIOD_D1,MACD_Fast_D,MACD_Slow_D,MACD_Shift_D,PRICE_CLOSE,MODE_MAIN,0);
   MACD_CurSignal_D=iMACD(NULL,PERIOD_D1,MACD_Fast_D,MACD_Slow_D,MACD_Shift_D,PRICE_CLOSE,MODE_SIGNAL,0);
   MACD_PrevMain_D=iMACD(NULL,PERIOD_D1,MACD_Fast_D,MACD_Slow_D,MACD_Shift_D,PRICE_CLOSE,MODE_MAIN,1);
   MACD_PrevSignal_D=iMACD(NULL,PERIOD_D1,MACD_Fast_D,MACD_Slow_D,MACD_Shift_D,PRICE_CLOSE,MODE_SIGNAL,1);
   //DI+ and DI- Daily
   DIplus_D=iADX(NULL,PERIOD_D1,ADX_Period_D,PRICE_CLOSE,MODE_PLUSDI,0);
   DIminus_D=iADX(NULL,PERIOD_D1,ADX_Period_D,PRICE_CLOSE,MODE_MINUSDI,0);
   //CCI Daily
   CCI_Current_D=iCCI(NULL,PERIOD_D1,CCI_Period_D,PRICE_CLOSE,0);
   CCI_Previous_D=iCCI(NULL,PERIOD_D1,CCI_Period_D,PRICE_CLOSE,1);
   //RSI Daily
   RSI_D=iRSI(NULL,PERIOD_D1,RSI_Period_D,PRICE_CLOSE,0);
   
   
   //-------MACD H4
   MACD_CurMain_H4=iMACD(NULL,PERIOD_H4,MACD_Fast_H4,MACD_Slow_H4,MACD_Shift_H4,PRICE_CLOSE,MODE_MAIN,0);
   MACD_CurSignal_H4=iMACD(NULL,PERIOD_H4,MACD_Fast_H4,MACD_Slow_H4,MACD_Shift_H4,PRICE_CLOSE,MODE_SIGNAL,0);
   MACD_PrevMain_H4=iMACD(NULL,PERIOD_H4,MACD_Fast_H4,MACD_Slow_H4,MACD_Shift_H4,PRICE_CLOSE,MODE_MAIN,1);
   MACD_PrevSignal_H4=iMACD(NULL,PERIOD_H4,MACD_Fast_H4,MACD_Slow_H4,MACD_Shift_H4,PRICE_CLOSE,MODE_SIGNAL,1);
   //CCI H4
   CCI_Current_H4=iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,0);
   CCI_Previous_H4=iCCI(NULL,PERIOD_H4,CCI_Period_H4,PRICE_CLOSE,1);
   //RSI H4
   RSI_H4=iRSI(NULL,PERIOD_H4,RSI_Period_H4,PRICE_CLOSE,0);
   //DI+ and DI- H4
   DIplus_H4=iADX(NULL,PERIOD_H4,ADX_Period_H4,PRICE_CLOSE,MODE_PLUSDI,0);
   DIminus_H4=iADX(NULL,PERIOD_H4,ADX_Period_H4,PRICE_CLOSE,MODE_MINUSDI,0);
   
     
   //ORDER OPENING-------------------

//BUY RULES---------------
   //Rules Weekly Options
   BuyRule_W_RSI  = (RSI_W>50);
   
   //Rules Daily Options
   BuyRule_D_SMA  = (SMA_D>200); 

   BuyRule_D_MACD = ((MACD_CurMain_D>MACD_CurSignal_D) && (MACD_PrevMain_D<MACD_PrevSignal_D));
   BuyRule_D_RSI  = (RSI_D>50);
   BuyRule_D_DI   = (DIplus_D>DIminus_D);
   BuyRule_D_CCI  = ((CCI_Previous_D<CCI_Current_D)&&(CCI_Current_D<-100));
 
   //Rules H4 Options
   BuyRule_H4_MACD = ((MACD_CurMain_H4>MACD_CurSignal_H4) && (MACD_PrevMain_H4<MACD_PrevSignal_H4));
   BuyRule_H4_RSI  = (RSI_H4>50);
   BuyRule_H4_DI   = (DIplus_H4>DIminus_H4);
   BuyRule_H4_CCI  = ((CCI_Previous_H4<CCI_Current_H4)&&(CCI_Current_H4<-100));

//SELL RULES---------------
   //Rules Weekly Options
   SellRule_W_RSI = (RSI_W<50);
   //Rules Daily Options
   SellRule_D_SMA = (SMA_D<200);
   
   SellRule_D_MACD = ((MACD_CurMain_D<MACD_CurSignal_D) && (MACD_PrevMain_D>MACD_PrevSignal_D));
   SellRule_D_RSI  = (RSI_D<50);
   SellRule_D_DI   = (DIplus_D<DIminus_D);
   SellRule_D_CCI  = ((CCI_Previous_D>CCI_Current_D)&&(CCI_Current_D>100));
   
   //Rules H4 Options
   SellRule_H4_MACD = ((MACD_CurMain_H4<MACD_CurSignal_H4) && (MACD_PrevMain_H4>MACD_PrevSignal_H4));
   SellRule_H4_RSI  = (RSI_H4<50);
   SellRule_H4_DI   = (DIplus_H4<DIminus_H4);
   SellRule_H4_CCI  = ((CCI_Previous_H4>CCI_Current_H4)&&(CCI_Current_H4>100));
   
   
 //---------------------------------------------------------------
 
   //Order Opening --------
   
   total=OrdersTotal();
   if(total<1)
     {
      //Checking buy possibility
      if(IsNewCandle())
        {
         if(BuyRule_W_RSI)
           {
          /*  if(BuyRule_D_SMA)
              {
               if((BuyRule_D_MACD && BuyRule_D_RSI)||(BuyRule_D_MACD && BuyRule_D_DI)||(BuyRule_D_MACD && BuyRule_D_CCI)||
                  (BuyRule_D_RSI &&  BuyRule_D_DI) ||(BuyRule_D_RSI && BuyRule_D_CCI)||(BuyRule_D_DI && BuyRule_D_CCI))
                  {*/
                   if((BuyRule_H4_MACD && BuyRule_H4_RSI)||(BuyRule_H4_MACD && BuyRule_H4_DI)||
                      (BuyRule_H4_MACD && BuyRule_H4_CCI)||(BuyRule_H4_RSI && BuyRule_H4_DI)||
                      (BuyRule_H4_RSI && BuyRule_H4_CCI) ||(BuyRule_H4_DI && BuyRule_H4_CCI))
                      {
                       double LotSize_Buy = 0;
                       double Equity_Buy = AccountEquity();
                       double RiskedAmount_Buy = Equity_Buy*RiskPercent*0.01;
                       
                       double SL_Buy = Low[1]-StopLoss_Pips*pips;
                       double SL_Buy_Loss = Bid - SL_Buy;
                       double TP_Buy = Bid + (SL_Buy_Loss*Reward_Ratio);
                       LotSize_Buy = (RiskedAmount_Buy/(SL_Buy_Loss/pips))/10;
                       
                       ticket=OrderSend(Symbol(),OP_BUY,LotSize_Buy,Ask,3,SL_Buy,TP_Buy,NULL,MagicNumber,0,Green);
                       if(ticket>0)
                         {
                          if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                          Comment("BUY order opened : ",OrderOpenPrice());
                         }
                          else Comment("Error opening BUY order : ",GetLastError());
                          return(0);
                         
                     }      // H4_Indicators Buy      
              //    }         // D_Indicators Buy
            //  }             //  D_SMI Buy
           }                // W_RSI Buy
         
      //Checking Sell Posibility
         
         if(SellRule_W_RSI)
           {
            if(SellRule_D_SMA)
              {
               if((SellRule_D_MACD && SellRule_D_RSI)||(SellRule_D_MACD && SellRule_D_DI)||(SellRule_D_MACD && SellRule_D_CCI)||
                  (SellRule_D_RSI  && SellRule_D_DI) ||(SellRule_D_RSI && SellRule_D_CCI)||
                  (SellRule_D_DI   && SellRule_D_CCI))
                  {
                   if((SellRule_H4_MACD && SellRule_H4_RSI)||(SellRule_H4_MACD && SellRule_H4_DI)||(SellRule_H4_MACD && SellRule_H4_CCI)||
                      (SellRule_H4_RSI  && SellRule_H4_DI) ||(SellRule_H4_RSI && SellRule_H4_CCI)||
                      (SellRule_H4_DI   && SellRule_H4_CCI))
                      {
                       double LotSize_Sell = 0;
                       double Equity_Sell = AccountEquity();
                       double RiskedAmount_Sell = Equity_Sell*RiskPercent*0.01;
                       
                       double SL_Sell = High[1] + StopLoss_Pips*pips;
                       double SL_Sell_Loss = SL_Sell - Ask;
                       double TP_Sell = Ask - (SL_Sell_Loss*Reward_Ratio);
                       
                       LotSize_Sell = (RiskedAmount_Sell/(SL_Sell_Loss/pips))/10;
                      
                      }    // H4_Indicators Sell
                  }        // D_Indicators Sell
              }            // D_SMA Sell
           }               // W_RSI Sell
              
                 
                       
                       
         
         
         
         
         
         }                  // IsNewCandle
     
     
     }                      // total 
   
    return(0);
}  // Start()
   
   
bool IsNewCandle()
{
 static int  BarsOnChart = 0;
 if(Bars == BarsOnChart)
 return(false);
 BarsOnChart = Bars;
 return(true);
} 
 

It' looking 3 different timeframes.

First rule is: Weekly=RSI > 50,
Second rule is: if (
Daily Price > MVA 200) check the other conditions.
If any of the following 2 triggers happened(Daily):

a)Daily=RSI > 50
b)Daily=MACD ShortEMA Cross over Long EMA (below the 0 level)

c)Daily=DMI DI+>DI-

d)Daily=CCI JustCrossOver -100

Then,

If any of the following 2 triggers happened(4Hour):

a)4Hour=RSI > 50

b)4Hour=MACD ShortEMA Cross over Long EMA (below the 0 level)

c)4Hour=DMI DI+>DI-

d)4Hour=CCI > -100

I defined all of them however I can't create a condition for the second trade which should be opened. For example, if RSI firstly go down to 50 and then waiting a upward cross, MACD should go below 0 and then waiting another signal which is based on crossing main line and its signal line, and lastly DI+ should go back below the DI- and then cross it upwards second time.

I add a picture which maybe explain it better.

Thank you for your interest,

Baris

 

Trading functions can be used in Expert Advisors and scripts.

Trading functions of OrderSend(), OrderClose(), OrderCloseBy(), OrderDelete(), and OrderModify() cannot be called from custom indicators

bool StatusRsi, RsiCrossed, StatusMACD, CrossingMainTheSignal, DIPlusBelowDIMinus, DIPlusAboveDIMinus, ConditionToOpenTrade;

if (RSI < 50)
StatusRsi = true;

if (StatusRsi && RSI > 50)
RsiCrossed = true;

if (MACD < 0)
StatusMACD = true;

if (StatusMACD && MACD > 0)
CrossingMainTheSignal == true;

if (DI+ < DI-)
DIPlusBelowDIMinus == true;

if (DIPlusBelowDIMinus && DI+ > DI-)
DIPlusAboveDIMinus == true

if (RsiCrossed && CrossingMainTheSignal && DIPlusAboveDIMinus)
ConditionToOpenTrade = true;

if (ConditionToOpenTrade)
OrderSend (....)
Reason: