Download MetaTrader 5

RSI open order at end time frame

To add comments, please log in or register
Fabio Geraci
357
Fabio Geraci 2015.05.04 00:14 

Hello all,

 first of all i am pretty new at EA coding, second I have looked for a related post in the forum itself and on mighty google, even on stackoverflow.

Now I wrote a simple EA to open an order if certain conditions are met, one of which is  double RSI_1 = iRSI(NULL,PERIOD_H1,14,PRICE_CLOSE,0) > 70. Well as you can see in the attachment the order is opened not at the close price of the candle but higher, according to the picture it should have opened at the next candle on the right.

 I would think, according to the documentation that by using price_close the rsi would get calculated at the close price for the time frame selected, but it doesn't look that way. what am i missing? how can i fix it?

 thanks 

Files:
Capture.JPG 30 kb
Stuart Browne
5887
Stuart Browne 2015.05.04 01:31  

Hi Fabio

You need to wait until the bar closes and the next one opens, then check the bar close. So you will be looking at Bar 1, not Bar 0. So you just need to change the Shift parameter in your RSI call:

 iRSI(NULL,PERIOD_H1,14,PRICE_CLOSE,1) 

Cheers

Fabio Geraci
357
Fabio Geraci 2015.05.04 05:44  
Stuart Browne:

Hi Fabio

You need to wait until the bar closes and the next one opens, then check the bar close. So you will be looking at Bar 1, not Bar 0. So you just need to change the Shift parameter in your RSI call:

 iRSI(NULL,PERIOD_H1,14,PRICE_CLOSE,1) 

Cheers

Stuart,

 

thank you for your reply, would that be valid for the others indicators i am using?

 

thanks 

Stuart Browne
5887
Stuart Browne 2015.05.04 05:59  
Fabio Geraci:

Stuart,

 

thank you for your reply, would that be valid for the others indicators i am using?

 

thanks 

Depends on the indicator and your strategy Fabio, but in most cases, yes.

Cheers
Stu
Fabio Geraci
357
Fabio Geraci 2015.05.04 07:14  
Thanks Stuart, i will do some testing on my strategy.
Fabio Geraci
357
Fabio Geraci 2015.05.05 12:16  
Stuart Browne:
Depends on the indicator and your strategy Fabio, but in most cases, yes.

Cheers
Stu

Hello Stuart,

I have implemented some more logic to my EA, but I am struggling to get to work as i thought it should:

double Macd_Main = iMACD(NULL,PERIOD_CURRENT,12,16,9,PRICE_CLOSE,MODE_MAIN,1); 

if(IMA_8>IMA_34 && Sto_1>85 && (Macd_Main>-0.00005||Macd_Main<0.00005)) // Here is your open buy rule 

according to the logic the EA should not open a new position but it actually opens a lot of them.

thanks for the help 

Files:
Capture2.JPG 185 kb
Stuart Browne
5887
Stuart Browne 2015.05.05 12:28  
Fabio Geraci:

Hello Stuart,

I have implemented some more logic to my EA, but I am struggling to get to work as i thought it should:

double Macd_Main = iMACD(NULL,PERIOD_CURRENT,12,16,9,PRICE_CLOSE,MODE_MAIN,1); 

if(IMA_8>IMA_34 && Sto_1>85 && (Macd_Main>-0.00005||Macd_Main<0.00005)) // Here is your open buy rule 

according to the logic the EA should not open a new position but it actually opens a lot of them.

thanks for the help 

G'day Fabio

At this stage, it's probably better if you post up the whole code so we can see what's going on. Unfortunately, I'm off on 2 weeks holiday as of tomorrow so if I don't answer, hopefully someone else will be able to help you. If not, please send me a message and I'll help you out when I get back.

Cheers
Stu
Fabio Geraci
357
Fabio Geraci 2015.05.05 14:21  
Stuart Browne:
G'day Fabio

At this stage, it's probably better if you post up the whole code so we can see what's going on. Unfortunately, I'm off on 2 weeks holiday as of tomorrow so if I don't answer, hopefully someone else will be able to help you. If not, please send me a message and I'll help you out when I get back.

Cheers
Stu
//
extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=50;
extern double TakeProfit=200;
extern int TrailingStop=0;
extern int RSI_TOP_IN=70;
extern int RSI_BOT_IN=30;
extern int RSI_TOP_OUT=60;
extern int RSI_BOT_OUT=40;
extern int Slippage=3;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
//
  double IMA_8 = iMA(NULL,PERIOD_CURRENT,8,0,MODE_SMA,PRICE_CLOSE,1);
  double IMA_34 = iMA(NULL,PERIOD_CURRENT,34,0,MODE_EMA,PRICE_CLOSE,1);
//
  double Sto_1 = iStochastic(NULL,PERIOD_CURRENT,14,3,3,MODE_SMA,1,MODE_MAIN,1);
//
  double Macd_Main = iMACD(NULL,PERIOD_CURRENT,12,16,9,PRICE_CLOSE,MODE_MAIN,1);
  double Macd_Sign = iMACD(NULL,PERIOD_CURRENT,12,16,9,PRICE_CLOSE,MODE_SIGNAL,1);
//
  double RSI_1 = iRSI(NULL,PERIOD_CURRENT,14,1,1);
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
     if(IMA_8>IMA_34 && Sto_1>85 && (Macd_Main>-0.00005||Macd_Main<0.00005)) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",MagicNumber,0,Blue);
        if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     
  }
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              if(Sto_1>85 && RSI_1>80) //here is your close buy rule
              {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
Fabio Geraci
357
Fabio Geraci 2015.05.05 14:55  

Stuart,

 by the way thank you for your time and help. 

To add comments, please log in or register