New to MQL4, need help with RSI coding please!

 

Hi, 

I'm totally new to MQL4. After hours of Googling and fixing but to no avail, was wondering if anyone can help me with this:

1) I tried to signal my buy trades with RSI < 30  but looks like it executes buy trades even when RSI is higher than 30... what could be possibly wrong? 

2) It doesn't execute any Sell trades! I pretty much modified one of the sample codes in the Libraries, and the sample codes executed sell trades okay, but why not with mine, when I just changed the buy/sell conditions? 

3) Is there a way to remove "close at stop"? looks like there's a quite huge loss always at the Close at Stop. Also, when we actually use the codes with actual trading, will this Close at Stop happen?  

 

Here are the codes:  

     if ((RSI < 31));

        {

         Print("RSI at Buy", RSI);

         ticket=OrderSend(Symbol(), OP_BUY ,Lots,Ask,10,moving_avg - 100 ,Ask + TakeProfit*Point,"buy",0,0,Green);

         if(ticket>0)

           {

            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

               Print("BUY order opened : ",OrderOpenPrice());

           }

         else

            Print("Error opening BUY order : ",GetLastError());

         return;

        }

      //--- check for short position (SELL) possibility

      if ((RSI > 69));

    

        {

         Print("RSI at Sell", RSI);

         ticket=OrderSend(Symbol(),OP_SELL ,Lots,Bid,3,moving_avg + 100,Bid-TakeProfit*Point,"Sell",0,0,Red);

         if(ticket>0)

           {

            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

               Print("SELL order opened : ",OrderOpenPrice());

           }

         else

            Print("Error opening SELL order : ",GetLastError());

        }

      return;
 
pw2232:

1) I tried to signal my buy trades with RSI < 30  but looks like it executes buy trades even when RSI is higher than 30... what could be possibly wrong? 

2) It doesn't execute any Sell trades! I pretty much modified one of the sample codes in the Libraries, and the sample codes executed sell trades okay, but why not with mine, when I just changed the buy/sell conditions? 

3) Is there a way to remove "close at stop"? looks like there's a quite huge loss always at the Close at Stop. Also, when we actually use the codes with actual trading, will this Close at Stop happen? 

  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. We have no idea what the variable RSI is or it's value. Post all relevant code. Print out your variables, and find out why.
  3. Same answer as #2
  4. No idea what you mean by "Close at Stop" all stops when hit close the ticket. If you mean the Stop Loss, set it to zero and trade with infinite risk! It's your OrderSend code, change it to what you want.
 
pw2232:

Hi, 

I'm totally new to MQL4. After hours of Googling and fixing but to no avail, was wondering if anyone can help me with this:

1) I tried to signal my buy trades with RSI < 30  but looks like it executes buy trades even when RSI is higher than 30... what could be possibly wrong? 

2) It doesn't execute any Sell trades! I pretty much modified one of the sample codes in the Libraries, and the sample codes executed sell trades okay, but why not with mine, when I just changed the buy/sell conditions? 

3) Is there a way to remove "close at stop"? looks like there's a quite huge loss always at the Close at Stop. Also, when we actually use the codes with actual trading, will this Close at Stop happen?  

 

Here are the codes:  

     if ((RSI < 31));

        {

         Print("RSI at Buy", RSI);

         ticket=OrderSend(Symbol(), OP_BUY ,Lots,Ask,10,moving_avg - 100 ,Ask + TakeProfit*Point,"buy",0,0,Green);

         if(ticket>0)

           {

            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

               Print("BUY order opened : ",OrderOpenPrice());

           }

         else

            Print("Error opening BUY order : ",GetLastError());

         return;

        }

      //--- check for short position (SELL) possibility

      if ((RSI > 69));

     

        {

         Print("RSI at Sell", RSI);

         ticket=OrderSend(Symbol(),OP_SELL ,Lots,Bid,3,moving_avg + 100,Bid-TakeProfit*Point,"Sell",0,0,Red);

         if(ticket>0)

           {

            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

               Print("SELL order opened : ",OrderOpenPrice());

           }

         else

            Print("Error opening SELL order : ",GetLastError());

        }

      return; 

you should use SRC button when posting code.  That said, how are you defining RSI variable? is it iRSI(....)?

iRSI - Technical Indicators - MQL4 Reference
iRSI - Technical Indicators - MQL4 Reference
  • docs.mql4.com
iRSI - Technical Indicators - MQL4 Reference
 

sorry...and thank you for your help.  here's my RSI code, just simple iRSI. 

And yes, next time will do SRC button :) 

double RSI = iRSI(NULL, 0, 20, PRICE_MEDIAN,1); 

 
whroeder1:
  1. Please edit your post.
    For large amounts of code, attach it

  2. We have no idea what the variable RSI is or it's value. Post all relevant code. Print out your variables, and find out why.
  3. Same answer as #2
  4. No idea what you mean by "Close at Stop" all stops when hit close the ticket. If you mean the Stop Loss, set it to zero and trade with infinite risk! It's your OrderSend code, change it to what you want.
4) no, not stop loss. the "Close at stop" I meant is a type... under the Results tab, there's a Type column, with "buy", "t/p" and at the end of a back-test always "close at stop" and this is what I'd like to remove.  Thanks so much! 
 
pw2232:
   double RSI = iRSI(NULL, 0, 20, PRICE_MEDIAN,1);

   total= OrdersTotal();
   if(total<1)
     {
      //--- no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ",AccountFreeMargin());
         return;
        }
      //--- check for long position (BUY) possibility
      if ((RSI < 31));

        {
         Print("RSI at Buy", RSI);
         ticket=OrderSend(Symbol(), OP_BUY ,Lots,Ask,10,moving_avg - 100 ,Ask + TakeProfit*Point,"buy",0,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("BUY order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening BUY order : ",GetLastError());
         return;
        }
      //--- check for short position (SELL) possibility
      if ((RSI > 69));
      

        {
         Print("RSI at Sell", RSI);
         ticket=OrderSend(Symbol(),OP_SELL ,Lots,Bid,3,moving_avg + 100,Bid-TakeProfit*Point,"Sell",0,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("SELL order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening SELL order : ",GetLastError());
        }
      //--- exit from the "no opened orders" block
      return;
     }
here's the code in SRC 

 

Can you please provide a screen shot showing the "Data Window" with the chart of where the trade executed.

There might be mismatch between the values you are seeing in the "Data Window" and what your RSI signal/out gets.

 
pw2232: the end of a back-test always "close at stop" and this is what I'd like to remove.
When the tester ends and you still have an open order, it closes it. Change your end date to before the last opened order.
Reason: